GridLab
Grid Application Toolkit

A simple API for Grid Applications
GAT

Menu



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

GATLocation.c

Go to the documentation of this file.
00001 /** @file GATLocation.c
00002  * Main file for the GATLocation class.
00003  * 
00004  * A GATLocation represents the location of an abstract or physical resource.
00005  * The location is represented as a URI - see RFC 2396 and RFC 2732.
00006  * 
00007  * @date Wed Sep 24 2003
00008  * 
00009  * @version $Header: /export/cvs-gridlab/wp-1/Codes/GATEngine/C-reference/src/GATLocation.c,v 1.17 2004/04/02 12:31:58 hartmutkaiser Exp $
00010  *
00011  *  Copyright (C) Tom Goodale
00012  *  This file is part of the GAT Engine.
00013  *  Contributed by Tom Goodale <goodale@aei.mpg.de>.
00014  *
00015  *  Use, modification and distribution is subject to the Gridlab Software
00016  *  License. (See accompanying file GLlicense.txt or copy at
00017  *  http://www.gridlab.org/GLlicense.txt)
00018  */
00019 
00020 static const char *rcsid = "$Header: /export/cvs-gridlab/wp-1/Codes/GATEngine/C-reference/src/GATLocation.c,v 1.17 2004/04/02 12:31:58 hartmutkaiser Exp $";
00021 
00022 /* System Header Files */
00023 
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 
00028 /* GAT Header Files */
00029 #include "GATLocation.h"
00030 #include "GATInternal.h"
00031 #include "GATErrors.h"
00032 #include "GATXdsWrapper.h"
00033 #include "GATUtil.h"
00034 
00035 /* define the vtable types */
00036 GATOBJECT_DEFINE_VTABLE(GATLocation);
00037 GATSERIALISABLE_DEFINE_VTABLE(GATLocation);
00038 
00039 /* Declare the converters to/from GATObject */
00040 GATOBJECT_DEFINE_CONVERTERS(GATLocation);
00041 
00042 /* Macros */
00043 
00044 /* Structures, unions and enums */
00045 
00046 struct GATLocation_S
00047 {
00048   GATLocation_vtable *GATObject__vtable;
00049   GATLocation_ISerialisable_vtable *GATSerialisable__vtable;
00050   char *uri;
00051 };
00052 
00053 /* Static function prototypes */
00054 
00055 static char *GetSubString(const char *string,
00056                           int start,
00057                           int end);
00058 
00059 static int
00060 GATLocation_DeSerialise_Create(GATContext context, GATObject stream, 
00061   char const *uri, GATLocation *new_object);
00062 
00063 /* File scope variables */
00064 static GATLocation_vtable GATLocation__vtable = {
00065   GATLocation_GetType,
00066   GATLocation_Destroy,
00067   GATLocation_Equals,
00068   GATLocation_Clone,
00069   GATLocation_GetInterface,
00070   NULL
00071 };
00072 
00073 static GATLocation_ISerialisable_vtable 
00074   GATLocation_ISerialisable__vtable = 
00075 {
00076   GATLocation_Serialise,
00077   GATLocation_DeSerialise,
00078   GATLocation_GetIsDirty,
00079 };
00080 
00081 /* External functions */
00082 
00083 /** GATLocation_Register_GATSerialisable
00084  *  The GATLocation_Register_GATSerialisable function registers the serialization
00085  *  vtable with the GAT engine to allow generic object creation.
00086  *  This function is called by the GAT engine, there is no need to use it 
00087  *  directly.
00088  */
00089 GATResult GATLocation_Register_GATSerialisable(void)
00090 {
00091   return GATObject_Register_GATSerialisable(GATType_GATLocation, 
00092     &GATLocation_ISerialisable__vtable);
00093 }
00094 
00095 /** GATLocation_Create
00096  *  The  GATLocation constructor.
00097  *  This is the constructor for GATLocation objects.
00098  *
00099  * @return A new GATLocation
00100  */
00101 GATLocation GATLocation_Create(const char *uri)
00102 {
00103   GATLocation this;
00104 
00105   this = (GATLocation)malloc(sizeof(*this));
00106 
00107   if(this)
00108   {
00109     this->GATObject__vtable = &GATLocation__vtable;
00110     this->GATSerialisable__vtable = &GATLocation_ISerialisable__vtable;
00111     this->uri = GetSubString(uri,0,strlen(uri));
00112 
00113     if(!this->uri)
00114     {
00115       free(this);
00116       this = NULL;
00117     }
00118   }
00119 
00120   return this;
00121 }
00122 
00123 /** GATLocation_Destroy
00124  *  The GATLocation destructor.
00125  *  This is the destructor for GATLocation objects.
00126  *
00127  * @param this An old GATLocation
00128  */
00129 void GATLocation_Destroy(GATLocation *this)
00130 {
00131   if(NULL != this && NULL != *this)
00132   {
00133     if((*this)->uri)
00134     {
00135       free((*this)->uri);
00136     }
00137     free(*this);
00138     *this = NULL;
00139   }
00140 }
00141 
00142 /** GATLocation_ToString
00143  *  Get a copy of the string representation of the location..
00144  *  Return the content of the GATLocation as a string.  This is
00145  *  effectively the string given in the constructor.
00146  *
00147  * @param this The GATLocation
00148  * 
00149  * @return A string containing the URI.
00150  */
00151 char *GATLocation_ToString(GATLocation_const this)
00152 
00153 {
00154   char *retval;
00155 
00156   retval = GetSubString(this->uri,0,strlen(this->uri));
00157 
00158   return retval;
00159 }
00160 
00161 /** int GATLocation_GetType(GATLocation_const location)
00162  *  @brief Return the type of the GATLocation
00163  *
00164  *  The function @c GATLocation_GetType always returns GATType_GATLocation. 
00165  *
00166  *  @param object The object to inspect
00167  *
00168  *  @return returns always @c #GATType_GATLocation.
00169  */
00170 GATType GATLocation_GetType(GATLocation_const location)
00171 {
00172   GAT_UNUSED_PARAMETER(location);
00173   return GATType_GATLocation;
00174 }
00175 
00176 /** GATLocation_Clone
00177  *  Makes a deep copy of the GATLocation.
00178  *  Return a new GATLocation which is equivalent to this one.
00179  *
00180  * @param this The GATLocation
00181  * 
00182  * @return A string containing the URI.
00183  */
00184 int GATLocation_Clone(GATLocation_const this, GATLocation *new_location)
00185 {
00186   int retval = GAT_INVALID_PARAMETER;
00187   GATLocation location = GATLocation_Create(this->uri);
00188   
00189   if (NULL != location)
00190   {
00191     if (NULL != new_location)
00192     {
00193       *new_location = location;
00194       retval = GAT_SUCCESS;
00195     }
00196   }
00197   else
00198   {
00199     retval = GAT_MEMORYFAILURE;
00200   }
00201   
00202   if (GAT_SUCCESS != retval)
00203   {
00204     GATLocation_Destroy(&location);
00205   }
00206   return retval;
00207 }
00208 
00209 /** GATLocation_Equals
00210  *  Compares two GATLocation objects for equality
00211  *
00212  * @param this The GATLocation
00213  * @param that The GATLocation to compare with
00214  *
00215  * @return 1 upon equality 0 otherwise
00216  */
00217 int GATLocation_Equals(GATLocation_const this, GATLocation_const that, 
00218   GATBool *isequal)
00219 {
00220   char *this_str = GATLocation_ToString(this);
00221   char *that_str = GATLocation_ToString(that);
00222   int result = GAT_INVALID_PARAMETER;
00223   
00224   if (NULL != this_str && NULL != that_str && NULL != isequal)
00225   {
00226     *isequal = strcmp(this_str, that_str) ? GATFalse : GATTrue;
00227     result = GAT_SUCCESS;
00228   }
00229   
00230   free(this_str);
00231   free(that_str);
00232   return result;
00233 }
00234 
00235 /** GATResult GATLocation_GetInterface(GATLocation_const file, GATInterface iftype, void const **ifp)
00236  *  @brief Get an interface supported by a GATObject
00237  *
00238  *  The function GATLocation_GetInterface allows to get a pointer to an 
00239  *  additional interface supported by this GATLocation.
00240  *
00241  *  @param object The object to be asked for the new interface.
00242  *  @param iftype The interface the object is to be asked for.
00243  *  @param ifp The pointer, through which the result is to be returned.
00244  *
00245  *  @return An error type.
00246  */
00247 GATResult 
00248 GATLocation_GetInterface(GATLocation_const object, GATInterface iftype, 
00249   void const **ifp)
00250 {
00251   GATResult retval = GAT_INVALID_PARAMETER;
00252 
00253   if (NULL != ifp)
00254   {
00255     *ifp = NULL;
00256     if (GATInterface_ISerialisable == iftype)
00257     {
00258       *ifp = (void const *) &object->GATSerialisable__vtable;
00259       retval = GAT_SUCCESS;
00260     }
00261     else
00262     {
00263       retval = GAT_NO_INTERFACE;
00264     }
00265   }
00266   return retval;
00267 }
00268 
00269 /* GATAdvertiseable API */
00270 
00271 /** GATResult GATLocation_Serialise(GATLocation object, GATObject stream, GATBool clear_dirty)
00272  *  @brief Serialise a GATLocation object
00273  *
00274  *  The function GATLocation_Serialise serialises the given GATLocation object 
00275  *  into the given stream. 
00276  *
00277  *  @param object The GATLocation object to serialise.
00278  *  @param stream The stream interface to use for the serialisation.
00279  *  @param clear_dirty If the clear_dirty parameter is set to GATTrue, the 
00280  *        internal dirty flag of this object is to be reset (no used here)
00281  *
00282  *  @return An error code.
00283  */
00284 GATResult 
00285 GATLocation_Serialise(GATLocation object, GATObject stream, GATBool clear_dirty)
00286 {
00287   GATResult retval = GAT_INVALID_HANDLE;
00288   if (NULL != object)
00289   {
00290     retval = GATXds_SerialiseObject(GATLocation_ToGATObject(object), stream, 
00291       clear_dirty, 0, "uint32 string", GATLOCATION_VERSION1, object->uri);
00292   }
00293   return retval;
00294 }
00295 
00296 /** GATLocation_VersionCallback
00297  *
00298  *  The function GATLocation_VersionCallback is used as a callback function
00299  *  during the de-serialisation of a GATLocation. It should be provided to test,
00300  *  whether the de-serialised version matches the expected version.
00301  *
00302  *  @param version The version number, which was de-serialised from the stream.
00303  *
00304  *  @return This function should return GATTrue, if the version matches the 
00305  *        expected value (version is valid), GATFalse otherwise.
00306  *
00307  *  @remark This function is called from the GAT engine, there is no need to 
00308  *        call it directly.
00309  */
00310 static GATBool
00311 GATLocation_VersionCallback(GATuint32 version)
00312 {
00313   GATBool retval = GATFalse;
00314   if ((version & ~GATLOCATION_MINOR_MASK) <= GATLOCATION_LASTVERSION)
00315   {
00316     retval = GATTrue;
00317   }
00318   return retval;
00319 }
00320 
00321 /** GATLocation_DeSerialiseCallback
00322  *
00323  *  The function GATLocation_DeSerialiseCallback is used as a callback function
00324  *  during the de-serialisation of a GATLocation. It should be provided for the
00325  *  instantiation of the new GATLocation object based on the already de-serialised 
00326  *  data items and the de-serialisation of the associated CPI provider data for 
00327  *  the given object.
00328  *
00329  *  @param context The GAT context to be used for object construction.
00330  *  @param stream The stream interface to use for the serialisation.
00331  *  @param object The pointer to the variable, which should receive the newly 
00332  *        constructed object.
00333  *  @param version The version of the saved data read from the input stream.
00334  *  @param args This parameter is the pointer to the va_list containing 
00335  *        pointers to the already de-serialised data items accordingly to the
00336  *        format string, provided during the call to the 
00337  *        GATLocation_DeSerialise function.
00338  *
00339  *  @return An error code.
00340  *
00341  *  @remark This function is called from the GAT engine, there is no need to 
00342  *        call it directly.
00343  */
00344 static GATResult 
00345 GATLocation_DeSerialiseCallback(GATContext context, GATObject stream, 
00346   GATObject *new_object, GATuint32 version, va_list args)
00347 {
00348   /* the version was eaten already */
00349   char const **uri = va_arg(args, char const **); 
00350   GATLocation object = NULL;
00351   
00352   /* construct the new object */
00353   GATResult retval = GATLocation_DeSerialise_Create(context, stream, *uri, &object);
00354 
00355   GAT_UNUSED_PARAMETER(version);
00356 
00357   if (GAT_SUCCESS == retval)
00358   {
00359     if (NULL != object)
00360     {
00361       *new_object = GATLocation_ToGATObject(object);
00362     }
00363     else
00364     {
00365       GATLocation_Destroy(&object);
00366       retval = GAT_INVALID_PARAMETER;
00367     }
00368   }
00369   return retval;
00370 }
00371 
00372 /** GATLocation GATLocation_DeSerialise(GATContext context, GATObject stream, GATBool clear_dirty)
00373  *  @brief De-serialise a GATLocation object
00374  *
00375  *  The function GATLocation_DeSerialise de-serialises a streamed GATLocation object  
00376  *  from the given stream  It constructs a new instance of the de-serialised 
00377  *  object.
00378  *
00379  *  @param context The GAT context to be used for object construction.
00380  *  @param stream The stream interface to use for the serialisation.
00381  *  @param result The pointer to a variable, which receives the status code of
00382  *        the operation.
00383  *
00384  *  @return The newly constructed GATLocation object.
00385  */
00386 GATLocation 
00387 GATLocation_DeSerialise(GATContext context, GATObject stream, GATResult *result)
00388 {
00389   GAT_USES_STATUS(context, "GATLocation_DeSerialise");
00390   
00391   GATObject object = NULL;     /* the new object will be created here */
00392   
00393   /* we must provide all instance data items to be de-serialised for this
00394      GATLocation object accordingly to the provided format string */
00395   GATuint32 version = 0;
00396   char *uri = NULL; 
00397 
00398   /* read the data */
00399   GAT_CREATE_STATUS(GATXds_DeSerialiseObject(context, stream, 
00400     GATLocation_DeSerialiseCallback, GATLocation_VersionCallback, &object, 
00401     "uint32 string", &version, &uri));
00402 
00403   free(uri); 
00404   if (NULL != result)
00405   { 
00406     *result = GAT_CURRENT_STATUS();
00407   }
00408   else
00409   {
00410     GAT_CREATE_STATUS(GAT_INVALID_PARAMETER);
00411   }
00412   GAT_STORE_STATUS();
00413   return GATObject_ToGATLocation(object);
00414 }
00415 
00416 /** GATLocation_GetIsDirty
00417  *  
00418  *  The function GATLocation_GetIsDirty retrieves the status of the dirty flag of 
00419  *  this GATLocation object.
00420  *
00421  *  @param file The GATLocation object to inspect for its dirty status.
00422  *  @param isdirty The pointer to a variable, which receives the dirty status.
00423  *
00424  *  @return An error code.
00425  */
00426 GATResult GATLocation_GetIsDirty(GATLocation_const object, GATBool *isdirty)
00427 {
00428   GATResult retval = GAT_INVALID_HANDLE;
00429   if (NULL != object)
00430   {
00431     if (NULL != isdirty)
00432     {
00433       *isdirty = GATFalse;    /* a GATLocation is never 'dirty' */
00434       retval = GAT_SUCCESS;
00435     }
00436     else
00437     {
00438       retval = GAT_INVALID_PARAMETER;
00439     }
00440   }  
00441   return retval;
00442 }
00443 
00444 
00445 /* Local functions */
00446 
00447 /** GATLocation_DeSerialise_Create
00448  *
00449  *  The function GATLocation_DeSerialise_Create creates a new GATLocation object.
00450  *
00451  *  @param context The GAT context to use for creation of the GATLocation object.
00452  *  @param stream The object from which the adaptor should read the streamed
00453  *        instance data.
00454 
00455 TODO: Add the description of your additional parameters here
00456 
00457  *  @param new_object The pointer to the variable, which should receive the 
00458  *        newly constructed GATLocation object.
00459  *
00460  *  @return An error code.
00461  */
00462 static GATResult 
00463 GATLocation_DeSerialise_Create(GATContext context, GATObject stream, 
00464   char const *uri, GATLocation *object)
00465 {
00466   GAT_USES_STATUS(context, "GATLocation_DeSerialise_Create");
00467   GATLocation new_object = (GATLocation) malloc(sizeof(struct GATLocation_S));
00468   GAT_UNUSED_PARAMETER(stream);
00469   if(NULL != new_object)
00470   {
00471     memset(new_object, 0, sizeof(struct GATLocation_S));
00472     new_object->GATObject__vtable = &GATLocation__vtable;
00473     new_object->GATSerialisable__vtable = &GATLocation_ISerialisable__vtable;
00474     
00475     new_object->uri = GATUtil_strdup(uri);
00476     GAT_CREATE_STATUS_IF(NULL == new_object->uri, GAT_MEMORYFAILURE);
00477   }
00478   else
00479   {
00480     GAT_CREATE_STATUS(GAT_MEMORYFAILURE);
00481   }
00482   
00483   if (GAT_SUCCEEDED(GAT_CURRENT_STATUS()))
00484   {
00485     if (NULL != new_object)
00486     {
00487       *object = new_object;
00488     }
00489     else
00490     {
00491       GATLocation_Destroy(&new_object);
00492       GAT_CREATE_STATUS(GAT_INVALID_PARAMETER);
00493     }
00494   }
00495   else
00496   {
00497     GATLocation_Destroy(&new_object);
00498   }
00499   return GAT_RETURN_STATUS();
00500 }
00501 
00502 /** GetSubString
00503  *  Copy a portion of a string.
00504  *  Creates a new string which is a substring of the old one.
00505  *
00506  * @param string The original string
00507  * @param start Starting location of substring.
00508  * @param end End location of substring.
00509  * 
00510  * @return A new string holding the substring.
00511  */
00512 static char *GetSubString(const char *string,
00513                           int start,
00514                           int end)
00515 {
00516   char *retval;
00517   int length;
00518 
00519   length = end-start;
00520 
00521   if(length > 0)
00522   {
00523     retval = (char *)malloc(length+1);
00524 
00525     if(retval)
00526     {
00527       strncpy(retval, string+start, length);
00528 
00529       retval[length] = 0;
00530     }
00531   }
00532   else
00533   {
00534     retval = NULL;
00535   }
00536 
00537   return retval;
00538 }