1 # ==================================================================================
2 # Copyright (c) 2019 Nokia
3 # Copyright (c) 2018-2019 AT&T Intellectual Property.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 # ==================================================================================
26 # Demonstrate NNG cleanup
27 def signal_handler(sig, frame):
28 print('SIGINT received! Cleaning up rmr')
35 mrc = rmr.rmr_init(b"4562", rmr.RMR_MAX_RCV_BYTES, 0x00)
36 while rmr.rmr_ready(mrc) == 0:
38 print("not yet ready")
39 rmr.rmr_set_stimeout(mrc, 2)
40 sbuf = rmr.rmr_alloc_msg(mrc, 256)
43 signal.signal(signal.SIGINT, signal_handler)
46 # generate a random value between 1 and 256 bytes, then gen some random bytes with several nulls thrown in
47 for val in [''.join([random.choice(string.ascii_letters + string.digits) for n in range(random.randint(1,256))]).encode("utf8"),
48 b"\x00" + os.urandom(4) + b"\x00" + os.urandom(4) + b"\x00"]:
49 rmr.set_payload_and_length(val, sbuf)
50 rmr.generate_and_set_transaction_id(sbuf)
51 sbuf.contents.state = 0
52 sbuf.contents.mtype = 0
53 print("Pre send summary: {}".format(rmr.message_summary(sbuf)))
54 sbuf = rmr.rmr_send_msg(mrc, sbuf)
55 print("Post send summary: {}".format(rmr.message_summary(sbuf)))
56 print("Waiting for return, will timeout after 2000ms")
57 sbuf = rmr.rmr_torcv_msg(mrc, sbuf, 2000)
58 summary = rmr.message_summary(sbuf)
59 if summary['message state'] == 12:
60 print("Nothing received yet")
62 print("Ack Message received!: {}".format(summary))