1 // vim: ts=4 sw=4 noet:
3 --------------------------------------------------------------------------------
4 Copyright (c) 2018-2019 AT&T Intellectual Property.
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
10 http://www.apache.org/licenses/LICENSE-2.0
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 --------------------------------------------------------------------------------
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.
27 Author: E. Scott Daniels
41 //---- support -----------------------------------------------------------------------------
43 static void bad_arg( char* what ) {
44 fprintf( stderr, "[ERR] option is unrecognised or isn't followed by meaningful data: %s\n", what );
47 static void usage( char* argv0 ) {
48 fprintf( stderr, "usage: %s [-d fifo-dir] [-e] [-m msg-type] [-s]\n", argv0 );
49 fprintf( stderr, " -d dir (default is /tmp/mcl/fifos)\n" );
50 fprintf( stderr, " -e disable extended headers expectation in FIFO data\n" );
51 fprintf( stderr, " -m msg-type (default is 0)\n" );
52 fprintf( stderr, " -M max msg to read (default is all)\n" );
53 fprintf( stderr, " -s stats only mode\n" );
57 We exit on any trapped signal so that we can kill -15 the proecss
58 and still get gcoverage information to keep sonar happy.
60 static void sigh( int sig ) {
61 fprintf( stderr, "\n[INFO] exiting on signal %d\n", sig );
65 //------------------------------------------------------------------------------------------
66 int main( int argc, char** argv ) {
67 void* ctx; // the mc listener library context
68 char* dname = "/tmp/mcl/fifos"; // default directory where we open fifos
69 int pidx = 1; // parameter index
75 int long_hdrs = 1; // -e is on command line turns this off, by default we expect long headers
77 char timestamp[MCL_TSTAMP_SIZE]; // we'll get the timestamp from this
80 int max = 0; // we'll force one reader down early to simulate MC going away
82 signal( SIGINT, sigh );
83 signal( SIGTERM, sigh );
85 while( pidx < argc && argv[pidx][0] == '-' ) { // simple argument parsing (-x or -x value)
86 switch( argv[pidx][1] ) {
89 dname = strdup( argv[pidx+1] );
92 bad_arg( argv[pidx] );
106 if( pidx+1 < argc ) {
107 mtype = atoi( argv[pidx+1] );
110 bad_arg( argv[pidx] );
116 if( pidx+1 < argc ) {
117 max = atoi( argv[pidx+1] );
120 bad_arg( argv[pidx] );
135 bad_arg( argv[pidx] );
148 ctx = mcl_mk_context( dname ); // initialise the library context
150 fprintf( stderr, "[FAIL] couldn't initialise the mc listener library" );
154 fprintf( stderr, "[INFO] max = %d\n", max );
155 while( max == 0 || count < max ) {
156 len = mcl_fifo_tsread1( ctx, mtype, buf, sizeof( buf ) -1, long_hdrs, timestamp );
159 if( time( NULL ) > blabber ) {
160 fprintf( stdout, "[%d] %ld messages received\n", mtype, count );
161 blabber = time( NULL ) + 2;
165 fprintf( stdout, "[%d] ts=%s count=%ld len=%d msg=%s\n", mtype, timestamp, count, len, buf );
177 fprintf( stderr, "[INFO] max reached: %d\n", max );