Пример #1
0
	//Process a received interior message
	void ConnectionModule::process_message(os::smart_ptr<crypto::message> msg,uint8_t datHead)
    {
		kill_test();

		//Check for a bad message, not polling case
		if(!msg)
			return;
		if(!manager->isPolling())
			return;
		
		lock.lock();
		message_lock.acquire();

		//Process message
        bool _isBase=false;
        bool _serverOn=false;
        if(datHead&1) _serverOn = true;
        if(datHead&2) _isBase = true;

		uint8_t curState=_gateway->currentState();

		//Received a legal message
		if(!master)
		{
			os::smart_ptr<crypto::gatewaySettings> tSet;
			if(msg->data()[0]==crypto::message::PING) tSet=os::smart_ptr<crypto::gatewaySettings>(new crypto::gatewaySettings(*msg),os::shared_type);
			else tSet=_gateway->getBrotherSettings();

			if(tSet)
			{
				//Bad name screening
				if(tSet->nodeName() == "NULL" || tSet->nodeName()==manager->getMaster()->getName())
				{
					message_lock.release();
					lock.unlock();
					return;
				}
				
				master = manager->find(tSet->groupID(),tSet->nodeName());
				if(master) master->bind(this);
			}
		}
		else if(msg->data()[0]==crypto::message::PING)
		{
			os::smart_ptr<crypto::gatewaySettings> tSet=os::smart_ptr<crypto::gatewaySettings>(new crypto::gatewaySettings(*msg),os::shared_type);
			//Bad name screening
			if(tSet->nodeName() == "NULL" || tSet->nodeName()==manager->getMaster()->getName())
			{
				message_lock.release();
				lock.unlock();
				return;
			}

			//Compare with master name
			os::smart_ptr<crypto::nodeGroup> ng=manager->getMaster()->getUser()->getKeyBank()->find(tSet->groupID(),tSet->nodeName());
			if(ng!=master->getNode())
			{
				master->remove(this);
				master = manager->find(tSet->groupID(),tSet->nodeName());
				if(master) master->bind(this);
			}
		}
		
		if(msg->data()[0]!=crypto::message::PING || (curState==crypto::gateway::UNKNOWN_STATE || curState==crypto::gateway::UNKNOWN_BROTHER
				|| curState==crypto::gateway::SETTINGS_EXCHANGED || curState==crypto::gateway::CONFIRM_ERROR_STATE))
			msg=_gateway->processMessage(msg);

		auto errMsg=_gateway->popError();
		while(errMsg)
		{
			tneterr<<errMsg->errorTitle()<<std::endl;
			errMsg=_gateway->popError();
		}

		//We don't have a master
		if(!master)
		{
			if(_gateway->secure())
			{
				os::smart_ptr<crypto::nodeGroup> ngr=_gateway->brotherNode();
				if(ngr) master=manager->insert(ngr,_isBase);
				master->bind(this);
			}
		}
		message_lock.release();
		lock.unlock();

        //Normal Message processing
		if(msg && !msg->encrypted())
        {
            os::smart_ptr<MessageType> found_type = getRegistry()->search(msg->data()[0]);
            
            //Unknown type, log
            if(found_type == NULL)
            {
                tneterr<<"No registered type!!!"<<endl;
				tneterr<<"Received marker "<<(int)msg->data()[0]<<endl;
            }
			else if(!found_type->gatewayMessage()) master->receive(msg,found_type);
        }
	}
Пример #2
0
	//Receive a message
	//Triggered anytime a message is received
	void Controller::receiveMessage(os::smart_ptr<crypto::message> msg, os::smart_ptr<tnet::MessageType> type, os::smart_ptr<tnet::Connection> con)
	{
		if(!msg || !type || !con)
			return;

		if(type->getID()!=motorControlMessageType.getID() && type->getID()!=connectionListMessage.getID())
			return;

		//Pull from data
		os::smart_ptr<siblingData> tempDat(new siblingData(con),os::shared_type);
		auto fnd=connectionData.search(tempDat);
		if(!fnd) connectionData.insert(tempDat);
		else tempDat=&fnd;
		
		//Connection list case
		if(type->getID()==connectionListMessage.getID())
		{
			std::string strGroup,strName;
			strName=msg->popString();
			strGroup=msg->popString();
			while(strName!="" && strGroup!="")
			{
				auto temp=network()->find(strGroup,strName);
				if(temp)
					tempDat->eligibleConnections.insert(temp);
				strName=msg->popString();
				strGroup=msg->popString();
			}
			return;
		}
		tempDat->remoteType=msg->data()[1];

		//Joystick case
		if(tempDat->remoteType==RemoteController::JOYSTICK)
		{
			tempDat->xVal=(float)msg->data()[2]/256.0f;
			tempDat->yVal=(float)msg->data()[3]/256.0f;

			//Default settings
			tempDat->rightWheel=0;
			tempDat->leftWheel=0;

			tempDat->strMaster[0]="";
			tempDat->strMaster[1]="";

			tempDat->strConnection[0]="";
			tempDat->strConnection[1]="";
		}
		//Bot case
		else if(tempDat->remoteType==RemoteController::BOT)
		{
			tempDat->rightWheel=(float)msg->data()[2]/128.0f-1.0f;
			tempDat->leftWheel=(float)msg->data()[3]/128.0f-1.0f;

			char* strm;
			int arrSize;
			if(crypto::size::GROUP_SIZE>crypto::size::NAME_SIZE) arrSize=crypto::size::GROUP_SIZE;
			else arrSize=crypto::size::NAME_SIZE;
			strm=new char[arrSize+1];

			//Master name
			memset(strm,0,arrSize+1);
			memcpy(strm,msg->data()+4,crypto::size::GROUP_SIZE);
			tempDat->strMaster[0]=std::string(strm);
			memset(strm,0,arrSize+1);
			memcpy(strm,msg->data()+4+crypto::size::GROUP_SIZE,crypto::size::NAME_SIZE);
			tempDat->strMaster[1]=std::string(strm);

			//Connection name
			memset(strm,0,arrSize+1);
			memcpy(strm,msg->data()+4+crypto::size::GROUP_SIZE+crypto::size::NAME_SIZE,crypto::size::GROUP_SIZE);
			tempDat->strConnection[0]=std::string(strm);
			memset(strm,0,arrSize+1);
			memcpy(strm,msg->data()+4+2*crypto::size::GROUP_SIZE+crypto::size::NAME_SIZE,crypto::size::NAME_SIZE);
			tempDat->strConnection[1]=std::string(strm);

			delete [] strm;

			//Default settings
			tempDat->xVal=.5;
			tempDat->yVal=.5;
		}
		else tempDat->resetData();
	}