Add metrics to the framework
[ric-plt/xapp-frame-cpp.git] / examples / rmr_dump.cpp
index 5cced21..860caf5 100644 (file)
@@ -27,9 +27,9 @@
                                on by the application.  The verbosity level may be used to increase
                                the amount of detail given for the tracked messages.
 
                                on by the application.  The verbosity level may be used to increase
                                the amount of detail given for the tracked messages.
 
-                               This writes to the TTY which is slow, so do not expect it to be able 
+                               This writes to the TTY which is slow, so do not expect it to be able
                                to process and report on a high rate of messages. Also, forwarded
                                to process and report on a high rate of messages. Also, forwarded
-                               messages will reach the intended target, however if the target 
+                               messages will reach the intended target, however if the target
                                attempts to send a response the response will come back to THIS
                                application, and not the message origination; this cannot be a bit
                                of middleware in it's current form.
                                attempts to send a response the response will come back to THIS
                                application, and not the message origination; this cannot be a bit
                                of middleware in it's current form.
        Date:           25 March 2020
        Author:         E. Scott Daniels
 
        Date:           25 March 2020
        Author:         E. Scott Daniels
 
+       Caution:        This example code is pulled directly into one or more of the documents
+                               (starting from the "start-example" tag below.  Use caution with
+                               line lengths and contents because of the requirement that this
+                               be documentation as well as working code.
 */
 */
+// start-example
 #include <stdio.h>
 #include <unistd.h>
 #include <atomic>
 #include <stdio.h>
 #include <unistd.h>
 #include <atomic>
        by the framework.
 */
 typedef struct {
        by the framework.
 */
 typedef struct {
-       int             vlevel;                         // verbosity level
-       bool    forward;                        // if true, message is forwarded
-       int             stats_freq;                     // header/stats after n messages
-       std::atomic<long>       pcount; // messages processed
-       std::atomic<long>       icount; // messages ignored
-       std::atomic<int>        hdr;    // number of messages before next header
+       int             vlevel;             // verbosity level
+       bool    forward;            // if true, message is forwarded
+       int             stats_freq;         // header/stats after n messages
+       std::atomic<long>       pcount; // messages processed
+       std::atomic<long>       icount; // messages ignored
+       std::atomic<int>        hdr;    // number of messages before next header
 } cb_info_t;
 
 } cb_info_t;
 
-// ----------------------------------------------------------
+// ----------------------------------------------------------------------
 
 /*
        Dump bytes to tty.
 
 /*
        Dump bytes to tty.
@@ -73,10 +78,10 @@ void dump( unsigned const char* buf, int len ) {
        for( i = 0; i < len; i++ ) {
                cheater[j++] =  isprint( buf[i] ) ? buf[i] : '.';
                fprintf( stdout, "%02x ", buf[i] );
        for( i = 0; i < len; i++ ) {
                cheater[j++] =  isprint( buf[i] ) ? buf[i] : '.';
                fprintf( stdout, "%02x ", buf[i] );
-                       
+
                if( j == 16 ) {
                        cheater[j] = 0;
                if( j == 16 ) {
                        cheater[j] = 0;
-                       fprintf( stdout, " | %s\n<RD> %04x | ", cheater, i+1 ); 
+                       fprintf( stdout, " | %s\n<RD> %04x | ", cheater, i+1 );
                        j = 0;
                }
        }
                        j = 0;
                }
        }
@@ -87,7 +92,7 @@ void dump( unsigned const char* buf, int len ) {
                        fprintf( stdout, "   " );
                }
                cheater[j] = 0;
                        fprintf( stdout, "   " );
                }
                cheater[j] = 0;
-               fprintf( stdout, " | %s\n", cheater );  
+               fprintf( stdout, " | %s\n", cheater );
        }
 }
 
        }
 }
 
@@ -101,10 +106,10 @@ void stats( cb_info_t& cbi ) {
        curv = cbi.hdr--;
 
        if( curv == 0 ) {                                       // stats when we reach 0
        curv = cbi.hdr--;
 
        if( curv == 0 ) {                                       // stats when we reach 0
-               fprintf( stdout, "ignored: %ld  processed: %ld\n", 
+               fprintf( stdout, "ignored: %ld  processed: %ld\n",
                        cbi.icount.load(), cbi.pcount.load() );
                if( cbi.vlevel > 0 ) {
                        cbi.icount.load(), cbi.pcount.load() );
                if( cbi.vlevel > 0 ) {
-                       fprintf( stdout, "\n     %5s %5s %2s %5s\n", 
+                       fprintf( stdout, "\n     %5s %5s %2s %5s\n",
                                "MTYPE", "SUBID", "ST", "PLLEN" );
                }
 
                                "MTYPE", "SUBID", "ST", "PLLEN" );
                }
 
@@ -112,8 +117,8 @@ void stats( cb_info_t& cbi ) {
        }
 }
 
        }
 }
 
-void cb1( Message& mbuf, int mtype, int subid, int len,
-                               Msg_component payload,  void* data ) {
+void cb1( xapp::Message& mbuf, int mtype, int subid, int len,
+                               xapp::Msg_component payload,  void* data ) {
        cb_info_t*      cbi;
        long total_count;
 
        cb_info_t*      cbi;
        long total_count;
 
@@ -135,7 +140,7 @@ void cb1( Message& mbuf, int mtype, int subid, int len,
 
        if( cbi->forward ) {
                // forward with no change to len or payload
 
        if( cbi->forward ) {
                // forward with no change to len or payload
-               mbuf.Send_msg( Message::NO_CHANGE, NULL );
+               mbuf.Send_msg( xapp::Message::NO_CHANGE, NULL );
        }
 }
 
        }
 }
 
@@ -143,8 +148,8 @@ void cb1( Message& mbuf, int mtype, int subid, int len,
        registered as the default callback; it counts the
        messages that we aren't giving details about.
 */
        registered as the default callback; it counts the
        messages that we aren't giving details about.
 */
-void cbd( Message& mbuf, int mtype, int subid, int len,
-                               Msg_component payload,  void* data ) {
+void cbd( xapp::Message& mbuf, int mtype, int subid, int len,
+                               xapp::Msg_component payload,  void* data ) {
        cb_info_t*      cbi;
 
        if( (cbi = (cb_info_t *) data) == NULL ) {
        cb_info_t*      cbi;
 
        if( (cbi = (cb_info_t *) data) == NULL ) {
@@ -156,7 +161,7 @@ void cbd( Message& mbuf, int mtype, int subid, int len,
 
        if( cbi->forward ) {
                // forward with no change to len or payload
 
        if( cbi->forward ) {
                // forward with no change to len or payload
-               mbuf.Send_msg( Message::NO_CHANGE, NULL );
+               mbuf.Send_msg( xapp::Message::NO_CHANGE, NULL );
        }
 }
 
        }
 }
 
@@ -187,7 +192,7 @@ int main( int argc, char** argv ) {
                                cbi->forward = true;
                                break;
 
                                cbi->forward = true;
                                break;
 
-                       case 'p':                                       // define port
+                       case 'p':                                       // define port
                                port = argv[ai+1];
                                ai++;
                                break;
                                port = argv[ai+1];
                                ai++;
                                break;
@@ -219,9 +224,13 @@ int main( int argc, char** argv ) {
 
                        default:
                                fprintf( stderr, "unrecognised option: %s\n", argv[ai] );
 
                        default:
                                fprintf( stderr, "unrecognised option: %s\n", argv[ai] );
-                               fprintf( stderr, "usage: %s [-f] [-p port] [-s stats-freq]  [-t thread-count] [-v | -V n] msg-type1 ... msg-typen\n", argv[0] );
-                               fprintf( stderr, "\tstats frequency is in number of messages received\n" );
-                               fprintf( stderr, "\tverbose levels (-V) 0 counts only, 1 message info 2 payload dump\n" );
+                               fprintf( stderr, "usage: %s [-f] [-p port] "
+                                                               "[-s stats-freq]  [-t thread-count] "
+                                                               "[-v | -V n] msg-type1 ... msg-typen\n",
+                                                               argv[0] );
+                               fprintf( stderr, "\tstats frequency is based on # of messages received\n" );
+                               fprintf( stderr, "\tverbose levels (-V) 0 counts only, "
+                                                               "1 message info 2 payload dump\n" );
                                exit( 1 );
                }
 
                                exit( 1 );
                }
 
@@ -235,7 +244,7 @@ int main( int argc, char** argv ) {
        x = std::unique_ptr<Xapp>( new Xapp( port, cbi->forward ) );
 
        // register callback for each type on the command line
        x = std::unique_ptr<Xapp>( new Xapp( port, cbi->forward ) );
 
        // register callback for each type on the command line
-       while( ai < argc ) {    
+       while( ai < argc ) {
                mtype = atoi( argv[ai] );
                ai++;
                fprintf( stderr, "<RD> capturing messages for type %d\n", mtype );
                mtype = atoi( argv[ai] );
                ai++;
                fprintf( stderr, "<RD> capturing messages for type %d\n", mtype );