GridLab
Grid Application Toolkit

A simple API for Grid Applications
GAT

Menu



Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members  

request.c

Go to the documentation of this file.
00001 /** @file request.c
00002  *  Source file for the GATRequestCPI provider class for the 
00003  *  request adaptor.
00004  *
00005  *  @date Mon Feb 02 2004
00006  *
00007  *  @version $Header: /export/cvs-gridlab/wp-1/Codes/GATEngine/C-reference/adaptors/resourcebroker/request.c,v 1.4 2004/04/26 15:45:00 hartmutkaiser Exp $
00008  *
00009  *  Copyright (C) Hartmut Kaiser
00010  *  This file is part of the GAT Engine.
00011  *  Contributed by Hartmut Kaiser <hartmutkaiser [at] t-online [dot] de>.
00012  *
00013  *  Use, modification and distribution is subject to the Gridlab Software
00014  *  License. (See accompanying file GLlicense.txt or copy at
00015  *  http://www.gridlab.org/GLlicense.txt)
00016  */
00017 
00018 static const char *rcsid = "$Header: /export/cvs-gridlab/wp-1/Codes/GATEngine/C-reference/adaptors/resourcebroker/request.c,v 1.4 2004/04/26 15:45:00 hartmutkaiser Exp $";
00019  
00020 /* System Header Files */
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <time.h>
00025 
00026 /* GAT Header Files */
00027 #include "GATCPI.h"
00028 #include "GATRequestNotifier.h"
00029 #include "GATRequestCPIInstanceData.h"
00030 
00031 #include "request.h"
00032 
00033 /* Macros */
00034 #ifndef PATH_MAX
00035   #define PATH_MAX 4096
00036 #endif /* ! PATH_MAX */
00037 
00038 /* Structures, unions and enums */
00039 
00040 /* Static function prototypes */
00041 static void
00042 resourcebroker_adaptor_GATRequestCPI_Destroy(void *data);
00043 
00044 /* instance specific functions */
00045 static GATResult
00046 resourcebroker_adaptor_GATRequestCPI_ServiceActions(void *data, 
00047   GATRequestCPI_Instance *instance_data, GATTimePeriod_const timeout /* = 0 */);
00048   
00049 static GATResult 
00050   resourcebroker_adaptor_GATRequestCPI_CreateInstance(void *adaptor_data, 
00051     GATRequestCPI_Instance *instance_data, void *initialisation_data);
00052 
00053 static void 
00054   resourcebroker_adaptor_GATRequestCPI_DestroyInstance(
00055     void *data, GATRequestCPI_Instance *instance_data);
00056 
00057 static GATResult 
00058   resourcebroker_adaptor_GATRequestCPI_CloneInstance(
00059     void *data, GATRequestCPI_Instance const *instance_data, 
00060     GATRequestCPI_Instance *new_instance_data);
00061 
00062 static GATResult 
00063   resourcebroker_adaptor_GATRequestCPI_EqualsInstance(
00064     void *data, GATRequestCPI_Instance const *lhs, 
00065     GATRequestCPI_Instance const *rhs, GATBool *isequal);
00066 
00067 /* CPI API functionality */
00068 static GATResult
00069   resourcebroker_adaptor_GATRequestCPI_InitRequestNotifier(
00070     void *data, GATRequestCPI_Instance const *instance_data, 
00071     GATRequestNotifier_const *notifier);
00072     
00073 /* local functions */
00074 static GATResult 
00075 resourcebroker_adaptor_GATRequestCPI_request_notifier_listener(
00076   void *data, GATRequestCPI_Instance const *instance_data, 
00077   GATTable_const notifier_data, void *context_data);
00078   
00079 
00080 /* File scope variables */
00081 
00082 /* External functions */
00083 
00084 /* Register the CPI to implement */
00085 GATResult resourcebroker_adaptor_Register_GATRequestCPI(
00086   GATContext error_context, GATRegistry registry, 
00087   GATTable_const system_config, GATTable_const instance_config, void *token)
00088 {
00089   GAT_USES_STATUS(error_context, "resourcebroker_adaptor_Register_GATRequestCPI");
00090   
00091   GATRequestCPI cpi = NULL;
00092   GATRequestCPI_Data cpidata;
00093 
00094   {
00095     memset(&cpidata, 0, sizeof(GATRequestCPI_Data));
00096       
00097     /*
00098      *  provide the appropriate callback functions
00099      */
00100     cpidata.data = NULL;   /* no adaptor instance data */
00101     cpidata.destroy = resourcebroker_adaptor_GATRequestCPI_Destroy;
00102     
00103     /* instance data handling */
00104     cpidata.service_actions = resourcebroker_adaptor_GATRequestCPI_ServiceActions;
00105     cpidata.create_instance = resourcebroker_adaptor_GATRequestCPI_CreateInstance;
00106     cpidata.destroy_instance = resourcebroker_adaptor_GATRequestCPI_DestroyInstance;
00107     cpidata.clone_instance = resourcebroker_adaptor_GATRequestCPI_CloneInstance;
00108     cpidata.equals_instance = resourcebroker_adaptor_GATRequestCPI_EqualsInstance;
00109 
00110     /* CPI specific API functions */
00111     cpidata.init_notifier = resourcebroker_adaptor_GATRequestCPI_InitRequestNotifier;
00112   
00113     /* Create a GATRequestCPI object. */
00114     cpi = GATRequestCPI_Create(GATREQUESTCPI_VERSION, &cpidata);
00115     if(NULL != cpi)
00116     {
00117       /* Need to pass preferences to allow matching
00118        * of a user's preferences with what this adaptor does.
00119        */
00120       GATPreferences preferences = GATPreferences_Create();
00121       if(NULL != preferences)
00122       {
00123         GAT_CREATE_STATUS(GATPreferences_Add(preferences, "Name", 
00124           "resourcebroker_adaptor"));
00125         GAT_CREATE_STATUS(GATPreferences_Add(preferences, "Security", "none"));
00126         GAT_CREATE_STATUS(GATPreferences_Add(preferences, "Local", "true"));
00127         
00128         GAT_CREATE_STATUS(GATRegistry_AddGATRequestCPI(registry, cpi, token, 
00129           preferences));
00130         
00131         GATPreferences_Destroy(&preferences);
00132       }
00133       else
00134       {
00135         GAT_CREATE_STATUS(GAT_MEMORYFAILURE);
00136       }
00137     }
00138     else
00139     {
00140       GAT_CREATE_STATUS(GAT_MEMORYFAILURE);
00141     }
00142   }
00143 
00144   if (GAT_FAILED(GAT_CURRENT_STATUS()))
00145   {
00146     GATRequestCPI_Destroy(&cpi);
00147   }
00148 
00149   return GAT_RETURN_STATUS();
00150 }
00151 
00152 /* Local functions */
00153 
00154 static void
00155 resourcebroker_adaptor_GATRequestCPI_Destroy(void *data)
00156 {
00157   /* nothing to do here (no internal data allocated) */
00158   assert(NULL == data);
00159 }
00160 
00161 
00162 /* instance specific functions */
00163 
00164 /** logicalfile_adaptor_LogicalFileCPI_ServiceActions
00165  *  
00166  *  The function logicalfile_adaptor_LogicalFileCPI_ServiceActions is called, whenever
00167  *  the client calls GATContext_ServiceActions. This function is called for 
00168  *  every created object.
00169  *  This function should be used to update all instance specific data, which 
00170  *  may have been changed asynchronously or to fire pending events.
00171  *
00172  *  @param data Adaptor-provided data object.
00173  *  @param instance_data The instance data of this CPI object
00174  *  @param timeout This may be a 0 timeout to indicate no timeout at all, or
00175  *        a specific time length.
00176  *
00177  *  @return An error code.
00178  */
00179 static GATResult
00180 resourcebroker_adaptor_GATRequestCPI_ServiceActions(void *data, 
00181   GATRequestCPI_Instance *instance_data, GATTimePeriod_const timeout)
00182 {
00183   if (NULL != instance_data)
00184   {
00185     /* TODO: include checkpoint callback listen -- ANDRE */
00186     GATResult retval = GAT_SUCCESS;
00187     GATRequestCPIInstance_Data *data = 
00188       (GATRequestCPIInstance_Data *)instance_data->instance_data;
00189     if (difftime(time(0), data->start_time) >= 2)
00190     {
00191       /* wait an appropriate amount of time */
00192       retval = GATRequest_IssueCommand(instance_data->source);
00193     }
00194     return retval;
00195   }
00196   return GAT_INVALID_HANDLE;
00197 }
00198   
00199 /** resourcebroker_adaptor_GATRequestCPI_CreateInstance
00200  *  Create a new CPI object instance.
00201  *  Adaptor implementation of create instance capability.
00202  *
00203  * @param data Adaptor-provided data object.
00204  * @param context The GATContext for this CPI instance
00205  * @param target The location of the file in question.
00206  * @param new_instance_data The pointer to the variable, where the instance 
00207  *      data of this CPI object is to be returned to.
00208  * 
00209  * @return An error code.
00210  */
00211 static GATResult 
00212 resourcebroker_adaptor_GATRequestCPI_CreateInstance(void *data, 
00213   GATRequestCPI_Instance *new_instance_data, void *initialisation_data)
00214 {
00215   if (NULL != new_instance_data)
00216   {
00217     GAT_USES_STATUS(new_instance_data->context, 
00218       "resourcebroker_adaptor_GATRequestCPI_CreateInstance");
00219 
00220     GAT_CREATE_STATUS(GATRequestCPIInstance_Data_Clone(initialisation_data, 
00221       (GATRequestCPIInstance_Data **)&new_instance_data->instance_data));
00222 
00223     return GAT_RETURN_STATUS();
00224   }
00225   return GAT_INVALID_HANDLE;
00226 }
00227 
00228 /** resourcebroker_adaptor_GATRequestCPI_DestroyInstance
00229  *  Destroy a CPI object instance.
00230  *  Adaptor implementation of destroy instance capability.
00231  *
00232  * @param data Adaptor-provided data object.
00233  * @param instance_data The instance data of the CPI object to destroy.
00234  */
00235 static void
00236 resourcebroker_adaptor_GATRequestCPI_DestroyInstance(void *data, 
00237   GATRequestCPI_Instance *instance_data)
00238 {
00239   GATRequestCPIInstance_Data_Destroy(
00240     (GATRequestCPIInstance_Data **) &instance_data->instance_data);
00241 }
00242 
00243 /** resourcebroker_adaptor_GATRequestCPI_CloneInstance
00244  *  Clone a CPI object instance.
00245  *  Adaptor implementation of the clone instance capability.
00246  *
00247  * @param data Adaptor-provided data object.
00248  * @param instance_data The instance data of this CPI object
00249  * @param new_instance_data The pointer to the variable, where the instance 
00250  *      data of the cloned CPI object is to be returned to.
00251  * 
00252  * @return An error code.
00253  */
00254 static GATResult 
00255 resourcebroker_adaptor_GATRequestCPI_CloneInstance(
00256   void *data, GATRequestCPI_Instance const *instance_data, 
00257   GATRequestCPI_Instance *new_instance_data)
00258 {
00259   if (NULL != instance_data)
00260   {
00261     GAT_USES_STATUS(instance_data->context, 
00262       "resourcebroker_adaptor_GATRequestCPI_CloneInstance");
00263     
00264     if (NULL != new_instance_data)
00265     {
00266       GAT_CREATE_STATUS(GATRequestCPIInstance_Data_Clone(
00267         (GATRequestCPIInstance_Data const *)instance_data->instance_data, 
00268         (GATRequestCPIInstance_Data **)&new_instance_data->instance_data));
00269     }
00270     else
00271     {
00272       GAT_CREATE_STATUS(GAT_INVALID_PARAMETER);
00273     }
00274     return GAT_RETURN_STATUS();
00275   }
00276   return GAT_INVALID_HANDLE;
00277 }
00278 
00279 /** resourcebroker_adaptor_GATRequestCPI_EqualsInstance
00280  *  Clone a CPI object instance.
00281  *  Adaptor implementation of the clone instance capability.
00282  *
00283  * @param data Adaptor-provided data object.
00284  * @param lhs The instance data of the left CPI object.
00285  * @param lhs The instance data of the right CPI object.
00286  * @param isequal The pointer to the variable, where the result is to be 
00287  *        returned to.
00288  *
00289  * @return An error code.
00290  */
00291 static GATResult 
00292 resourcebroker_adaptor_GATRequestCPI_EqualsInstance(
00293   void *data, GATRequestCPI_Instance const *lhs, 
00294   GATRequestCPI_Instance const *rhs, GATBool *isequal)
00295 {
00296   if (NULL != lhs && NULL != rhs)
00297   {
00298     GAT_USES_STATUS(lhs->context, 
00299       "resourcebroker_adaptor_GATRequestCPI_EqualsInstance");
00300 
00301     GAT_CREATE_STATUS(GATRequestCPIInstance_Data_Equals(
00302       (GATRequestCPIInstance_Data const *) lhs->instance_data, 
00303       (GATRequestCPIInstance_Data const *) rhs->instance_data, isequal));
00304 
00305     return GAT_RETURN_STATUS();
00306   }
00307   return GAT_INVALID_HANDLE;
00308 }
00309 
00310 
00311 /* CPI specific API functions */
00312 
00313 /** resourcebroker_adaptor_GATRequestCPI_InitRequestNotifier
00314  *
00315  *  Calls the adaptor to return the associated request notifier.
00316  *
00317  *  @param data Adaptor-provided data object.
00318  *  @param instance_data The instance data of the CPI object to ask for the 
00319  *        request notifier.
00320  *  @param notifier The pointer to a variable, which should receive the 
00321  *        returned request notifier.
00322  *
00323  *  @return An error code.
00324  */
00325 static GATResult
00326 resourcebroker_adaptor_GATRequestCPI_InitRequestNotifier(
00327   void *data, GATRequestCPI_Instance const *instance_data, 
00328   GATRequestNotifier_const *notifier)
00329 {
00330   GATResult retval = GAT_MEMORYFAILURE;
00331   GATRequestNotifier new_notifier = GATRequestNotifier_Create(data, 
00332     instance_data, resourcebroker_adaptor_GATRequestCPI_request_notifier_listener, 0);
00333     
00334   if (NULL != new_notifier)
00335   {
00336     if (NULL != notifier)
00337     {
00338       *notifier = new_notifier;
00339       retval = GAT_SUCCESS;
00340     }
00341     else
00342     {
00343       GATRequestNotifier_Destroy(&new_notifier);
00344       retval = GAT_INVALID_PARAMETER;
00345     }
00346   }
00347   return retval;
00348 }
00349 
00350 /** resourcebroker_adaptor_GATRequestCPI_request_notifier_listener
00351  *  
00352  */
00353 static GATResult 
00354 resourcebroker_adaptor_GATRequestCPI_request_notifier_listener(
00355   void *data, GATRequestCPI_Instance const *instance_data, 
00356   GATTable_const notifier_data, void *context_data)
00357 {
00358   GATResult retval = GAT_INVALID_HANDLE;
00359   if (NULL != notifier_data)
00360   {
00361     /* test, whether a checkpoint_file1 key exists */
00362     char buffer[PATH_MAX+1];
00363     retval = GATTable_Get_String(notifier_data, "checkpoint_file1",
00364       buffer, sizeof(buffer));
00365     if (GAT_SUCCEEDED(retval))
00366     {
00367       retval = !strcmp(buffer, "some_name") ? GAT_SUCCESS : GAT_FAIL;
00368     }
00369   }    
00370   return retval;
00371 }
00372 
00373