Previous Menu Next
Example Code: Receiving Events
The following code listing provides a trivial example of receiving events from an engine.

In order to keep the example small, safety checks such as examining API call return values are not performed.

#include <stdio.h>
#ifndef  _WIN32
#include <unistd.h>    /* for getopt() */
#else
#include <WINSOCK.H>
#endif
#include <sys/signal.h>
#include <EIO/EIO.h>

#define MAX_PDU_LEN 1000 /* max size of event buffer */

int numEvents = 0;
const char* const progname = "ecsreceiver";

/* 	When SIGNET or SIGTERM is called, reset event
	I/O connection and the stack
*/
void
signal_handler( int sig )
{
	EIO_stackReset();
	ESOK_stackReset();
	exit(0);
}


/*	Called when an event is ready for collecting
	All this example does is print out some
	argument values
*/
int32
recvFn(
	EIO_ConnectionId id,
	const void* pdu,
	int32 pdu_length,
	int32 create_time,
	const char* encoding_type,
	const char* syntax,
      )
{
	numEvents++;
	fprintf (stdout, "%d:%d:%s:%s:",
		  numEvents, create_time, 
		  encoding_type, syntax);

    /* NOTE: pdu is a pointer to a block of data that is 
    ** not necessarily nul-terminated!
    */
    fwrite (pdu, 1, pdu_length, stdout);
    fprintf( stdout, "\n" );
}


int
main( int argc, char *argv[] )
{
	ESOK_Remote remote;
	ESOK_ConnectionId cid= 0;
	EIO_ConnectionId eid = 0;
	int32 create_time = 0;
	int32 streamId = 0;
	char* syntax = NULL;

	/* encoding type is mdl 
	   for 8-bit text events */
	char* encoding_type = "mdl";

	char  pdu [MAX_PDU_LEN];
	int32 pdu_length = 0;
	int instance = 1;

	extern char* optarg;

	int rc;
	
	signal (SIGTERM, signal_handler);
	signal (SIGINT, signal_handler);
  
	ESOK_stackInit ();		/*manpage*/
  
	EIO_stackInit ();		/*manpage*/
  
	ESOK_buildRemote(instance, &remote); /*manpage*/
  
	ESOK_open( &remote, &cid );	         /*manpage*/
  
	EIO_open (cid, &eid);		/*manpage*/

  
	EIO_addFilter (			/*manpage*/
			eid, encoding_type, 
			syntax );
  
					/*manpage*/
	EIO_registerReceiveFn( eid, recvFn );
					/*manpage*/
  
	while ( (rc = ESOK_process(1000)) 
		       == ECS_SUCCESS
	        || rc == ESOK_NOTHING_DONE );

	/* We only get here if ESOK_process() returned an error */
	fprintf (stderr, "Error %d processing events from ECS.\n", rc);
	
  
	EIO_close( eid );		/*manpage*/
  
	EIO_stackReset();		/*manpage*/
  
	ESOK_close( cid );		/*manpage*/
  
	ESOK_stackReset();		/*manpage*/

	exit (0);
}