Initial source commit
[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 mtype;
49         int     sz;
50         int i;
51         int ai;
52         int response_to = 0;                            // max timeout wating for a response
53         char*   port = (char *) "4555";
54         
55
56         ai = 1;
57         while( ai < argc ) {                            // very simple flag processing (no bounds/error checking)
58                 if( argv[ai][0] != '-' )  {
59                         break;
60                 }
61
62                 switch( argv[ai][1] ) {                 // we only support -x so -xy must be -x -y
63                         case 'p': 
64                                 port = argv[ai+1];      
65                                 ai++;
66                                 break;
67
68                         case 't':                                       // timeout in seconds; we need to convert to ms for rmr calls
69                                 response_to = atoi( argv[ai+1] ) * 1000;
70                                 ai++;
71                                 break;
72                 }
73                 ai++;
74         }
75
76         fprintf( stderr, "<XAPP> response timeout set to: %d\n", response_to );
77         fprintf( stderr, "<XAPP> listening on port: %s\n", port );
78
79         xfw = std::unique_ptr<Xapp>( new Xapp( port, true ) );          // new xAPP thing; wait for a route table
80         msg = xfw->Alloc_msg( 2048 );
81
82         for( i = 0; i < 100; i++ ) {
83                 sz = msg->Get_available_size();                 // we'll reuse a message if we received one back; ensure it's big enough
84                 if( sz < 2048 ) {
85                         fprintf( stderr, "<SNDR> fail: message returned did not have enough size: %d [%d]\n", sz, i );
86                         exit( 1 );
87                 }
88
89                 payload = msg->Get_payload();                                                                                   // direct access to payload
90                 snprintf( (char *) payload.get(), 2048, "This is message %d\n", i );    // something silly to send
91
92                 // payload updated in place, nothing to copy from, so payload parm is nil
93                 if ( ! msg->Send_msg( 1, Message::NO_SUBID, strlen( (char *) payload.get() )+1, NULL )) {
94                         fprintf( stderr, "<SNDR> send failed: %d\n", i );
95                 }
96
97                 msg = xfw->Receive( response_to );
98                 if( msg != NULL ) {
99                         mtype = msg->Get_mtype();
100                         payload = msg->Get_payload();
101                         fprintf( stderr, "got: mtype=%d payload=(%s)\n", mtype, (char *) payload.get() );
102                 } else {
103                         msg = xfw->Alloc_msg( 2048 );                           // nothing back, need a new message to send
104                 }
105
106                 sleep( 1 );
107         }
108 }