Next: Job Management
Up: Some Useful Programs
Previous: Some Useful Programs
  Contents
As our first full-blown code example of the ground covered in this chapter, we will
create an echolalia server and client. Echolalia is a psychiatric disorder in which
causes those afflicted to mechanically repeat the uttering of other people, kinda
of like a ditto-head, ``Johnson, we should buy more shares of SCO.'' ``Yes boss,
we should by more shares of SCO.'', but much more insidious.
Our echolalia server will simply repeat everything ``said'' by our echolalia client,
and our echolalia client will ``say'' only those things pass to it via the command
line. So, for example, we would first start the echolalia server as follows
% echolaliaserver
After the echolalia server is up and running we could start the echolalia client as
follows
% echolaliaclient
The echolalia client will then wait for user input. For example, the user my type
in the following, then hit return
Hey, you repeat everything I say.
The server would then print out
Hey, you repeat everything I say.
The idea is relatively simple, though it can be of use in tracking networking problems
between a server and client. Let us now move on to code.
The full code for the echolalia client is as follows
#include <string.h>
#include <stdio.h>
#include "GAT.h"
static GATPipe Obtain_GATPipe( void );
static void Send_Messages( GATPipe pipe );
int main( int argc, char *argv[] )
{
GATPipe pipe;
/* Obtain GATPipe */
pipe = Obtain_GATPipe();
/* Send Messages on GATPipe */
if( NULL != pipe )
Send_Messages( pipe );
/* Return to OS */
return 1;
}
static GATPipe Obtain_GATPipe( void )
{
GATPipe pipe;
GATResult result;
const char *path;
GATObject object;
GATList_String paths;
GATString stringPath;
GATContext context;
GATTable description;
GATList_String_Iterator beginning;
GATAdvertService advertService;
/* Set GATPipe */
pipe = NULL;
/* Create GATContext */
context = GATContext_Create();
/* Check GATContext creation */
if( NULL != context )
{
/* Create GATAdvertService */
advertService = GATAdvertService_Create( context, NULL );
/* Check GATAdvertService creation */
if( NULL != advertService )
{
/* Create GATTable */
description = GATTable_Create();
/* Check GATTable creation */
if( NULL != description )
{
/* Add Meta-Data to GATTable */
result = GATTable_Add_String( description,
(const void *) "name",
"echolalia" );
/* Check last call */
if( GAT_SUCCEEDED( result ) )
{
/* Find GATPipe */
result = GATAdvertService_Find( advertService, description, &paths );
/* Check last call */
if( GAT_SUCCEEDED( result ) )
{
/* Obtain Beginning Iterator */
beginning = GATList_String_Begin( paths );
/* Check last call */
if( NULL != beginning )
{
/* Check for non-empty list */
if( beginning != GATList_String_End( paths ) )
{
/* Get path */
path = GATList_String_Get( beginning );
/* Create GATString */
stringPath = GATString_Create( path, strlen( path ) + 1, "ASCII" );
/* Check GATString creation */
if( NULL != stringPath )
{
/* Get Advertisable */
result = GATAdvertService_GetAdvertisable( advertService,
stringPath,
&object );
/* Check Last Call */
if( GAT_SUCCEEDED( result ) )
{
/* Convert GATObject to GATPipe */
pipe = GATObject_ToGATPipe( object );
}
/* Destroy GATString */
GATString_Destroy( &stringPath );
}
}
}
/* Destroy GATList_String */
GATList_String_Destroy( &paths );
}
}
/* Destroy GATTable */
GATTable_Destroy( &description );
}
/* Destroy GATAdvertService */
GATAdvertService_Destroy( &advertService );
}
/* Destroy GATContext */
GATContext_Destroy( &context );
}
/* Return to caller */
return pipe;
}
static void Send_Messages( GATPipe pipe )
{
GATuint32 size;
char input[2048];
GATResult result;
GATObject object;
GATuint32 writtenBytes;
/* Convert GATPipe to GATObject */
object = GATPipe_ToGATObject( pipe );
/* Loop Forever */
while( GATTrue == GATTrue )
{
/* Read Input from User */
if( NULL != fgets( input, sizeof( input ), stdin ) )
{
/* Obtain size */
size = (GATuint32) (strlen( input ) + 1);
/* Write to GATObject */
result = GATStreamable_Write( object,
(const void *) input,
size,
&writtenBytes );
/* Check for Error */
if( GAT_FAILED( result ) )
{
/* Print Error Message */
printf( "An error occured in writing: %s\n", input );
}
}
else
{
/* Print Error Message */
printf( "An error occured in reading from stdin.\n" );
}
}
/* Return to caller */
return;
}
The full code for the echolalia server is as follows
#include <stdio.h>
#include <string.h>
#include "GAT.h"
static void Read_From_GATEndpoint( GATEndpoint endpoint );
static GATResult Advertise_GATEndpoint( GATContext context, GATEndpoint endpoint );
int main( int argc, char *argv[] )
{
GATResult result;
GATContext context;
GATEndpoint endpoint;
/* Create GATContext */
context = GATContext_Create();
/* Check GATContext creation */
if( NULL != context )
{
/* Create GATEndpoint */
endpoint = GATEndpoint_Create( context, NULL );
/* Check GATEndpoint creation */
if( NULL != endpoint )
{
/* Advertise GATEndpoint */
result = Advertise_ GATEndpoint( context, endpoint );
/* Check Last Call */
if( GAT_SUCCEEDED( result ) )
{
/* Read GATEndpoint */
Read_From_GATEndpoint( endpoint );
}
/* Destroy GATEndpoint */
GATEndpoint_Destroy( &endpoint );
}
/* Destroy GATContext */
GATContext_Destroy( &context );
}
/* Return to OS */
return 1;
}
static GATResult Advertise_GATEndpoint( GATContext context, GATEndpoint endpoint )
{
GATString path;
GATResult result;
GATObject object;
GATTable description;
GATAdvertService advertService;
/* Set GATResult */
result = GAT_MEMORYFAILURE;
/* Create GATAdvertService */
advertService = GATAdvertService_Create( context, NULL );
/* Check GATAdvertService creation */
if( NULL != advertService )
{
/* Convert GATEndpoint to GATObject */
object = GATEndpoint_ToGATObject( endpoint );
/* Create GATTable */
description = GATTable_Create();
/* Check GATTable create */
if( NULL != description )
{
/* Create GATString */
path = GATString_Create( "/usr/share/bin/echolaliad", 26, "ASCII" );
/* Check GATString creation */
if( NULL != path )
{
/* Add Meta-Data to GATTable */
result =
GATTable_Add_String( description, (const void *) "name", "echolalia" );
/* Check last call */
if( GAT_SUCCEEDED( result ) )
{
/* Add GATEndpoint to GATAdvertService */
result =
GATAdvertService_Add( advertService, object, description, path );
}
/* Destroy GATString */
GATString_Destroy( &path );
}
/* Destroy GATTable */
GATTable_Destroy( &description );
}
/* Destroy GATAdvertService */
GATAdvertService_Destroy( &advertService );
}
/* Return to caller */
return result;
}
static void Read_From_GATEndpoint( GATEndpoint endpoint )
{
GATPipe pipe;
GATResult result;
char input[2048];
GATObject object;
GATuint32 readBytes;
/* Wait For Incoming Call */
result = GATEndpoint_Listen( endpoint, &pipe );
/* Check last call */
if( GAT_SUCCEEDED( result ) )
{
/* Convert GATPipe to GATObject */
object = GATPipe_ToGATObject( pipe );
/* Loop Until Failure */
while( GAT_SUCCEEDED( result ) )
{
/* Read from GATObject */
result = GATStreamable_Read( object, (void *) input, 2048, &readBytes );
/* Check Last Call */
if( GAT_SUCCEEDED( result ) )
{
/* Print Result */
printf( "%s", input );
}
}
/* Destroy GATPipe */
GATPipe_Destroy( &pipe );
}
/* Return to caller */
return;
}
Next: Job Management
Up: Some Useful Programs
Previous: Some Useful Programs
  Contents
Andre Merzky
2004-05-13
|