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
41 #include "ricxfcpp/message.hpp"
42 #include "ricxfcpp/msg_component.hpp"
43 #include "ricxfcpp/xapp.hpp"
45 // ----------------------------------------------------------
47 // counts/time values for crude rate estimation; only valid when threads == 1
55 void cb1( Message& mbuf, int mtype, int subid, int len, Msg_component payload, void* data ) {
59 //fprintf( stderr, "callback 1 got a message type = %d len = %d\n", mtype, len );
60 mbuf.Send_response( 101, -1, 5, (unsigned char *) "OK1\n" ); // validate that we can use the same buffer for 2 rts calls
61 mbuf.Send_response( 101, -1, 5, (unsigned char *) "OK2\n" );
65 if( now - cb1_lastts > 5 ) { // crude rate estimation starting with second timer pop
67 total_count = cb1_count + cb2_count;
68 fprintf( stderr, "cb1: %ld diff=%ld ~rate=%ld\n", total_count, now - cb1_lastts, (total_count-cb1_lastc)/(now - cb1_lastts) );
69 cb1_lastc = total_count;
75 void cb2( Message& mbuf, int mtype, int subid, int len, Msg_component payload, void* data ) {
76 //fprintf( stderr, "callback 2 got a message type = %d len = %d\n", mtype, len );
77 //mbuf.Send_msg( 101, -1, 4, (unsigned char *) "HI\n" ); // send, including the trailing 0
81 void cbd( Message& mbuf, int mtype, int subid, int len, Msg_component payload, void* data ) {
82 //fprintf( stderr, "default callback got a message type = %d len = %d\n", mtype, len );
86 int main( int argc, char** argv ) {
88 char* port = (char *) "4560";
89 int ai = 1; // arg processing index
93 while( ai < argc ) { // very simple flag processing (no bounds/error checking)
94 if( argv[ai][0] != '-' ) {
98 switch( argv[ai][1] ) { // we only support -x so -xy must be -x -y
105 nthreads = atoi( argv[ai+1] );
113 fprintf( stderr, "<XAPP> listening on port: %s\n", port );
114 fprintf( stderr, "<XAPP> starting %d threads\n", nthreads );
116 x = new Xapp( port, true );
117 x->Add_msg_cb( 1, cb1, NULL );
118 x->Add_msg_cb( 2, cb2, NULL );
119 x->Add_msg_cb( x->DEFAULT_CALLBACK, cbd, NULL );