void NegotiatorIPCHandler::ProcessIPCClientMessage(std::string message, std::string &response)
{
    LOG4CPLUS_TRACE_METHOD(msLogger, __PRETTY_FUNCTION__);
    ChannelSupervisor::Messages::Message * msg = NULL;
    CError err = CSError(CSError::ERROR_OTHER);

    LOG4CPLUS_TRACE(msLogger,
            "NegotiatorIPCHandler::ProcessIPCClientMessage() message = \n" + message);

    pugi::xml_document* doc = new pugi::xml_document();
    ParseResponse(doc, (Int8*) message.c_str());

    if (Notification::NOTIFICATIONTTYPE_ALLOCATE == Notification::GetNotificationType(doc))
    {
        LOG4CPLUS_INFO(msLogger, "NegotiatorIPCHandler::ProcessIPCClientMessage()==>ALLOCATE=====");
        AllocateChannelRequest * request = new AllocateChannelRequest(doc);
        std::string tag = request->GetTag();
        UInt32 channel = request->GetChannelID();
        delete request;
        err = NegotiateChannel(tag, channel);
        LOG4CPLUS_INFO(msLogger,
                "NegotiatorIPCHandler::ProcessIPCClientMessage()==>IDLE====="
                        + convertIntegerToString(channel));
        msg = new AllocateChannelResponse(tag.c_str(), channel);
        if (!err.isNoError())
        {
            // TODO correctly set other types of errors
            if (err.getCode() == CSError::NEGOTIATION_CHANNEL_TIMEOUT)
            {
                ((AllocateChannelResponse*) msg)->SetError(ERRORCODE_TIMEOUT_OCCURRED, "timeout");
            } else
            {
                ((AllocateChannelResponse*) msg)->SetError(ERRORCODE_OTHER, "other error");
            }
        }
    } else if (Notification::NOTIFICATIONTTYPE_DEALLOCATE == Notification::GetNotificationType(doc))
    {
        LOG4CPLUS_INFO(msLogger,
                "NegotiatorIPCHandler::ProcessIPCClientMessage()==>DEALLOCATE=====");
        DeallocateChannelRequest * request = new DeallocateChannelRequest(doc);
        UInt32 channel = request->GetChannelId();
        delete request;
        err = ChannelDeallocated(channel);
        msg = new DeallocateChannelResponse(channel);
        if (!err.isNoError())
        {
            // TODO correctly set other types of errors
            if (err.getCode() == CSError::DEALLOCATION_CHANNEL_MAP_WRONG_CID)
                ((DeallocateChannelResponse*) msg)->SetError(ERRORCODE_WRONG_CHANNELID,
                        "wrong cid");
            else if (err.getCode() == CSError::DEALLOCATION_CHANNEL_MAP_TIMEOUT)
                ((DeallocateChannelResponse*) msg)->SetError(ERRORCODE_TIMEOUT_OCCURRED, "timeout");
            else
                ((DeallocateChannelResponse*) msg)->SetError(ERRORCODE_OTHER, "other error");
        }
    } else if (Request::REQUESTTYPE_CA_ALLOCATION_DONE == Request::GetRequestType(doc))
    {
        LOG4CPLUS_INFO(msLogger,
                "NegotiatorIPCHandler::ProcessIPCClientMessage()==>CA_ALLOCATION_DONE=====");
        CAAllocateDoneRequest * request = new CAAllocateDoneRequest(doc);
        std::string tag = request->GetTag();
        UInt32 channel = request->GetChannelId();
        delete request;
        err = UpdateMap(tag, channel);
        msg = new CAAllocateDoneResponse(tag.c_str(), channel);
        if (!err.isNoError())
        {
            // TODO correctly set other types of errors
            if (err.getCode() == CSError::UPDATE_MAP_TIMEOUT)
            {
                ((CAAllocateDoneResponse*) msg)->SetError(ERRORCODE_TIMEOUT_OCCURRED, "timeout");
            } else
            {
                ((CAAllocateDoneResponse*) msg)->SetError(ERRORCODE_OTHER, "other error");
            }
        }
    } else //error here
    {
        LOG4CPLUS_ERROR(msLogger,
                "NegotiatorIPCHandler::ProcessIPCClientMessage()==> UNKNOWN REQUEST");
        msg = new CommandRejectResponse("");
    }

    std::stringstream messageToBeSent;

    msg->Write(messageToBeSent);
    response = messageToBeSent.str();

    if (msg)
        delete msg;
    msg = NULL;
}
Example #2
0
BaseError NegotiatorClient::FreeChannel(const UInt32 channelId)
{
    LOG4CPLUS_TRACE_METHOD(msLogger, __PRETTY_FUNCTION__);
    DeallocateChannelRequest * req = NULL;
    DeallocateChannelResponse * res = NULL;

    BaseError err = CSError(CSError::ERROR_OTHER);

    //prepare message for channel deallocation
    std::stringstream messageToBeSent;
    req = new DeallocateChannelRequest(channelId);
    req->Write(messageToBeSent);
    LOG4CPLUS_TRACE(msLogger,
            "NegotiatorClient::FreeChannel()=>Send \n" + messageToBeSent.str() + "\n");

    //send data (blocking call, wait for response)
    UInt32 bufSize = 2048;
    UInt8 buf[bufSize];
    memset(buf, 0, bufSize);
    err = m_ipcClient->sendMessage((const UInt8 *) (messageToBeSent.str().c_str()),
            messageToBeSent.str().size(), buf, bufSize);

    if (!err.isNoError())
    {
        LOG4CPLUS_TRACE(msLogger,
                "NegotiatorClient::FreeChannel()=> ERROR!! No data received from Negotiator processs");
        err = CSError(CSError::IPC_NO_DATA_ERROR, "no data received from negotiator ipc server");
    } else
    {
        //receive response
        LOG4CPLUS_TRACE(msLogger,
                "NegotiatorClient::FreeChannel()=> Received \n==========\n"
                        + (std::string)((char*) buf) + "\n===========\n");
        pugi::xml_document* doc = new pugi::xml_document();
        ParseResponse_(doc, (char*) (buf));
        res = new DeallocateChannelResponse(doc);

        //get the error from the response
        ErrorCode errc;
        const char * errstr;
        res->GetError(errc, errstr);
        LOG4CPLUS_TRACE(msLogger, "err code" + convertIntegerToString((int) errc));

        if (errc == ERRORCODE_SUCCESS)
        {
            UInt32 cId = res->GetChannelId();
            LOG4CPLUS_TRACE(msLogger, "deallocated channel id" + convertIntegerToString(cId));
            if (cId != channelId)
                err = CSError(CSError::DEALLOCATION_CHANNEL_MAP_WRONG_CID, "wrong cid");
            else
                err = CSError::NoCSError(defErrMsg);
        } else if (errc == ERRORCODE_TIMEOUT_OCCURRED)
        {
            LOG4CPLUS_TRACE(msLogger, "Timeout occurred. Err string: " + (std::string) errstr);
            err = CSError(CSError::DEALLOCATION_CHANNEL_MAP_TIMEOUT, "deallocate map timeout");
        } else
        {
            LOG4CPLUS_TRACE(msLogger, "Error occurred. Err string: " + (std::string) errstr);
            err = CSError(CSError::DEALLOCATION_CHANNEL_MAP_ERROR, "error during map deallocation");
        }
    }

    if (res)
        delete res;
    res = 0;

    if (req)
        delete req;
    req = 0;

    return err;
}
Example #3
0
void NegotiatorIPCHandler::ProcessIPCClientMessage(std::string message, std::string &response)
{
    LOG4CPLUS_TRACE_METHOD(msLogger, "NegotiatorIPCHandler::ProcessIPCClientMessage()==>START=====");
    Message * msg = NULL;
    CError err = CSError(CSError::ERROR_OTHER);

    LOG4CPLUS_TRACE(msLogger, "NegotiatorIPCHandler::ProcessIPCClientMessage() message = \n" + message);

    pugi::xml_document* doc = new pugi::xml_document();
    ParseResponse(doc, (Int8*) message.c_str());

    if (m_state == IDLE) //process allocate and deallocate
    {

        LOG4CPLUS_TRACE(msLogger, "NegotiatorIPCHandler::ProcessIPCClientMessage()==>IDLE=====");
        if (Notification::NOTIFICATIONTTYPE_ALLOCATE == Notification::GetNotificationType(doc))
        {
            LOG4CPLUS_TRACE(msLogger, "NegotiatorIPCHandler::ProcessIPCClientMessage()==>ALLOCATE=====");
            AllocateChannelRequest * request = new AllocateChannelRequest(doc);
            std::string tag = request->GetTag();
            UInt32 channel = 0;
            err = NegotiateChannel(tag, channel);
            LOG4CPLUS_TRACE(msLogger, "NegotiatorIPCHandler::ProcessIPCClientMessage()==>IDLE=====" + convertIntegerToString(channel));
            msg = new AllocateChannelResponse(tag.c_str(), channel);

            if (!err.isNoError())
            {
                if (err.getCode() == CSError::UPDATE_MAP_WRONG_CID)
                    ((AllocateChannelResponse*) msg)->SetError(ERRORCODE_WRONG_CHANNELID, "wrong cid");
                else if (err.getCode() == CSError::UPDATE_MAP_TIMEOUT)
                    ((AllocateChannelResponse*) msg)->SetError(ERRORCODE_TIMEOUT_OCCURRED, "timeout");
                else
                    ((AllocateChannelResponse*) msg)->SetError(ERRORCODE_OTHER, "other error");
            }

            if (request)
            {
                delete request;
            }
            request = 0;

            m_state = NEGOTIATED;
        }
        else if (Notification::NOTIFICATIONTTYPE_DEALLOCATE == Notification::GetNotificationType(doc))
        {
            LOG4CPLUS_TRACE(msLogger, "NegotiatorIPCHandler::ProcessIPCClientMessage()==>DEALLOCATE=====");
            DeallocateChannelRequest * request = new DeallocateChannelRequest(doc);
            std::string tag = request->GetTag();
            UInt32 channel = request->GetChannelId();
            err = ChannelDeallocated(tag, channel);
            msg = new DeallocateChannelResponse(tag.c_str(), channel);

            if (!err.isNoError())
            {
                if (err.getCode() == CSError::DEALLOCATION_CHANNEL_MAP_WRONG_CID)
                    ((DeallocateChannelResponse*) msg)->SetError(ERRORCODE_WRONG_CHANNELID, "wrong cid");
                else if (err.getCode() == CSError::DEALLOCATION_CHANNEL_MAP_TIMEOUT)
                    ((DeallocateChannelResponse*) msg)->SetError(ERRORCODE_TIMEOUT_OCCURRED, "timeout");
                else
                    ((DeallocateChannelResponse*) msg)->SetError(ERRORCODE_OTHER, "other error");
            }

            if (request)
                delete request;
            request = 0;

            m_state = IDLE;
        }
        else
        {
            msg = new CommandRejectResponse("");
        }
    }
    else if (m_state == NEGOTIATED) //process update map only
    {
        LOG4CPLUS_TRACE(msLogger, "NegotiatorIPCHandler::ProcessIPCClientMessage()==>CA_ALLOCATION_DONE=====");
        if (Request::REQUESTTYPE_CA_ALLOCATION_DONE == Request::GetRequestType(doc))
        {
            CAAllocateDoneRequest * request = new CAAllocateDoneRequest(doc);
            std::string tag = request->GetTag();
            UInt32 channel  = request->GetChannelId();
            err = UpdateMap(tag, channel);

            msg = new CAAllocateDoneResponse(tag.c_str(), channel);

            if (!err.isNoError())
            {
                if (err.getCode() == CSError::UPDATE_MAP_WRONG_CID)
                    ((CAAllocateDoneResponse*) msg)->SetError(ERRORCODE_WRONG_CHANNELID, "wrong cid");
                else if (err.getCode() == CSError::UPDATE_MAP_TIMEOUT)
                    ((CAAllocateDoneResponse*) msg)->SetError(ERRORCODE_TIMEOUT_OCCURRED, "timeout");
                else
                    ((CAAllocateDoneResponse*) msg)->SetError(ERRORCODE_OTHER, "other error");
            }

            if (request)
            {
                delete request;
            }
            request = 0;

            m_state = IDLE;
        }
        else
        {
            LOG4CPLUS_TRACE(msLogger, "NegotiatorIPCHandler::ProcessIPCClientMessage()==>REJECT=====");
            msg = new CommandRejectResponse("");
        }
    }
    else //error here
    {
        LOG4CPLUS_TRACE(msLogger, "NegotiatorIPCHandler::ProcessIPCClientMessage()==>ERROR===== State = "+  convertIntegerToString((int)m_state));
        msg = new CommandRejectResponse("");
    }

    std::stringstream messageToBeSent;

    msg->Write(messageToBeSent);
    response = messageToBeSent.str();

    if (msg)
        delete msg;
    msg = NULL;
}