Define message-summary dict keys as constants
[ric-plt/xapp-frame-py.git] / examples / rmr / rcv_all.py
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
18 #   Mnemonic:   rcv_all.py
19 #   Abstract:   This example shows how to receive all queued messages into
20 #               a bunch (an array of summaries).  RMR is initialised in multi-
21 #               threaded call mode so that it will queue messages on a 2K ring
22 #               and prevent the remote application(s) from blocking if we don't
23 #               do timely receives.  Then we read 'bursts' of messages sleeping
24 #               between reads to allow some message to pile up.
25 #
26 #               Because this programme does not send messages, there is no reason
27 #               to wait for RMR to initialise a route table (no call to rmr_ready
28 #               is needed.
29
30 from rmr import rmr
31 from rmr import helpers
32 import time
33 import sys
34 import signal
35
36
37 #    Ensure things terminate nicely
38 #
39 def signal_handler(sig, frame):
40     print('SIGINT received! Cleaning up rmr')
41     rmr.rmr_close(mrc)
42     print("Byeee")
43     sys.exit(0)
44
45 listen_port = "4560".encode('utf-8')                                          # port RMR will listen on (RMR needs string, not value)
46 mrc = rmr.rmr_init( listen_port, rmr.RMR_MAX_RCV_BYTES, rmr.RMRFL_MTCALL )    # put into multi-threaded call mode
47
48 signal.signal(signal.SIGINT, signal_handler)    # cleanup on ctl-c
49
50 while True:
51
52     # three calling options:
53     #mbunch = helpers.rmr_rcvall_msgs( mrc, [2, 4, 6] )     # get types 2, 4 and 6 only
54     #mbunch = helpers.rmr_rcvall_msgs( mrc, [2] )           # get types 2 only
55     mbunch = helpers.rmr_rcvall_msgs( mrc )                 # get all message types
56
57     if mbunch == None or  len( mbunch ) < 1:
58         print( "no messages" )
59     else:
60         print( "got %d messages" % len( mbunch ) )
61         for mb in mbunch:
62             print( "type=%d payload=%s" % (mb[rmr.RMR_MS_MSG_TYPE], mb[rmr.RMR_MS_PAYLOAD] ) )
63
64     time.sleep( 1 )            # sleep to allow some to accumulate
65