Example #1
0
	void DescriptionsManager::processMessage(const Message* message)
	{
		// if we have a disconnection message
		{
			const Disconnected *disconnected = dynamic_cast<const Disconnected *>(message);
			if (disconnected)
			{
				NodesDescriptionsMap::iterator it = nodesDescriptions.find(disconnected->source);
				if (it != nodesDescriptions.end())
					nodesDescriptions.erase(it);
			}
		}
		
		// if we have an initial description
		{
			const Description *description = dynamic_cast<const Description *>(message);
			if (description)
			{
				NodesDescriptionsMap::iterator it = nodesDescriptions.find(description->source);
				
				// We can receive a description twice, for instance if there is another IDE connected
				if (it != nodesDescriptions.end())
					return;
				
				// Call a user function when a node protocol version mismatches
				if (description->protocolVersion != ASEBA_PROTOCOL_VERSION)
				{
					nodeProtocolVersionMismatch(description->name, description->protocolVersion);
					return;
				}
				
				// create node and copy description into it
				nodesDescriptions[description->source] = NodeDescription(*description);
				checkIfNodeDescriptionComplete(description->source, nodesDescriptions[description->source]);
			}
		}
		
		// if we have a named variabledescription
		{
			const NamedVariableDescription *description = dynamic_cast<const NamedVariableDescription *>(message);
			if (description)
			{
				NodesDescriptionsMap::iterator it = nodesDescriptions.find(description->source);
				
				// we must have received a description first
				if (it == nodesDescriptions.end())
					return;
				
				// copy description into array if array is empty
				if (it->second.namedVariablesReceptionCounter < it->second.namedVariables.size())
				{
					it->second.namedVariables[it->second.namedVariablesReceptionCounter++] = *description;
					checkIfNodeDescriptionComplete(it->first, it->second);
				}
			}
		}
		
		// if we have a local event description
		{
			const LocalEventDescription *description = dynamic_cast<const LocalEventDescription *>(message);
			if (description)
			{
				NodesDescriptionsMap::iterator it = nodesDescriptions.find(description->source);
				
				// we must have received a description first
				if (it == nodesDescriptions.end())
					return;
				
				// copy description into array if array is empty
				if (it->second.localEventsReceptionCounter < it->second.localEvents.size())
				{
					it->second.localEvents[it->second.localEventsReceptionCounter++] = *description;
					checkIfNodeDescriptionComplete(it->first, it->second);
				}
			}
		}
		
		// if we have a native function description
		{
			const NativeFunctionDescription *description = dynamic_cast<const NativeFunctionDescription *>(message);
			if (description)
			{
				NodesDescriptionsMap::iterator it = nodesDescriptions.find(description->source);
				
				// we must have received a description first
				if (it == nodesDescriptions.end())
					return;
				
				// copy description into array
				if (it->second.nativeFunctionReceptionCounter < it->second.nativeFunctions.size())
				{
					it->second.nativeFunctions[it->second.nativeFunctionReceptionCounter++] = *description;
					checkIfNodeDescriptionComplete(it->first, it->second);
				}
			}
		}
	}
Example #2
0
	void NodesManager::processMessage(const Message* message)
	{
		// check whether the node is known
		NodesMap::iterator nodeIt(nodes.find(message->source));
		if (nodeIt == nodes.end())
		{
			// node is not known, so ignore excepted if the message type 
			// is node present and it is not a known mismatch protocol,
			// in that case, request description...
			if ((message->type == ASEBA_MESSAGE_NODE_PRESENT) &&
				mismatchingNodes.find(message->source) == mismatchingNodes.end())
			{
				GetNodeDescription getNodeDescription(message->source);
				sendMessage(getNodeDescription);
			}
			// or if message type is description, in that case, proceed further
			if (message->type != ASEBA_MESSAGE_DESCRIPTION)
				return;
		}
		else
		{
			// node is known, check if connected...
			if (!nodeIt->second.connected)
			{
				// if not, build complete, set as connected and notify client
				nodeIt->second.connected = true;
				if (nodeIt->second.isComplete())
				{
					// only notify connections of completed known nodes
					nodeConnected(nodeIt->first);
				}
			}
			// update last seen time
			nodeIt->second.lastSeen = UnifiedTime();
		}
		
		// if we have a disconnection message
		{
			// FIXME: handle disconnected state
			const Disconnected *disconnected = dynamic_cast<const Disconnected *>(message);
			if (disconnected)
			{
				NodesMap::iterator nodeIt = nodes.find(disconnected->source);
				assert (nodeIt != nodes.end());
				nodes.erase(nodeIt);
			}
		}
		
		// if we have an initial description
		{
			const Description *description = dynamic_cast<const Description *>(message);
			if (description)
			{
				NodesMap::iterator nodeIt = nodes.find(description->source);
				
				// We can receive a description twice, for instance if there is another IDE connected
				if (nodeIt != nodes.end() || (mismatchingNodes.find(description->source) != mismatchingNodes.end()))
					return;
				
				// Call a user function when a node protocol version mismatches
				if ((description->protocolVersion < ASEBA_MIN_TARGET_PROTOCOL_VERSION) ||
					(description->protocolVersion > ASEBA_PROTOCOL_VERSION))
				{
					nodeProtocolVersionMismatch(description->source, description->name, description->protocolVersion);
					mismatchingNodes.insert(description->source);
					return;
				}
				
				// create node and copy description into it
				nodes[description->source] = Node(*description);
				checkIfNodeDescriptionComplete(description->source, nodes[description->source]);
			}
		}
		
		// if we have a named variable description
		{
			const NamedVariableDescription *description = dynamic_cast<const NamedVariableDescription *>(message);
			if (description)
			{
				NodesMap::iterator nodeIt = nodes.find(description->source);
				assert (nodeIt != nodes.end());
				
				// copy description into array if array is empty
				if (nodeIt->second.namedVariablesReceptionCounter < nodeIt->second.namedVariables.size())
				{
					nodeIt->second.namedVariables[nodeIt->second.namedVariablesReceptionCounter++] = *description;
					checkIfNodeDescriptionComplete(nodeIt->first, nodeIt->second);
				}
			}
		}
		
		// if we have a local event description
		{
			const LocalEventDescription *description = dynamic_cast<const LocalEventDescription *>(message);
			if (description)
			{
				NodesMap::iterator nodeIt = nodes.find(description->source);
				assert (nodeIt != nodes.end());
				
				// copy description into array if array is empty
				if (nodeIt->second.localEventsReceptionCounter < nodeIt->second.localEvents.size())
				{
					nodeIt->second.localEvents[nodeIt->second.localEventsReceptionCounter++] = *description;
					checkIfNodeDescriptionComplete(nodeIt->first, nodeIt->second);
				}
			}
		}
		
		// if we have a native function description
		{
			const NativeFunctionDescription *description = dynamic_cast<const NativeFunctionDescription *>(message);
			if (description)
			{
				NodesMap::iterator nodeIt = nodes.find(description->source);
				assert (nodeIt != nodes.end());
				
				// copy description into array
				if (nodeIt->second.nativeFunctionReceptionCounter < nodeIt->second.nativeFunctions.size())
				{
					nodeIt->second.nativeFunctions[nodeIt->second.nativeFunctionReceptionCounter++] = *description;
					checkIfNodeDescriptionComplete(nodeIt->first, nodeIt->second);
				}
			}
		}
	}