CError NegotiatorIPCHandler::NegotiateChannel(std::string tag, UInt32 & cid)
{
    LOG4CPLUS_TRACE_METHOD(msLogger, __PRETTY_FUNCTION__);
    LOG4CPLUS_TRACE(msLogger, "NegotiatorIPCHandler::NegotiateChannel()==>START");

    CError err = CSError(CSError::ERROR_OTHER);

    //get channel id from the map
    UInt32 channelId = m_map->getNextFreeChannelId(cid);
    LOG4CPLUS_INFO(msLogger,
            "NegotiatorIPCHandler::NegotiateChannel()==>channel"
                    + convertIntegerToString(channelId));

    Notification * noti = NULL;

    noti = new AllocateNotification(tag.c_str(), channelId);
    std::stringstream messageToBeSent;
    noti->Write(messageToBeSent);
    delete noti;

    LOG4CPLUS_INFO(msLogger,
            "NegotiatorIPCHandler::NegotiateChannel()=>Message: \n==========\n"
                    + messageToBeSent.str() + "\n===========\n");

    m_negotiatorTube->sendDataTube(messageToBeSent.str().c_str());

    messageToBeSent.str(std::string());

    LOG4CPLUS_INFO(msLogger, "NegotiatorIPCHandler::NegotiateChannel()==>WAIT FOR NEGOTIATED");
    waitStateForTag(NegotiaterStates::NEGOTIATED, tag, OTHER_SIDE_RESP_TIMEOUT_MS);
    //here both messages received
    LOG4CPLUS_INFO(msLogger,
            "NegotiatorIPCHandler::NegotiateChannel()==>NEGOTIATED "
                    + convertIntegerToString(channelId));

    //get remote notification
    AllocateNotification * remoteNotification =
            (AllocateNotification *) m_negotiatorStates->GetNotification(tag.c_str());

    UInt32 remoteCid = 0;

    ErrorCode errc;
    const char * errstr;
    if (remoteNotification)
    {
        remoteNotification->GetError(errc, errstr);

        if (errc == ERRORCODE_SUCCESS)
        {
            remoteCid = remoteNotification->GetChannelId();
            if (remoteCid >= channelId)   //use remote
            {
                if (!m_map->isChannelok(remoteCid))
                {
                    return err;
                } else
                {
                    channelId = remoteCid;
                }
            }
        } else
        {
            LOG4CPLUS_ERROR(msLogger,
                    "NegotiatorIPCHandler::NegotiateChannel()==>remote notification error"
                            + (std::string)(errstr));
            return err;
        }
    } else
    {
        LOG4CPLUS_ERROR(msLogger,
                "(AllocateNotification *) m_negotiatorStates->GetNotification \
                returned NULL! other side response timed out");
        return CSError(CSError::NEGOTIATION_CHANNEL_TIMEOUT);
    }

    //create map Notification message
    noti = new MapCheckNotification(tag.c_str(), channelId);
    //send the message
    noti->Write(messageToBeSent);
    delete noti;
    LOG4CPLUS_INFO(msLogger,
            "NegotiatorIPCHandler::NegotiateChannel()=>Message: \n==========\n"
                    + messageToBeSent.str() + "\n===========\n");

    m_negotiatorTube->sendDataTube(messageToBeSent.str().c_str());

    LOG4CPLUS_INFO(msLogger, "NegotiatorIPCHandler::NegotiateChannel()==>WAIT FOR CHECKEDINMAP");
    waitStateForTag(NegotiaterStates::CHECKEDINMAP, tag, OTHER_SIDE_RESP_TIMEOUT_MS);
    LOG4CPLUS_INFO(msLogger,
            "NegotiatorIPCHandler::NegotiateChannel()==>CHECKEDINMAP "
                    + convertIntegerToString(channelId));

    //get remote notification
    MapCheckNotification * mapRemoteNotification =
            (MapCheckNotification *) m_negotiatorStates->GetNotification(tag.c_str());

    if (mapRemoteNotification)
    {
        mapRemoteNotification->GetError(errc, errstr);

        if (errc == ERRORCODE_SUCCESS)
        {
            cid = channelId;
            err = CError(CError::NO_ERROR, "ChannelSupervisorProcess");
        } else
        {
            LOG4CPLUS_ERROR(msLogger,
                    "NegotiatorIPCHandler::NegotiateChannel()==>remote Error "
                            + convertIntegerToString((int) errc) + (std::string)(errstr));
            err = CSError(CSError::ERROR_OTHER);
        }
    } else
    {
        LOG4CPLUS_ERROR(msLogger,
                "(MapCheckNotification *) m_negotiatorStates->GetNotification returned \
                NULL! other side response timed out");
        err = CSError(CSError::NEGOTIATION_CHANNEL_TIMEOUT);
    }
    return err;
}
Exemple #2
0
TEST_RESULT TestAllocateNotificationMessage()
{
	std::cout << "create noti with no cid" << std::endl;
	{
		std::stringstream messageToBeSent;
		Message * noti  = new AllocateNotification("gutentag");

		noti->Write(messageToBeSent);
		std::cout << messageToBeSent.str() << std::endl;
	}

	std::cout << "create noti with cid" << std::endl;
	{
		std::stringstream messageToBeSent;
		Message * noti = new AllocateNotification("gutentag", 15);

		noti->Write(messageToBeSent);
		std::cout << messageToBeSent.str() << std::endl;
	}

	std::cout << "create noti with error" << std::endl;
	{
		std::stringstream messageToBeSent;
		Notification * noti = new AllocateNotification("gutentag", 15);
		noti->SetError(ERRORCODE_WRONG_CHANNELID, "crab happened");
		noti->Write(messageToBeSent);
		std::cout << messageToBeSent.str() << std::endl;
	}

	std::cout << "create Allocate noti with error and cid" << std::endl;
	{
		std::stringstream messageToBeSent;
		AllocateNotification * noti = new AllocateNotification("gutentag", 15);
		noti->SetError(ERRORCODE_WRONG_CHANNELID, "crab happened 2");
		noti->Write(messageToBeSent);
		std::cout << messageToBeSent.str() << std::endl;

		pugi::xml_document* doc = new pugi::xml_document();
		ParseResponse(doc, ( char*)(messageToBeSent.str().c_str()));
		AllocateNotification * res = new AllocateNotification(doc);

		std::cout << "channel id" << res->GetChannelId() << std::endl;
		ErrorCode errc;
		const char * errstr;
		res->GetError(errc, errstr);
		std::string messageName = res->GetMessageName();

		std::cout << "message name" << messageName << std::endl;
		std::cout << "err code" << errc << std::endl;
		std::cout << "err str" << errstr << std::endl;


	}

	std::cout << "test static functionality" << std::endl;
		{
			std::stringstream messageToBeSent;
			AllocateNotification * noti = new AllocateNotification("gutentag", 25);
			noti->SetError(ERRORCODE_WRONG_CHANNELID, "crab happened 3");
			noti->Write(messageToBeSent);
			std::cout << messageToBeSent.str() << std::endl;

			pugi::xml_document* doc = new pugi::xml_document();
			ParseResponse(doc, ( char*)(messageToBeSent.str().c_str()));

			if (Notification::NOTIFICATIONTTYPE_ALLOCATE == Notification::GetNotificationType(doc))
				std::cout << "type obtained properly" << std::endl;

			std::cout << "tag" << Notification::GetNotificationTag(doc) << std::endl;

		}


	return PASSED;

}
CError NegotiatorIPCHandler::NegotiateChannel(std::string tag, UInt32 & cid)
{
    LOG4CPLUS_TRACE(msLogger, "NegotiatorIPCHandler::NegotiateChannel()==>START");

    CError err = CSError(CSError::ERROR_OTHER);

    //get channel id from the map
    UInt32 channelId = m_map->getNextFreeChannelId();
    LOG4CPLUS_TRACE(msLogger, "NegotiatorIPCHandler::NegotiateChannel()==>channel"+  convertIntegerToString(channelId));

    Notification * noti = NULL;

    noti = new AllocateNotification(tag.c_str(), channelId);
    std::stringstream messageToBeSent;

    noti->Write(messageToBeSent);
    LOG4CPLUS_TRACE(msLogger, "NegotiatorIPCHandler::NegotiateChannel()=>Message: \n==========\n" + messageToBeSent.str()+"\n===========\n");

    m_negotiatorTube->sendDataTube(messageToBeSent.str().c_str());

    messageToBeSent.str(std::string());

    LOG4CPLUS_TRACE(msLogger, "NegotiatorIPCHandler::NegotiateChannel()==>WAIT FOR NEGOTIATED");
    m_negotiatorStates->mStateCond.lock();
    if(m_negotiatorStates->GetTagState(tag.c_str()) != NegotiaterStates::NEGOTIATED)
    {
        m_negotiatorStates->mStateCond.wait();
    }
    m_negotiatorStates->mStateCond.unlock();

    //here both messages received
    LOG4CPLUS_TRACE(msLogger, "NegotiatorIPCHandler::NegotiateChannel()==>NEGOTIATED"+  convertIntegerToString(channelId));

    //get remote notification
    AllocateNotification * remoteNotification =
        (AllocateNotification *) m_negotiatorStates->GetNotification(tag.c_str());

    UInt32 remoteCid = 0;

    ErrorCode errc;
    const char * errstr;

    remoteNotification->GetError(errc, errstr);
    LOG4CPLUS_TRACE(msLogger, "NegotiatorIPCHandler::NegotiateChannel()==>remote notification error"+(std::string)(errstr));

    if (errc == ERRORCODE_SUCCESS)
    {
        remoteCid = remoteNotification->GetChannelId();
        if (remoteCid >= channelId)   //use remote
        {
            if (!m_map->isChannelok(remoteCid))
            {
                if (noti)
                    delete noti;
                noti = 0;
                return err;
            }

            else
                channelId = remoteCid;
        }
    }
    else
    {
        if (noti)
            delete noti;
        noti = 0;

        return err;
    }

    if (noti)
        delete noti;
    noti = 0;
    LOG4CPLUS_TRACE(msLogger, "NegotiatorIPCHandler::NegotiateChannel()=>Map Check...."+(std::string)(errstr));

    //create map Notification message
    noti = new MapCheckNotification(tag.c_str(), channelId);

    //send the message
    noti->Write(messageToBeSent);
    LOG4CPLUS_TRACE(msLogger, "NegotiatorIPCHandler::NegotiateChannel()=>Message: \n==========\n"
                    + messageToBeSent.str()+"\n===========\n");

    m_negotiatorTube->sendDataTube(messageToBeSent.str().c_str());

    LOG4CPLUS_TRACE(msLogger, "NegotiatorIPCHandler::NegotiateChannel()==>WAIT FOR CHECKEDINMAP");

    m_negotiatorStates->mStateCond.lock();
    if(m_negotiatorStates->GetTagState(tag.c_str()) != NegotiaterStates::CHECKEDINMAP)
    {
        m_negotiatorStates->mStateCond.wait();
    }
    m_negotiatorStates->mStateCond.unlock();

    LOG4CPLUS_TRACE(msLogger, "NegotiatorIPCHandler::NegotiateChannel()==>CHECKEDINMAP"
                    + convertIntegerToString(channelId));

    //get remote notification
    MapCheckNotification * mapRemoteNotification =
        (MapCheckNotification *) m_negotiatorStates->GetNotification(tag.c_str());

    mapRemoteNotification->GetError(errc, errstr);
    LOG4CPLUS_TRACE(msLogger, "NegotiatorIPCHandler::NegotiateChannel()==>remote Error"
                    + convertIntegerToString((int)errc)+(std::string)(errstr));

    if (errc == ERRORCODE_SUCCESS)
    {
        cid = channelId;
        err = CError(CError::NO_ERROR, "ChannelSupervisorProcess");
    }
    else
    {
        err = CSError(CSError::ERROR_OTHER);
    }
    if (noti)
        delete noti;
    return err;
}