diff options
author | Remko Tronçon <git@el-tramo.be> | 2009-08-01 10:07:16 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2009-08-01 10:22:55 (GMT) |
commit | 5a15e4df671aac30bba332801528a6283c2e4874 (patch) | |
tree | 8668089b7b9db89451b8b06ba7be29056a805f53 | |
parent | 336aa7d1c0a5aa223a6543d50ee17d82560c2b84 (diff) | |
download | swift-contrib-5a15e4df671aac30bba332801528a6283c2e4874.zip swift-contrib-5a15e4df671aac30bba332801528a6283c2e4874.tar.bz2 |
Made Slimber fully functional again.
-rw-r--r-- | Slimber/LinkLocalPresenceManager.cpp | 19 | ||||
-rw-r--r-- | Slimber/LinkLocalPresenceManager.h | 3 | ||||
-rw-r--r-- | Slimber/Server.cpp | 47 | ||||
-rw-r--r-- | Slimber/Server.h | 14 | ||||
-rw-r--r-- | Slimber/UnitTest/LinkLocalPresenceManagerTest.cpp | 23 | ||||
-rw-r--r-- | Swiften/LinkLocal/LinkLocalConnector.cpp | 16 | ||||
-rw-r--r-- | Swiften/LinkLocal/LinkLocalConnector.h | 17 | ||||
-rw-r--r-- | Swiften/LinkLocal/LinkLocalService.cpp | 4 | ||||
-rw-r--r-- | Swiften/LinkLocal/LinkLocalService.h | 3 | ||||
-rw-r--r-- | Swiften/LinkLocal/LinkLocalServiceBrowser.h | 5 | ||||
-rw-r--r-- | Swiften/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp | 9 |
11 files changed, 94 insertions, 66 deletions
diff --git a/Slimber/LinkLocalPresenceManager.cpp b/Slimber/LinkLocalPresenceManager.cpp index b964786..d5834c7 100644 --- a/Slimber/LinkLocalPresenceManager.cpp +++ b/Slimber/LinkLocalPresenceManager.cpp @@ -18,6 +18,15 @@ LinkLocalPresenceManager::LinkLocalPresenceManager(LinkLocalServiceBrowser* brow boost::bind(&LinkLocalPresenceManager::handleServiceRemoved, this, _1)); } +boost::optional<LinkLocalService> LinkLocalPresenceManager::getServiceForJID(const JID& j) const { + foreach(const LinkLocalService& service, browser->getServices()) { + if (service.getJID() == j) { + return service; + } + } + return boost::optional<LinkLocalService>(); +} + void LinkLocalPresenceManager::handleServiceAdded(const LinkLocalService& service) { boost::shared_ptr<RosterPayload> roster(new RosterPayload()); roster->addItem(getRosterItem(service)); @@ -31,7 +40,7 @@ void LinkLocalPresenceManager::handleServiceChanged(const LinkLocalService& serv void LinkLocalPresenceManager::handleServiceRemoved(const LinkLocalService& service) { boost::shared_ptr<RosterPayload> roster(new RosterPayload()); - roster->addItem(RosterItemPayload(getJIDForService(service), "", RosterItemPayload::Remove)); + roster->addItem(RosterItemPayload(service.getJID(), "", RosterItemPayload::Remove)); onRosterChanged(roster); } @@ -52,7 +61,7 @@ std::vector<boost::shared_ptr<Presence> > LinkLocalPresenceManager::getAllPresen } RosterItemPayload LinkLocalPresenceManager::getRosterItem(const LinkLocalService& service) const { - return RosterItemPayload(getJIDForService(service), getRosterName(service), RosterItemPayload::Both); + return RosterItemPayload(service.getJID(), getRosterName(service), RosterItemPayload::Both); } String LinkLocalPresenceManager::getRosterName(const LinkLocalService& service) const { @@ -73,13 +82,9 @@ String LinkLocalPresenceManager::getRosterName(const LinkLocalService& service) return ""; } -JID LinkLocalPresenceManager::getJIDForService(const LinkLocalService& service) const { - return JID(service.getName()); -} - boost::shared_ptr<Presence> LinkLocalPresenceManager::getPresence(const LinkLocalService& service) const { boost::shared_ptr<Presence> presence(new Presence()); - presence->setFrom(getJIDForService(service)); + presence->setFrom(service.getJID()); switch (service.getInfo().getStatus()) { case LinkLocalServiceInfo::Available: presence->setShow(StatusShow::Online); diff --git a/Slimber/LinkLocalPresenceManager.h b/Slimber/LinkLocalPresenceManager.h index 2af2313..c9be5a7 100644 --- a/Slimber/LinkLocalPresenceManager.h +++ b/Slimber/LinkLocalPresenceManager.h @@ -20,6 +20,8 @@ namespace Swift { boost::shared_ptr<RosterPayload> getRoster() const; std::vector<boost::shared_ptr<Presence> > getAllPresence() const; + boost::optional<LinkLocalService> getServiceForJID(const JID&) const; + boost::signal<void (boost::shared_ptr<RosterPayload>)> onRosterChanged; boost::signal<void (boost::shared_ptr<Presence>)> onPresenceChanged; @@ -30,7 +32,6 @@ namespace Swift { RosterItemPayload getRosterItem(const LinkLocalService& service) const; String getRosterName(const LinkLocalService& service) const; - JID getJIDForService(const LinkLocalService& service) const; boost::shared_ptr<Presence> getPresence(const LinkLocalService& service) const; private: diff --git a/Slimber/Server.cpp b/Slimber/Server.cpp index a63201b..36595b1 100644 --- a/Slimber/Server.cpp +++ b/Slimber/Server.cpp @@ -54,14 +54,12 @@ Server::Server( linkLocalServiceBrowser->onServiceRegistered.connect( boost::bind(&Server::handleServiceRegistered, this, _1)); - /* serverFromNetworkConnectionServer = boost::shared_ptr<BoostConnectionServer>(new BoostConnectionServer( linkLocalConnectionPort, &boostIOServiceThread.getIOService())); serverFromNetworkConnectionServer->onNewConnection.connect( boost::bind(&Server::handleNewLinkLocalConnection, this, _1)); serverFromNetworkConnectionServer->start(); - */ } Server::~Server() { @@ -168,7 +166,6 @@ void Server::handleElementReceived(boost::shared_ptr<Element> element, boost::sh } } } - /* else { JID toJID = stanza->getTo(); boost::shared_ptr<Session> outgoingSession = @@ -177,20 +174,20 @@ void Server::handleElementReceived(boost::shared_ptr<Element> element, boost::sh outgoingSession->sendElement(stanza); } else { - if (linkLocalServiceBrowser->hasItem(toJID)) { + boost::optional<LinkLocalService> service = + presenceManager->getServiceForJID(toJID); + if (service) { boost::shared_ptr<LinkLocalConnector> connector = getLinkLocalConnectorForJID(toJID); if (!connector) { connector = boost::shared_ptr<LinkLocalConnector>( new LinkLocalConnector( - toJID, - linkLocalServiceBrowser->getHostname(toJID), - linkLocalServiceBrowser->getPort(toJID), - linkLocalServiceBrowser, + *service, + linkLocalServiceBrowser->getQuerier(), boost::shared_ptr<BoostConnection>(new BoostConnection(&boostIOServiceThread.getIOService())))); connector->onConnectFinished.connect( boost::bind(&Server::handleConnectFinished, this, connector, _1)); - connectors_.push_back(connector); + connectors.push_back(connector); connector->connect(); } connector->queueElement(element); @@ -202,10 +199,8 @@ void Server::handleElementReceived(boost::shared_ptr<Element> element, boost::sh } } } - */ } -/* void Server::handleNewLinkLocalConnection(boost::shared_ptr<Connection> connection) { boost::shared_ptr<IncomingLinkLocalSession> session( new IncomingLinkLocalSession( @@ -216,14 +211,16 @@ void Server::handleNewLinkLocalConnection(boost::shared_ptr<Connection> connecti void Server::handleLinkLocalSessionFinished(boost::shared_ptr<Session> session) { std::cout << "Link local session from " << session->getRemoteJID() << " ended" << std::endl; - linkLocalSessions_.erase(std::remove(linkLocalSessions_.begin(), linkLocalSessions_.end(), session), linkLocalSessions_.end()); + linkLocalSessions.erase( + std::remove(linkLocalSessions.begin(), linkLocalSessions.end(), session), + linkLocalSessions.end()); } void Server::handleLinkLocalElementReceived(boost::shared_ptr<Element> element, boost::shared_ptr<Session> session) { if (boost::shared_ptr<Stanza> stanza = boost::dynamic_pointer_cast<Stanza>(element)) { JID fromJID = session->getRemoteJID(); - if (!linkLocalServiceBrowser->hasItem(fromJID)) { - return; // TODO: Queue + if (!presenceManager->getServiceForJID(fromJID.toBare())) { + return; // TODO: Send error back } stanza->setFrom(fromJID); serverFromClientSession->sendElement(stanza); @@ -238,27 +235,28 @@ void Server::handleConnectFinished(boost::shared_ptr<LinkLocalConnector> connect else { boost::shared_ptr<OutgoingLinkLocalSession> outgoingSession( new OutgoingLinkLocalSession( - selfJID, connector->getRemoteJID(), connector->getConnection(), + selfJID, connector->getService().getJID(), connector->getConnection(), &payloadParserFactories, &payloadSerializers)); foreach(const boost::shared_ptr<Element> element, connector->getQueuedElements()) { outgoingSession->queueElement(element); } registerLinkLocalSession(outgoingSession); } - connectors_.erase(std::remove(connectors_.begin(), connectors_.end(), connector), connectors_.end()); + connectors.erase(std::remove(connectors.begin(), connectors.end(), connector), connectors.end()); } - void Server::registerLinkLocalSession(boost::shared_ptr<Session> session) { - session->onSessionFinished.connect(boost::bind(&Server::handleLinkLocalSessionFinished, this, session)); - session->onElementReceived.connect(boost::bind(&Server::handleLinkLocalElementReceived, this, _1, session)); - linkLocalSessions_.push_back(session); - //tracers.push_back(boost::shared_ptr<SessionTracer>(new SessionTracer(session))); + session->onSessionFinished.connect( + boost::bind(&Server::handleLinkLocalSessionFinished, this, session)); + session->onElementReceived.connect( + boost::bind(&Server::handleLinkLocalElementReceived, this, _1, session)); + linkLocalSessions.push_back(session); + tracers.push_back(boost::shared_ptr<SessionTracer>(new SessionTracer(session))); session->startSession(); } boost::shared_ptr<Session> Server::getLinkLocalSessionForJID(const JID& jid) { - foreach(const boost::shared_ptr<Session> session, linkLocalSessions_) { + foreach(const boost::shared_ptr<Session> session, linkLocalSessions) { if (session->getRemoteJID() == jid) { return session; } @@ -267,14 +265,13 @@ boost::shared_ptr<Session> Server::getLinkLocalSessionForJID(const JID& jid) { } boost::shared_ptr<LinkLocalConnector> Server::getLinkLocalConnectorForJID(const JID& jid) { - foreach(const boost::shared_ptr<LinkLocalConnector> connector, connectors_) { - if (connector->getRemoteJID() == jid) { + foreach(const boost::shared_ptr<LinkLocalConnector> connector, connectors) { + if (connector->getService().getJID() == jid) { return connector; } } return boost::shared_ptr<LinkLocalConnector>(); } -*/ void Server::handleServiceRegistered(const DNSSDServiceID& service) { selfJID = JID(service.getName()); diff --git a/Slimber/Server.h b/Slimber/Server.h index 3ed0a58..38c45de 100644 --- a/Slimber/Server.h +++ b/Slimber/Server.h @@ -16,6 +16,7 @@ namespace Swift { class DNSSDServiceID; class String; class VCardCollection; + class LinkLocalConnector; class LinkLocalServiceBrowser; class LinkLocalPresenceManager; class BoostConnectionServer; @@ -42,16 +43,13 @@ namespace Swift { void handleRosterChanged(boost::shared_ptr<RosterPayload> roster); void handlePresenceChanged(boost::shared_ptr<Presence> presence); void handleServiceRegistered(const DNSSDServiceID& service); -/* void handleNewLinkLocalConnection(boost::shared_ptr<Connection> connection); void handleLinkLocalSessionFinished(boost::shared_ptr<Session> session); void handleLinkLocalElementReceived(boost::shared_ptr<Element> element, boost::shared_ptr<Session> session); void handleConnectFinished(boost::shared_ptr<LinkLocalConnector> connector, bool error); - void registerLinkLocalSession(boost::shared_ptr<Session> session); boost::shared_ptr<Session> getLinkLocalSessionForJID(const JID& jid); boost::shared_ptr<LinkLocalConnector> getLinkLocalConnectorForJID(const JID& jid); - */ - + void registerLinkLocalSession(boost::shared_ptr<Session> session); void unregisterService(); LinkLocalServiceInfo getLinkLocalServiceInfo(boost::shared_ptr<Presence> presence); @@ -82,11 +80,9 @@ namespace Swift { boost::shared_ptr<ServerFromClientSession> serverFromClientSession; boost::shared_ptr<Presence> lastPresence; JID selfJID; - /* - boost::shared_ptr<BoostConnectionServer> serverFromNetworkConnectionServer_; - std::vector< boost::shared_ptr<Session> > linkLocalSessions_; - std::vector< boost::shared_ptr<LinkLocalConnector> > connectors_; -*/ + boost::shared_ptr<BoostConnectionServer> serverFromNetworkConnectionServer; + std::vector< boost::shared_ptr<Session> > linkLocalSessions; + std::vector< boost::shared_ptr<LinkLocalConnector> > connectors; std::vector< boost::shared_ptr<SessionTracer> > tracers; }; } diff --git a/Slimber/UnitTest/LinkLocalPresenceManagerTest.cpp b/Slimber/UnitTest/LinkLocalPresenceManagerTest.cpp index f15daf4..f77a8cb 100644 --- a/Slimber/UnitTest/LinkLocalPresenceManagerTest.cpp +++ b/Slimber/UnitTest/LinkLocalPresenceManagerTest.cpp @@ -29,6 +29,8 @@ class LinkLocalPresenceManagerTest : public CppUnit::TestFixture { CPPUNIT_TEST(testGetRoster_InfoWithLastName); CPPUNIT_TEST(testGetRoster_InfoWithFirstAndLastName); CPPUNIT_TEST(testGetRoster_NoInfo); + CPPUNIT_TEST(testGetServiceForJID); + CPPUNIT_TEST(testGetServiceForJID_NoMatch); CPPUNIT_TEST_SUITE_END(); public: @@ -176,6 +178,27 @@ class LinkLocalPresenceManagerTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(String(""), item->getName()); } + void testGetServiceForJID() { + std::auto_ptr<LinkLocalPresenceManager> testling(createTestling()); + + addService("alice@wonderland"); + addService("rabbit@teaparty"); + addService("queen@garden"); + + boost::optional<LinkLocalService> service = testling->getServiceForJID(JID("rabbit@teaparty")); + CPPUNIT_ASSERT(service); + CPPUNIT_ASSERT_EQUAL(String("rabbit@teaparty"), service->getID().getName()); + } + + void testGetServiceForJID_NoMatch() { + std::auto_ptr<LinkLocalPresenceManager> testling(createTestling()); + + addService("alice@wonderland"); + addService("queen@garden"); + + CPPUNIT_ASSERT(!testling->getServiceForJID(JID("rabbit@teaparty"))); + } + private: std::auto_ptr<LinkLocalPresenceManager> createTestling() { std::auto_ptr<LinkLocalPresenceManager> testling( diff --git a/Swiften/LinkLocal/LinkLocalConnector.cpp b/Swiften/LinkLocal/LinkLocalConnector.cpp index 13d57e8..40558d4 100644 --- a/Swiften/LinkLocal/LinkLocalConnector.cpp +++ b/Swiften/LinkLocal/LinkLocalConnector.cpp @@ -12,22 +12,18 @@ namespace Swift { LinkLocalConnector::LinkLocalConnector( - const JID& remoteJID, - const String& hostname, - int interfaceIndex, - int port, + const LinkLocalService& service, boost::shared_ptr<DNSSDQuerier> querier, boost::shared_ptr<Connection> connection) : - remoteJID(remoteJID), - hostname(hostname), - interfaceIndex(interfaceIndex), - port(port), + service(service), querier(querier), connection(connection) { } void LinkLocalConnector::connect() { - resolveQuery = querier->createResolveHostnameQuery(hostname, interfaceIndex); + resolveQuery = querier->createResolveHostnameQuery( + service.getHostname(), + service.getID().getNetworkInterfaceID()); resolveQuery->onHostnameResolved.connect(boost::bind( &LinkLocalConnector::handleHostnameResolved, boost::dynamic_pointer_cast<LinkLocalConnector>(shared_from_this()), @@ -41,7 +37,7 @@ void LinkLocalConnector::handleHostnameResolved(const boost::optional<HostAddres resolveQuery.reset(); connection->onConnectFinished.connect( boost::bind(boost::ref(onConnectFinished), _1)); - connection->connect(HostAddressPort(*address, port)); + connection->connect(HostAddressPort(*address, service.getPort())); } else { onConnectFinished(true); diff --git a/Swiften/LinkLocal/LinkLocalConnector.h b/Swiften/LinkLocal/LinkLocalConnector.h index 134656c..fa293c4 100644 --- a/Swiften/LinkLocal/LinkLocalConnector.h +++ b/Swiften/LinkLocal/LinkLocalConnector.h @@ -5,13 +5,12 @@ #include <boost/enable_shared_from_this.hpp> #include <vector> -#include "Swiften/JID/JID.h" #include "Swiften/Network/Connection.h" +#include "Swiften/LinkLocal/LinkLocalService.h" namespace Swift { class ConnectionFactory; class HostAddress; - class String; class Element; class PayloadParserFactoryCollection; class PayloadSerializerCollection; @@ -21,15 +20,12 @@ namespace Swift { class LinkLocalConnector : public boost::enable_shared_from_this<LinkLocalConnector> { public: LinkLocalConnector( - const JID& remoteJID, - const String& hostname, - int interfaceIndex, - int port, + const LinkLocalService& service, boost::shared_ptr<DNSSDQuerier> querier, boost::shared_ptr<Connection> connection); - const JID& getRemoteJID() const { - return remoteJID; + const LinkLocalService& getService() const { + return service; } void connect(); @@ -50,10 +46,7 @@ namespace Swift { void handleConnected(bool error); private: - JID remoteJID; - String hostname; - int interfaceIndex; - int port; + LinkLocalService service; boost::shared_ptr<DNSSDQuerier> querier; boost::shared_ptr<DNSSDResolveHostnameQuery> resolveQuery; boost::shared_ptr<Connection> connection; diff --git a/Swiften/LinkLocal/LinkLocalService.cpp b/Swiften/LinkLocal/LinkLocalService.cpp index f567a63..f1114ed 100644 --- a/Swiften/LinkLocal/LinkLocalService.cpp +++ b/Swiften/LinkLocal/LinkLocalService.cpp @@ -20,4 +20,8 @@ String LinkLocalService::getDescription() const { return getName(); } +JID LinkLocalService::getJID() const { + return JID(getName()); +} + } diff --git a/Swiften/LinkLocal/LinkLocalService.h b/Swiften/LinkLocal/LinkLocalService.h index f7e9e3c..8ae593c 100644 --- a/Swiften/LinkLocal/LinkLocalService.h +++ b/Swiften/LinkLocal/LinkLocalService.h @@ -1,6 +1,7 @@ #pragma once #include "Swiften/Base/String.h" +#include "Swiften/JID/JID.h" #include "Swiften/LinkLocal/DNSSD/DNSSDServiceID.h" #include "Swiften/LinkLocal/DNSSD/DNSSDResolveServiceQuery.h" #include "Swiften/LinkLocal/LinkLocalServiceInfo.h" @@ -36,6 +37,8 @@ namespace Swift { String getDescription() const; + JID getJID() const; + private: DNSSDServiceID id; DNSSDResolveServiceQuery::Result info; diff --git a/Swiften/LinkLocal/LinkLocalServiceBrowser.h b/Swiften/LinkLocal/LinkLocalServiceBrowser.h index 7ef661c..66973d5 100644 --- a/Swiften/LinkLocal/LinkLocalServiceBrowser.h +++ b/Swiften/LinkLocal/LinkLocalServiceBrowser.h @@ -36,6 +36,11 @@ namespace Swift { std::vector<LinkLocalService> getServices() const; + // FIXME: Ugly that we need this + boost::shared_ptr<DNSSDQuerier> getQuerier() const { + return querier; + } + boost::signal<void (const LinkLocalService&)> onServiceAdded; boost::signal<void (const LinkLocalService&)> onServiceChanged; boost::signal<void (const LinkLocalService&)> onServiceRemoved; diff --git a/Swiften/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp b/Swiften/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp index 1b9b0aa..b052e6a 100644 --- a/Swiften/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp +++ b/Swiften/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp @@ -74,8 +74,13 @@ class LinkLocalConnectorTest : public CppUnit::TestFixture { private: boost::shared_ptr<LinkLocalConnector> createConnector(const String& hostname, int port) { - boost::shared_ptr<LinkLocalConnector> result(new LinkLocalConnector( - JID("rabbit@teaparty"), hostname, 0, port, querier, connection)); + LinkLocalService service( + DNSSDServiceID("myname", "local."), + DNSSDResolveServiceQuery::Result( + "myname._presence._tcp.local", hostname, port, + LinkLocalServiceInfo().toTXTRecord())); + boost::shared_ptr<LinkLocalConnector> result( + new LinkLocalConnector(service, querier, connection)); result->onConnectFinished.connect( boost::bind(&LinkLocalConnectorTest::handleConnected, this, _1)); return result; |