Add route table guide and formatting tweaks
[ric-plt/lib/rmr.git] / doc / src / library / code_sr.im
1
2 .if false
3 ==================================================================================
4         Copyright (c) 2019-2020 Nokia
5         Copyright (c) 2018-2020 AT&T Intellectual Property.
6
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10
11        http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18 ==================================================================================
19 .fi
20
21 .** example combination send/receive loop; no example to pull yet
22
23 &h2(Receive and Send Sample)
24 The following code snippet receives messages and responds to the
25 sender if the message type is odd.  The code illustrates how the
26 received message may be used to return a message to the source.
27 Variable type definitions are omitted for clarity and should be
28 obvious.
29
30 &space
31 It should also be noted that things like the message type which id
32 returned to the sender (99) is a random value that these applications
33 would have agreed on in advance and is &bold(not) an RMR definition.
34
35 &space
36
37 &indent
38 &ex_start
39 mrc = rmr_init( listen_port, MAX_BUF_SZ, RMRFL_NOFLAGS );
40 rmr_set_stimeout( mrc, 1 );        // allow RMR to retry failed sends for ~1ms
41
42 while( ! rmr_ready( mrc ) ) {        // we send, therefore we need a route table
43     sleep( 1 );
44 }
45
46 mbuf = NULL;                        // ensure our buffer pointer is nil for 1st call
47
48 while( TRUE ) {
49     mbuf = rmr_rcv_msg( mrc, mbuf );        // wait for message
50
51     if( mbuf == NULL || mbuf->state != RMR_OK ) {
52         break;
53     }
54
55     if( mbuf->mtype % 2 ) {                // respond to odd message types
56         plen = rmr_payload_size( mbuf );        // max size
57
58                                                 // reset necessary fields in msg
59         mbuf->mtype = 99;                       // response type
60         mbuf->sub_id = RMR_VOID_SUBID;          // we turn subid off
61         mbuf->len = snprintf( mbuf->payload, plen, "pong: %s", get_info() );
62
63         mbuf = rmr_rts_msg( mrc, mbuf );        // return to sender
64         if( mbuf == NULL || mbuf->state != RMR_OK ) {
65             fprintf( stderr, "return to sender failed\n" );
66         }
67     }
68 }
69
70 fprintf( stderr, "abort: receive failure\n" );
71 rmr_close( mrc );
72
73 &ex_end
74 &uindent