00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 static const char *rcsid = "$Header: /export/cvs-gridlab/wp-1/Codes/GATEngine/C-reference/adaptors/filestream/adaptor.c,v 1.6 2004/04/22 10:25:05 hartmutkaiser Exp $";
00021
00022
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <errno.h>
00027
00028
00029 #include <GATCPI.h>
00030
00031
00032 #define __countof(x) (sizeof(x)/sizeof(x[0]))
00033
00034
00035
00036
00037 typedef struct GATFileStreamInstance_Data {
00038 FILE *file;
00039 } GATFileStreamInstance_Data;
00040
00041
00042 static void
00043 filestream_adaptor_FileStreamCPI_Destroy(void *data);
00044
00045
00046 GATResult
00047 filestream_adaptor_FileStreamCPI_CreateInstance(void *adaptor_data,
00048 GATFileStreamCPI_Instance *data);
00049
00050 void
00051 filestream_adaptor_FileStreamCPI_DestroyInstance(void *adaptor_data,
00052 GATFileStreamCPI_Instance *data);
00053
00054 GATResult
00055 filestream_adaptor_FileStreamCPI_CloneInstance(void *adaptor_data,
00056 GATFileStreamCPI_Instance const *data,
00057 GATFileStreamCPI_Instance *new_data);
00058
00059 GATResult
00060 filestream_adaptor_FileStreamCPI_EqualsInstance(void *adaptor_data,
00061 GATFileStreamCPI_Instance const *lhs, GATFileStreamCPI_Instance const *rhs,
00062 GATBool *isequal);
00063
00064
00065 static GATResult
00066 filestream_adaptor_FileStreamCPI_Read(void *data,
00067 GATFileStreamCPI_Instance const *instance_data, void *buffer, GATuint32 size,
00068 GATuint32 *readBytes);
00069
00070 static GATResult
00071 filestream_adaptor_FileStreamCPI_Write(void *data,
00072 GATFileStreamCPI_Instance const *instance_data, void const *buffer,
00073 GATuint32 size, GATuint32 *writtenBytes);
00074
00075 static GATResult
00076 filestream_adaptor_FileStreamCPI_Seek(void *data,
00077 GATFileStreamCPI_Instance const *instance_data, GATOrigin origin,
00078 GATint32 offset, GATuint32 *new_position);
00079
00080 static GATResult
00081 filestream_adaptor_FileStreamCPI_Close(void *data,
00082 GATFileStreamCPI_Instance *instance_data);
00083
00084
00085 static GATResult
00086 filestream_adaptor_FileStreamCPI_GetMetrics(void *data,
00087 GATFileStreamCPI_Instance const *instance_data, GATList_GATMetric *metrics);
00088
00089
00090 static GATResult
00091 filestream_adaptor_FileStreamCPI_GetMetricEvent(void *data,
00092 GATFileStreamCPI_Instance const *instance_data, GATMetric metric,
00093 GATMetricEvent *event);
00094
00095
00096 static GATResult
00097 filestream_adaptor_FireReadEvent(GATContext context,
00098 GATMonitorable_Impl monitorable, GATObject_const source, char const *name);
00099
00100 static GATResult
00101 filestream_adaptor_FireWriteEvent(GATContext context,
00102 GATMonitorable_Impl monitorable, GATObject_const source, char const *name);
00103
00104 static GATResult
00105 filestream_adaptor_FireSeekEvent(GATContext context,
00106 GATMonitorable_Impl monitorable, GATObject_const source, char const *name);
00107
00108 static GATResult
00109 filestream_adaptor_FireCloseEvent(GATContext context,
00110 GATMonitorable_Impl monitorable, GATObject_const source, char const *name);
00111
00112
00113 static GATStaticMetric metric_data[] = {
00114
00115 {
00116 "filestream.read",
00117 GATMeasurementType_EventLike,
00118 GATType_String,
00119 "",
00120 0,
00121 0
00122 },
00123
00124 {
00125 "filestream.write",
00126 GATMeasurementType_EventLike,
00127 GATType_String,
00128 "",
00129 0,
00130 0
00131 },
00132
00133 {
00134 "filestream.seek",
00135 GATMeasurementType_EventLike,
00136 GATType_String,
00137 "",
00138 0,
00139 0
00140 },
00141
00142 {
00143 "filestream.close",
00144 GATMeasurementType_EventLike,
00145 GATType_String,
00146 "",
00147 0,
00148 0
00149 },
00150 };
00151
00152
00153 #define METRIC_FILESTREAM_READ 0
00154 #define METRIC_FILESTREAM_WRITE 1
00155 #define METRIC_FILESTREAM_SEEK 2
00156 #define METRIC_FILESTREAM_CLOSE 3
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 GATResult
00182 filestream_adaptor_register(GATContext error_context, GATRegistry registry,
00183 GATTable_const system_config, GATTable_const instance_config, void *token)
00184 {
00185 GATResult retcode = GAT_MEMORYFAILURE;
00186
00187 if (!GATVERSION_ISCOMPATIBLE())
00188 {
00189 retcode = GAT_UNKNOWN_VERSION;
00190 }
00191 else
00192 {
00193 GATFileStreamCPI cpi = NULL;
00194 GATFileStreamCPI_Data cpidata;
00195
00196 memset(&cpidata, 0, sizeof(GATFileStreamCPI_Data));
00197
00198
00199
00200
00201
00202
00203 cpidata.data = NULL;
00204 cpidata.destroy = filestream_adaptor_FileStreamCPI_Destroy;
00205
00206
00207
00208
00209 cpidata.create_instance = filestream_adaptor_FileStreamCPI_CreateInstance;
00210 cpidata.destroy_instance = filestream_adaptor_FileStreamCPI_DestroyInstance;
00211 cpidata.clone_instance = filestream_adaptor_FileStreamCPI_CloneInstance;
00212 cpidata.equals_instance = filestream_adaptor_FileStreamCPI_EqualsInstance;
00213
00214
00215 cpidata.read = filestream_adaptor_FileStreamCPI_Read;
00216 cpidata.write = filestream_adaptor_FileStreamCPI_Write;
00217 cpidata.seek = filestream_adaptor_FileStreamCPI_Seek;
00218 cpidata.close = filestream_adaptor_FileStreamCPI_Close;
00219
00220
00221 cpidata.get_metrics = filestream_adaptor_FileStreamCPI_GetMetrics;
00222 cpidata.get_metric_event = filestream_adaptor_FileStreamCPI_GetMetricEvent;
00223
00224
00225 cpi = GATFileStreamCPI_Create(GATFILESTREAMCPI_VERSION, &cpidata);
00226 if(NULL != cpi)
00227 {
00228
00229
00230
00231 GATPreferences preferences = GATPreferences_Create();
00232 if(NULL != preferences)
00233 {
00234
00235 GATPreferences_Add(preferences, "Name", "filestream_adaptor");
00236 GATPreferences_Add(preferences, "Security", "none");
00237 GATPreferences_Add(preferences, "Local", "true");
00238
00239 retcode = GATRegistry_AddGATFileStreamCPI(registry, cpi, token,
00240 preferences);
00241
00242 GATPreferences_Destroy(&preferences);
00243 }
00244 }
00245
00246 if (GAT_SUCCESS != retcode)
00247 {
00248 GATFileStreamCPI_Destroy(&cpi);
00249 }
00250 }
00251 return retcode;
00252 }
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264 static void
00265 filestream_adaptor_FileStreamCPI_Destroy(void *data)
00266 {
00267
00268 }
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281 GATResult
00282 filestream_adaptor_FileStreamCPI_CreateInstance(void *adaptor_data,
00283 GATFileStreamCPI_Instance *data)
00284 {
00285 if (NULL != data)
00286 {
00287 GAT_USES_STATUS(data->context, "filestream_adaptor_FileStreamCPI_CreateInstance");
00288 char *location = GATLocation_ToString(data->location);
00289
00290 if (NULL != location)
00291 {
00292 FILE *file = NULL;
00293 if (GATFileStreamMode_Read == data->mode)
00294 {
00295 file = fopen(location, "r");
00296 }
00297 else if (GATFileStreamMode_Write == data->mode)
00298 {
00299 file = fopen(location, "w");
00300 }
00301 else if (GATFileStreamMode_Append == data->mode)
00302 {
00303 file = fopen(location, "a");
00304 }
00305 else
00306 {
00307 file = fopen(location, "r+");
00308 }
00309
00310 if (NULL != file)
00311 {
00312 data->instance_data = (void *) file;
00313 }
00314 else
00315 {
00316 GAT_CREATE_STATUS(GAT_FILEOPEN_ERROR);
00317 }
00318 free(location);
00319 }
00320 else
00321 {
00322 GAT_CREATE_STATUS(GAT_MEMORYFAILURE);
00323 }
00324
00325 return GAT_RETURN_STATUS();
00326 }
00327 return GAT_INVALID_HANDLE;
00328 }
00329
00330
00331
00332
00333
00334
00335
00336
00337 void
00338 filestream_adaptor_FileStreamCPI_DestroyInstance(void *adaptor_data,
00339 GATFileStreamCPI_Instance *data)
00340 {
00341 if (NULL != data)
00342 {
00343 GAT_USES_STATUS(data->context,
00344 "filestream_adaptor_FileStreamCPI_DestroyInstance");
00345
00346 if (NULL != data->instance_data)
00347 {
00348 fclose((FILE *)data->instance_data);
00349 data->instance_data = NULL;
00350 }
00351
00352 GAT_STORE_STATUS();
00353 }
00354 }
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367 GATResult
00368 filestream_adaptor_FileStreamCPI_CloneInstance(void *adaptor_data,
00369 GATFileStreamCPI_Instance const *instance_data,
00370 GATFileStreamCPI_Instance *new_instance_data)
00371 {
00372 if (NULL != instance_data && NULL != new_instance_data)
00373 {
00374 GAT_USES_STATUS(instance_data->instance_data,
00375 "filestream_adaptor_FileStreamCPI_CloneInstance");
00376
00377 if (NULL != instance_data->instance_data)
00378 {
00379 new_instance_data->instance_data = instance_data->instance_data;
00380 }
00381 else
00382 {
00383 GAT_CREATE_STATUS(GAT_INVALID_PARAMETER);
00384 }
00385
00386 return GAT_RETURN_STATUS();
00387 }
00388 return GAT_INVALID_HANDLE;
00389 }
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403 GATResult
00404 filestream_adaptor_FileStreamCPI_EqualsInstance(void *data,
00405 GATFileStreamCPI_Instance const *lhs, GATFileStreamCPI_Instance const *rhs,
00406 GATBool *isequal)
00407 {
00408 if (NULL != lhs && NULL != rhs)
00409 {
00410 GAT_USES_STATUS(lhs->context,
00411 "filestream_adaptor_FileStreamCPI_EqualsInstance");
00412
00413 if (NULL != isequal)
00414 {
00415 *isequal = GATFalse;
00416 if (lhs->instance_data == rhs->instance_data)
00417 {
00418 *isequal = GATTrue;
00419 }
00420 }
00421 else
00422 {
00423 GAT_CREATE_STATUS(GAT_INVALID_PARAMETER);
00424 }
00425
00426 return GAT_RETURN_STATUS();
00427 }
00428 return GAT_INVALID_HANDLE;
00429 }
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445 static GATResult
00446 filestream_adaptor_FileStreamCPI_Read(void *data,
00447 GATFileStreamCPI_Instance const *instance_data, void *buffer,
00448 GATuint32 size, GATuint32 *readBytes)
00449 {
00450 if (NULL != instance_data)
00451 {
00452 GAT_USES_STATUS(instance_data->context,
00453 "filestream_adaptor_FileStreamCPI_Read");
00454 if ((NULL != buffer) && (NULL != readBytes))
00455 {
00456 *readBytes = (GATuint32) fread(buffer, 1, (size_t) size,
00457 (FILE *) instance_data->instance_data);
00458
00459 if (size == *readBytes)
00460 {
00461 GAT_CREATE_STATUS(filestream_adaptor_FireReadEvent(
00462 instance_data->context, instance_data->monitorable,
00463 instance_data->source, "read"));
00464 }
00465 else
00466 {
00467 GAT_CREATE_STATUS(POSIX_TO_GAT(errno));
00468 }
00469 }
00470 else
00471 {
00472 GAT_CREATE_STATUS(GAT_INVALID_PARAMETER);
00473 }
00474 return GAT_RETURN_STATUS();
00475 }
00476 return GAT_INVALID_HANDLE;
00477 }
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490 static GATResult
00491 filestream_adaptor_FileStreamCPI_Write(void *data,
00492 GATFileStreamCPI_Instance const *instance_data, void const *buffer,
00493 GATuint32 size, GATuint32 *writtenBytes)
00494 {
00495 if (NULL != instance_data)
00496 {
00497 GAT_USES_STATUS(instance_data->context,
00498 "filestream_adaptor_FileStreamCPI_Write");
00499 if ((NULL != buffer) && (NULL != writtenBytes))
00500 {
00501 *writtenBytes = (GATuint32) fwrite (buffer, 1, (size_t) size,
00502 (FILE *) instance_data->instance_data );
00503 if (size == *writtenBytes)
00504 {
00505 GAT_CREATE_STATUS(filestream_adaptor_FireWriteEvent(
00506 instance_data->context, instance_data->monitorable,
00507 instance_data->source, "write"));
00508 }
00509 else
00510 {
00511 GAT_CREATE_STATUS(POSIX_TO_GAT(errno));
00512 }
00513 }
00514 else
00515 {
00516 GAT_CREATE_STATUS(GAT_INVALID_PARAMETER);
00517 }
00518 return GAT_RETURN_STATUS();
00519 }
00520 return GAT_INVALID_HANDLE;
00521 }
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534 static GATResult
00535 filestream_adaptor_FileStreamCPI_Seek(void *data,
00536 GATFileStreamCPI_Instance const *instance_data, GATOrigin origin,
00537 GATint32 offset, GATuint32 *new_position)
00538 {
00539 if (NULL != instance_data)
00540 {
00541 GAT_USES_STATUS(instance_data->context,
00542 "filestream_adaptor_FileStreamCPI_Seek");
00543 if (NULL != new_position)
00544 {
00545 int wherefrom;
00546
00547 if (GATOrigin_Set == origin)
00548 {
00549 wherefrom = SEEK_SET;
00550 }
00551 else if (GATOrigin_Current == origin)
00552 {
00553 wherefrom = SEEK_CUR;
00554 }
00555 else if (GATOrigin_End == origin)
00556 {
00557 wherefrom = SEEK_END;
00558 }
00559 else
00560 {
00561 GAT_CREATE_STATUS(GAT_INVALID_PARAMETER);
00562 }
00563
00564 if (GAT_SUCCEEDED(GAT_CURRENT_STATUS()))
00565 {
00566 if (0 == fseek ((FILE *) instance_data->instance_data, (long) offset,
00567 wherefrom ))
00568 {
00569 long newPosition = ftell ((FILE *) instance_data->instance_data);
00570 if (-1 != newPosition)
00571 {
00572 if (NULL != new_position)
00573 {
00574 *new_position = (GATuint32) newPosition;
00575 GAT_CREATE_STATUS(filestream_adaptor_FireSeekEvent(
00576 instance_data->context, instance_data->monitorable,
00577 instance_data->source, "seek"));
00578 }
00579 else
00580 {
00581 GAT_CREATE_STATUS(GAT_INVALID_PARAMETER);
00582 }
00583 }
00584 else
00585 {
00586 GAT_CREATE_STATUS(POSIX_TO_GAT(errno));
00587 }
00588 }
00589 else
00590 {
00591 GAT_CREATE_STATUS(POSIX_TO_GAT(errno));
00592 }
00593 }
00594 }
00595 return GAT_RETURN_STATUS();
00596 }
00597 return GAT_INVALID_HANDLE;
00598 }
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608 static GATResult
00609 filestream_adaptor_FileStreamCPI_Close(void *data,
00610 GATFileStreamCPI_Instance *instance_data)
00611 {
00612 if (NULL != instance_data)
00613 {
00614 GAT_USES_STATUS(instance_data->context,
00615 "filestream_adaptor_FileStreamCPI_Close");
00616 if (0 == fclose ((FILE *) instance_data->instance_data))
00617 {
00618 GAT_CREATE_STATUS(filestream_adaptor_FireCloseEvent(
00619 instance_data->context, instance_data->monitorable,
00620 instance_data->source, "close"));
00621 }
00622 else
00623 {
00624 GAT_CREATE_STATUS(POSIX_TO_GAT(errno));
00625 }
00626 instance_data->instance_data = NULL;
00627
00628 return GAT_RETURN_STATUS();
00629 }
00630 return GAT_INVALID_HANDLE;
00631 }
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650 static GATResult
00651 filestream_adaptor_FileStreamCPI_GetMetrics(void *data,
00652 GATFileStreamCPI_Instance const *instance_data, GATList_GATMetric *metrics)
00653 {
00654 return GATMetric_CreateListOfMetrics(metric_data, __countof(metric_data),
00655 metrics);
00656 }
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677 static GATResult
00678 filestream_adaptor_FileStreamCPI_GetMetricEvent(void *data,
00679 GATFileStreamCPI_Instance const *instance_data, GATMetric metric,
00680 GATMetricEvent *event)
00681 {
00682
00683 GATResult retval = GAT_SUCCESS;
00684 return retval;
00685 }
00686
00687
00688
00689
00690
00691
00692
00693 static GATResult
00694 filestream_adaptor_FireReadEvent(GATContext context,
00695 GATMonitorable_Impl monitorable, GATObject_const source, char const *name)
00696 {
00697 GAT_USES_STATUS(context, "filestream_adaptor_FireReadEvent");
00698 if (NULL != monitorable && NULL != source && NULL != name)
00699 {
00700 GATMetric metric = NULL;
00701
00702 GAT_CREATE_STATUS(GATMetric_CreateMetric(
00703 &metric_data[METRIC_FILESTREAM_READ], &metric));
00704 if (GAT_SUCCEEDED(GAT_CURRENT_STATUS()))
00705 {
00706 GATMetricEvent event = GATMetricEvent_Create_EventLike(source, metric,
00707 (void *)name, (GATuint32)(strlen(name) + 1));
00708
00709 if (NULL != event)
00710 {
00711 GAT_CREATE_STATUS(GATMonitorable_Impl_FireEvent(monitorable, metric,
00712 event));
00713 }
00714 else
00715 {
00716 GAT_CREATE_STATUS(GAT_MEMORYFAILURE);
00717 }
00718
00719 GATMetricEvent_Destroy(&event);
00720 GATMetric_Destroy(&metric);
00721 }
00722 }
00723 else
00724 {
00725 GAT_CREATE_STATUS(GAT_INVALID_PARAMETER);
00726 }
00727 return GAT_RETURN_STATUS();
00728 }
00729
00730
00731
00732
00733
00734
00735 static GATResult
00736 filestream_adaptor_FireWriteEvent(GATContext context,
00737 GATMonitorable_Impl monitorable, GATObject_const source, char const *name)
00738 {
00739 GAT_USES_STATUS(context, "filestream_adaptor_FireWriteEvent");
00740 if (NULL != monitorable && NULL != source && NULL != name)
00741 {
00742 GATMetric metric = NULL;
00743
00744 GAT_CREATE_STATUS(GATMetric_CreateMetric(
00745 &metric_data[METRIC_FILESTREAM_WRITE], &metric));
00746 if (GAT_SUCCEEDED(GAT_CURRENT_STATUS()))
00747 {
00748 GATMetricEvent event = GATMetricEvent_Create_EventLike(source, metric,
00749 (void *)name, (GATuint32)(strlen(name) + 1));
00750
00751 if (NULL != event)
00752 {
00753 GAT_CREATE_STATUS(GATMonitorable_Impl_FireEvent(monitorable, metric,
00754 event));
00755 }
00756 else
00757 {
00758 GAT_CREATE_STATUS(GAT_MEMORYFAILURE);
00759 }
00760
00761 GATMetricEvent_Destroy(&event);
00762 GATMetric_Destroy(&metric);
00763 }
00764 }
00765 else
00766 {
00767 GAT_CREATE_STATUS(GAT_INVALID_PARAMETER);
00768 }
00769 return GAT_RETURN_STATUS();
00770 }
00771
00772
00773
00774
00775
00776
00777 static GATResult
00778 filestream_adaptor_FireSeekEvent(GATContext context,
00779 GATMonitorable_Impl monitorable, GATObject_const source, char const *name)
00780 {
00781 GAT_USES_STATUS(context, "filestream_adaptor_FireSeekEvent");
00782 if (NULL != monitorable && NULL != source && NULL != name)
00783 {
00784 GATMetric metric = NULL;
00785
00786 GAT_CREATE_STATUS(GATMetric_CreateMetric(
00787 &metric_data[METRIC_FILESTREAM_SEEK], &metric));
00788 if (GAT_SUCCEEDED(GAT_CURRENT_STATUS()))
00789 {
00790 GATMetricEvent event = GATMetricEvent_Create_EventLike(source, metric,
00791 (void *)name, (GATuint32)(strlen(name) + 1));
00792
00793 if (NULL != event)
00794 {
00795 GAT_CREATE_STATUS(GATMonitorable_Impl_FireEvent(monitorable, metric,
00796 event));
00797 }
00798 else
00799 {
00800 GAT_CREATE_STATUS(GAT_MEMORYFAILURE);
00801 }
00802
00803 GATMetricEvent_Destroy(&event);
00804 GATMetric_Destroy(&metric);
00805 }
00806 }
00807 else
00808 {
00809 GAT_CREATE_STATUS(GAT_INVALID_PARAMETER);
00810 }
00811 return GAT_RETURN_STATUS();
00812 }
00813
00814
00815
00816
00817
00818
00819 static GATResult
00820 filestream_adaptor_FireCloseEvent(GATContext context,
00821 GATMonitorable_Impl monitorable, GATObject_const source, char const *name)
00822 {
00823 GAT_USES_STATUS(context, "filestream_adaptor_FireCloseEvent");
00824 if (NULL != monitorable && NULL != source && NULL != name)
00825 {
00826 GATMetric metric = NULL;
00827
00828 GAT_CREATE_STATUS(GATMetric_CreateMetric(
00829 &metric_data[METRIC_FILESTREAM_CLOSE], &metric));
00830 if (GAT_SUCCEEDED(GAT_CURRENT_STATUS()))
00831 {
00832 GATMetricEvent event = GATMetricEvent_Create_EventLike(source, metric,
00833 (void *)name, (GATuint32)(strlen(name) + 1));
00834
00835 if (NULL != event)
00836 {
00837 GAT_CREATE_STATUS(GATMonitorable_Impl_FireEvent(monitorable, metric,
00838 event));
00839 }
00840 else
00841 {
00842 GAT_CREATE_STATUS(GAT_MEMORYFAILURE);
00843 }
00844
00845 GATMetricEvent_Destroy(&event);
00846 GATMetric_Destroy(&metric);
00847 }
00848 }
00849 else
00850 {
00851 GAT_CREATE_STATUS(GAT_INVALID_PARAMETER);
00852 }
00853 return GAT_RETURN_STATUS();
00854 }