Tommy Carpenter | fbb5928 | 2020-04-08 07:04:51 -0400 | [diff] [blame] | 1 | # ================================================================================== |
| 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 | # ================================================================================== |
| 17 | from ricxappframe.rmr import rmr |
| 18 | import time |
| 19 | import sys |
| 20 | import signal |
| 21 | |
| 22 | |
Lott, Christopher (cl778h) | 6127090 | 2020-05-06 09:23:55 -0400 | [diff] [blame^] | 23 | # Demonstrate RMR cleanup |
Tommy Carpenter | fbb5928 | 2020-04-08 07:04:51 -0400 | [diff] [blame] | 24 | def signal_handler(sig, frame): |
Lott, Christopher (cl778h) | 6127090 | 2020-05-06 09:23:55 -0400 | [diff] [blame^] | 25 | print("SIGINT received! Cleaning up RMR") |
Tommy Carpenter | fbb5928 | 2020-04-08 07:04:51 -0400 | [diff] [blame] | 26 | rmr.rmr_close(mrc) |
| 27 | print("Byeee") |
| 28 | sys.exit(0) |
| 29 | |
| 30 | |
| 31 | # init rmr |
| 32 | mrc = rmr.rmr_init("4560".encode("utf-8"), rmr.RMR_MAX_RCV_BYTES, 0x00) |
| 33 | while rmr.rmr_ready(mrc) == 0: |
| 34 | time.sleep(1) |
| 35 | print("not yet ready") |
| 36 | rmr.rmr_set_stimeout(mrc, 2) |
| 37 | |
| 38 | # capture ctrl-c |
| 39 | signal.signal(signal.SIGINT, signal_handler) |
| 40 | |
| 41 | |
| 42 | sbuf = None |
| 43 | while True: |
| 44 | print("Waiting for a message, will timeout after 2000ms") |
| 45 | sbuf = rmr.rmr_torcv_msg(mrc, sbuf, 2000) |
| 46 | summary = rmr.message_summary(sbuf) |
Lott, Christopher (cl778h) | 6127090 | 2020-05-06 09:23:55 -0400 | [diff] [blame^] | 47 | if summary[rmr.RMR_MS_MSG_STATE] == 12: |
Tommy Carpenter | fbb5928 | 2020-04-08 07:04:51 -0400 | [diff] [blame] | 48 | print("Nothing received =(") |
| 49 | else: |
| 50 | print("Message received!: {}".format(summary)) |
| 51 | val = b"message recieved OK yall!" |
| 52 | rmr.set_payload_and_length(val, sbuf) |
| 53 | sbuf = rmr.rmr_rts_msg(mrc, sbuf) |
| 54 | time.sleep(1) |