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::UpdateMapWithCID(std::string const& tag, UInt32 & channelId)
{
    LOG4CPLUS_TRACE_METHOD(msLogger, __PRETTY_FUNCTION__);
    LOG4CPLUS_TRACE(msLogger,
            "NegotiatorClient::UpdateMapWithCID()=> tag =" + tag + "channel id ="
                    + convertIntegerToString(channelId));

    CAAllocateDoneRequest * req = NULL;
    CAAllocateDoneResponse * res = NULL;

    BaseError err = CSError(CSError::ERROR_OTHER);

//prepare message for channel allocation
    std::stringstream messageToBeSent;
    req = new CAAllocateDoneRequest(tag.c_str(), channelId);
    req->Write(messageToBeSent);
    LOG4CPLUS_INFO(msLogger,
            "NegotiatorClient::UpdateMapWithCID()=>Send \n" + messageToBeSent.str() + "\n");

    UInt32 bufSize = 1000;
    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_ERROR(msLogger,
                "NegotiatorClient::UpdateMapWithCID()=> 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_INFO(msLogger,
                "NegotiatorClient::UpdateMapWithCID()=> Received \n==========\n"
                        + (std::string)((char*) buf) + "\n===========\n");

        pugi::xml_document* doc = new pugi::xml_document();
        ParseResponse_(doc, (char*) (buf));
        res = new CAAllocateDoneResponse(doc);

        //get the error from the response
        ErrorCode errc;
        const char * errstr;
        res->GetError(errc, errstr);

        if (errc == ERRORCODE_SUCCESS)
        {
            channelId = res->GetChannelId();
            LOG4CPLUS_INFO(msLogger, "Negotiated channel id" + convertIntegerToString(channelId));
            err = CSError::NoCSError(defErrMsg);
        } else if (errc == ERRORCODE_TIMEOUT_OCCURRED)
        {
            LOG4CPLUS_ERROR(msLogger, "Timeout occurred. Err string: " + (std::string) errstr);
            err = CSError(CSError::UPDATE_MAP_TIMEOUT, "update map timeout");
        } else if (errc == ERRORCODE_WRONG_CHANNELID)
        {
            LOG4CPLUS_ERROR(msLogger, "Wrong channel id. Err string: " + (std::string) errstr);
            err = CSError(CSError::UPDATE_MAP_WRONG_CID, "update map timeout");
        } else
        {
            LOG4CPLUS_ERROR(msLogger, "Error occurred. Err string: " + (std::string) errstr);
            err = CSError(CSError::UPDATE_MAP_ERROR, "error during map update");
        }
    }

    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;
}