GridLab
Grid Application Toolkit

A simple API for Grid Applications
GAT

Menu



next up previous contents
Next: Event System Up: Some Useful Programs Previous: Some Useful Programs   Contents

See GAT run. Run GAT, run

In this chapter we will divert a bit from our usual track and present a rather large example program instead of several smaller programs. Though, we will keep with out habit of christening the example with a epithet suggestive of the program's functionality. Do you remember those children's books which you used to read in first grade to get your head around the complexities of this little miracle of the written word? Yes, I knew you'd remember. The one timeless poetic line from those days which has stuck with me since those days, ``See Jane run. Run Jane, run.'' Beautific! In honour of this highest of poetics we christen this example of this chapter gatrun.

What does this gatrun thing do exactly you ask? Well, everything. Instead of giving a laundry list of the functionality of gatrun, we'll simply disperse with all these perfunctory ceremoniousness and just drop the manpage. Here it is

NAME
     gatrun - runs or otherwise manipulates a specified job 

SYNOPSIS
     gatrun {-k jobid           | 
             -s jobid           | 
             -u jobid           | 
             -c jobid           | 
             vo file.hrd file.sd}

DESCRIPTION
     The command line utility gatrun is used to run or otherwise manipulate
     a specified job. In particular the main functionalities provided by the
     utility are as follows:

     - Killing a job with a specified jobid  
     - Running a job specified through GATRL 
     - Unscheduling a job with a particular jobid
     - Checkpointing a job with a specified jobid
     - Finding the status of a job with a particular jobid

     In addition this utility introduces a new specification, as if there
     were a dearth of them, called GATRL, which aims to be the most simple
     specification of a hardware resource description and a software 
     description known to man. 

     A GATRL file is simply a set of name/value pairs separated by an "="
     sign

     name=value

     Each name/value pair occupies a single line in a GATRL file. For example, 
     to specify the name value pairs size=big and color=red and a GATRL file
     would contain the following lines

     size=big
     color=red

     The motto for GATRL is "GATRL it's not rocket science."

     Using the simple GATRL file format introduced above one can specify a
     hardware resource description and a software description. One simply 
     uses the corresponding supported name/value pairs in the GATRL file.

     For example, if I wanted to specify a hardware resource description
     using a GATRL file I might write something like this

     memory.size=1024
     machine.type=Power Macintosh
     cpu.type=powerpc
     
     Note that in a hardware resource description the value corresponding
     to the name "memory.size" is a Float the utility gatrun takes care to
     make sure that the supported names have values of the apropos type.
     All other values are treated as strings. 

     One tricky point which arises is the conversion of a GATRL file in
     to a software description. This is tricky as a software description
     contains various classes. For example the name "location" has a value
     of type GATLocation. For most of these types the mapping between a
     GATRL file and the type is obvious. For example one know what is 
     implied by

     location=http://www.google.com/index.html

     or 

     stdin=file:///Users/leonardo/stdin.tmp

     The tricky values to deal with correspond to the names 

     arguments - The value is of type GATList_String
     environment - The value is of type GATTable

     (Note, gatrun does not support the names "pre-staged files" and 
     "post-staged files".) Actually how the names "arguments" and
     "environment" are dealt with in a GATRL file is also relatively 
     simple.

     The name "arguments" has a value which is a ";" separated set of
     strings. For example for the command ls one might pass 

     arguments=-l;/tmp/

     Similarly, the value for the name "environment" is a ";" separated
     set of name/value pairs where each name and value is separated by
     an "=" sign. For example, to set the environment variable "HOME" to
     "/Users/leonardo" and the environment variable "SHELL" to the value
     "/bin/tcsh" one would have a line in the software description GATRL
     file looking like

     environment=HOME=/Users/leonardo;SHELL=/bin/tcsh

     Quite simple really. 

     The following options are available:

     -k jobid 
            Kills the job specified by the passed jobid

     -s jobid 
            Prints out the status of the job specified by the passed jobid 

     -u jobid 
            Unschedules the job specified by the passed jobid 

     -c jobid 
            Checkpoints the job specified by the passed jobid 

EXAMPLES
     The following shows how to kill the job with jobid 132
         
         gatrun -k 132

     To find the status of the job with jobid 423 one would use

         gatrun -s 423

     To unschedule the job with jobid 498 one would use

         gatrun -u 498

     To checkpoint the job with jobid 9AAT67 one would use

         gatrun -c 9AAT67

     Starting the job specified by the GATRL hardware resource description
     file File.hrd and the GATRL software description file File.sd within 
     the virtual organization gridlab.org would look as follows

         gatrun gridlab.org File.hrd File.sd

DIAGNOSTICS
     The gatrun utility exits 0 on success, and >0 if an error occurs.

COMPATIBILITY
     The gatrun utility is compatible with itself, maybe. 

SEE ALSO
     gatsaunter(1), gatamble(1), gatperambulation(1), gatstroll(1) 

STANDARDS
     The gatrun utility conforms to IEEE Std 1003.1-2001 (``POSIX.1''), not!

HISTORY
     An gatrun command appeared in Version 1 of this manual

BUGS
     To maintain backward compatibility with the wheel we have rounded the
     jutting corners of this sharp utility to protect the bunglesome users.

Ok time to jump in with both feet. Here's the code

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "GAT.h"

static void GATRun_PrintUsage( void );
static GATResult GATRun_AdvertiseJob( GATJob job );
static GATResult GATRun_Run( int argc, char *argv[] );
static GATResult GATRun_Run_Run( int argc, char *argv[] );
static GATResult GATRun_Run_Kill( int argc, char *argv[] );
static GATResult GATRun_Run_Status( int argc, char *argv[] );
static GATResult GATRun_Run_Unschedule( int argc, char *argv[] );
static GATResult GATRun_Run_Checkpoint( int argc, char *argv[] );
static GATResult GATRun_CommandLineValid( int argc, char *argv[] );
static GATResult GATRun_GetJobByID( const char *jobid, GATJob *job );
static GATResult GATRun_ParseGATRL( const char *file, GATTable *table );
static GATResult GATRun_CreateJob( int argc, char *argv[], GATJob *job );
static GATResult GATRun_CommandLineValidCaseOne( int argc, char *argv[] );
static GATResult GATRun_CommandLineValidCaseTwo( int argc, char *argv[] );
static  GATResult GATRun_GATTable_AddFloat( GATTable table, const char *name, const char *value );
static  GATResult GATRun_GATTable_AddGATFile( GATTable table, const char *name, const char *value );
static  GATResult GATRun_GATTable_AddGATTable( GATTable table, const char *name, const char *value );
static  GATResult GATRun_GATTable_AddGATLocation( GATTable table, const char *name, const char *value );
static  GATResult GATRun_GATTable_AddGATList_String( GATTable table, const char *name, const char *value );

int main( int argc, char *argv[] )
{
  int returnValue;
  GATResult result;
  
  /* Check Command Line Arguments */
  result = GATRun_CommandLineValid( argc, argv );
  if( GAT_FAILED( result ) )
  {
    /* Print Usage */
    GATRun_PrintUsage();
  }
  else
  {
    /* Run gatrun */
    result = GATRun_Run( argc, argv );
  }
  

  /* Set returnValue */
  if( GAT_SUCCEEDED( result ) )
  {
    returnValue = 0;
  }
  else
  {
    returnValue = 1;
  }
  
  /* Return to OS */
  return returnValue;
}

/**
 * Checks the command line arguments are valid. 
 *
 * @pram argc Number of command line arguments
 * @param argv Command line arguments
 * @return GATResult indicating completion status
 */
static GATResult GATRun_CommandLineValid( int argc, char *argv[] )
{
  GATResult result;
  
  /* Assume Invalid Parameter */
  result = GAT_INVALID_PARAMETER;
  
  /* Check Number of Arguments: Case One -- Kill, Status, Unschedule, and Checkpoint */
  if( 3 == argc )
  {
    /* Check Kill, Status, Unschedule, and Checkpoint Options */
    result = GATRun_CommandLineValidCaseOne( argc, argv );
  }
  
  /* Check Number of Arguments: Case Two -- Run */
  if( 5 == argc )
  {
    /* Check Run Options */
    result = GATRun_CommandLineValidCaseTwo( argc, argv );
  }
      
  /* Return to caller */
  return result;
}

/**
 * Checks the command line arguments are valid for the Kill, Status, Unschedule, 
 * and Checkpoint command line arguments. 
 *
 * @pram argc Number of command line arguments
 * @param argv Command line arguments
 * @return GATResult indicating completion status
 */
static GATResult GATRun_CommandLineValidCaseOne( int argc, char *argv[] )
{
  GATResult result;
  
  /* Assume Invalid Parameter */
  result = GAT_INVALID_PARAMETER;
  
  /* Check Kill */
  if( 0 == strcmp( "-k", argv[1] ) )
    result = GAT_SUCCESS;
  
  /* Check Status */
  if( 0 == strcmp( "-s", argv[1] ) )
    result = GAT_SUCCESS;
  
  /* Check Unschedule */
  if( 0 == strcmp( "-u", argv[1] ) )
    result = GAT_SUCCESS; 
    
  /* Check Checkpoint */
  if( 0 == strcmp( "-c", argv[1] ) )
    result = GAT_SUCCESS;           
        
  /* Return to caller */
  return result;
}

/**
 * Checks the command line arguments are valid for the Run command 
 * line arguments. 
 *
 * @pram argc Number of command line arguments
 * @param argv Command line arguments
 * @return GATResult indicating completion status
 */
static GATResult GATRun_CommandLineValidCaseTwo( int argc, char *argv[] )
{
  /* 
   * Note:
   *
   * We assume all specified files and vo's are valid; thus the existence of the 
   * correct number of command line arguments is sufficient to stamp the args
   * as valid.
   *
   */
  
  /* Return to caller */
  return GAT_SUCCESS;
}

/**
 * Prints the usage for gatrun
 */
static void GATRun_PrintUsage( void )
{
  printf("NAME\n");
  printf("     gatrun - runs or otherwise manipulates a specified job \n");
  printf("\n");
  printf("SYNOPSIS\n");
  printf("     gatrun {-k jobid           | \n");
  printf("             -s jobid           | \n");
  printf("             -u jobid           | \n");
  printf("             -c jobid           | \n");
  printf("             vo file.hrd file.sd}\n");
  printf("\n");
  printf("DESCRIPTION\n");
  printf("     The command line utility gatrun is used to run or otherwise manipulate\n");
  printf("     a specified job. In particular the main functionalities provided by the\n");
  printf("     utility are as follows:\n");
  printf("\n");
  printf("     - Killing a job with a specified jobid  \n");
  printf("     - Running a job specified through GATRL \n");
  printf("     - Unscheduling a job with a particular jobid\n");
  printf("     - Checkpointing a job with a specified jobid\n");
  printf("     - Finding the status of a job with a particular jobid\n");
  printf("\n");
  printf("     In addition this utility introduces a new specification, as if there\n");
  printf("     were a dearth of them, called GATRL, which aims to be the most simple\n");
  printf("     specification of a hardware resource description and a software \n");
  printf("     description known to man. \n");
  printf("\n");
  printf("     A GATRL file is simply a set of name/value pairs separated by an \"=\"\n");
  printf("     sign\n");
  printf("\n");
  printf("     name=value\n");
  printf("\n");
  printf("     Each name/value pair occupies a single line in a GATRL file. For example, \n");
  printf("     to specify the name value pairs size=big and color=red and a GATRL file\n");
  printf("     would contain the following lines\n");
  printf("\n");
  printf("     size=big\n");
  printf("     color=red\n");
  printf("\n");
  printf("     The motto for GATRL is \"GATRL it's not rocket science.\"\n");
  printf("\n");
  printf("     Using the simple GATRL file format introduced above one can specify a\n");
  printf("     hardware resource description and a software description. One simply \n");
  printf("     uses the corresponding supported name/value pairs in the GATRL file.\n");
  printf("\n");
  printf("     For example, if I wanted to specify a hardware resource description\n");
  printf("     using a GATRL file I might write something like this\n");
  printf("\n");
  printf("     memory.size=1024\n");
  printf("     machine.type=Power Macintosh\n");
  printf("     cpu.type=powerpc\n");
  printf("     \n");
  printf("     Note that in a hardware resource description the value corresponding\n");
  printf("     to the name \"memory.size\" is a Float the utility gatrun takes care to\n");
  printf("     make sure that the supported names have values of the apropos type.\n");
  printf("     All other values are treated as strings. \n");
  printf("\n");
  printf("     One tricky point which arises is the conversion of a GATRL file in\n");
  printf("     to a software description. This is tricky as a software description\n");
  printf("     contains various classes. For example the name \"location\" has a value\n");
  printf("     of type GATLocation. For most of these types the mapping between a\n");
  printf("     GATRL file and the type is obvious. For example one know what is \n");
  printf("     implied by\n");
  printf("\n");
  printf("     location=http://www.google.com/index.html\n");
  printf("\n");
  printf("     or \n");
  printf("\n");
  printf("     stdin=file:///Users/leonardo/stdin.tmp\n");
  printf("\n");
  printf("     The tricky values to deal with correspond to the names \n");
  printf("\n");
  printf("     arguments - The value is of type GATList_String\n");
  printf("     environment - The value is of type GATTable\n");
  printf("\n");
  printf("     (Note, gatrun does not support the names \"pre-staged files\" and \n");
  printf("     \"post-staged files\".) Actually how the names \"arguments\" and\n");
  printf("     \"environment\" are dealt with in a GATRL file is also relatively \n");
  printf("     simple.\n");
  printf("\n");
  printf("     The name \"arguments\" has a value which is a \";\" separated set of\n");
  printf("     strings. For example for the command ls one might pass \n");
  printf("\n");
  printf("     arguments=-l;/tmp/\n");
  printf("\n");
  printf("     Similarly, the value for the name \"environment\" is a \";\" separated\n");
  printf("     set of name/value pairs where each name and value is separated by\n");
  printf("     an \"=\" sign. For example, to set the environment variable \"HOME\" to\n");
  printf("     \"/Users/leonardo\" and the environment variable \"SHELL\" to the value\n");
  printf("     \"/bin/tcsh\" one would have a line in the software description GATRL\n");
  printf("     file looking like\n");
  printf("\n");
  printf("     environment=HOME=/Users/leonardo;SHELL=/bin/tcsh\n");
  printf("\n");
  printf("     Quite simple really. \n");
  printf("\n");
  printf("     The following options are available:\n");
  printf("\n");
  printf("     -k jobid \n");
  printf("            Kills the job specified by the passed jobid\n");
  printf("\n");
  printf("     -s jobid \n");
  printf("            Prints out the status of the job specified by the passed jobid \n");
  printf("\n");
  printf("     -u jobid \n");
  printf("            Unschedules the job specified by the passed jobid \n");
  printf("\n");
  printf("     -c jobid \n");
  printf("            Checkpoints the job specified by the passed jobid \n");
  printf("\n");
  printf("EXAMPLES\n");
  printf("     The following shows how to kill the job with jobid 132\n");
  printf("         \n");
  printf("         gatrun -k 132\n");
  printf("\n");
  printf("     To find the status of the job with jobid 423 one would use\n");
  printf("\n");
  printf("         gatrun -s 423\n");
  printf("\n");
  printf("     To unschedule the job with jobid 498 one would use\n");
  printf("\n");
  printf("         gatrun -u 498\n");
  printf("\n");
  printf("     To checkpoint the job with jobid 9AAT67 one would use\n");
  printf("\n");
  printf("         gatrun -c 9AAT67\n");
  printf("\n");
  printf("     Starting the job specified by the GATRL hardware resource description\n");
  printf("     file File.hrd and the GATRL software description file File.sd within \n");
  printf("     the virtual organization gridlab.org would look as follows\n");
  printf("\n");
  printf("         gatrun gridlab.org File.hrd File.sd\n");
  printf("\n");
  printf("DIAGNOSTICS\n");
  printf("     The gatrun utility exits 0 on success, and >0 if an error occurs.\n");
  printf("\n");
  printf("COMPATIBILITY\n");
  printf("     The gatrun utility is compatible with itself, maybe. \n");
  printf("\n");
  printf("SEE ALSO\n");
  printf("     gatsaunter(1), gatamble(1), gatperambulation(1), gatstroll(1) \n");
  printf("\n");
  printf("STANDARDS\n");
  printf("     The gatrun utility conforms to IEEE Std 1003.1-2001 (``POSIX.1''), not!\n");
  printf("\n");
  printf("HISTORY\n");
  printf("     An gatrun command appeared in Version 1 of this manual\n");
  printf("\n");
  printf("BUGS\n");
  printf("     To maintain backward compatibility with the wheel we have rounded the\n");
  printf("     jutting corners of this sharp utility  to protect the bunglesome users.\n");
  
  /* Return to caller */
  return;
}

/**
 * The entry point for this utility after the format of the command line arguments
 * has been checked
 *
 * @pram argc Number of command line arguments
 * @param argv Command line arguments
 * @return GATResult indicating completion status
 */
static GATResult GATRun_Run( int argc, char *argv[] )
{
  GATResult result;
  
  /* Assume Invalid Parameter */
  result = GAT_INVALID_PARAMETER;
    
  /* Branch on Number of Arguments: Case One -- Kill, Status, Unschedule, and Checkpoint */
  if( 3 == argc )
  {
    /* Run Kill */
    if( 0 == strcmp( "-k", argv[1] ) )
    result = GATRun_Run_Kill( argc, argv );
    
    /* Run Status */
    if( 0 == strcmp( "-s", argv[1] ) )
    result = GATRun_Run_Status( argc, argv );
    
    /* Run Unschedule */
    if( 0 == strcmp( "-u", argv[1] ) )
    result = GATRun_Run_Unschedule( argc, argv );   
    
    /* Run Checkpoint */
    if( 0 == strcmp( "-c", argv[1] ) )
     result = GATRun_Run_Checkpoint( argc, argv );
  }
  
  /* Branch on Number of Arguments: Case Two -- Run */
  if( 5 == argc )
  {
    /* Run Run */
    result = GATRun_Run_Run( argc, argv );
  }
        
  /* Return to caller */
  return result;
}

/**
 * Kills the job with the jobid argv[2]
 *
 * @pram argc Number of command line arguments
 * @param argv Command line arguments
 * @return GATResult indicating completion status
 */
static GATResult GATRun_Run_Kill( int argc, char *argv[] )
{
  GATJob job;
  GATResult result;
  
  /* Get GATJob by ID */
  result = GATRun_GetJobByID( argv[2], &job );
  
  /* Check last call */
  if( GAT_SUCCEEDED( result ) )
  {
    /* Stop GATJob */
    result = GATJob_Stop( job );
  
    /* Destroy GATJob */
    GATJob_Destroy( &job );
  }
  
  /* Check result */
  if( GAT_SUCCEEDED( result ) )
  {
    /* Print status message */
    print( "Killed job %s\n", argv[2] );
  }
  else
  {
    /* Print status message */
    print( "Could not kill job %s\n", argv[2] );
  } 
  
  /* Return to caller */
  return result;
}

/**
 * Prints the status of the job with the jobid argv[2]
 *
 * @pram argc Number of command line arguments
 * @param argv Command line arguments
 * @return GATResult indicating completion status
 */
static GATResult GATRun_Run_Status( int argc, char *argv[] )
{
  GATJob job;
  GATResult result;
  GATJobState state;
  
  /* Get GATJob by ID */
 result = GATRun_GetJobByID( argv[2], &job );
  
  /* Check last call */
  if( GAT_SUCCEEDED( result ) )
  {
    /* Get State of GATJob */
    result = GATJob_GetState( job,  &state );
    
    /* Check last call */
    if( GAT_SUCCEEDED( result ) )
    {
      /* Print Unknown State */
      if( GATJobState_Unknown == state )
        printf( "The job %s is in an unknown state.\n", argv[2] );
        
      /* Print Initial State */
      if( GATJobState_Initial == state )
        printf( "The job %s is in the initial state.\n", argv[2] );        
        
      /* Print Scheduled State */
      if( GATJobState_Scheduled == state )
        printf( "The job %s is in the scheduled state.\n", argv[2] );     
        
      /* Print Running State */
      if( GATJobState_Running == state )
        printf( "The job %s is in the running state.\n", argv[2] );    
        
      /* Print Stopped State */
      if( GATJobState_Stopped == state )
        printf( "The job %s is in the stopped state.\n", argv[2] );                           
    }
  
    /* Destroy GATJob */
    GATJob_Destroy( &job );
  }
  
  /* Check result */
  if( GAT_FAILED( result ) )
  {
    /* Print status message */
    print( "Could not determine status of job %s\n", argv[2] );
  } 
    
  /* Return to caller */
  return result;
}

/**
 * Unschedules the job with the jobid argv[2]
 *
 * @pram argc Number of command line arguments
 * @param argv Command line arguments
 * @return GATResult indicating completion status
 */
static GATResult GATRun_Run_Unschedule( int argc, char *argv[] )
{
  GATJob job;
  GATResult result;
  
  /* Get GATJob by ID */
  result = GATRun_GetJobByID( argv[2], &job );
  
  /* Check last call */
  if( GAT_SUCCEEDED( result ) )
  {
    /* Unschedule GATJob */
    result = GATJob_UnSchedule( job );
  
    /* Destroy GATJob */
    GATJob_Destroy( &job );
  }
  
  /* Check result */
  if( GAT_SUCCEEDED( result ) )
  {
    /* Print status message */
    print( "Unscheduled job %s\n", argv[2] );
  }
  else
  {
    /* Print status message */
    print( "Could not unschedule job %s\n", argv[2] );
  } 
    
  /* Return to caller */
  return result;
}

/**
 * Checkpoints the job with the jobid argv[2]
 *
 * @pram argc Number of command line arguments
 * @param argv Command line arguments
 * @return GATResult indicating completion status
 */
static GATResult GATRun_Run_Checkpoint( int argc, char *argv[] )
{
  GATJob job;
  GATResult result;
  
  /* Get GATJob by ID */
  result = GATRun_GetJobByID( argv[2], &job );
  
  /* Check last call */
  if( GAT_SUCCEEDED( result ) )
  {
    /* Checkpoint GATJob */
    result = GATJob_Checkpoint( job );
  
    /* Destroy GATJob */
    GATJob_Destroy( &job );
  }
  
  /* Check result */
  if( GAT_SUCCEEDED( result ) )
  {
    /* Print status message */
    print( "Checkpointed job %s\n", argv[2] );
  }
  else
  {
    /* Print status message */
    print( "Could not checkpoint job %s\n", argv[2] );
  } 
    
  /* Return to caller */
  return result;
}

/**
 * Obtains the job with the specified jobid from the GATAdvertService
 *
 * @param jobid The jobid
 * @param job The obtained GATJob
 * @return GATResult indicating completion status
 */
static GATResult GATRun_GetJobByID( const char *jobid, GATJob *job )
{
  GATString path;
  GATResult result;
  char const *cpath;
  GATObject object;
  GATTable metadata;
  GATList_String paths;
  GATContext context;
  GATList_String_Iterator beginning;
  GATAdvertService advertService;
  
  /* Assume Invalid Parameter */
  result = GAT_INVALID_PARAMETER;

  /* Check Parameter */
  if( NULL !=  job )
  {
    /* Assume Memory Failure */
    result = GAT_MEMORYFAILURE; 
    
    /* Create GATContext */
    context = GATContext_Create();
    
    /* Check creation */
    if( NULL != context )
    {
      /* Create GATAdvertService */
      advertService = GATAdvertService_Create( context, NULL );
      
      /* Check creation */
      if( NULL != advertService )
      {
        /* Create GATTable */
        metadata = GATTable_Create();
        
        /* Check creation */
        if( NULL != metadata )
        {
          /* Add jobid=jobid to metadata */
          result = GATTable_Add_String( metadata, (const void *) "jobid", jobid );
          
          /* Check last call */
          if( GAT_SUCCEEDED( result ) )
          {
            /* Find GATJobs */
            result = GATAdvertService_Find( advertService, metadata, &paths );
            
            /* Check last call */
            if( GAT_SUCCEEDED( result ) )
            {
              /* Obtain GATList_String_Iterator */
              beginning = GATList_String_Begin( paths );
              
              /* Check last call */
              if( NULL != beginning )
              {
                /* Check Existence of GATJob */
                if( beginning != GATList_String_Begin(paths) )
                {
                  /* Obtain path as C string */
                  cpath = GATList_String_Get( beginning );
                
                  /* Check last call */
                  if( NULL != cpath )
                  {
                    /* Create GATString path */
                    path = GATString_Create( cpath, strlen( cpath ) + 1, "ASCII" );
                  
                    /* Check GATString creation */
                    if( NULL != path )
                    {
                      /* Get Advertisable */
                      result = GATAdvertService_GetAdvertisable( advertService, path, &object );
                    
                      /* Check last call */
                      if( GAT_SUCCEEDED( result ) )
                      {
                        /* Convert GATObject to GATJob */
                        *job = GATObject_ToGATJob( object );
                      }
                  
                      /* Destroy GATString */
                      GATString_Destroy( &path );
                    }
                    else
                    {
                      /* Indicate Memory Failure */
                      result = GAT_MEMORYFAILURE;
                    }
                  }
                  else
                  {
                    /* Indicate Memory Failure */
                    result = GAT_MEMORYFAILURE;
                  }
                }
                else
                {
                  /* Indicate No Such GATJob Exists */
                  result = GAT_NO_MATCHING_RESOURCE;
                }
              }
              else
              {
                /* Indicate Memory Failure */
                result = GAT_MEMORYFAILURE; 
              }
              
              /* Destroy GATList_String */
              GATList_String_Destroy( &paths );
            }
          }
        
          /* Destroy GATTable */
          GATTable_Destroy( &metadata );
        }
      
        /* Destroy GATAdvertService */
        GATAdvertService_Destroy( &advertService );
      }
      
      /* Destroy GATContext */
      GATContext_Destroy( &context );
    }  
  }
   
  /* Return to caller */
  return result;
}

/**
 * Runs the job within the virtual organization argv[1] on hardware
 * described by the hardware resource description resulting from
 * parsing the GATRL file argv[2]. The job itself is described by
 * the software description resulting from parsing the GATRL
 * file argv[3]. The resultant GATJob is placed in the GATAdvert
 * Service with the meta-data
 *
 * jobid = <GATJob's JobID>
 *
 * so as to allow other processes to access the job.
 *
 * @pram argc Number of command line arguments
 * @param argv Command line arguments
 * @return GATResult indicating completion status
 */
static GATResult GATRun_Run_Run( int argc, char *argv[] )
{
  GATJob job;
  GATResult result;
  
  /* Create GATJob */
  result = GATRun_CreateJob( argc, argv, &job );
  
  /* Check GATJob creation */
  if( GAT_SUCCEEDED( result ) )
  {
    /* Print Status */
    printf( "Successfully submitted the job.\n" );
    
    /* Advertise GATJob */
    result = GATRun_AdvertiseJob( job );
    
    /* Check Last Call */
    if( GAT_SUCCEEDED( result ) )
    {
      /* Print status */
      printf( "Successfully stored the job so other processes can access it.\n" );
    }
    
    /* Destroy GATJob */
    GATJob_Destroy( &job );
  }
  
  /* Return to caller */
  return result;
}

/**
 * Runs the job within the virtual organization argv[1] on hardware
 * described by the hardware resource description resulting from
 * parsing the GATRL file argv[2]. The job itself is described by
 * the software description resulting from parsing the GATRL
 * file argv[3] and returns the GATJob through job.
 *
 * @pram argc Number of command line arguments
 * @param argv Command line arguments
 * @param job The GATJob corresponding to the passed arguments
 * @return GATResult indicating completion status
 */
static GATResult GATRun_CreateJob( int argc, char *argv[], GATJob *job )
{
  GATString vo;
  GATResult result;
  GATContext context;
  GATJobDescription jobDescription;
  GATTable softwareDescriptionTable;
  GATResourceBroker resourceBroker;
  GATSoftwareDescription softwareDescription;
  GATTable hardwareResourceDescriptionTable;
  GATResourceDescription resourceDescription;
  GATHardwareResourceDescription hardwareResourceDescription;
  
  /* Assume Invalid Parameter */
  result = GAT_INVALID_PARAMETER;

  /* Check Parameter */
  if( NULL !=  job )
  {
    /* Assume Memory Failure */
    result = GAT_MEMORYFAILURE;
    
    /* Create GATContext */
    context = GATContext_Create();
    
    /* Check GATContext Creation */
    if( NULL != context )
    {
      /* Create GATString */
      vo = GATString_Create( argv[1], strlen( argv[1] ) + 1, "ASCII" );
      
      /* Check GATString Creation */
      if( NULL != vo )
      {
        /* Create GATResourceBroker *
        resourceBroker = GATResourceBroker_Create( context, NULL, vo );
        
        /* Check GATResourceBroker Creation */
        if( NULL != resourceBroker )
        {
          /* Obtain GATTable for GATHardwareResourceDescription */
          result = GATRun_ParseGATRL( argv[2], &hardwareResourceDescriptionTable );
          
          /* Check Last Call */
          if( GET_SUCCEEDED( result ) )
          {
            /* Assume Memory Failure */
            result = GAT_MEMORYFAILURE;
            
            /* Create GATHardwareResourceDescription */
            hardwareResourceDescription = GATHardwareResourceDescription_Create( hardwareResourceDescriptionTable );
            
            /* Check GATHardwareResourceDescription Creation */
            if( NULL != hardwareResourceDescription )
            {
              /* Obtain GATTable for GATSoftwareDescription */
              result = GATRun_ParseGATRL( argv[3], &softwareDescriptionTable );
              
              /* Check Last Call */
              if( GAT_SUCCEEDED( result ) )
              {
                /* Assume Memory Failure */
                result = GAT_MEMORYFAILURE;
                
                /* Create GATSoftwareDescription */
                softwareDescription = GATSoftwareDescription_Create( softwareDescriptionTable );
                
                /* Check GATSoftwareDescription Creation */
                if( NULL != softwareDescription )
                {
                  /* Convert GATHardwareResourceDescription to GATResourceDescription */
                  resourceDescription = GATHardwareResourceDescription_ToGATResourceDescription( hardwareResourceDescription );
                  
                  /* Create GATJobDescription */
                  jobDescription = GATJobDescription_Create( context, softwareDescription, resourceDescription );
                  
                  /* Check Last Call */
                  if( NULL != jobDescription )
                  {
                    /* Create GATJob */
                    result = GATResourceBroker_SubmitJob( resourceBroker, jobDescription, job );
                  
                    /* Destroy GATJobDescription */
                    GATJobDescription_Destroy( &jobDescription );
                  }
                
                  /* Destroy GATSoftwareDescription */
                  GATSoftwareDescription_Destroy( &softwareDescription );
                }
              }
            
              /* Destroy GATHardwareResourceDescription */
              GATHardwareResourceDescription_Destroy( &hardwareResourceDescription );
            }
          
            /* Destroy GATTable */
            GATTable_Destroy( &hardwareResourceDescriptionTable );
          }

          /* Destroy GATResourceBroker */
          GATResourceBroker_Destroy( &resourceBroker );
        }
      
        /* Destroy GATString */
        GATString_Destroy( &vo );
      }
    
      /* Destroy GATContext */
      GATContext_Destroy( &context );
    }
  }
    
  /* Return to caller */
  return result;
}

/**
 * Parses the specified GATRL file into a set of name/value paris and places
 * those name value pairs in the passed GATTable
 *
 * @param fileName The specified GATRL file
 * @param table The GATTable into which names/values are parsed. 
 * @return GATResult indicating completion status
 */ 
static GATResult GATRun_ParseGATRL( const char *fileName, GATTable *table )
{
  FILE *file;
  char *value;
  char *name;
  GATResult result;
  char nextLine[2048];
  
  /* Assume Invalid Parameter */
  result = GAT_INVALID_PARAMETER;

  /* Check Parameter */
  if( NULL !=  table )
  {
    /* Create GATTable */
    *table = GATTable_Create();
    
    /* Check GATTable Creation */
    if( NULL = (*table) )
    {
      /* Assume IO Error */
      result = GAT_IO_ERROR;
      
      /* Open file */
      file = fopen( fileName, "r" );
      
      /* Check Last Call */
      if( NULL == file )
      {
        /* Assume Invalid GATRL */
        result = GAT_UNKNOWN_FORMAT;      
        
        /* Read Next Line */
        while( NULL != fgets( nextLine, 2048, file ) )
        {
          /* Read name */
          name = strtok( nextLine, "=" );
          
          /* Check Last Call */
          if( NULL != name )
          {
            /* Read value */
            value = strtok( NULL, "=" );
            
            /* Check Last Call */
            if( NULL != value )
            {
              /* Add name/values to table */
              if( 0 == strcmp( "memory.size", name ) )
              {
                /* Add memory.size, value is a float */
                result = GATRun_GATTable_AddFloat( *table, name, value );
              } else if( 0 == strcmp( "memory.accesstime", name ) )
              {
                /* Add memory.accesstime, value is a float */
                result = GATRun_GATTable_AddFloat( *table, name, value );
              } else if( 0 == strcmp( "memory.str", name ) )
              {
                /* Add memory.str, value is a float */
                result = GATRun_GATTable_AddFloat( *table, name, value );
              } else if( 0 == strcmp( "cpu.speed", name ) )
              {
                /* Add cpu.speed, value is a float */
                result = GATRun_GATTable_AddFloat( *table, name, value );
              } else if( 0 == strcmp( "disk.size", name ) )
              {
                /* Add disk.size, value is a float */
                result = GATRun_GATTable_AddFloat( *table, name, value );
              } else if( 0 == strcmp( "disk.accesstime", name ) )
              {
                /* Add disk.accesstime, value is a float */
                result = GATRun_GATTable_AddFloat( *table, name, value );
              } else if( 0 == strcmp( "disk.str", name ) )
              {
                /* Add disk.str, value is a float */
                result = GATRun_GATTable_AddFloat( *table, name, value );
              } else if( 0 == strcmp( "location", name ) )
              {
                /* Add location, value is a GATLocation */
                result = GATRun_GATTable_AddGATLocation( *table, name, value );
              } else if( 0 == strcmp( "arguments", name ) )
              {
                /* Add arguments, value is a GATList_String */
                result = GATRun_GATTable_AddGATList_String( *table, name, value );
              } else if( 0 == strcmp( "environment", name ) )
              {
                /* Add environment, value is a GATTable */
                result = GATRun_GATTable_AddGATTable( *table, name, value );
              } else if( 0 == strcmp( "stdin", name ) )
              {
                /* Add stdin, value is a GATFile */
                result = GATRun_GATTable_AddGATFile( *table, name, value );
              } else if( 0 == strcmp( "stdout", name ) )
              {
                /* Add stdout, value is a GATFile */
                result = GATRun_GATTable_AddGATFile( *table, name, value );
              } else if( 0 == strcmp( "stderr", name ) )
              {
                /* Add stderr, value is a GATFile */
                result = GATRun_GATTable_AddGATFile( *table, name, value );
              } else
              {
                /* Add name, value is a C String */
                result = GATTable_Add_String( *table, (const void *) name, value);
              }
              
              /* On Failure, break while Loop*/
              if( GAT_FAILED( result ) )
              {
                break;
              }
            }
          }
        }
        
        /* Close file */
        fclose( file );
      }
    }
  }
  
  /* Return to Caller */
  return result;
}

/**
 * Adds the name/value pair to the passed table interpreting the
 * value as a C String representation of a GATfloat32. 
 *
 * @param table The GATTable which to augment
 * @param name The name of the name/value pair
 * @param value The value of the name/value pair
 * @return A GATResult indicating completion status
 */
static  GATResult GATRun_GATTable_AddFloat( GATTable table, const char *name, const char *value )

  GATfloat32 floatValue;
  
  /Convert value to float */
  floatValue = (GATfloat32) atof( value );
  
  /* Add to table */
  return GATTable_Add_float( table, (const void *) name, floatValue );
}

/**
 * Adds the name/value pair to the passed table interpreting the
 * value as a C String representation of a GATLocation. 
 *
 * @param table The GATTable which to augment
 * @param name The name of the name/value pair
 * @param value The value of the name/value pair
 * @return A GATResult indicating completion status
 */
static  GATResult GATRun_GATTable_AddGATLocation( GATTable table, const char *name, const char *value )
{
  GATResult result;
  GATObject object;
  GATLocation location;
  
  /* Set result to a memory failure */
  result = GAT_MEMORYFAILURE;
    
  /* Create GATLocation */
  location = GATLocation_Create( value );

  /* Check GATLocation Creation */
  if( NULL != location )
  {
    /* Convert GATLocation to GATObject */
    object = GATLocation_ToGATObject( location );
    
    /* Add object to table */
    result = GATTable_Add_GATObject( table, (const void *) name, object );
  
    /* Destroy GATLocation */
    GATLocation_Destroy( &location );
  }

  /* Return to Caller */
  return result;
}

/**
 * Adds the name/value pair to the passed table interpreting the
 * value as a C String representation of a GATList_String, elements
 * in value are semi-colon delimited. 
 *
 * @param table The GATTable which to augment
 * @param name The name of the name/value pair
 * @param value The value of the name/value pair
 * @return A GATResult indicating completion status
 */
static  GATResult GATRun_GATTable_AddGATList_String( GATTable table, const char *name, const char *value )
{
  char *nextString;
  GATResult result;
  GATObject object;
  GATList_String strings;
  GATList_String_Iterator beginning;
  
  /* Set result to a memory failure */
  result = GAT_MEMORYFAILURE;
  
  /* Create GATList_String */
  strings = GATList_String_Create();
  
  /* Check GATList_String Creation */
  if( NULL != strings )
  {
    /* Obtain GATList_String_Iterator */
    beginning = GATList_String_Begin( strings );
    
    /* Check Last Call */
    if( NULL != beginning )
    {
      /* Set result to a unknown format */
      result = GAT_UNKNOWN_FORMAT;
      
      /* Obtain First nextString */
      nextString = strtok( value, ";" );
      
      /* Tokenize value */
      while( NULL != nextString )
      {
        /* Insert nextString to strings */
        result = GATList_String_Insert( strings, beginning, nextString );
        
        /* On Failure, break while loop */
        if( GAT_FAILED( result ) )
          break;
          
        /* Obtain next nextString */
        nextString = strtok( NULL, ";" );
      }
      
      /* Check result Call */
      if( GAT_SUCCEEDED( result ) )
      {
        /* Convert GATList_String to GATObject */
        object = GATList_String_ToGATObject( strings );
        
        /* Add object to table */
        result = GATTable_Add_GATObject( table, (const void *) name, object );
      }
    }
  
    /* Destroy GATList_String */
    GATList_String_Destroy( &strings );
  }
  
  /* Return to Caller */
  return result;
}

/**
 * Adds the name/value pair to the passed table interpreting the
 * value as a C String representation of a GATTable, name/value
 * pairs in value are semi-colon delimited and the name and
 * value are separated by an equals. 
 *
 * @param tableOne The GATTable which to augment
 * @param name The name of the name/value pair
 * @param value The value of the name/value pair
 * @return A GATResult indicating completion status
 */
static  GATResult GATRun_GATTable_AddGATTable( GATTable tableOne, const char *name, const char *value )
{
  GATResult result;
  char *nextValue;
  char *nextName;
  GATObject object;
  GATTable tableTwo;
  
  /* Set result to a memory failure */
  result = GAT_MEMORYFAILURE;
  
  /* Create GATTable */
  tableTwo = GATTable_Create();
  
  /* Check GATTable Creation */
  if( NULL != tableTwo )
  {
    /* Set result to a unknown format */
    result = GAT_UNKNOWN_FORMAT;
        
    /* Obtain First nextName */
    nextName = strtok( value, ";=" );
    
    /* Tokenize Value */
    while( NULL != nextName )
    {
      /* Obtain Next nextValue */
      nextValue =  strtok( NULL, ";=" );
      
      /* Check Last Call */
      if( NULL == nextValue )
      {
        /* Set result to a unknown format */
        result = GAT_UNKNOWN_FORMAT;
        
        /* Break while */
        break;
      }
      
      /* Add nextName and nextValue to tableTwo */
      result = GATTable_Add_String( tableTwo, (const void *) nextName, nextValue );
      
      /* Check Last Call */
      if( GAT_FAILED( result ) )
      {
        /* Break while */
        break;      
      }
      
      /* Obtain Next nextName */
      nextName = strtok( NULL, ";=" );
    }
    
    /* Check result Call */
    if( GAT_SUCCEEDED( result ) )
    {
      /* Convert GATTable to GATObject */
      object = GATTable_ToGATObject( tableTwo );
        
      /* Add object to tableOne */
      result = GATTable_Add_GATObject( tableOne, (const void *) name, object );    
    }
            
    /* Destroy GATTable */
    GATTable_Destroy( &tableTwo );
  }
  
  /* Return To Caller */
  return result;  
}

/**
 * Adds the name/value pair to the passed table interpreting the
 * value as a C String representation of a GATFile. 
 *
 * @param table The GATTable which to augment
 * @param name The name of the name/value pair
 * @param value The value of the name/value pair
 * @return A GATResult indicating completion status
 */
static  GATResult GATRun_GATTable_AddGATFile( GATTable table, const char *name, const char *value )
{
  GATFile file;
  GATResult result;
  GATObject object;
  GATContext context;
  
  /* Set result to a memory failure */
  result = GAT_MEMORYFAILURE;
  
  /* Create GATContext */
  context = GATContext_Create();
  
  /* Check GATContext Creation */
  if( NULL != context )
  {
    /* Create GATFile */
    file = GATFile_Create_Name( context, value, NULL );
    
    /* Check GATFile Creation */
    if( NULL != file )
    {
      /* Convert GATFile to GATObject */
      object = GATFile_ToGATObject( file );
      
      /* Add object to table */
      result = GATTable_Add_GATObject( table, (const void *) name, object );      
      
      /* Destroy GATFile */
      GATFile_Destroy( &file );
    }
  
    /* Destroy GATContext */
    GATContext_Destroy( &context );
  }

  /* Return to Caller */
  return result;
}

/**
 * Advertises the passed GATJob in the GATAdvertService with the meta-data
 * 
 * jobid=<jobid>
 *
 * and the POSIX path
 *
 * /tmp/jobs/<jobid>
 *
 * @param job The GATJob to advertise
 * @return A GATResult indicating completion status
 */
static GATResult GATRun_AdvertiseJob( GATJob job )
{
  GATString path;
  GATResult result;
  GATObject object;
  char tmpPath[2048];
  GATTable metadata;
  GATContext context;
  GATJobID_const jobid;
  const char *jobidString;
  GATAdvertService advertService;
  
  /* Set result to a memory failure */
  result = GAT_MEMORYFAILURE;
    
  /* Create GATContext */
  context = GATContext_Create();
  
  /* Check GATContext Creation */
  if( NULL != context )
  {
    /* Create GATAdvertService */
    advertService = GATAdvertService_Create( context, NULL );
    
    /* Check GATAdvertService Creation */
    if( NULL != advertService )
    {
      /* Get GATJobID */
      result = GATJob_GetJobID( job, &jobid );
      
      /* Check Last Call */
      if( GAT_SUCCEEDED( result ) )
      {
        /* Add prefix to tmpPath */
        strcpy( tmpPath, "/tmp/jobs/" );
        
        /* Obtain jobidString */
        jobidString = GATString_GetBuffer( jobid ); /* May not be ASCII !!! */
        
        /* Add suffix to tmpPath */
        strcat( tmpPath, jobidString );
        
        /* Set result to a memory failure */
        result = GAT_MEMORYFAILURE;
        
        /* Create GATString */
        path = GATString_Create( tmpPath, strlen( tmpPath) + 1, "ASCII" );
        
        /* Check GATString Creation */
        if( NULL != path )
        {
          /* Create GATTable */
          metadata = GATTable_Create();
          
          /* Check GATTable Creation */
          if( NULL != metadata )
          {
            /* Add jobid=<jobid> to metadata */
            result = GATTable_Add_String( metadata, (const void *) "jobid", jobidString );
            
            /* Check Last Call */
            if( GAT_SUCCEEDED( result ) )
            {
              /* Convert GATJob to GATObject */
              object = GATJob_ToGATObject( job );
              
              /* Add object to advertService at path with metadata */
              result = GATAdvertService_Add( advertService, object, metadata, path );
            }
          
            /* Destroy GATTable */
            GATTable_Destroy( &metadata );
          }
        
          /* Destroy GATString */
          GATString_Destroy( &path );
        }
      }
    
      /* Destroy GATAdvertService */
      GATAdvertService_Destroy( &advertService );
    }
  
    /* Destroy GATContext */
    GATContext_Destroy( &context );
  }
  
  /* Return to caller */
  return result;
}


next up previous contents
Next: Event System Up: Some Useful Programs Previous: Some Useful Programs   Contents
Andre Merzky 2004-05-13