Add ability to capture raw messages
[ric-app/mc.git] / src / sidecars / listener / pipe_reader.c
1 // vim: ts=4 sw=4 noet:
2 /*
3 --------------------------------------------------------------------------------
4         Copyright (c) 2018-2019 AT&T Intellectual Property.
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10            http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 --------------------------------------------------------------------------------
18 */
19
20 /*
21         Mnemonic:       pipe_reader.c
22         Abstract:       Read a single pipe which was associated with a message type.  This
23                                 programme is primarily for verification or example of how to use the
24                                 read1() function in the mc-listener library.
25
26         Date:           22 August 2019
27         Author:         E. Scott Daniels
28 */
29
30 #include <unistd.h>
31 #include <errno.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <time.h>
35 #include <string.h>
36
37
38 #include "mcl.h"
39
40 //---- support -----------------------------------------------------------------------------
41
42 static void bad_arg( char* what ) {
43         fprintf( stderr, "[ERR] option is unrecognised or isn't followed by meaningful data: %s\n", what );
44 }
45
46 static void usage( char* argv0 ) {
47         fprintf( stderr, "usage: %s [-d fifo-dir] [-e] [-m msg-type] [-s]\n", argv0 );
48         fprintf( stderr, "   -d  dir (default is /tmp/mcl/fifos)\n" );
49         fprintf( stderr, "   -e  disable extended headers expectation in FIFO data\n" );
50         fprintf( stderr, "   -m  msg-type (default is 0)\n" );
51         fprintf( stderr, "   -s  stats only mode\n" );
52 }
53
54 //------------------------------------------------------------------------------------------
55 int main( int argc,  char** argv ) {
56         void*   ctx;                                                    // the mc listener library context
57         char*   dname = "/tmp/mcl/fifos";               // default directory where we open fifos
58         int             pidx = 1;                                               // parameter index
59         int             error = 0;
60         int             len;
61         int             mtype = 0;
62         char    buf[4096];
63         int             flush_often = 0;
64         int             long_hdrs = 1;                                  // -e is on command line turns this off, by default we expect long headers
65         int             stats_only = 0;
66         char    timestamp[MCL_TSTAMP_SIZE];             // we'll get the timestamp from this
67         long    count = 0;
68         int             blabber = 0;
69
70         while( pidx < argc && argv[pidx][0] == '-' ) {                  // simple argument parsing (-x  or -x value)
71                 switch( argv[pidx][1] ) {
72                         case 'd':
73                                 if( pidx+1 < argc ) {
74                                         dname = strdup( argv[pidx+1] );
75                                         pidx++; 
76                                 } else {
77                                         bad_arg( argv[pidx] ); 
78                                         error = 1;
79                                 }       
80                                 break;
81
82                         case 'e':
83                                 long_hdrs = 0;
84                                 break;
85
86                         case 'f':
87                                 flush_often = 1;
88                                 break;
89
90                         case 'm':
91                                 if( pidx+1 < argc ) {
92                                         mtype = atoi( argv[pidx+1] );
93                                         pidx++; 
94                                 } else {
95                                         bad_arg( argv[pidx] ); 
96                                         error = 1;
97                                 }       
98                                 break;
99
100                         case 's':
101                                 stats_only = 1;
102                                 break;
103
104                         case 'h':
105                         case '?':
106                                 usage( argv[0] );
107                                 exit( 0 );
108                                 break;
109
110                         default:
111                                 bad_arg( argv[pidx] );
112                                 error = 1;
113                                 break;
114                 }
115
116                 pidx++;
117         }
118
119         if( error ) {
120                 usage( argv[0] );
121                 exit( 1 );
122         }
123         
124         ctx = mcl_mk_context( dname );                  // initialise the library context
125         if( ctx == NULL ) {
126                 fprintf( stderr, "[FAIL] couldn't initialise the mc listener library" );
127                 exit( 1 );
128         }
129
130         while( 1 ) {
131                 len = mcl_fifo_tsread1( ctx, mtype, buf, sizeof( buf ) -1, long_hdrs, timestamp );
132                 if( len > 0 ) {
133                         if( stats_only ) {
134                                 if( time( NULL ) > blabber ) {
135                                         fprintf( stdout, "[%d] %ld messages received\n", mtype, count );
136                                         blabber = time( NULL ) + 2;
137                                 }
138                         } else {
139                                 buf[len] = 0;
140                                 fprintf( stdout, "[%d] ts=%s count=%ld len=%d  msg=%s\n",  mtype, timestamp,  count, len, buf );
141                                 if( flush_often ) {
142                                         fflush( stdout );
143                                 }
144                         }
145
146                         count++;
147                 }
148         }
149
150         fprintf( stderr, "[INFO] mc listener is finished.\n" );
151 }
152