Example #1
0
int YARPPort::Connect(const char *src_name, const char *dest_name)
{
	if (ACE_OS::strcmp (src_name, dest_name) == 0)
	{
		ACE_DEBUG ((LM_ERROR, "Silly you, you tried it, didn't you?\n"));
		return YARP_FAIL;
	}

	// checking syntax.
	if (src_name[0] != '*' && src_name[0] != '/')
	{
		ACE_DEBUG ((LM_ERROR, "The syntax of port connect is wrong\n"));
		ACE_DEBUG ((LM_ERROR, "Please remember that valid names must start with\n"));
		ACE_DEBUG ((LM_ERROR, "/ [forward slash]\n"));
		ACE_DEBUG ((LM_ERROR, "The special symbol '*' is allowed when requesting\n"));
		ACE_DEBUG ((LM_ERROR, "the active port connections\n"));
		return YARP_FAIL;
	}

	// request port connections.
	if (src_name[0] == '*')
	{
		YARPUniqueNameID* id = YARPNameService::LocateName (src_name+1, NULL);
		if (id->getServiceType() != YARP_QNET) id->setServiceType (YARP_TCP);
		id->setRequireAck(1);

		YARPEndpointManager::CreateOutputEndpoint (*id);
		int ret = YARPEndpointManager::ConnectEndpoints (*id, "explicit_connect");

		if (ret == YARP_OK)
		{
			if (id->isValid())
			{
				Port p;
				p.SayServer (id->getNameID(), src_name);
			}

			YARPEndpointManager::Close (*id);
			YARPNameService::DeleteName (id);

			return YARP_OK;
		}

		YARPEndpointManager::Close (*id);
		YARPNameService::DeleteName (id);

		return YARP_FAIL;
	}

	// assuming a valid name starting with /
	if (src_name[0] == '/')
	{
		// NULL 2nd param means no net specified, only IP addr.
		YARPUniqueNameID* id = YARPNameService::LocateName (src_name, NULL);
		if (id->getServiceType() != YARP_QNET) id->setServiceType (YARP_TCP);
		id->setRequireAck(1);

		YARPEndpointManager::CreateOutputEndpoint (*id);
		int ret = YARPEndpointManager::ConnectEndpoints (*id, "explicit_connect");

		if (ret == YARP_OK)
		{
			if (id->isValid())
			{
				Port p;
				p.SayServer (id->getNameID(), dest_name);
			}

			YARPEndpointManager::Close (*id);
			YARPNameService::DeleteName (id);

			return YARP_OK;
		}

		YARPEndpointManager::Close (*id);
		YARPNameService::DeleteName (id);

		// continues to next 'if'.
	}

	// can't connect, try a different strategy.
	if (dest_name[0] == '!')
	{
		ACE_DEBUG ((LM_INFO, "trying again with direct connection\n"));

		// tries again...
		YARPUniqueNameID* id = YARPNameService::LocateName (dest_name+1, NULL);
		if (id->getServiceType() != YARP_QNET) id->setServiceType (YARP_TCP);
		id->setRequireAck(1);

		YARPEndpointManager::CreateOutputEndpoint (*id);
		int ret = YARPEndpointManager::ConnectEndpoints (*id, "explicit_connect");
		if (ret == YARP_OK)
		{
			if (id->isValid())
			{
				Port p;
				const int len = ACE_OS::strlen(src_name);
				char *tmp_name = new char[len+2];
				ACE_ASSERT (tmp_name != NULL);
				
				tmp_name[len+1] = 0;
				ACE_OS::strcpy (tmp_name+1, src_name);
				tmp_name[0] = '~';
				p.SayServer (id->getNameID(), tmp_name);

				delete[] tmp_name;
			}

			YARPEndpointManager::Close (*id);
			YARPNameService::DeleteName (id);
			return YARP_OK;
		}

		YARPEndpointManager::Close (*id);
		YARPNameService::DeleteName (id);

		return YARP_FAIL;
	}

	return YARP_FAIL;
}