GridLab
Grid Application Toolkit

A simple API for Grid Applications
GAT

Menu



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

GATFileCPI.c

Go to the documentation of this file.
00001 /** @file GATFileCPI.c
00002  * Main file for the GATFileCPI class.
00003  * 
00004  * A GATFileCPI encapsulates all the methods that a GATFile capability
00005  * provider provides.
00006  * 
00007  * @date Thu Sep 18 2003
00008  * 
00009  * @version $Header: /export/cvs-gridlab/wp-1/Codes/GATEngine/C-reference/src/GATFileCPI.c,v 1.25 2004/04/02 12:31:57 hartmutkaiser Exp $
00010  *
00011  *  Copyright (C) Tom Goodale
00012  *  Copyright (C) Hartmut Kaiser
00013  *  This file is part of the GAT Engine.
00014  *  Contributed by Tom Goodale <goodale@aei.mpg.de> and
00015  *    Hartmut Kaiser <hartmutkaiser [at] t-online [dot] de>.
00016  *
00017  *  Use, modification and distribution is subject to the Gridlab Software
00018  *  License. (See accompanying file GLlicense.txt or copy at
00019  *  http://www.gridlab.org/GLlicense.txt)
00020  */
00021 
00022 static const char *rcsid = "$Header: /export/cvs-gridlab/wp-1/Codes/GATEngine/C-reference/src/GATFileCPI.c,v 1.25 2004/04/02 12:31:57 hartmutkaiser Exp $";
00023 
00024 /* System Header Files */
00025 
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 
00029 /* GAT Header Files */
00030 
00031 #include "GATFileCPI.h"
00032 
00033 /* Macros */
00034 
00035 /* Structures, unions and enums */
00036 
00037 struct GATFileCPI_S
00038 {
00039   /* CPI data (type local data) */
00040   void *data;
00041   GATFileCPI_Adaptor_Destroy destroy;
00042 
00043   /* CPI instance data */
00044   GATFileCPI_Adaptor_ServiceActions service_actions;
00045   GATFileCPI_Adaptor_CreateInstance create_instance;
00046   GATFileCPI_Adaptor_DestroyInstance destroy_instance;
00047   GATFileCPI_Adaptor_CloneInstance clone_instance;
00048   GATFileCPI_Adaptor_EqualsInstance equals_instance;
00049 
00050   /* CPI serialisation */
00051   GATFileCPI_Adaptor_Serialise serialise;
00052   GATFileCPI_Adaptor_DeSerialise deserialise;
00053   
00054   /* CPI functionality */
00055   GATFileCPI_Adaptor_Copy copy;
00056   GATFileCPI_Adaptor_Move move;
00057   GATFileCPI_Adaptor_Delete remove;
00058   GATFileCPI_Adaptor_IsReadable isreadable;
00059   GATFileCPI_Adaptor_IsWritable iswritable;
00060   GATFileCPI_Adaptor_GetLength length;
00061   GATFileCPI_Adaptor_LastWriteTime lastwritetime;
00062   
00063   /* CPI monitoring support */
00064   GATFileCPI_Adaptor_GetMetrics get_metrics;
00065   GATFileCPI_Adaptor_GetMetricEvent get_metric_event;
00066 };
00067 
00068 /* Static function prototypes */
00069 static GATBool GATFileCPI_IsValidData_V1(GATFileCPI_Data *data);
00070 
00071 /* File scope variables */
00072 
00073 /* External functions */
00074 
00075 /** GATFileCPI_Create
00076  *  The  GATFileCPI constructor.
00077  *  This is the constructor for GATFileCPI objects.
00078  *  
00079  *  @param version Version of the GATFileCPI_Data structure
00080  *  @param data Pointer to adaptor CPI instance data structure.
00081  *
00082  * @return A new GATFileCPI
00083  */
00084 GATFileCPI GATFileCPI_Create(unsigned long int version, GATFileCPI_Data *data)
00085 {
00086   GATFileCPI new_cpi = NULL;
00087 
00088   if (version <= GATFILECPI_VERSION)
00089   {
00090     if (version == GATFILECPI_VERSION && 
00091         GATTrue == GATFileCPI_IsValidData_V1(data))
00092     {
00093       new_cpi = (GATFileCPI)malloc(sizeof(struct GATFileCPI_S));
00094       if(NULL != new_cpi)
00095       {
00096         new_cpi->data = data->data;
00097         new_cpi->destroy = data->destroy;
00098         new_cpi->service_actions = data->service_actions;
00099         
00100         new_cpi->destroy_instance = data->destroy_instance;
00101         new_cpi->create_instance = data->create_instance;
00102         new_cpi->equals_instance = data->equals_instance;
00103         new_cpi->clone_instance = data->clone_instance;
00104 
00105         new_cpi->serialise = data->serialise;
00106         new_cpi->deserialise = data->deserialise;
00107 
00108         new_cpi->copy = data->copy;
00109         new_cpi->move = data->move;
00110         new_cpi->remove = data->remove;
00111         new_cpi->isreadable = data->isreadable;
00112         new_cpi->iswritable = data->iswritable;
00113         new_cpi->length = data->length;
00114         new_cpi->lastwritetime = data->lastwritetime;
00115         
00116         new_cpi->get_metrics = data->get_metrics;
00117         new_cpi->get_metric_event = data->get_metric_event;
00118       }
00119     }
00120 /*  add functionality for newer versions here
00121     else if (version == GATFILECPI_VERSION2 && GATFileCPI_IsValidData_V2(data))
00122     {
00123       ...
00124     }
00125  */
00126     else
00127     {
00128       /* Missing required functions */
00129       /* error_code = GAT_INVALID_PARAMETER; */
00130     }
00131   }
00132   else
00133   {
00134     /* unknown version */
00135     /* error_code = GAT_UNKNOWN_VERSION; */
00136   }
00137   return new_cpi;
00138 }
00139 
00140 /** GATFileCPI_Destroy
00141  *  @brief The GATFileCPI destructor.
00142  *
00143  *  This is the destructor for GATFileCPI objects.
00144  *
00145  *  @param this An old GATFileCPI
00146  */
00147 void GATFileCPI_Destroy(GATFileCPI *cpi)
00148 {
00149   if(NULL != cpi && NULL != *cpi)
00150   {
00151     (*cpi)->destroy((*cpi)->data);
00152     free(*cpi);
00153     *cpi = NULL;
00154   }
00155 }
00156 
00157 /** GATFileCPI_CreateInstance
00158  *  @brief Create a new CPI object instance
00159  *
00160  *  Calls the adaptor to create a new CPI object instance.
00161  *
00162  *  @param this The CPI object.
00163  *  @param instance_data The instance data of the attached CPI object
00164  *
00165  *  @return An error code.
00166  */
00167 GATResult 
00168 GATFileCPI_CreateInstance(GATFileCPI cpi, GATFileCPI_Instance *data)
00169 {
00170   return cpi->create_instance(cpi->data, data);
00171 }
00172 
00173 /** GATFileCPI_DestroyInstance
00174  *  @brief Create a new CPI object instance
00175  *
00176  *  Calls the adaptor to destroy a CPI object instance.
00177  *
00178  *  @param this The CPI object.
00179  *  @param instance_data The instance data of the attached CPI object
00180  */
00181 void 
00182 GATFileCPI_DestroyInstance(GATFileCPI cpi, GATFileCPI_Instance *data)
00183 {
00184   cpi->destroy_instance(cpi->data, data);
00185 }
00186 
00187 /** GATFileCPI_EqualsInstance
00188  *  @brief Compares two CPI object instances.
00189  *
00190  *  Calls the adaptor to compare two CPI object instances.
00191  *
00192  *  @param this The CPI object.
00193  *  @param lhs The instance data of the left CPI object
00194  *  @param rhs The instance data of the right CPI object
00195  *  @param isequal The pointer to the variable, where the result is to be 
00196  *        returned to.
00197  *
00198  *  @return An error code.
00199  */
00200 GATResult 
00201 GATFileCPI_EqualsInstance(GATFileCPI cpi, GATFileCPI_Instance const *lhs, 
00202   GATFileCPI_Instance const *rhs, GATBool *isequal)
00203 {
00204   return cpi->equals_instance(cpi->data, lhs, rhs, isequal);
00205 }
00206 
00207 /** GATFileCPI_CloneInstance
00208  *  @brief Clones a CPI object instance.
00209  *
00210  *  Calls the adaptor to clone a CPI object instance.
00211  *
00212  *  @param this The CPI object.
00213  *  @param instance_data The instance data of the CPI object to clone.
00214  *  @param rhs The new instance data is to be returned here.
00215  *
00216  *  @return An error code.
00217  */
00218 GATResult 
00219 GATFileCPI_CloneInstance(GATFileCPI cpi, 
00220   GATFileCPI_Instance const *data, GATFileCPI_Instance *new_data) 
00221 {
00222   return cpi->clone_instance(cpi->data, data, new_data);
00223 }
00224 
00225 /** GATFileCPI_Serialise
00226  *  @brief Serialise the instance data.
00227  *
00228  *  Calls the adaptor to serialise the instance data.
00229  *
00230  *  @param this The CPI object.
00231  *  @param instance_data The instance data of the attached CPI object
00232  *  @param stream The stream to use be used for serialisation.
00233  *  @param clear_dirty If the clear_dirty parameter is set to GATTrue, the 
00234  *        internal dirty flag of this object is to be reset.
00235  *
00236  *  @return An error code.
00237  */
00238 GATResult 
00239 GATFileCPI_Serialise(GATFileCPI cpi, 
00240   GATFileCPI_Instance const *data, GATObject stream, GATBool clear_dirty)
00241 {
00242   return cpi->serialise(cpi->data, data, stream, clear_dirty);
00243 }
00244   
00245 /** GATFileCPI_DeSerialise
00246  *  @brief De-serialise the instance data
00247  *
00248  *  Call the adaptor to de-serialise the instance data.
00249  *
00250  *  @param context The GAT context to be used for object construction.
00251  *  @param stream The stream interface to use for the serialisation.
00252  *  @param instance_data The pointer to a variable, which contains the client 
00253  *        data of the new object. The member instance data of this object 
00254  *        may receive the pointer to the new instance data of the CPI object.
00255  *
00256  *  @return An error code.
00257  */
00258 GATResult 
00259 GATFileCPI_DeSerialise(GATFileCPI cpi,
00260   GATObject stream, GATFileCPI_Instance *data)
00261 {
00262   return cpi->deserialise(cpi->data, stream, data);
00263 }
00264   
00265 /** GATFileCPI_Copy
00266  *  @brief Copy a file from one location to another.
00267  *
00268  *  Calls the adaptor to copy a file from one place to another.
00269  *
00270  *  @param this The CPI object.
00271  *  @param instance_data The instance data of the attached CPI object
00272  *  @param targetLocation Location to copy file to.
00273  *
00274  *  @return An error code.
00275  */
00276 GATResult GATFileCPI_Copy(GATFileCPI cpi, GATFileCPI_Instance const *data,
00277   GATLocation_const targetLocation, GATFileMode mode)
00278 {
00279   return cpi->copy(cpi->data, data, targetLocation, mode);
00280 }
00281 
00282 /** GATFileCPI_Move
00283  *  @brief Move a file from one location to another.
00284  *
00285  *  Calls the adaptor to move a file from one place to another.
00286  *
00287  *  @param this The CPI object.
00288  *  @param instance_data The instance data of the attached CPI object
00289  *  @param targetLocation Location to move the file to.
00290  *
00291  *  @return An error code.
00292  */
00293 GATResult 
00294 GATFileCPI_Move(GATFileCPI cpi, GATFileCPI_Instance const *data, 
00295   GATLocation_const targetLocation, GATFileMode mode)
00296 {
00297   return cpi->move(cpi->data, data, targetLocation, mode);
00298 }
00299 
00300 /** GATFileCPI_Delete
00301  *  @brief Delete a file.
00302  *
00303  *  Calls the adaptor to delete a file.
00304  *
00305  *  @param this The CPI object.
00306  *  @param instance_data The instance data of the attached CPI object
00307  *
00308  *  @return An error code.
00309  */
00310 GATResult 
00311 GATFileCPI_Delete(GATFileCPI file, GATFileCPI_Instance const *data)
00312 {
00313   return file->remove(file->data, data);
00314 }
00315 
00316 /** GATFileCPI_IsReadable
00317  *  @brief Check the file attributes for the readable flag
00318  *
00319  *  The function GATFile_IsReadable calls the adaptor which checks the file 
00320  *  attributes of the given file and returns, whether it is readable.
00321  *
00322  *  @param this The CPI object.
00323  *  @param instance_data The instance data of the attached CPI object
00324  *
00325  *  @return If the inspected file is readable, the function returns 
00326  *        @c #GAT_SUCCESS, if it is not readable, @c #GAT_FALSE is returned.
00327  *        Otherwise the function returns any of the possible errorcodes.
00328  */
00329 GATResult 
00330 GATFileCPI_IsReadable(GATFileCPI cpi, GATFileCPI_Instance const *data)
00331 {
00332   return cpi->isreadable(cpi->data, data);
00333 }
00334 
00335 /** GATFileCPI_IsWritable
00336  *  @brief Check the file attributes for the writable flag
00337  *
00338  *  The function GATFileCPI_IsWritable calls the adaptor which checks the file 
00339  *  attributes of the given file and returns, whether it is writable.
00340  *
00341  *  @param this The CPI object.
00342  *  @param instance_data The instance data of the attached CPI object
00343  *
00344  *  @return If the inspected file is writable, the function returns 
00345  *        @c #GAT_SUCCESS, if it is not writable, @c #GAT_FALSE is returned.
00346  *        Otherwise the function returns any of the possible errorcodes.
00347  */
00348 GATResult 
00349 GATFileCPI_IsWritable(GATFileCPI cpi, GATFileCPI_Instance const *data)
00350 {
00351   return cpi->iswritable(cpi->data, data);
00352 }
00353 
00354 /** GATFileCPI_GetLength
00355  *  @brief Returns the size of the given file
00356  *
00357  *  The function GATFile_GetLength returns the file size of the given size  
00358  *  measured in bytes.
00359  *
00360  *  @param this The CPI object.
00361  *  @param instance_data The instance data of the attached CPI object
00362  *
00363  *  @return The size of the given file measured in bytes. If an error occurs, 
00364  *        the function returns (unsigned long)(-1).
00365  */
00366 GATResult 
00367 GATFileCPI_GetLength(GATFileCPI cpi, GATFileCPI_Instance const *data, 
00368   unsigned long *length)
00369 {
00370   return cpi->length(cpi->data, data, length);
00371 }
00372 
00373 /** GATFileCPI_LastWriteTime
00374  *  @brief Returns the time of last modification of the given file
00375  *
00376  *  The function GATFile_LastWriteTime returns the time of last modification of
00377  *  the inspected file.
00378  *
00379  *  @param this The CPI object.
00380  *  @param instance_data The instance data of the attached CPI object
00381  *
00382  *  @return The time of the last modification of the file. If an error occurs, 
00383  *        the function will return 0 (zero).
00384  */
00385 GATResult 
00386 GATFileCPI_LastWriteTime(GATFileCPI cpi, 
00387   GATFileCPI_Instance const *data, GATTime *lw_time)
00388 {
00389   return cpi->lastwritetime(cpi->data, data, lw_time);
00390 }
00391 
00392 /** GATFileCPI_GetMetrics
00393  *
00394  *  The function GATFileCPI_GetMetrics returns the list of metrics supported
00395  *  by this adaptor.
00396  *
00397  *  @param this The CPI object.
00398  *  @param instance_data The instance data of the attached CPI object
00399  *  @param The pointer to the variable, which receives the resulting list of
00400  *        metrics.
00401  *
00402  *  @return An error code.
00403  */
00404 GATResult 
00405 GATFileCPI_GetMetrics(GATFileCPI cpi, GATFileCPI_Instance const *data,
00406   GATList_GATMetric *metrics)
00407 {
00408   return cpi->get_metrics(cpi->data, data, metrics);
00409 }
00410 
00411 /** GATFileCPI_GetMetricEvent
00412  *
00413  *  The function GATFileCPI_GetMetricEvent returns the metric event, which is 
00414  *  associated with the given metric.
00415  *
00416  *  @param this The CPI object.
00417  *  @param instance_data The instance data of the attached CPI object
00418  *  @param metric The continuous metric, for which the metric event is to be 
00419  *        returned. 
00420  *  @param event The pointer to the variable, which receives the resulting
00421  *        metric event.
00422  *
00423  *  @return An error code.
00424  */
00425 GATResult
00426 GATFileCPI_GetMetricEvent(GATFileCPI cpi, GATFileCPI_Instance const *data,
00427   GATMetric metric, GATMetricEvent *event)
00428 {
00429   return cpi->get_metric_event(cpi->data, data, metric, event);
00430 }
00431 
00432 /* Local functions */
00433 static GATBool 
00434 GATFileCPI_IsValidData_V1(GATFileCPI_Data *data)
00435 {
00436   return (
00437       NULL != data->destroy && 
00438 //      NULL != data->service_actions &&
00439       
00440       NULL != data->destroy_instance && 
00441       NULL != data->create_instance && 
00442       NULL != data->clone_instance && 
00443       NULL != data->equals_instance &&  
00444       
00445       NULL != data->serialise && 
00446       NULL != data->deserialise && 
00447            
00448       NULL != data->copy && 
00449       NULL != data->move && 
00450       NULL != data->remove && 
00451       NULL != data->isreadable &&
00452       NULL != data->iswritable &&
00453       NULL != data->length &&
00454       NULL != data->lastwritetime &&
00455       
00456       NULL != data->get_metrics &&
00457       NULL != data->get_metric_event
00458     ) ? GATTrue : GATFalse;
00459 }