dd24d2b373f9de2ecd51a24d5e9df0adf2891a46
[ric-plt/xapp-frame-cpp.git] / examples / xapp_t2.cpp
1 // vi: ts=4 sw=4 noet:
2 /*
3 ==================================================================================
4         Copyright (c) 2020 Nokia
5         Copyright (c) 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 */
20
21 /*
22         Mnemonic:       xapp_t2.cpp
23         Abstract:       This is a simple demo xapp which controls it's own listen
24                                 loop (does not register callbacks and does not invoke the
25                                 run function in the xapp instance.
26
27         Date:           18 March 2020
28         Author:         E. Scott Daniels
29                 
30 */
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <unistd.h>
35
36 #include <iostream>
37 #include <memory>
38
39 #include "ricxfcpp/xapp.hpp"
40
41 // ----------------------------------------------------------
42
43 extern int main( int argc, char** argv ) {
44         std::unique_ptr<Xapp> xfw;
45         std::unique_ptr<Message> msg;
46         Msg_component payload;                          // special type of unique pointer to the payload 
47
48         int     sz;
49         int i;
50         int ai;
51         int response_to = 0;                            // max timeout wating for a response
52         char*   port = (char *) "4555";
53         int     mtype = 0;
54         int rmtype;                                                     // received message type
55         int delay = 1000000;                            // mu-sec delay; default 1s
56         
57
58         ai = 1;
59         while( ai < argc ) {                            // very simple flag processing (no bounds/error checking)
60                 if( argv[ai][0] != '-' )  {
61                         break;
62                 }
63
64                 switch( argv[ai][1] ) {                 // we only support -x so -xy must be -x -y
65                         case 'd':                                       // delay between messages (mu-sec)
66                                 delay = atoi( argv[ai+1] );
67                                 ai++;
68                                 break;
69                                 
70                         case 'p': 
71                                 port = argv[ai+1];      
72                                 ai++;
73                                 break;
74
75                         case 't':                                       // timeout in seconds; we need to convert to ms for rmr calls
76                                 response_to = atoi( argv[ai+1] ) * 1000;
77                                 ai++;
78                                 break;
79                 }
80                 ai++;
81         }
82
83         fprintf( stderr, "<XAPP> response timeout set to: %d\n", response_to );
84         fprintf( stderr, "<XAPP> listening on port: %s\n", port );
85
86         xfw = std::unique_ptr<Xapp>( new Xapp( port, true ) );          // new xAPP thing; wait for a route table
87         msg = xfw->Alloc_msg( 2048 );
88
89         for( i = 0; i < 100; i++ ) {
90                 mtype++;
91                 if( mtype > 10 ) {
92                         mtype = 0;
93                 }
94
95                 sz = msg->Get_available_size();                 // we'll reuse a message if we received one back; ensure it's big enough
96                 if( sz < 2048 ) {
97                         fprintf( stderr, "<SNDR> fail: message returned did not have enough size: %d [%d]\n", sz, i );
98                         exit( 1 );
99                 }
100
101                 payload = msg->Get_payload();                                                                                   // direct access to payload
102                 snprintf( (char *) payload.get(), 2048, "This is message %d\n", i );    // something silly to send
103
104                 // payload updated in place, nothing to copy from, so payload parm is nil
105                 if ( ! msg->Send_msg( mtype, Message::NO_SUBID, strlen( (char *) payload.get() )+1, NULL )) {
106                         fprintf( stderr, "<SNDR> send failed: %d\n", i );
107                 }
108
109                 msg = xfw->Receive( response_to );
110                 if( msg != NULL ) {
111                         rmtype = msg->Get_mtype();
112                         payload = msg->Get_payload();
113                         fprintf( stderr, "got: mtype=%d payload=(%s)\n", rmtype, (char *) payload.get() );
114                 } else {
115                         msg = xfw->Alloc_msg( 2048 );                           // nothing back, need a new message to send
116                 }
117
118                 if( delay > 0 ) {
119                         usleep( delay );
120                 }
121         }
122 }