Move rmr python api docs over.
[ric-plt/xapp-frame-py.git] / examples / rmr / rcv_all.py
1 # vim: ts=4 sw=4 expandtab:
2 # ==================================================================================
3 #       Copyright (c) 2019 Nokia
4 #       Copyright (c) 2018-2019 AT&T Intellectual Property.
5 #
6 #   Licensed under the Apache License, Version 2.0 (the "License");
7 #   you may not use this file except in compliance with the License.
8 #   You may obtain a copy of the License at
9 #
10 #          http://www.apache.org/licenses/LICENSE-2.0
11 #
12 #   Unless required by applicable law or agreed to in writing, software
13 #   distributed under the License is distributed on an "AS IS" BASIS,
14 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #   See the License for the specific language governing permissions and
16 #   limitations under the License.
17 # ==================================================================================
18
19 #   Mnemonic:   rcv_all.py
20 #   Abstract:   This example shows how to receive all queued messages into
21 #               a bunch (an array of summaries).  RMR is initialised in multi-
22 #               threaded call mode so that it will queue messages on a 2K ring
23 #               and prevent the remote application(s) from blocking if we don't
24 #               do timely receives.  Then we read 'bursts' of messages sleeping
25 #               between reads to allow some message to pile up.
26 #
27 #               Because this programme does not send messages, there is no reason
28 #               to wait for RMR to initialise a route table (no call to rmr_ready
29 #               is needed.
30 #
31 #   Date:       26 September 2019
32 #
33 # ---------------------------------------------------------------------------------
34
35 from rmr import rmr
36 from rmr import helpers
37 import time
38 import sys
39 import signal
40
41
42 #    Ensure things terminate nicely
43 #
44 def signal_handler(sig, frame):
45     print('SIGINT received! Cleaning up rmr')
46     rmr.rmr_close(mrc)
47     print("Byeee")
48     sys.exit(0)
49
50 listen_port = "4560".encode('utf-8')                                          # port RMR will listen on (RMR needs string, not value)
51 mrc = rmr.rmr_init( listen_port, rmr.RMR_MAX_RCV_BYTES, rmr.RMRFL_MTCALL )    # put into multi-threaded call mode
52
53 signal.signal(signal.SIGINT, signal_handler)    # cleanup on ctl-c
54
55 while True:
56
57     # three calling options:
58     #mbunch = helpers.rmr_rcvall_msgs( mrc, [2, 4, 6] )     # get types 2, 4 and 6 only
59     #mbunch = helpers.rmr_rcvall_msgs( mrc, [2] )           # get types 2 only
60     mbunch = helpers.rmr_rcvall_msgs( mrc )                 # get all message types
61
62     if mbunch == None or  len( mbunch ) < 1:
63         print( "no messages" )
64     else:
65         print( "got %d messages" % len( mbunch ) )
66         for mb in mbunch:
67             print( "type=%d payload=%s" % (mb["message type"], mb["payload"] ) )
68
69     time.sleep( 1 )            # sleep to allow some to accumulate
70