diff options
Diffstat (limited to 'Swiften/LinkLocal')
-rw-r--r-- | Swiften/LinkLocal/AppleDNSSDService.cpp | 10 | ||||
-rw-r--r-- | Swiften/LinkLocal/AppleDNSSDService.h | 7 | ||||
-rw-r--r-- | Swiften/LinkLocal/AvahiDNSSDService.cpp | 16 | ||||
-rw-r--r-- | Swiften/LinkLocal/AvahiDNSSDService.h | 4 | ||||
-rw-r--r-- | Swiften/LinkLocal/DNSSDService.h | 43 | ||||
-rw-r--r-- | Swiften/LinkLocal/LinkLocalRoster.cpp | 20 | ||||
-rw-r--r-- | Swiften/LinkLocal/LinkLocalRoster.h | 20 | ||||
-rw-r--r-- | Swiften/LinkLocal/LinkLocalServiceID.h | 64 |
8 files changed, 110 insertions, 74 deletions
diff --git a/Swiften/LinkLocal/AppleDNSSDService.cpp b/Swiften/LinkLocal/AppleDNSSDService.cpp index 969cbb2..5262357 100644 --- a/Swiften/LinkLocal/AppleDNSSDService.cpp +++ b/Swiften/LinkLocal/AppleDNSSDService.cpp @@ -70,11 +70,11 @@ void AppleDNSSDService::unregisterService() { registerSDRef = NULL; } -void AppleDNSSDService::startResolvingService(const Service& service) { +void AppleDNSSDService::startResolvingService(const LinkLocalServiceID& service) { boost::lock_guard<boost::mutex> lock(sdRefsMutex); DNSServiceRef resolveSDRef; - DNSServiceErrorType result = DNSServiceResolve(&resolveSDRef, 0, service.networkInterface, service.name.getUTF8Data(), service.type.getUTF8Data(), service.domain.getUTF8Data(), &AppleDNSSDService::handleServiceResolvedGlobal, this); + DNSServiceErrorType result = DNSServiceResolve(&resolveSDRef, 0, service.getNetworkInterfaceID(), service.getName().getUTF8Data(), service.getType().getUTF8Data(), service.getDomain().getUTF8Data(), &AppleDNSSDService::handleServiceResolvedGlobal, this); if (result != kDNSServiceErr_NoError) { std::cerr << "Error creating service resolve query" << std::endl; haveError = true; @@ -87,7 +87,7 @@ void AppleDNSSDService::startResolvingService(const Service& service) { interruptSelect(); } -void AppleDNSSDService::stopResolvingService(const Service& service) { +void AppleDNSSDService::stopResolvingService(const LinkLocalServiceID& service) { boost::lock_guard<boost::mutex> lock(sdRefsMutex); ServiceSDRefMap::iterator i = resolveSDRefs.find(service); @@ -238,7 +238,7 @@ void AppleDNSSDService::handleServiceDiscovered(DNSServiceRef, DNSServiceFlags f return; } else { - Service service(serviceName, regtype, replyDomain, interfaceIndex); + LinkLocalServiceID service(serviceName, regtype, replyDomain, interfaceIndex); if (flags & kDNSServiceFlagsAdd) { MainEventLoop::postEvent(boost::bind(boost::ref(onServiceAdded), service), shared_from_this()); } @@ -258,7 +258,7 @@ void AppleDNSSDService::handleServiceRegistered(DNSServiceRef, DNSServiceFlags, haveError = true; } else { - MainEventLoop::postEvent(boost::bind(boost::ref(onServiceRegistered), Service(name, regtype, domain, 0)), shared_from_this()); + MainEventLoop::postEvent(boost::bind(boost::ref(onServiceRegistered), LinkLocalServiceID(name, regtype, domain, 0)), shared_from_this()); } } diff --git a/Swiften/LinkLocal/AppleDNSSDService.h b/Swiften/LinkLocal/AppleDNSSDService.h index fe4a648..7fdfd3e 100644 --- a/Swiften/LinkLocal/AppleDNSSDService.h +++ b/Swiften/LinkLocal/AppleDNSSDService.h @@ -6,6 +6,7 @@ #include <dns_sd.h> #include "Swiften/LinkLocal/DNSSDService.h" +#include "Swiften/LinkLocal/LinkLocalServiceID.h" #include "Swiften/EventLoop/EventOwner.h" namespace Swift { @@ -21,8 +22,8 @@ namespace Swift { virtual void updateService(const LinkLocalServiceInfo&); virtual void unregisterService(); - virtual void startResolvingService(const Service&); - virtual void stopResolvingService(const Service&); + virtual void startResolvingService(const LinkLocalServiceID&); + virtual void stopResolvingService(const LinkLocalServiceID&); virtual void resolveHostname(const String& hostname, int interfaceIndex = 0); @@ -48,7 +49,7 @@ namespace Swift { boost::mutex sdRefsMutex; DNSServiceRef browseSDRef; DNSServiceRef registerSDRef; - typedef std::map<Service, DNSServiceRef> ServiceSDRefMap; + typedef std::map<LinkLocalServiceID, DNSServiceRef> ServiceSDRefMap; ServiceSDRefMap resolveSDRefs; typedef std::vector<DNSServiceRef> HostnameSDRefs; HostnameSDRefs hostnameResolveSDRefs; diff --git a/Swiften/LinkLocal/AvahiDNSSDService.cpp b/Swiften/LinkLocal/AvahiDNSSDService.cpp index 1dd5c28..72039b7 100644 --- a/Swiften/LinkLocal/AvahiDNSSDService.cpp +++ b/Swiften/LinkLocal/AvahiDNSSDService.cpp @@ -62,16 +62,16 @@ void AvahiDNSSDService::unregisterService() { avahi_threaded_poll_unlock(threadedPoll); } -void AvahiDNSSDService::startResolvingService(const Service& service) { +void AvahiDNSSDService::startResolvingService(const LinkLocalServiceID& service) { avahi_threaded_poll_lock(threadedPoll); AvahiServiceResolver* resolver = avahi_service_resolver_new( client, - service.networkInterface, + service.getNetworkInterfaceID(), AVAHI_PROTO_INET, - service.name.getUTF8Data(), - service.type.getUTF8Data(), - service.domain.getUTF8Data(), + service.getName().getUTF8Data(), + service.getType().getUTF8Data(), + service.getDomain().getUTF8Data(), AVAHI_PROTO_UNSPEC, static_cast<AvahiLookupFlags>(0), &AvahiDNSSDService::handleServiceResolvedGlobal, @@ -112,13 +112,13 @@ void AvahiDNSSDService::handleServiceDiscovered(AvahiServiceBrowser *, AvahiIfIn return; case AVAHI_BROWSER_NEW: { std::cerr << "Service added: " << name << " " << type << " " << domain << std::endl; - Service service(name, type, domain, interfaceIndex); + LinkLocalServiceID service(name, type, domain, interfaceIndex); MainEventLoop::postEvent(boost::bind(boost::ref(onServiceAdded), service), shared_from_this()); } break; case AVAHI_BROWSER_REMOVE: { std::cerr << "Service removed: " << name << " " << type << " " << domain << std::endl; - Service service(name, type, domain, interfaceIndex); + LinkLocalServiceID service(name, type, domain, interfaceIndex); MainEventLoop::postEvent(boost::bind(boost::ref(onServiceRemoved), service), shared_from_this()); } break; @@ -146,7 +146,7 @@ void AvahiDNSSDService::handleServiceResolved(AvahiServiceResolver *, AvahiIfInd HostAddress hostAddress(reinterpret_cast<const unsigned char*>(&address->data.ipv4.address), 4); hostnameAddresses[String(hostname)] = hostAddress; MainEventLoop::postEvent(boost::bind(boost::ref(onServiceResolved), - Service(name, type, domain, interfaceIndex), + LinkLocalServiceID(name, type, domain, interfaceIndex), ResolveResult(hostname, port, LinkLocalServiceInfo::createFromTXTRecord(data))), shared_from_this()); diff --git a/Swiften/LinkLocal/AvahiDNSSDService.h b/Swiften/LinkLocal/AvahiDNSSDService.h index 8d31e41..a5be943 100644 --- a/Swiften/LinkLocal/AvahiDNSSDService.h +++ b/Swiften/LinkLocal/AvahiDNSSDService.h @@ -26,8 +26,8 @@ namespace Swift { virtual void updateService(const LinkLocalServiceInfo&); virtual void unregisterService(); - virtual void startResolvingService(const Service&); - virtual void stopResolvingService(const Service&); + virtual void startResolvingService(const LinkLocalServiceID&); + virtual void stopResolvingService(const LinkLocalServiceID&); virtual void resolveHostname(const String& hostname, int interfaceIndex = 0); diff --git a/Swiften/LinkLocal/DNSSDService.h b/Swiften/LinkLocal/DNSSDService.h index 9689352..4cf1a6e 100644 --- a/Swiften/LinkLocal/DNSSDService.h +++ b/Swiften/LinkLocal/DNSSDService.h @@ -4,6 +4,7 @@ #include <map> #include "Swiften/Base/String.h" +#include "Swiften/LinkLocal/LinkLocalServiceID.h" #include "Swiften/LinkLocal/LinkLocalServiceInfo.h" namespace Swift { @@ -12,36 +13,6 @@ namespace Swift { class DNSSDService { public: - struct Service { - Service(const String& name, const String& type, const String& domain, int networkInterface) : name(name), type(type), domain(domain), networkInterface(networkInterface) {} - bool operator==(const Service& o) const { - return name == o.name && type == o.type && domain == o.domain && (networkInterface != 0 && o.networkInterface != 0 ? networkInterface == o.networkInterface : true); - } - bool operator<(const Service& o) const { - if (o.name == name) { - if (o.type == type) { - if (o.domain == domain) { - return networkInterface < o.networkInterface; - } - else { - return domain < o.domain; - } - } - else { - return type < o.type; - } - } - else { - return o.name < name; - } - } - - String name; - String type; - String domain; - int networkInterface; - }; - struct ResolveResult { ResolveResult(const String& host, int port, const LinkLocalServiceInfo& info) : host(host), port(port), info(info) {} String host; @@ -58,17 +29,17 @@ namespace Swift { virtual void updateService(const LinkLocalServiceInfo&) = 0; virtual void unregisterService() = 0; - virtual void startResolvingService(const Service&) = 0; - virtual void stopResolvingService(const Service&) = 0; + virtual void startResolvingService(const LinkLocalServiceID&) = 0; + virtual void stopResolvingService(const LinkLocalServiceID&) = 0; virtual void resolveHostname(const String& hostname, int interfaceIndex = 0) = 0; boost::signal<void ()> onStarted; boost::signal<void (bool)> onStopped; - boost::signal<void (const Service&)> onServiceAdded; - boost::signal<void (const Service&)> onServiceRemoved; - boost::signal<void (const Service&)> onServiceRegistered; - boost::signal<void (const Service&, const ResolveResult&)> onServiceResolved; + boost::signal<void (const LinkLocalServiceID&)> onServiceAdded; + boost::signal<void (const LinkLocalServiceID&)> onServiceRemoved; + boost::signal<void (const LinkLocalServiceID&)> onServiceRegistered; + boost::signal<void (const LinkLocalServiceID&, const ResolveResult&)> onServiceResolved; boost::signal<void (const String&, const boost::optional<HostAddress>&)> onHostnameResolved; }; } diff --git a/Swiften/LinkLocal/LinkLocalRoster.cpp b/Swiften/LinkLocal/LinkLocalRoster.cpp index 25143a6..5a6ccfd 100644 --- a/Swiften/LinkLocal/LinkLocalRoster.cpp +++ b/Swiften/LinkLocal/LinkLocalRoster.cpp @@ -30,11 +30,11 @@ std::vector<boost::shared_ptr<Presence> > LinkLocalRoster::getAllPresence() cons return result; } -RosterItemPayload LinkLocalRoster::getRosterItem(const DNSSDService::Service& service, const DNSSDService::ResolveResult& resolveResult) const { +RosterItemPayload LinkLocalRoster::getRosterItem(const LinkLocalServiceID& service, const DNSSDService::ResolveResult& resolveResult) const { return RosterItemPayload(getJIDForService(service), getRosterName(service, resolveResult), RosterItemPayload::Both); } -String LinkLocalRoster::getRosterName(const DNSSDService::Service& service, const DNSSDService::ResolveResult& resolveResult) const { +String LinkLocalRoster::getRosterName(const LinkLocalServiceID& service, const DNSSDService::ResolveResult& resolveResult) const { if (!resolveResult.info.getNick().isEmpty()) { return resolveResult.info.getNick(); } @@ -48,14 +48,14 @@ String LinkLocalRoster::getRosterName(const DNSSDService::Service& service, cons else if (!resolveResult.info.getLastName().isEmpty()) { return resolveResult.info.getLastName(); } - return service.name; + return service.getName(); } -JID LinkLocalRoster::getJIDForService(const DNSSDService::Service& service) const { - return JID(service.name); +JID LinkLocalRoster::getJIDForService(const LinkLocalServiceID& service) const { + return JID(service.getName()); } -boost::shared_ptr<Presence> LinkLocalRoster::getPresence(const DNSSDService::Service& service, const DNSSDService::ResolveResult& resolveResult) const { +boost::shared_ptr<Presence> LinkLocalRoster::getPresence(const LinkLocalServiceID& service, const DNSSDService::ResolveResult& resolveResult) const { boost::shared_ptr<Presence> presence(new Presence()); presence->setFrom(getJIDForService(service)); switch (resolveResult.info.getStatus()) { @@ -73,14 +73,14 @@ boost::shared_ptr<Presence> LinkLocalRoster::getPresence(const DNSSDService::Ser return presence; } -void LinkLocalRoster::handleServiceAdded(const DNSSDService::Service& service) { +void LinkLocalRoster::handleServiceAdded(const LinkLocalServiceID& service) { if (selfService && *selfService == service) { return; } dnsSDService->startResolvingService(service); } -void LinkLocalRoster::handleServiceRemoved(const DNSSDService::Service& service) { +void LinkLocalRoster::handleServiceRemoved(const LinkLocalServiceID& service) { if (selfService && *selfService == service) { return; } @@ -91,7 +91,7 @@ void LinkLocalRoster::handleServiceRemoved(const DNSSDService::Service& service) onRosterChanged(roster); } -void LinkLocalRoster::handleServiceResolved(const DNSSDService::Service& service, const DNSSDService::ResolveResult& result) { +void LinkLocalRoster::handleServiceResolved(const LinkLocalServiceID& service, const DNSSDService::ResolveResult& result) { std::pair<ServiceMap::iterator, bool> r = services.insert(std::make_pair(service, result)); if (r.second) { boost::shared_ptr<RosterPayload> roster(new RosterPayload()); @@ -104,7 +104,7 @@ void LinkLocalRoster::handleServiceResolved(const DNSSDService::Service& service onPresenceChanged(getPresence(service, result)); } -void LinkLocalRoster::handleServiceRegistered(const DNSSDService::Service& service) { +void LinkLocalRoster::handleServiceRegistered(const LinkLocalServiceID& service) { selfService = service; } diff --git a/Swiften/LinkLocal/LinkLocalRoster.h b/Swiften/LinkLocal/LinkLocalRoster.h index c18d8fc..128e39d 100644 --- a/Swiften/LinkLocal/LinkLocalRoster.h +++ b/Swiften/LinkLocal/LinkLocalRoster.h @@ -28,21 +28,21 @@ namespace Swift { int getPort(const JID&) const; private: - RosterItemPayload getRosterItem(const DNSSDService::Service& service, const DNSSDService::ResolveResult& info) const; - String getRosterName(const DNSSDService::Service& service, const DNSSDService::ResolveResult& info) const; - JID getJIDForService(const DNSSDService::Service& service) const; - boost::shared_ptr<Presence> getPresence(const DNSSDService::Service& service, const DNSSDService::ResolveResult& info) const; + RosterItemPayload getRosterItem(const LinkLocalServiceID& service, const DNSSDService::ResolveResult& info) const; + String getRosterName(const LinkLocalServiceID& service, const DNSSDService::ResolveResult& info) const; + JID getJIDForService(const LinkLocalServiceID& service) const; + boost::shared_ptr<Presence> getPresence(const LinkLocalServiceID& service, const DNSSDService::ResolveResult& info) const; void handleStopped(bool); - void handleServiceRegistered(const DNSSDService::Service& service); - void handleServiceAdded(const DNSSDService::Service&); - void handleServiceRemoved(const DNSSDService::Service&); - void handleServiceResolved(const DNSSDService::Service& service, const DNSSDService::ResolveResult& result); + void handleServiceRegistered(const LinkLocalServiceID& service); + void handleServiceAdded(const LinkLocalServiceID&); + void handleServiceRemoved(const LinkLocalServiceID&); + void handleServiceResolved(const LinkLocalServiceID& service, const DNSSDService::ResolveResult& result); private: boost::shared_ptr<DNSSDService> dnsSDService; - boost::optional<DNSSDService::Service> selfService; - typedef std::map<DNSSDService::Service, DNSSDService::ResolveResult> ServiceMap; + boost::optional<LinkLocalServiceID> selfService; + typedef std::map<LinkLocalServiceID, DNSSDService::ResolveResult> ServiceMap; ServiceMap services; }; } diff --git a/Swiften/LinkLocal/LinkLocalServiceID.h b/Swiften/LinkLocal/LinkLocalServiceID.h new file mode 100644 index 0000000..72615d4 --- /dev/null +++ b/Swiften/LinkLocal/LinkLocalServiceID.h @@ -0,0 +1,64 @@ +#pragma once + +#include "Swiften/Base/String.h" + +namespace Swift { + class LinkLocalServiceID { + public: + LinkLocalServiceID( + const String& name, + const String& type, + const String& domain, + int networkInterface = 0) : + name(name), + type(type), + domain(domain), + networkInterface(networkInterface) { + } + + bool operator==(const LinkLocalServiceID& o) const { + return name == o.name && type == o.type && domain == o.domain && (networkInterface != 0 && o.networkInterface != 0 ? networkInterface == o.networkInterface : true); + } + + bool operator<(const LinkLocalServiceID& o) const { + if (o.name == name) { + if (o.type == type) { + if (o.domain == domain) { + return networkInterface < o.networkInterface; + } + else { + return domain < o.domain; + } + } + else { + return type < o.type; + } + } + else { + return o.name < name; + } + } + + const String& getName() const { + return name; + } + + const String& getType() const { + return type; + } + + const String& getDomain() const { + return domain; + } + + int getNetworkInterfaceID() const { + return networkInterface; + } + + private: + String name; + String type; + String domain; + int networkInterface; + }; +} |