// FIXME Currently The client is using the old xml spec and therefore can't be used with the new objects tree. // This is a temporary workaround to prevent error messages when the response does not contain an 'Objects' node ResponseCommon * ResponseCommon_NewClient(const OperationCommon * operation, TreeNode objectsNode) { ResponseCommon * response = NULL; if (operation != NULL) { if (objectsNode != NULL) { response = Awa_MemAlloc(sizeof(*response)); if (response != NULL) { memset(response, 0, sizeof(*response)); response->OperationCommon = operation; response->ObjectsNode = Tree_Copy(objectsNode); LogNew("ResponseCommon", response); } else { LogErrorWithEnum(AwaError_OutOfMemory); } } else { LogError("Content Response is NULL"); } } else { LogError("Operation is NULL"); } return response; }
OperationCommon * OperationCommon_NewWithExistingObjectsTree(const Session * session, SessionType sessionType, TreeNode objectsTree) { OperationCommon * operation = NULL; if (session != NULL) { operation = Awa_MemAlloc(sizeof(*operation)); if (operation != NULL) { memset(operation, 0, sizeof(*operation)); operation->Session = session; operation->SessionType = sessionType; operation->ObjectsTree = objectsTree; if (operation->ObjectsTree != NULL) { LogNew("OperationCommon", operation); } else { LogErrorWithEnum(AwaError_Internal, "Unable to initialise operation"); Awa_MemSafeFree(operation); operation = NULL; } } else { LogErrorWithEnum(AwaError_OutOfMemory); } } else { LogErrorWithEnum(AwaError_SessionInvalid, "Session is NULL"); } return operation; }
ResponseCommon * ResponseCommon_New(const OperationCommon * operation, TreeNode objectsNode) { ResponseCommon * response = NULL; if (operation != NULL) { if (objectsNode != NULL) { response = Awa_MemAlloc(sizeof(*response)); if (response != NULL) { memset(response, 0, sizeof(*response)); response->OperationCommon = operation; response->ObjectsNode = Tree_Copy(objectsNode); // build the Values data structure if (ResponseCommon_BuildValues(response) == AwaError_Success) { // not all responses have Values, so success includes no values } else { // there was an error building values LogDebug("Failed to build response values - continuing"); } if (ResponseCommon_BuildPathResults(response) == AwaError_Success) { // not all responses have path results } else { // there was an error building values LogErrorWithEnum(AwaError_ResponseInvalid, "Failed to build path results - continuing"); } response->NulledValues = Map_New(); LogNew("ResponseCommon", response); } else { LogErrorWithEnum(AwaError_OutOfMemory); } } else { LogError("Content Response is NULL"); } } else { LogError("Operation is NULL"); } return response; }
AwaClientSubscribeOperation * AwaClientSubscribeOperation_New(const AwaClientSession * session) { AwaClientSubscribeOperation * operation = NULL; if (session != NULL) { if (ClientSession_IsConnected(session) != false) { operation = Awa_MemAlloc(sizeof(*operation)); if (operation != NULL) { memset(operation, 0, sizeof(*operation)); operation->Common = OperationCommon_NewWithClientSession(session); if (operation->Common != NULL) { operation->Subscribers = Map_New(); if (operation->Subscribers != NULL) { LogNew("AwaClientSubscribeOperation", operation); } else { LogErrorWithEnum(AwaError_OutOfMemory, "Could not create subscriber list."); Awa_MemSafeFree(operation); operation = NULL; } } else { LogErrorWithEnum(AwaError_Internal, "Unable to initialise operation."); Awa_MemSafeFree(operation); operation = NULL; } } else { LogErrorWithEnum(AwaError_OutOfMemory); } } else { LogError("Session is not connected"); } } else { LogError("Session is NULL"); } return operation; }
StringIterator * StringIterator_New(void) { StringIterator * iterator = Awa_MemAlloc(sizeof (*iterator)); if (iterator != NULL) { memset(iterator, 0, sizeof(*iterator)); iterator->Iterator = Iterator_New(); LogNew("StringIterator", iterator); } else { LogErrorWithEnum(AwaError_OutOfMemory); } return iterator; }
AwaChangeSet * ChangeSet_NewWithClientID(Session * session, SessionType sessionType, TreeNode objectsTree, const char * clientID) { AwaChangeSet * changeSet = NULL; if (objectsTree != NULL) { changeSet = Awa_MemAlloc(sizeof(*changeSet)); if (changeSet != NULL) { memset(changeSet, 0, sizeof(*changeSet)); changeSet->OperationCommon = OperationCommon_New(session, sessionType); if (changeSet->OperationCommon != NULL) { changeSet->ResponseCommon = ResponseCommon_New(changeSet->OperationCommon, objectsTree); if (changeSet->ResponseCommon != NULL) { changeSet->ClientID = clientID; LogNew("AwaChangeSet New", changeSet); } else { LogErrorWithEnum(AwaError_OutOfMemory); OperationCommon_Free(&changeSet->OperationCommon); Awa_MemSafeFree(changeSet); changeSet = NULL; } } else { LogErrorWithEnum(AwaError_OutOfMemory); Awa_MemSafeFree(changeSet); changeSet = NULL; } } else { LogErrorWithEnum(AwaError_OutOfMemory); } } else { LogErrorWithEnum(AwaError_OperationInvalid, "Objects tree is NULL"); } return changeSet; }
AwaArray * AwaArray_New(void) { AwaArray * array = Awa_MemAlloc(sizeof(*array)); if (array != NULL) { memset(array, 0, sizeof(*array)); ListInit(&array->ValueList); LogNew("AwaArray", array); } else { LogErrorWithEnum(AwaError_OutOfMemory); } return array; }
AwaServerExecuteOperation * AwaServerExecuteOperation_New(const AwaServerSession * session) { // AwaServerExecuteResponse is an alias for ResponseCommon AwaServerExecuteOperation * operation = NULL; if (session != NULL) { if (ServerSession_IsConnected(session) != false) { operation = Awa_MemAlloc(sizeof(*operation)); if (operation != NULL) { memset(operation, 0, sizeof(*operation)); operation->ServerOperation = ServerOperation_New(session); if (operation->ServerOperation != NULL) { LogNew("AwaServerExecuteOperation", operation); } else { LogErrorWithEnum(AwaError_Internal, "Unable to initialise operation"); Awa_MemSafeFree(operation); operation = NULL; } } else { LogErrorWithEnum(AwaError_OutOfMemory); } } else { LogErrorWithEnum(AwaError_SessionInvalid); } } else { LogErrorWithEnum(AwaError_SessionInvalid, "Session is NULL"); } return operation; }
static AwaClientSubscription * ClientSubscription_New(const char * path, void * callback, void * context, AwaSubscribeType subscribeType) { AwaClientSubscription * subscription = NULL; if ((path != NULL) && (callback != NULL)) { subscription = Awa_MemAlloc(sizeof(*subscription)); if (subscription != NULL) { subscription->Path = strdup(path); if (subscription->Path != NULL) { subscription->Operations = List_New(); if (subscription->Operations != NULL) { subscription->Callback = (void *)callback; subscription->Context = context; subscription->Session = NULL; subscription->Type = subscribeType; } else { free((void *)subscription->Path); Awa_MemSafeFree(subscription); LogErrorWithEnum(AwaError_OutOfMemory); subscription = NULL; } } else { Awa_MemSafeFree(subscription); LogErrorWithEnum(AwaError_OutOfMemory); subscription = NULL; } } else { LogErrorWithEnum(AwaError_OutOfMemory); } } return subscription; }
AwaServerListClientsOperation * AwaServerListClientsOperation_New(const AwaServerSession * session) { AwaServerListClientsOperation * operation = NULL; if (session != NULL) { if (ServerSession_IsConnected(session) != false) { operation = Awa_MemAlloc(sizeof(*operation)); if (operation != NULL) { memset(operation, 0, sizeof(*operation)); operation->ServerResponse = NULL; operation->ServerOperation = ServerOperation_New(session); if (operation->ServerOperation != NULL) { operation->ClientResponseMap = Map_New(); LogNew("AwaServerListClientsOperation", operation); } else { LogErrorWithEnum(AwaError_Internal, "Unable to initialise operation"); Awa_MemSafeFree(operation); operation = NULL; } } else { LogErrorWithEnum(AwaError_OutOfMemory); } } else { LogErrorWithEnum(AwaError_SessionNotConnected); } } else { LogError("Session is NULL"); } return operation; }
AwaServerSession * AwaServerSession_New(void) { AwaServerSession * session = Awa_MemAlloc(sizeof(*session)); if (session != NULL) { memset(session, 0, sizeof(*session)); session->SessionCommon = SessionCommon_New(SessionType_Server); if (session->SessionCommon != NULL) { session->Observers = Map_New(); if (session->Observers) { session->NotificationQueue = Queue_New(); } else { LogErrorWithEnum(AwaError_OutOfMemory, "Could not create observer list"); SessionCommon_Free(&session->SessionCommon); Awa_MemSafeFree(session); session = NULL; } } else { LogErrorWithEnum(AwaError_OutOfMemory, "Could not create common session"); SessionCommon_Free(&session->SessionCommon); Awa_MemSafeFree(session); session = NULL; } } else { LogErrorWithEnum(AwaError_OutOfMemory, "Could not create ServerSession"); } return session; }
// TODO: is maintaining a separate ClientResponseMap unnecessary? Can the ServerResponse's own Map be used instead? const AwaServerListClientsResponse * AwaServerListClientsOperation_GetResponse(const AwaServerListClientsOperation * operation, const char * clientID) { AwaServerListClientsResponse * listClientsResponse = NULL; if (operation != NULL) { if (clientID != NULL) { if (operation->ServerResponse != NULL) { // check that client response exists: const ResponseCommon * clientResponse = ServerResponse_GetClientResponse(operation->ServerResponse, clientID); if (clientResponse != NULL) { // look up existing Response in map, return it. // if it doesn't exist, create new Response and add to map, return it. Map_Get(operation->ClientResponseMap, clientID, (void *)&listClientsResponse); if (listClientsResponse == NULL) { LogDebug("Create new AwaServerListClientsResponse"); listClientsResponse = Awa_MemAlloc(sizeof(*listClientsResponse)); if (listClientsResponse != NULL) { memset(listClientsResponse, 0, sizeof(*listClientsResponse)); listClientsResponse->Response = clientResponse; // cache the listClientsResponse if (Map_Put(operation->ClientResponseMap, clientID, (void *)listClientsResponse) == false) { // do not return the response if we can't retain it, as it will eventually leak LogErrorWithEnum(AwaError_Internal, "Map put failed"); Awa_MemSafeFree(listClientsResponse); listClientsResponse = NULL; } } else { LogErrorWithEnum(AwaError_OutOfMemory); } } else { LogDebug("Retrieved cached AwaServerListClientsResponse"); } } else { LogErrorWithEnum(AwaError_ClientNotFound, "Client ID %s not found", clientID); } } else { LogErrorWithEnum(AwaError_ResponseInvalid, "operation response is NULL"); } } else { LogErrorWithEnum(AwaError_ClientIDInvalid, "clientID is NULL"); } } else { LogErrorWithEnum(AwaError_OperationInvalid, "operation is NULL"); } return listClientsResponse; }