3 ==================================================================================
4 Copyright (c) 2020 Nokia
5 Copyright (c) 2020 AT&T Intellectual Property.
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
11 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================================
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.
28 Author: E. Scott Daniels
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.
44 #include "ricxfcpp/xapp.hpp"
46 extern int main( int argc, char** argv ) {
47 std::unique_ptr<Xapp> xfw;
48 std::unique_ptr<Message> msg;
49 Msg_component payload; // special type of unique pointer to the payload
55 int response_to = 0; // max timeout wating for a response
56 char* port = (char *) "4555";
58 int rmtype; // received message type
59 int delay = 1000000; // mu-sec delay; default 1s
62 // very simple flag processing (no bounds/error checking)
64 if( argv[ai][0] != '-' ) {
68 // we only support -x so -xy must be -x -y
69 switch( argv[ai][1] ) {
70 // delay between messages (mu-sec)
72 delay = atoi( argv[ai+1] );
81 // timeout in seconds; we need to convert to ms for rmr calls
83 response_to = atoi( argv[ai+1] ) * 1000;
90 fprintf( stderr, "<XAPP> response timeout set to: %d\n", response_to );
91 fprintf( stderr, "<XAPP> listening on port: %s\n", port );
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 );
97 for( i = 0; i < 100; i++ ) {
103 // we'll reuse a received message; get max size
104 sz = msg->Get_available_size();
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 );
110 // payload updated in place, prevent copy by passing nil
111 if ( ! msg->Send_msg( mtype, Message::NO_SUBID, len, NULL )) {
112 fprintf( stderr, "<SNDR> send failed: %d\n", i );
115 // receive anything that might come back
116 msg = xfw->Receive( response_to );
118 rmtype = msg->Get_mtype();
119 payload = msg->Get_payload();
120 fprintf( stderr, "got: mtype=%d payload=(%s)\n",
121 rmtype, (char *) payload.get() );
123 msg = xfw->Alloc_msg( 2048 );