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/src/GATObject.c,v 1.34 2004/04/18 18:35:10 hartmutkaiser Exp $";
00021
00022
00023 #include "xds_p.h"
00024
00025
00026 #include "GAT.h"
00027 #include "GATInternal.h"
00028 #include "GATInterfaceMap.h"
00029
00030
00031
00032
00033
00034
00035 GATOBJECT_DEFINE_VTABLE(GATObject);
00036 GATSERIALISABLE_DEFINE_VTABLE(GATObject);
00037 GATSTREAMABLE_DEFINE_VTABLE(GATObject);
00038 GATMONITORABLE_DEFINE_VTABLE(GATObject);
00039 GATRESOURCE_DEFINE_VTABLE(GATObject);
00040
00041
00042
00043 struct GATObject_S {
00044 GATObject_vtable *__vtable;
00045 };
00046
00047 struct GATSerialise_S {
00048 GATObject_ISerialisable_vtable *__vtable;
00049 };
00050
00051 struct GATStreamable_S {
00052 GATObject_IStreamable_vtable *__vtable;
00053 };
00054
00055 struct GATMonitorable_S {
00056 GATObject_IMonitorable_vtable *__vtable;
00057 };
00058
00059 struct GATResource_S {
00060 GATObject_IResource_vtable *__vtable;
00061 };
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 GATType GATObject_GetType(GATObject_const object)
00080 {
00081 GATType retval = GATType_NoType;
00082 if (NULL != object)
00083 {
00084 retval = object->__vtable->get_type(object);
00085 }
00086 return retval;
00087 }
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 void GATObject_Destroy(GATObject *object)
00098 {
00099 if (NULL != object && NULL != *object)
00100 (*object)->__vtable->destroy(object);
00101 }
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 GATResult GATObject_Equals(GATObject_const lhs, GATObject_const rhs,
00116 GATBool *isequal)
00117 {
00118 GATResult retval = GAT_INVALID_HANDLE;
00119 if (NULL != lhs && NULL != rhs)
00120 {
00121 if (lhs->__vtable->get_type(lhs) != rhs->__vtable->get_type(rhs))
00122 {
00123 if (NULL == isequal)
00124 {
00125 retval = GAT_INVALID_PARAMETER;
00126 }
00127 else
00128 {
00129 *isequal = GATFalse;
00130 }
00131 }
00132 else
00133 {
00134 retval = lhs->__vtable->equals(lhs, rhs, isequal);
00135 }
00136 }
00137 return retval;
00138 }
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 GATResult GATObject_Clone(GATObject_const object, GATObject *new_object)
00153 {
00154 GATResult retval = GAT_INVALID_HANDLE;
00155 if (NULL != object)
00156 {
00157 retval = object->__vtable->clone(object, new_object);
00158 }
00159 return retval;
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174 GATResult GATObject_GetInterface(GATObject_const object, GATInterface iftype,
00175 void const **ifp)
00176 {
00177 GATResult retval = GAT_INVALID_HANDLE;
00178 if (NULL != object)
00179 {
00180 if (NULL != object->__vtable->get_interface)
00181 {
00182 retval = object->__vtable->get_interface(object, iftype, ifp);
00183 }
00184 else
00185 {
00186 retval = GAT_NOTIMPL;
00187 }
00188 }
00189 return retval;
00190 }
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 GATResult
00206 GATObject_GetCPIInstanceData(GATObject object, void **data)
00207 {
00208 GATResult retval = GAT_INVALID_HANDLE;
00209 if (NULL != object)
00210 {
00211 if (NULL != object->__vtable->get_instancedata)
00212 {
00213 retval = object->__vtable->get_instancedata(object, data);
00214 }
00215 else
00216 {
00217 retval = GAT_NOTIMPL;
00218 }
00219 }
00220 return retval;
00221 }
00222
00223
00224 GATObject GATObject_ToGATObject(GATObject object)
00225 {
00226 return object;
00227 }
00228
00229 GATObject_const GATObject_ToGATObject_const(GATObject_const object)
00230 {
00231 return object;
00232 }
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249 GATResult
00250 GATSerialisable_Serialise(GATObject object, GATObject stream,
00251 GATBool clear_dirty)
00252 {
00253 GATSerialise serialise = NULL;
00254 GATResult retval = GATObject_GetInterface(object, GATInterface_ISerialisable,
00255 (void const **) &serialise);
00256
00257 if (GAT_SUCCESS == retval)
00258 {
00259 if (NULL != serialise->__vtable->serialise)
00260 {
00261
00262 GATuint32 type = GATObject_GetType(object);
00263 retval = GATuint32_Serialise(type, stream);
00264 if (GAT_SUCCESS == retval)
00265 {
00266
00267 retval = serialise->__vtable->serialise(object, stream, clear_dirty);
00268 }
00269 }
00270 else
00271 {
00272 retval = GAT_NOTIMPL;
00273 }
00274 }
00275 return retval;
00276 }
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292 GATObject
00293 GATSerialisable_DeSerialise(GATContext context, GATObject stream,
00294 GATResult *result)
00295 {
00296
00297 GATObject new_object = NULL;
00298 GATuint32 type = GATType_NoType;
00299 GATResult retval = GATuint32_DeSerialise(stream, &type);
00300
00301 if (GAT_SUCCESS == retval)
00302 {
00303
00304 GATObject_ISerialisable_vtable *vtable = NULL;
00305
00306 retval = GATObject_Get_GATSerialisable((GATType)type, &vtable);
00307 if (GAT_SUCCESS == retval)
00308 {
00309 new_object = vtable->deserialise(context, stream, &retval);
00310 }
00311 }
00312
00313 if (GAT_SUCCESS != retval)
00314 {
00315 GATObject_Destroy(&new_object);
00316 }
00317
00318 if (NULL != result)
00319 {
00320 *result = retval;
00321 }
00322 return new_object;
00323 }
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336 GATResult
00337 GATSerialisable_GetIsDirty(GATObject_const object, GATBool *isdirty)
00338 {
00339 GATSerialise serialise = NULL;
00340 GATResult retval = GATObject_GetInterface(object, GATInterface_ISerialisable,
00341 (void const **) &serialise);
00342
00343 if (GAT_SUCCESS == retval)
00344 {
00345 if (NULL != serialise->__vtable->isdirty)
00346 {
00347
00348 retval = serialise->__vtable->isdirty(object, isdirty);
00349 }
00350 else
00351 {
00352 retval = GAT_NOTIMPL;
00353 }
00354 }
00355 return retval;
00356 }
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376 GATResult
00377 GATStreamable_Read(GATObject object, void *buffer, GATuint32 size,
00378 GATuint32 *read_bytes)
00379 {
00380 GATStreamable stream = NULL;
00381 GATResult retval = GATObject_GetInterface(object, GATInterface_IStreamable,
00382 (void const **) &stream);
00383 if (GAT_SUCCESS == retval)
00384 {
00385 if (NULL != stream->__vtable->sread)
00386 {
00387
00388 retval = stream->__vtable->sread(object, buffer, size, read_bytes);
00389 }
00390 else
00391 {
00392 retval = GAT_NOTIMPL;
00393 }
00394 }
00395 return retval;
00396 }
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414 GATResult
00415 GATStreamable_Write(GATObject object, void const *buffer, GATuint32 size,
00416 GATuint32 *written_bytes)
00417 {
00418 GATStreamable stream = NULL;
00419 GATResult retval = GATObject_GetInterface(object, GATInterface_IStreamable,
00420 (void const **) &stream);
00421 if (GAT_SUCCESS == retval)
00422 {
00423 if (NULL != stream->__vtable->swrite)
00424 {
00425
00426 retval = stream->__vtable->swrite(object, buffer, size, written_bytes);
00427 }
00428 else
00429 {
00430 retval = GAT_NOTIMPL;
00431 }
00432 }
00433 return retval;
00434 }
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453 GATResult
00454 GATStreamable_Seek(GATObject object, GATOrigin origin, GATint32 offset,
00455 GATuint32 *new_position)
00456 {
00457 GATStreamable stream = NULL;
00458 GATResult retval = GATObject_GetInterface(object, GATInterface_IStreamable,
00459 (void const **) &stream);
00460 if (GAT_SUCCESS == retval)
00461 {
00462 if (NULL != stream->__vtable->seek)
00463 {
00464
00465 retval = stream->__vtable->seek(object, origin, offset, new_position);
00466 }
00467 else
00468 {
00469 retval = GAT_NOTIMPL;
00470 }
00471 }
00472 return retval;
00473 }
00474
00475
00476
00477 GATResult
00478 GATMonitorable_AddMetricListener(GATObject object,
00479 GATMetricListener listener, void *listener_data, GATMetric metric,
00480 GATuint32 *cookie)
00481 {
00482 GATMonitorable monitorable = NULL;
00483 GATResult retval = GATObject_GetInterface(object, GATInterface_IMonitorable,
00484 (void const **) &monitorable);
00485 if (GAT_SUCCESS == retval)
00486 {
00487 if (NULL != monitorable->__vtable->addlistener)
00488 {
00489
00490 retval = monitorable->__vtable->addlistener(object, listener,
00491 listener_data, metric, cookie);
00492 }
00493 else
00494 {
00495 retval = GAT_NOTIMPL;
00496 }
00497 }
00498 return retval;
00499 }
00500
00501 GATResult
00502 GATMonitorable_RegisterPolling(GATObject object,
00503 GATMetric metric, GATMetricEvent *event, GATuint32 *cookie)
00504 {
00505 GATMonitorable monitorable = NULL;
00506 GATResult retval = GATObject_GetInterface(object, GATInterface_IMonitorable,
00507 (void const **) &monitorable);
00508 if (GAT_SUCCESS == retval)
00509 {
00510 if (NULL != monitorable->__vtable->registerpolling)
00511 {
00512
00513 retval = monitorable->__vtable->registerpolling(object, metric, event,
00514 cookie);
00515 }
00516 else
00517 {
00518 retval = GAT_NOTIMPL;
00519 }
00520 }
00521 return retval;
00522 }
00523
00524 GATResult
00525 GATMonitorable_RemoveRegisteredMetric(GATObject object,
00526 GATMetric metric, GATuint32 cookie)
00527 {
00528 GATMonitorable monitorable = NULL;
00529 GATResult retval = GATObject_GetInterface(object, GATInterface_IMonitorable,
00530 (void const **) &monitorable);
00531 if (GAT_SUCCESS == retval)
00532 {
00533 if (NULL != monitorable->__vtable->removemetric)
00534 {
00535
00536 retval = monitorable->__vtable->removemetric(object, metric, cookie);
00537 }
00538 else
00539 {
00540 retval = GAT_NOTIMPL;
00541 }
00542 }
00543 return retval;
00544 }
00545
00546 GATResult
00547 GATMonitorable_GetMetrics(GATObject_const object, GATList_GATMetric *metrics)
00548 {
00549 GATMonitorable monitorable = NULL;
00550 GATResult retval = GATObject_GetInterface(object, GATInterface_IMonitorable,
00551 (void const **) &monitorable);
00552 if (GAT_SUCCESS == retval)
00553 {
00554 if (NULL != monitorable->__vtable->getmetrics)
00555 {
00556
00557 retval = monitorable->__vtable->getmetrics(object, metrics);
00558 }
00559 else
00560 {
00561 retval = GAT_NOTIMPL;
00562 }
00563 }
00564 return retval;
00565 }
00566
00567
00568
00569 GATResult
00570 GATResource_GetResourceDescription(GATObject_const object,
00571 GATResourceDescription_const *description)
00572 {
00573 GATResource resource = NULL;
00574 GATResult retval = GATObject_GetInterface(object, GATInterface_IResource,
00575 (void const **) &resource);
00576 if (GAT_SUCCESS == retval)
00577 {
00578 if (NULL != resource->__vtable->getresourcedescription)
00579 {
00580
00581 retval = resource->__vtable->getresourcedescription(object, description);
00582 }
00583 else
00584 {
00585 retval = GAT_NOTIMPL;
00586 }
00587 }
00588 return retval;
00589 }
00590
00591 GATResult
00592 GATResource_GetReservation(GATObject_const object,
00593 GATReservation_const *reservation)
00594 {
00595 GATResource resource = NULL;
00596 GATResult retval = GATObject_GetInterface(object, GATInterface_IResource,
00597 (void const **) &resource);
00598 if (GAT_SUCCESS == retval)
00599 {
00600 if (NULL != resource->__vtable->getreservation)
00601 {
00602
00603 retval = resource->__vtable->getreservation(object, reservation);
00604 }
00605 else
00606 {
00607 retval = GAT_NOTIMPL;
00608 }
00609 }
00610 return retval;
00611 }
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625 GATResult
00626 GATuint32_Serialise(GATuint32 data, GATObject stream)
00627 {
00628 GATResult retval = GAT_FAIL;
00629 xds_t *xds = NULL;
00630 void *xds_buffer = NULL;
00631 GATuint32 xds_buffer_size = 0;
00632
00633 if (XDS_OK == (retval = xds_init(&xds, XDS_ENCODE)) &&
00634 XDS_OK == (retval = xds_register(xds, "uint32", &xml_encode_uint32, NULL)) &&
00635 XDS_OK == (retval = xds_encode(xds, "uint32", data)) &&
00636 XDS_OK == (retval = xds_getbuffer(xds, XDS_GIFT, &xds_buffer, (size_t *) &xds_buffer_size)))
00637 {
00638 assert(NULL != xds_buffer);
00639 retval = GATStreamable_Write(stream, xds_buffer, xds_buffer_size, 0);
00640 }
00641 else
00642 {
00643 retval = XDS_TO_GAT(retval);
00644 }
00645
00646 xds_destroy(xds);
00647 free(xds_buffer);
00648 return retval;
00649 }
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661 GATResult
00662 GATuint32_DeSerialise(GATObject stream, GATuint32 *data)
00663 {
00664 char buffer[64];
00665 GATuint32 xds_read_bytes = 0;
00666 GATResult retval = GATStreamable_Read(stream, buffer, sizeof(buffer),
00667 &xds_read_bytes);
00668
00669 if (GAT_SUCCESS == retval)
00670 {
00671 xds_t *xds = NULL;
00672
00673 if (XDS_OK == (retval = xds_init (&xds, XDS_DECODE)) &&
00674 XDS_OK == (retval = xds_register ( xds, "uint32", &xml_decode_uint32, NULL)) &&
00675 XDS_OK == (retval = xds_setbuffer ( xds, XDS_LOAN, buffer, sizeof(buffer))) &&
00676 XDS_OK == (retval = xds_decode ( xds, "uint32", data)))
00677 {
00678 retval = GATStreamable_Seek (stream, GATOrigin_Current,
00679 xds->buffer_len - xds_read_bytes, 0);
00680 }
00681 else
00682 {
00683 retval = XDS_TO_GAT(retval);
00684 }
00685 xds_destroy(xds);
00686 }
00687 return retval;
00688 }
00689
00690