/**
 * Find local service matching filter.
 */
std::set<ServiceDescription> ServiceManager::findLocal(const ServiceFilter& svcFilter) {
	std::set<ServiceDescription> foundSvcs;
	std::map<intptr_t, ServiceDescription>::iterator svcDescIter = _localSvcDesc.begin();
	while(svcDescIter != _localSvcDesc.end()) {
		if (svcFilter.matches(svcDescIter->second)) {
			foundSvcs.insert(svcDescIter->second);
			UM_LOG_INFO("we have a match for %s", svcFilter.getServiceName().c_str());
		}
		svcDescIter++;
	}
	return foundSvcs;
}
ServiceDescription ServiceManager::find(const ServiceFilter& svcFilter, int timeout) {
	if(_svcPub->waitForSubscribers(1, timeout) < 1) {
		// there is no other ServiceManager yet
		UM_LOG_INFO("Failed to find another ServiceManager");
		return ServiceDescription();
	}

	Message* findMsg = svcFilter.toMessage();
	std::string reqId = UUID::getUUID();
	findMsg->putMeta("um.rpc.type", "discover");
	findMsg->putMeta("um.rpc.reqId", reqId.c_str());
	findMsg->putMeta("um.rpc.mgrId", _svcSub->getUUID());
//	Thread::sleepMs(1000);
	if(_findRequests.find(reqId) != _findRequests.end())
		UM_LOG_WARN("Find request %s already received", reqId.c_str());
	_findRequests[reqId] = new Monitor();

	_svcPub->send(findMsg);
	delete findMsg;

	RScopeLock lock(_mutex);
	_findRequests[reqId]->wait(_mutex, timeout);
	delete _findRequests[reqId];
	_findRequests.erase(reqId);

	if (_findResponses.find(reqId) == _findResponses.end()) {
		UM_LOG_INFO("Failed to find %s", svcFilter.getServiceName().c_str());
		return ServiceDescription();
	}

	// TODO: Remove other replies as they come in!

	Message* foundMsg = _findResponses[reqId];
	assert(foundMsg != NULL);
	ServiceDescription svcDesc(foundMsg);
	svcDesc._svcManager = this;
	_findResponses.erase(reqId);
	delete foundMsg;
	return svcDesc;

}