blob: 1dc4da32900aa29b5e8e49dd6cfe05d84ab8b44f [file] [log] [blame]
E. Scott Daniels8cb3c6f2020-03-19 11:36:37 -04001// 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
E. Scott Daniels3a2533f2020-04-22 12:40:27 -040029
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.
E. Scott Daniels8cb3c6f2020-03-19 11:36:37 -040034*/
E. Scott Daniels3a2533f2020-04-22 12:40:27 -040035// start-example
E. Scott Daniels8cb3c6f2020-03-19 11:36:37 -040036
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
E. Scott Daniels8cb3c6f2020-03-19 11:36:37 -040046extern int main( int argc, char** argv ) {
47 std::unique_ptr<Xapp> xfw;
E. Scott Daniels6ef23e12020-07-15 08:03:22 -040048 std::unique_ptr<xapp::Message> msg;
49 xapp::Msg_component payload; // special type of unique pointer to the payload
E. Scott Daniels8cb3c6f2020-03-19 11:36:37 -040050
E. Scott Daniels8cb3c6f2020-03-19 11:36:37 -040051 int sz;
E. Scott Daniels3a2533f2020-04-22 12:40:27 -040052 int len;
E. Scott Daniels8cb3c6f2020-03-19 11:36:37 -040053 int i;
54 int ai;
55 int response_to = 0; // max timeout wating for a response
56 char* port = (char *) "4555";
E. Scott Daniels0b08d9d2020-03-27 10:18:37 -040057 int mtype = 0;
58 int rmtype; // received message type
59 int delay = 1000000; // mu-sec delay; default 1s
E. Scott Daniels8cb3c6f2020-03-19 11:36:37 -040060
E. Scott Daniels3a2533f2020-04-22 12:40:27 -040061
62 // very simple flag processing (no bounds/error checking)
63 while( ai < argc ) {
E. Scott Daniels8cb3c6f2020-03-19 11:36:37 -040064 if( argv[ai][0] != '-' ) {
65 break;
66 }
67
E. Scott Daniels3a2533f2020-04-22 12:40:27 -040068 // we only support -x so -xy must be -x -y
69 switch( argv[ai][1] ) {
70 // delay between messages (mu-sec)
71 case 'd':
E. Scott Daniels0b08d9d2020-03-27 10:18:37 -040072 delay = atoi( argv[ai+1] );
73 ai++;
74 break;
E. Scott Daniels3a2533f2020-04-22 12:40:27 -040075
76 case 'p':
77 port = argv[ai+1];
E. Scott Daniels8cb3c6f2020-03-19 11:36:37 -040078 ai++;
79 break;
80
E. Scott Daniels3a2533f2020-04-22 12:40:27 -040081 // timeout in seconds; we need to convert to ms for rmr calls
82 case 't':
E. Scott Daniels8cb3c6f2020-03-19 11:36:37 -040083 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
E. Scott Daniels3a2533f2020-04-22 12:40:27 -040093 // get an instance and wait for a route table to be loaded
94 xfw = std::unique_ptr<Xapp>( new Xapp( port, true ) );
E. Scott Daniels8cb3c6f2020-03-19 11:36:37 -040095 msg = xfw->Alloc_msg( 2048 );
96
97 for( i = 0; i < 100; i++ ) {
E. Scott Daniels0b08d9d2020-03-27 10:18:37 -040098 mtype++;
99 if( mtype > 10 ) {
100 mtype = 0;
101 }
102
E. Scott Daniels3a2533f2020-04-22 12:40:27 -0400103 // we'll reuse a received message; get max size
104 sz = msg->Get_available_size();
E. Scott Daniels8cb3c6f2020-03-19 11:36:37 -0400105
E. Scott Daniels3a2533f2020-04-22 12:40:27 -0400106 // 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 );
E. Scott Daniels8cb3c6f2020-03-19 11:36:37 -0400109
E. Scott Daniels3a2533f2020-04-22 12:40:27 -0400110 // payload updated in place, prevent copy by passing nil
E. Scott Daniels6ef23e12020-07-15 08:03:22 -0400111 if ( ! msg->Send_msg( mtype, xapp::Message::NO_SUBID, len, NULL )) {
E. Scott Daniels8cb3c6f2020-03-19 11:36:37 -0400112 fprintf( stderr, "<SNDR> send failed: %d\n", i );
113 }
114
E. Scott Daniels3a2533f2020-04-22 12:40:27 -0400115 // receive anything that might come back
E. Scott Daniels8cb3c6f2020-03-19 11:36:37 -0400116 msg = xfw->Receive( response_to );
117 if( msg != NULL ) {
E. Scott Daniels0b08d9d2020-03-27 10:18:37 -0400118 rmtype = msg->Get_mtype();
E. Scott Daniels8cb3c6f2020-03-19 11:36:37 -0400119 payload = msg->Get_payload();
E. Scott Daniels3a2533f2020-04-22 12:40:27 -0400120 fprintf( stderr, "got: mtype=%d payload=(%s)\n",
121 rmtype, (char *) payload.get() );
E. Scott Daniels8cb3c6f2020-03-19 11:36:37 -0400122 } else {
E. Scott Daniels3a2533f2020-04-22 12:40:27 -0400123 msg = xfw->Alloc_msg( 2048 );
E. Scott Daniels8cb3c6f2020-03-19 11:36:37 -0400124 }
125
E. Scott Daniels0b08d9d2020-03-27 10:18:37 -0400126 if( delay > 0 ) {
127 usleep( delay );
128 }
E. Scott Daniels8cb3c6f2020-03-19 11:36:37 -0400129 }
130}