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
#!/usr/bin/env python3
 
# Test whether maximum packet size is honoured on a PUBLISH to a client
# MQTTv5
 
from mosq_test_helper import *
 
rc = 1
 
keepalive = 10
props = mqtt5_props.gen_uint32_prop(mqtt5_props.PROP_MAXIMUM_PACKET_SIZE, 20)
connect_packet = mosq_test.gen_connect("test", proto_ver=5, keepalive=keepalive, properties=props)
connack_packet = mosq_test.gen_connack(rc=0, proto_ver=5)
 
mid = 1
subscribe_packet = mosq_test.gen_subscribe(mid, "test/topic", 2, proto_ver=5)
suback_packet = mosq_test.gen_suback(mid, 2, proto_ver=5)
 
mid=1
publish1_packet = mosq_test.gen_publish(topic="test/topic", mid=mid, qos=2, payload="12345678901234567890", proto_ver=5)
pubrec1_packet = mosq_test.gen_pubrec(mid, proto_ver=5)
pubrel1_packet = mosq_test.gen_pubrel(mid, proto_ver=5)
pubcomp1_packet = mosq_test.gen_pubcomp(mid, proto_ver=5)
 
mid=2
publish2_packet = mosq_test.gen_publish(topic="test/topic", mid=mid, qos=2, payload="7890", proto_ver=5)
pubrec2_packet = mosq_test.gen_pubrec(mid, proto_ver=5)
pubrel2_packet = mosq_test.gen_pubrel(mid, proto_ver=5)
pubcomp2_packet = mosq_test.gen_pubcomp(mid, proto_ver=5)
 
port = mosq_test.get_port()
broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port)
 
try:
    sock = mosq_test.do_client_connect(connect_packet, connack_packet, port=port)
    mosq_test.do_send_receive(sock, subscribe_packet, suback_packet)
 
    mosq_test.do_send_receive(sock, publish1_packet, pubrec1_packet, "pubrec 1")
    mosq_test.do_send_receive(sock, pubrel1_packet, pubcomp1_packet, "pubcomp 1")
 
    # We shouldn't receive the publish here because it is > MAXIMUM_PACKET_SIZE
    mosq_test.do_ping(sock)
 
    mosq_test.do_send_receive(sock, publish2_packet, pubrec2_packet, "pubrec 2")
    mosq_test.do_send_receive(sock, pubrel2_packet, pubcomp2_packet, "pubcomp 2")
 
    if mosq_test.expect_packet(sock, "publish2", publish2_packet):
        rc = 0
finally:
    broker.terminate()
    broker.wait()
    (stdo, stde) = broker.communicate()
    if rc:
        print(stde.decode('utf-8'))
 
exit(rc)