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 registers 3 callback functions
24 (2 for specific message types, and one default callback). It
25 defaults to starting 1 thread, but can cause the xapp environment
26 to start n listeners. When running 1 thread it should emit
27 message countes ever few seconds with a crudly computed msg/sec
30 In addition, the callback for message type 1 will send two response
31 messages to verify/illustrate that the same received buffer can
32 be used to construct multiple messages and given to the return to
33 sender function in RMR for multiple message reponse generation.
36 Author: E. Scott Daniels
38 Caution: This example code is pulled directly into one or more of the documents
39 (starting from the "start-example" tag below. Use caution with
40 line lengths and contents because of the requirement that this
41 be documentation as well as working code.
46 #include "ricxfcpp/message.hpp"
47 #include "ricxfcpp/msg_component.hpp"
48 #include "ricxfcpp/xapp.hpp"
50 // counts; not thread safe
58 // respond with 2 messages for each type 1 received
59 void cb1( Message& mbuf, int mtype, int subid, int len,
60 Msg_component payload, void* data ) {
64 // illustrate that we can use the same buffer for 2 rts calls
65 mbuf.Send_response( 101, -1, 5, (unsigned char *) "OK1\n" );
66 mbuf.Send_response( 101, -1, 5, (unsigned char *) "OK2\n" );
71 // just count messages
72 void cb2( Message& mbuf, int mtype, int subid, int len,
73 Msg_component payload, void* data ) {
77 // default to count all unrecognised messages
78 void cbd( Message& mbuf, int mtype, int subid, int len,
79 Msg_component payload, void* data ) {
83 int main( int argc, char** argv ) {
85 char* port = (char *) "4560";
86 int ai = 1; // arg processing index
89 // very simple flag processing (no bounds/error checking)
91 if( argv[ai][0] != '-' ) {
95 switch( argv[ai][1] ) { // we only support -x so -xy must be -x -y
102 nthreads = atoi( argv[ai+1] );
110 fprintf( stderr, "<XAPP> listening on port: %s\n", port );
111 fprintf( stderr, "<XAPP> starting %d threads\n", nthreads );
113 x = new Xapp( port, true );
114 x->Add_msg_cb( 1, cb1, NULL ); // register callbacks
115 x->Add_msg_cb( 2, cb2, NULL );
116 x->Add_msg_cb( x->DEFAULT_CALLBACK, cbd, NULL );
118 x->Run( nthreads ); // let framework drive
119 // control should not return