blob: 48016491ff7d4af256967532c298d94c45992341 [file] [log] [blame]
Tommy Carpenterfbb59282020-04-08 07:04:51 -04001# ==================================================================================
2# Copyright (c) 2019 Nokia
3# Copyright (c) 2018-2019 AT&T Intellectual Property.
4#
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
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
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# ==================================================================================
17import time
18import random
19import string
20import os
21import signal
22import sys
23from ricxappframe.rmr import rmr
24
25
Lott, Christopher (cl778h)61270902020-05-06 09:23:55 -040026# Demonstrate RMR cleanup
Tommy Carpenterfbb59282020-04-08 07:04:51 -040027def signal_handler(sig, frame):
Lott, Christopher (cl778h)61270902020-05-06 09:23:55 -040028 print("SIGINT received! Cleaning up RMR")
Tommy Carpenterfbb59282020-04-08 07:04:51 -040029 rmr.rmr_close(mrc)
30 print("Byeee")
31 sys.exit(0)
32
33
34# Init rmr
35mrc = rmr.rmr_init(b"4562", rmr.RMR_MAX_RCV_BYTES, 0x00)
36while rmr.rmr_ready(mrc) == 0:
37 time.sleep(1)
38 print("not yet ready")
39rmr.rmr_set_stimeout(mrc, 2)
40sbuf = rmr.rmr_alloc_msg(mrc, 256)
41
42# capture ctrl-c
43signal.signal(signal.SIGINT, signal_handler)
44
45while True:
46 # generate a random value between 1 and 256 bytes, then gen some random bytes with several nulls thrown in
47 for val in [
48 "".join([random.choice(string.ascii_letters + string.digits) for n in range(random.randint(1, 256))]).encode("utf8"),
49 b"\x00" + os.urandom(4) + b"\x00" + os.urandom(4) + b"\x00",
50 ]:
51 rmr.set_payload_and_length(val, sbuf)
52 rmr.generate_and_set_transaction_id(sbuf)
53 sbuf.contents.state = 0
54 sbuf.contents.mtype = 0
55 print("Pre send summary: {}".format(rmr.message_summary(sbuf)))
56 sbuf = rmr.rmr_send_msg(mrc, sbuf)
57 print("Post send summary: {}".format(rmr.message_summary(sbuf)))
58 print("Waiting for return, will timeout after 2000ms")
59 sbuf = rmr.rmr_torcv_msg(mrc, sbuf, 2000)
60 summary = rmr.message_summary(sbuf)
Lott, Christopher (cl778h)61270902020-05-06 09:23:55 -040061 if summary[rmr.RMR_MS_MSG_STATE] == 12:
Tommy Carpenterfbb59282020-04-08 07:04:51 -040062 print("Nothing received yet")
63 else:
64 print("Ack Message received!: {}".format(summary))
65
66 time.sleep(1)