APUE Learning Example Source Code
guowenxue
2019-06-26 157be0b0d4c7d4809cfcafc76235cc18388378c8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
 
# MQTT v5. Test whether session expiry interval works correctly.
 
from mosq_test_helper import *
 
rc = 1
keepalive = 60
 
props = mqtt5_props.gen_uint32_prop(mqtt5_props.PROP_SESSION_EXPIRY_INTERVAL, 1)
connect_packet = mosq_test.gen_connect("clean-qos2-test", keepalive=keepalive, clean_session=False, proto_ver=5, properties=props)
connack1_packet = mosq_test.gen_connack(flags=0, rc=0, proto_ver=5)
 
connack2_packet = mosq_test.gen_connack(flags=1, rc=0, proto_ver=5)
 
props = mqtt5_props.gen_uint32_prop(mqtt5_props.PROP_SESSION_EXPIRY_INTERVAL, 3)
disconnect_packet = mosq_test.gen_disconnect(proto_ver=5, properties=props)
 
props = mqtt5_props.gen_uint32_prop(mqtt5_props.PROP_SESSION_EXPIRY_INTERVAL, 0)
disconnect2_packet = mosq_test.gen_disconnect(proto_ver=5, properties=props)
 
port = mosq_test.get_port()
broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port)
 
try:
    # First connect, clean start is false, we expect a normal connack
    sock = mosq_test.do_client_connect(connect_packet, connack1_packet, port=port, connack_error="connack 1")
    # Forceful disconnect
    sock.close()
 
    # Immediate second connect, clean start is false, we expect a connack with
    # previous state
    sock = mosq_test.do_client_connect(connect_packet, connack2_packet, port=port, connack_error="connack 2")
    sock.close()
 
    # Session should expire in one second, so sleep longer
    time.sleep(2)
 
    # Third connect, clean start is false, session should have expired so we
    # expect a normal connack
    sock = mosq_test.do_client_connect(connect_packet, connack1_packet, port=port, connack_error="connack 3")
    # Send DISCONNECT with new session expiry, then close
    sock.send(disconnect_packet)
    sock.close()
 
    # Immediate reconnect, clean start is false, we expect a connack with
    # previous state
    sock = mosq_test.do_client_connect(connect_packet, connack2_packet, port=port, connack_error="connack 4")
    # Send DISCONNECT with new session expiry, then close
    sock.send(disconnect_packet)
    sock.close()
 
    # Session should expire in three seconds if it has been updated, sleep for
    # 2 to check it is updated from 1 second.
    time.sleep(2)
 
    # Immediate reconnect, clean start is false, we expect a connack with
    # previous state
    sock = mosq_test.do_client_connect(connect_packet, connack2_packet, port=port, connack_error="connack 5")
    # Send DISCONNECT with new session expiry, then close
    sock.send(disconnect_packet)
    sock.close()
 
    # Session should expire in three seconds, so sleep longer
    time.sleep(4)
    # Third connect, clean start is false, session should have expired so we
    # expect a normal connack
    sock = mosq_test.do_client_connect(connect_packet, connack1_packet, port=port, connack_error="connack 6")
    # Send DISCONNECT with 0 session expiry, then close
    sock.send(disconnect2_packet)
    sock.close()
 
    # Immediate reconnect, session should have been removed.
    sock = mosq_test.do_client_connect(connect_packet, connack1_packet, port=port, connack_error="connack 7")
    sock.close()
    rc = 0
 
    sock.close()
finally:
    broker.terminate()
    broker.wait()
    (stdo, stde) = broker.communicate()
    if rc:
        print(stde.decode('utf-8'))
 
exit(rc)