Next: An Equal Opportunity Employer
Up: Some Useful Programs
Previous: Some Useful Programs
  Contents
As an example of the ground covered in this chapter, we now introduce a command line
utility which we'll paint with the epithet hwloupe, the prefix hw standing for
hardware and the suffix loupe representing itself, a small magnifying glass.
This command line utility will when presented with a set of name/value pairs
% hwloupe virtualorg memory.size=1024 cpu.type=powerpc
will create a GATHardwareResourceDescription corresponding to this set of name value
pairs, then find all resources which are described by this GATHardwareResourceDescription
in the virtual organization virtualorg. Upon doing so it will print out its finding
in the following format
memory.size=1024
memory.accesstime=10
memory.str=100
machine.type=Power Macintosh
machine.node=L-DaVinci1s-Computer.local
cpu.type=powerpc
cpu.speed=1
disk.size=100
disk.acesstime=4
disk.str=500
memory.size=2048
memory.accesstime=5
memory.str=1000
machine.type=Power Macintosh
machine.node=L-DaVinci2s-Computer.local
cpu.type=powerpc
cpu.speed=10
disk.size=1000
disk.acesstime=4
disk.str=5000
...
Here's the full source of hwloupe
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "GAT.h"
static void printGATList_GATResource( GATList_GATResource resources );
static GATResult addNameValue( GATTable table, const char *name, const char *value );
int main(int argc, char *argv[])
{
int counter;
char *name;
char *value;
int returnValue;
GATTable table;
GATResult result;
GATString string;
GATContext context;
GATList_GATResource resources;
GATResourceBroker resourceBroker;
GATHardwareResourceDescription hardwareResDes
/* Check command line arguments */
if( argc < 2 )
{
/* Print out error message */
printf( "Usage: hwloupe virtualorg [name=value]*\n" );
/* Return to OS */
return 1;
}
/* Set result to a memory failure */
result = GAT_MEMORYFAILURE;
/* Create GATContext */
context = GATContext_Create( );
/* Check GATContext creation */
if( NULL != context )
{
/* Create GATString */
string = GATString_Create( argv[1], strlen( argv[1] ) + 1, "ASCII" );
/* Check GATString creation */
if( NULL != string )
{
/* Create GATResourceBroker */
resourceBroker = GATResourceBroker_Create( context, NULL, string );
/* Check GATResourceBroker creation */
if( NULL != resourceBroker )
{
/* Create GATTable */
table = GATTable_Create();
/* Check GATTable creation */
if( NULL != GATTable)
{
/* Loop over command line arguments */
for( count = 2; count < argc; count++ )
{
/* Obtain name */
name = strtok( argv[count], "=" );
/* Obtain value */
if( NULL != name )
value = strtok( NULL, "=" );
/* Place name and value in GATTable */
result = addNameValue( table, name, value );
/* Print out addition error */
if( GAT_FAILED( result ) )
printf( "Error in adding the name/value pair: %s/%s\n", name, value );
}
/* Set result to a memory failure */
result = GAT_MEMORYFAILURE;
/* Create GATHardwareResourceDescription *
hardwareResDes =
GATHardwareResourceDescription_Create( table );
/* Check GATHardwareResourceDescription creation */
if( NULL != hardwareResDes )
{
/* Find Resources */
result =
GATResourceBroker_FindResources( resourceBroker,
hardwareResDes,
&resources );
/* Check success of last call */
if( GAT_SUCCEEDED( result ) )
{
/* Print resources */
printGATList_GATResource( resources );
/* Destroy GATList_GATResource */
GATList_GATResource_Destroy( &resources );
}
/* Destroy GATHardwareResourceDescription */
GATHardwareResourceDescription_Destroy( &hardwareResDes );
}
/* Destroy GATTable */
GATTable_Destroy( &table );
}
/* Destroy GATResourceBroker */
GATResourceBroker_Destroy( &resourceBroker );
}
/* Destroy GATString */
GATString_Destroy( &string );
}
/* Destroy GATContext */
GATContext_Destroy( &context );
}
/* Return to OS */
return returnValue;
}
static GATResult addNameValue( GATTable table, const char *name, const char *value )
{
int added;
GATResult result;
GATfloat32 floatValue;
/* Set result to an invalid parameter */
result = GAT_INVALID_PARAMETER;
/* Check parameters */
if( (NULL != table) && (NULL != name) & (NULL != value) )
{
/* Set boolean */
added = 0;
/* Check if name is "memory.size" */
if( 0 == strcmp( name, "memory.size" ) )
{
/* Convert value into GATfloat32 */
floatValue = (GATfloat32) atof( value );
result = GATTable_Add_float( table, (const void *) name, floatValue );
}
/* Check if name is "memory.accesstime" */
if( 0 == strcmp( name, "memory.accesstime" ) )
{
/* Convert value into GATfloat32 */
floatValue = (GATfloat32) atof( value );
/* Add name/value pair */
result = GATTable_Add_float( table, (const void *) name, floatValue );
/* Flip boolean */
added = 1;
}
/* Check if name is "memory.str" */
if( 0 == strcmp( name, "memory.str" ) )
{
/* Convert value into GATfloat32 */
floatValue = (GATfloat32) atof( value );
/* Add name/value pair */
result = GATTable_Add_float( table, (const void *) name, floatValue );
/* Flip boolean */
added = 1;
}
/* Check if name is "cpu.speed" */
if( 0 == strcmp( name, "cpu.speed" ) )
{
/* Convert value into GATfloat32 */
floatValue = (GATfloat32) atof( value );
/* Add name/value pair */
result = GATTable_Add_float( table, (const void *) name, floatValue );
/* Flip boolean */
added = 1;
}
/* Check if name is "disk.size" */
if( 0 == strcmp( name, "disk.size" ) )
{
/* Convert value into GATfloat32 */
floatValue = (GATfloat32) atof( value );
/* Add name/value pair */
result = GATTable_Add_float( table, (const void *) name, floatValue );
/* Flip boolean */
added = 1;
}
/* Check if name is "disk.accesstime" */
if( 0 == strcmp( name, "disk.accesstime" ) )
{
/* Convert value into GATfloat32 */
floatValue = (GATfloat32) atof( value );
/* Add name/value pair */
result = GATTable_Add_float( table, (const void *) name, floatValue );
/* Flip boolean */
added = 1;
}
/* Check if name is "disk.str" */
if( 0 == strcmp( name, "disk.str" ) )
{
/* Convert value into GATfloat32 */
floatValue = (GATfloat32) atof( value );
/* Add name/value pair */
result = GATTable_Add_float( table, (const void *) name, floatValue );
/* Flip boolean */
added = 1;
}
/* All others add as string values */
if( 0 == added )
result = GATTable_Add_String( table, (const void *) name, value );
}
/* Return to caller */
return result;
}
static void printGATList_GATResource( GATList_GATResource resources )
{
int counter;
void **keys;
float nextFloat;
GATResult result;
GATType nextType;
char nextString[2048];
GATTable_const table;
GATResource resource;
GATList_GATResource_Iterator end;
GATList_GATResource_Iterator current;
GATHardwareResource hardwareResource;
GATHardwareResourceDescription hardwareResDes;
/* Check parameters */
if( NULL != resources )
{
/* Obtain Beginning Iterator */
current = GATList_GATResource_Begin( resources );
/* Check last call */
if( NULL != current )
{
/* Obtain Ending Iterator */
end = GATList_GATResource_End( resources );
/* Check last call */
if( NULL != end )
{
/* Loop over resources */
while( (NULL != current) && (current != end) )
{
/* Obtain next GATResource */
resource = GATList_GATResource_Get( current );
/* Check last call */
if( NULL != resource )
{
/* Convert the GATResource to a GATHardwareResource
hardwareResource = GATResource_ToGATHardwareResource( resource );
/* Obtain the GATHardwareResourceDescription */
result =
GATHardwareResource_GetResourceDescription( hardwareResource,
&hardwareResDes );
/* Check last call */
if( GAT_SUCCEEDED( result ) )
{
/* Obtain GATTable */
table =
GATHardwareResourceDescription_GetDescription( hardwareResDes );
/* Check last call */
if( NULL != table )
{
/* Obtain keys */
keys = GATTable_GetKeys( table );
/* Check last call */
if( NULL != keys )
{
/* Set counter */
counter = 0;
/* Loop over keys */
while( NULL != keys[counter] )
{
/* Obtain value's type */
nextType = GATTable_Get_ElementType( table, keys[counter]);
/* Check type */
if( GATfloat32 == nextType )
{
/* Obtain float */
result =
GATTable_Get_float( table, keys[counter], &nextFloat );
/* Check last call */
if( GAT_SUCCEEDED( result ) )
{
/* Print name=value */
printf( "%s=%f\n", (char *) keys[counter], nextFloat );
}
}
/* Check type */
if( GATType_String == nextType )
{
/* Obtain string */
result =
GATTable_Get_String( table, keys[counter],
nextString, 2048 );
/* Check last call */
if( GAT_SUCCEEDED( result ) )
{
/* Print name=value */
printf( "%s=%s\n", (char *) keys[counter], nextString );
}
}
/* Increament counter */
counter = counter + 1;
}
/* Print blank line to separate resources */
printf( "\n" );
/* Destroy keys */
GATTable_ReleaseKeys( table, keys );
}
}
/* Destroy GATHardwareResourceDescription */
GATHardwareResourceDescription_Destroy( &hardwareResDes );
}
}
/* Obtain Next Iterator */
current = GATList_GATResource_Next( current );
}
}
}
}
}
Andre Merzky
2004-05-13
|