Example #1
0
AwaError AwaServerSession_DispatchCallbacks(AwaServerSession * session)
{
    AwaError result = AwaError_Unspecified;
    if (session != NULL)
    {
        IPCMessage * notification;

        while (Queue_Pop(session->NotificationQueue, (void **)&notification))
        {
            ServerNotification_Process(session, notification);

            IPCMessage_Free(&notification);
        }
        result = AwaError_Success;
    }
    else
    {
        result = LogErrorWithEnum(AwaError_SessionInvalid, "session is NULL");
    }
    return result;
}
AwaError AwaServerListClientsOperation_Perform(AwaServerListClientsOperation * operation, AwaTimeout timeout)
{
    AwaError result = AwaError_Unspecified;

    if (timeout >= 0)
    {
        if (operation != NULL)
        {
            // build an IPC message and inject our content (object paths) into it
            IPCMessage * request = IPCMessage_NewPlus(IPC_MESSAGE_TYPE_REQUEST, IPC_MESSAGE_SUB_TYPE_LIST_CLIENTS, ServerOperation_GetSessionID(operation->ServerOperation));
            IPCMessage * response = NULL;

            // a timeout of 0 means an infinite timeout
            result = IPC_SendAndReceive(ServerSession_GetChannel(ServerOperation_GetSession(operation->ServerOperation)), request, &response, timeout);

            if (result == AwaError_Success)
            {
                IPCResponseCode responseCode = IPCMessage_GetResponseCode(response);
                if (responseCode == IPCResponseCode_Success)
                {
                    // Free an old Clients record if it exists
                    if (operation->ServerResponse != NULL)
                    {
                        ServerResponse_Free(&operation->ServerResponse);
                    }

                    // Detach the response's content and add it to the Server Response
                    TreeNode contentNode = IPCMessage_GetContentNode(response);
                    TreeNode clientsNode = Xml_Find(contentNode, "Clients");
                    operation->ServerResponse = ServerResponse_NewFromServerOperation(operation->ServerOperation, clientsNode);

                    // if there are any cached Responses, free them
                    Map_FreeValues(operation->ClientResponseMap);

                    LogDebug("Perform ListClients Operation successful");
                    result = AwaError_Success;
                }
                else if (responseCode == IPCResponseCode_FailureBadRequest)
                {
                    result = LogErrorWithEnum(AwaError_IPCError, "Unable to perform List Clients operation");
                }
                else
                {
                    result = LogErrorWithEnum(AwaError_IPCError, "Unexpected IPC response code: %d", responseCode);
                }
            }

            IPCMessage_Free(&request);
            IPCMessage_Free(&response);
        }
        else
        {
            result = LogErrorWithEnum(AwaError_OperationInvalid, "Operation is NULL");
        }
    }
    else
    {
        result = LogErrorWithEnum(AwaError_OperationInvalid, "Invalid timeout specified");
    }
    return result;
}
Example #3
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 AwaServerExecuteOperation_Perform(AwaServerExecuteOperation * operation, AwaTimeout timeout)
{
    AwaError result = AwaError_Unspecified;

    if (timeout >= 0)
    {
        if (operation != NULL)
        {
            const AwaServerSession * session = ServerOperation_GetSession(operation->ServerOperation);
            if (session != NULL)
            {
                if (ServerSession_IsConnected(session))
                {
                    TreeNode clientsTree = ServerOperation_GetClientsTree(operation->ServerOperation);
                    if (clientsTree != NULL)
                    {
                        if (TreeNode_GetChildCount(clientsTree) > 0)
                        {
                            // build an IPC message and inject our content into it
                            IPCMessage * request = IPCMessage_NewPlus(IPC_MESSAGE_TYPE_REQUEST, IPC_MESSAGE_SUB_TYPE_EXECUTE, ServerOperation_GetSessionID(operation->ServerOperation));

                            // Add Content to message
                            IPCMessage_AddContent(request, clientsTree);

                            // Send via IPC
                            IPCMessage * response = NULL;
                            result = IPC_SendAndReceive(ServerSession_GetChannel(session), request, &response, timeout);

                            // Process the response
                            if (result == AwaError_Success)
                            {
                                IPCResponseCode responseCode = IPCMessage_GetResponseCode(response);
                                if (responseCode == IPCResponseCode_Success)
                                {
                                    // Free an old Execute response record if it exists
                                    if (operation->Response != NULL)
                                    {
                                        ServerResponse_Free(&operation->Response);
                                    }

                                    // Detach the response's content and add it to the Server Response
                                    TreeNode contentNode = IPCMessage_GetContentNode(response);
                                    TreeNode clientsNode = Xml_Find(contentNode, "Clients");
                                    operation->Response = ServerResponse_NewFromServerOperation(operation->ServerOperation, clientsNode);

                                    LogDebug("Perform Execute Operation successful");

                                    result = ServerResponse_CheckForErrors(operation->Response);
                                }
                                else if (responseCode == IPCResponseCode_FailureBadRequest)
                                {
                                    result = LogErrorWithEnum(AwaError_IPCError, "Unable to perform Execute operation: Bad Request");
                                }
                                else
                                {
                                    result = LogErrorWithEnum(AwaError_IPCError, "Unexpected IPC response code: %d", responseCode);
                                }
                            }

                            IPCMessage_Free(&request);
                            IPCMessage_Free(&response);
                        }
                        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;
}
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;
}