Add API allowing xAPPs to send alarm messages
[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         Caution:        This example code is pulled directly into one or more of the documents
31                                 (starting from the "start-example" tag below.  Use caution with
32                                 line lengths and contents because of the requirement that this
33                                 be documentation as well as working code.
34 */
35 // start-example
36
37 #include <stdio.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 #include <iostream>
42 #include <memory>
43
44 #include "ricxfcpp/xapp.hpp"
45
46 extern int main( int argc, char** argv ) {
47         std::unique_ptr<Xapp> xfw;
48         std::unique_ptr<xapp::Message> msg;
49         xapp::Msg_component payload;                            // special type of unique pointer to the payload
50
51         int     sz;
52         int len;
53         int i;
54         int ai;
55         int response_to = 0;                            // max timeout wating for a response
56         char*   port = (char *) "4555";
57         int     mtype = 0;
58         int rmtype;                                                     // received message type
59         int delay = 1000000;                            // mu-sec delay; default 1s
60
61
62         // very simple flag processing (no bounds/error checking)
63         while( ai < argc ) {
64                 if( argv[ai][0] != '-' )  {
65                         break;
66                 }
67
68                 // we only support -x so -xy must be -x -y
69                 switch( argv[ai][1] ) {
70                         // delay between messages (mu-sec)
71                         case 'd':
72                                 delay = atoi( argv[ai+1] );
73                                 ai++;
74                                 break;
75
76                         case 'p':
77                                 port = argv[ai+1];
78                                 ai++;
79                                 break;
80
81                         // timeout in seconds; we need to convert to ms for rmr calls
82                         case 't':
83                                 response_to = atoi( argv[ai+1] ) * 1000;
84                                 ai++;
85                                 break;
86                 }
87                 ai++;
88         }
89
90         fprintf( stderr, "<XAPP> response timeout set to: %d\n", response_to );
91         fprintf( stderr, "<XAPP> listening on port: %s\n", port );
92
93         // get an instance and wait for a route table to be loaded
94         xfw = std::unique_ptr<Xapp>( new Xapp( port, true ) );
95         msg = xfw->Alloc_msg( 2048 );
96
97         for( i = 0; i < 100; i++ ) {
98                 mtype++;
99                 if( mtype > 10 ) {
100                         mtype = 0;
101                 }
102
103                 // we'll reuse a received message; get max size
104                 sz = msg->Get_available_size();
105
106                 // direct access to payload; add something silly
107                 payload = msg->Get_payload();
108                 len = snprintf( (char *) payload.get(), sz, "This is message %d\n", i );
109
110                 // payload updated in place, prevent copy by passing nil
111                 if ( ! msg->Send_msg( mtype, xapp::Message::NO_SUBID,  len, NULL )) {
112                         fprintf( stderr, "<SNDR> send failed: %d\n", i );
113                 }
114
115                 // receive anything that might come back
116                 msg = xfw->Receive( response_to );
117                 if( msg != NULL ) {
118                         rmtype = msg->Get_mtype();
119                         payload = msg->Get_payload();
120                         fprintf( stderr, "got: mtype=%d payload=(%s)\n",
121                                 rmtype, (char *) payload.get() );
122                 } else {
123                         msg = xfw->Alloc_msg( 2048 );
124                 }
125
126                 if( delay > 0 ) {
127                         usleep( delay );
128                 }
129         }
130 }