/**
 * @brief Adds a path of interest to a Set operation, as a request to create an optional Resource.
 *        If the path refers to an optional resource that does not exist, the operation will result in the creation of the resource.
 *        The resource will have the default value assigned, if not overridden in the same operation.
 * @param[in] operation The current Set operation to add the create request to.
 * @param[in] path The path of the resource requested for creation.
 * @return Error_Success on success, error code on failure.
 */
AwaError AwaClientSetOperation_CreateOptionalResource(AwaClientSetOperation * operation, const char * path)
{
    AwaError result = AwaError_Unspecified;

    if (operation != NULL)
    {
        TreeNode objectsTree = OperationCommon_GetObjectsTree(operation->Common);
        if (objectsTree != NULL)
        {
            if (path != NULL)
            {
                if (Path_IsValidForResource(path))
                {
                    TreeNode resultNode;
                    if (ObjectsTree_AddPath(objectsTree, path, &resultNode) == InternalError_Success && resultNode != NULL)
                    {
                        if (ClientSetOperation_AddCreate(resultNode) == InternalError_Success)
                        {
                            result = AwaError_Success;
                        }
                        else
                        {
                            result = LogErrorWithEnum(AwaError_Internal, "Failed to add value to path");
                        }
                    }
                    else
                    {
                        result = LogErrorWithEnum(AwaError_Internal, "AddPath failed");
                    }
                }
                else
                {
                    result = LogErrorWithEnum(AwaError_PathInvalid, "%s is not a resource path", path);
                }
            }
            else
            {
                result = LogErrorWithEnum(AwaError_PathInvalid, "Path is NULL");
            }
        }
        else
        {
            result = LogErrorWithEnum(AwaError_Internal, "ObjectsTree is NULL");
        }
    }
    else
    {
        result = LogErrorWithEnum(AwaError_OperationInvalid, "Operation is NULL");
    }
    return result;
}
Example #2
0
AwaError AwaClientSubscribeOperation_Perform(AwaClientSubscribeOperation * operation, AwaTimeout timeout)
{
    AwaError result = AwaError_Unspecified;

    if (timeout >= 0)
    {
        if (operation != NULL)
        {
            PerformAddPathCallbackContext addPathContext;
            addPathContext.OperationCommon = operation->Common;
            addPathContext.Result = AwaError_Success;
            Map_ForEach(operation->Subscribers, ClientSubscribe_PerformAddPathCallback,
                        (void *)&addPathContext);
            result = addPathContext.Result;

            if (result != AwaError_Success)
            {
                goto error;
            }

            const AwaClientSession * session = OperationCommon_GetSession(operation->Common, NULL);
            if (ClientSession_IsConnected(session))
            {
                TreeNode objectsTree = OperationCommon_GetObjectsTree(operation->Common);
                if (objectsTree != NULL)
                {
                    if (TreeNode_GetChildCount(objectsTree) > 0)
                    {
                        // build an IPC message and inject our content (object paths) into it
                        IPCMessage * subscribeRequest = IPCMessage_NewPlus(IPC_MESSAGE_TYPE_REQUEST, IPC_MESSAGE_SUB_TYPE_SUBSCRIBE, OperationCommon_GetSessionID(operation->Common));
                        IPCMessage_AddContent(subscribeRequest, objectsTree);
                        IPCMessage * subscribeResponse = NULL;
                        result = IPC_SendAndReceive(ClientSession_GetChannel(session), subscribeRequest, &subscribeResponse, timeout);

                        if (result == AwaError_Success)
                        {
                            IPCResponseCode responseCode = IPCMessage_GetResponseCode(subscribeResponse);
                            if (responseCode == IPCResponseCode_Success)
                            {
                                // Free an old response if it exists
                                if (operation->Response != NULL)
                                {
                                    result = SubscribeResponse_Free(&operation->Response);
                                }

                                if (result == AwaError_Success)
                                {
                                    // Detach the response content and store it in the operation's ClientGetResponse
                                    TreeNode contentNode = IPCMessage_GetContentNode(subscribeResponse);
                                    TreeNode objectsNode = Xml_Find(contentNode, "Objects");
                                    operation->Response = SubscribeResponse_New(operation, objectsNode);

                                    if (operation->Response)
                                    {
                                        PerformSuccessfulCallbackContext successContext;
                                        successContext.Session = OperationCommon_GetSession(operation->Common, SessionType_Client);
                                        successContext.Response = operation->Response;
                                        successContext.Result = AwaError_Success;
                                        Map_ForEach(operation->Subscribers, ClientSubscribe_PerformSuccessfulCallback,
                                                    (void *)&successContext);

                                        result = successContext.Result;
                                        LogDebug("Perform Subscribe Operation finished %s\n", (result == AwaError_Success? "successfully" : "with errors"));
                                    }
                                    else
                                    {
                                        result = LogErrorWithEnum(AwaError_Internal, "An internal error occurred when parsing Get Response");
                                    }
                                }
                            }
                            else
                            {
                                result = LogErrorWithEnum(AwaError_IPCError, "Unexpected IPC response code: %d", responseCode);
                            }
                        }

                        IPCMessage_Free(&subscribeRequest);
                        IPCMessage_Free(&subscribeResponse);
                    }
                    else
                    {
                        result = LogErrorWithEnum(AwaError_OperationInvalid, "No paths specified");
                    }
                }
                else
                {
                    result = LogErrorWithEnum(AwaError_Internal, "ObjectsTree is NULL");
                }
            }
            else
            {
                LogError("Session is not connected");
            }
        }
        else
        {
            result = LogErrorWithEnum(AwaError_OperationInvalid, "Operation is NULL");
        }
    }
    else
    {
        result = LogErrorWithEnum(AwaError_OperationInvalid, "Invalid timeout");
    }
error:
    return result;
}
AwaError AwaClientSetOperation_Perform(AwaClientSetOperation * operation, AwaTimeout timeout)
{
    AwaError result = AwaError_Unspecified;

    if (timeout >= 0)
    {
        if (operation != NULL)
        {
            const AwaClientSession * session = OperationCommon_GetSession(operation->Common, NULL);
            if (session != NULL)
            {
                if (ClientSession_IsConnected(session))
                {
                    TreeNode objectsTree = OperationCommon_GetObjectsTree(operation->Common);
                    if (objectsTree != NULL)
                    {
                        if (TreeNode_GetChildCount(objectsTree) > 0)
                        {
                            IPCMessage * setRequest = IPCMessage_NewPlus(IPC_MESSAGE_TYPE_REQUEST, IPC_MESSAGE_SUB_TYPE_SET, OperationCommon_GetSessionID(operation->Common));
                            IPCMessage_AddContent(setRequest, objectsTree);

                            // Serialise message
                            char * requestBuffer = IPC_SerialiseMessageToXML(setRequest);
                            Awa_MemSafeFree(requestBuffer);

                            // Send via IPC
                            IPCMessage * setResponse = NULL;
                            result = IPC_SendAndReceive(ClientSession_GetChannel(session), setRequest, &setResponse, timeout);

                            // Process the response
                            if (result == AwaError_Success)
                            {
                                // Free an old Read response record if it exists
                                if (operation->Response != NULL)
                                {
                                    ResponseCommon_Free(&operation->Response);
                                }

                                TreeNode contentNode = IPCMessage_GetContentNode(setResponse);
                                TreeNode objectsNode = Xml_Find(contentNode, "Objects");
                                operation->Response = ResponseCommon_New(operation->Common, objectsNode);

                                result = ResponseCommon_CheckForErrors(operation->Response);

                                LogDebug("Perform Set Operation successful");
                            }
                            // Free allocated memory
                            IPCMessage_Free(&setRequest);
                            IPCMessage_Free(&setResponse);
                        }
                        else
                        {
                            result = LogErrorWithEnum(AwaError_OperationInvalid, "No paths specified");
                        }
                    }
                    else
                    {
                        result = LogErrorWithEnum(AwaError_Internal, "objectsTree is NULL");
                    }
                }
                else
                {
                    result = LogErrorWithEnum(AwaError_SessionNotConnected, "session is not connected");
                }
            }
            else
            {
                result = LogErrorWithEnum(AwaError_SessionInvalid, "session is NULL");
            }
        }
        else
        {
            result = LogErrorWithEnum(AwaError_OperationInvalid, "operation is NULL");
        }
    }
    else
    {
        result = LogErrorWithEnum(AwaError_OperationInvalid, "Invalid timeout specified");
    }
    return result;
}