static const Value * ResponseCommon_GetValue(const ResponseCommon * response, const char * path) { const Value * storedValue = NULL; if (Map_Get(response->Values, path, (void **)&storedValue)) { // success } else { LogErrorWithEnum(AwaError_PathNotFound, "path %s not found", path); } return storedValue; }
static void ClientSubscriberList_InvokeSubscribeCallback(MapType * subscribers, const char * path, const AwaChangeSet * changeSet) { AwaClientSubscription * subscription = NULL; Map_Get(subscribers, path, (void **)&subscription); if (subscription != NULL) { switch (subscription->Type) { case AwaSubscribeType_Change: { AwaClientSubscribeToChangeCallback callback = (AwaClientSubscribeToChangeCallback)subscription->Callback; callback(changeSet, subscription->Context); break; } case AwaSubscribeType_Execute: { AwaExecuteArguments arguments = {NULL, 0}; AwaError result = AwaError_Unspecified; if ((result = ChangeSet_GetExecuteArguments(changeSet, path, &arguments)) == AwaError_Success) { AwaClientSubscribeToExecuteCallback callback = (AwaClientSubscribeToExecuteCallback)subscription->Callback; callback(&arguments, subscription->Context); } else { LogErrorWithEnum(result, "Failed to get arguments from execute notification"); } break; } default: LogErrorWithEnum(AwaError_Unsupported, "Unsupported subscribe type: %d", subscription->Type); break; } } else { LogDebug("No subscription for path %s\n", path); } }
static AwaError ClientSubscribeOperation_Add(AwaClientSubscribeOperation * operation, AwaClientSubscription * subscription, bool cancel) { AwaError result = AwaError_Unspecified; if (operation != NULL) { if (subscription != NULL) { AwaClientSubscription * existingSubscription = NULL; Map_Get(operation->Subscribers, subscription->Path, (void **)&existingSubscription); if (existingSubscription == NULL) { if (Map_Put(operation->Subscribers, subscription->Path, subscription)) { List_Add(subscription->Operations, operation); subscription->Cancel = cancel; result = AwaError_Success; } else { result = LogErrorWithEnum(AwaError_Internal, "Failed to add subscription to operation"); } } else { result = LogErrorWithEnum(AwaError_OperationInvalid, "A subscription already exists for path %s in the given operation.", subscription->Path); } } else { result = LogErrorWithEnum(AwaError_SubscriptionInvalid, "Subscription is NULL"); } } else { result = LogErrorWithEnum(AwaError_OperationInvalid, "Operation is NULL"); } return result; }
AwaError ResponseCommon_GetPathResult(const ResponseCommon * response, const char * path, const PathResult ** result) { AwaError error = AwaError_Unspecified; if (response != NULL) { if (path != NULL) { if (result) { if (Map_Get(response->PathResults, path, (void**)result)) { error = AwaError_Success; } else { *result = NULL; error = LogErrorWithEnum(AwaError_PathNotFound, "path %s not found", path); } } else { error = LogErrorWithEnum(AwaError_Internal, "Result is null"); } } else { error = LogErrorWithEnum(AwaError_PathInvalid, "Path is null"); } } else { error = LogErrorWithEnum(AwaError_ResponseInvalid, "Response is null"); } return error; }
// 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; }