Add user manual source
[ric-plt/lib/rmr.git] / doc / src / library / code_sr.im
1
2 .if false
3 ==================================================================================
4         Copyright (c) 2019 Nokia
5         Copyright (c) 2018-2019 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 sender code
22
23 &h2(Receive and Send Sample)
24 The following code snippet receives messages and responds to the sender
25 if the message type is odd.
26 The code illustrates how the received message may be used to return
27 a message to the source.
28 Variable type definitions are omitted for clarity and should be obvious.
29
30 &space
31 It should also be noted that
32 things like the message type which id returned to the sender (99) is
33 a random value that these applications would have agreed on in advance
34 and is &bold(not) an RMR definition.
35
36 &space
37
38 &indent
39 &ex_start
40 mrc = rmr_init( listen_port, MAX_BUF_SZ, RMRFL_NOFLAGS );
41 rmr_set_stimeout( mrc, 1 );        // allow RMR to retry failed sends for ~1ms
42
43 while( ! rmr_ready( mrc ) ) {        // we send, therefore we need a route table
44     sleep( 1 );
45 }
46
47 mbuf = NULL;                        // ensure our buffer pointer is nil for 1st call
48
49 while( TRUE ) {
50     mbuf = rmr_rcv_msg( mrc, mbuf );        // wait for message
51
52     if( mbuf == NULL || mbuf->state != RMR_OK ) {
53         break;
54     }
55
56     if( mbuf->mtype % 2 ) {                // respond to odd message types
57         plen = rmr_payload_size( mbuf );        // max size
58
59                                                 // reset necessary fields in msg
60         mbuf->mtype = 99;                       // response type
61         mbuf->sub_id = RMR_VOID_SUBID;          // we turn subid off
62         mbuf->len = snprintf( mbuf->payload, plen, "pong: %s", get_info() );
63
64         mbuf = rmr_rts_msg( mrc, mbuf );        // return to sender
65         if( mbuf == NULL || mbuf->state != RMR_OK ) {
66             fprintf( stderr, "return to sender failed\n" );
67         }
68     }
69 }
70
71 fprintf( stderr, "abort: receive failure\n" );
72 rmr_close( mrc );
73
74 &ex_end
75 &uindent