gloox  1.0.9
linklocalmanager.h
1 /*
2  Copyright (c) 2012-2013 by Jakob Schroeter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 #ifndef LINKLOCALMANAGER_H___
14 #define LINKLOCALMANAGER_H___
15 
16 #include "config.h"
17 
18 #ifdef HAVE_MDNS
19 
20 #include "linklocal.h"
21 #include "macros.h"
22 #include "gloox.h"
23 #include "util.h"
24 #include "logsink.h"
25 #include "connectiontcpserver.h"
26 #include "mutex.h"
27 #include "jid.h"
28 
29 #include <string>
30 
31 #include <dns_sd.h>
32 
33 namespace gloox
34 {
35 
36  class ConnectionHandler;
37  class ConnectionTCPClient;
38 
39  namespace LinkLocal
40  {
41 
42  class Handler;
43 
167  class GLOOX_API Manager
168  {
169 
170  public:
171 
180  Manager( const std::string& user, ConnectionHandler* connHandler, const LogSink &logInstance );
181 
186  virtual ~Manager();
187 
199  void addTXTData( const std::string& key, const std::string& value );
200 
208  void removeTXTData( const std::string& key );
209 
215  void registerService();
216 
221  void deregisterService();
222 
229  void setUser( const std::string& user ) { m_user = user; }
230 
238  void setHost( const std::string& host ) { m_host = host; }
239 
247  void setDomain( const std::string& domain ) { m_domain = domain; }
248 
259  void setPort( const int port ) { m_port = port; addTXTData( "port.p2pj", util::int2string( m_port ) ); }
260 
272  void setInterface( unsigned iface ) { m_interface = iface; }
273 
280  bool startBrowsing();
281 
285  void stopBrowsing();
286 
294  int socket() const { return m_browseFd; }
295 
301  void recv( int timeout );
302 
308  void registerLinkLocalHandler( Handler* handler ) { m_linkLocalHandler = handler; }
309 
310 // /**
311 // *
312 // */
313 // static const StringMap decodeTXT( const std::string& txt );
314 
315  private:
316  static void handleBrowseReply( DNSServiceRef sdRef, DNSServiceFlags flags, unsigned interfaceIndex,
317  DNSServiceErrorType errorCode, const char* serviceName, const char* regtype,
318  const char* replyDomain, void* context );
319 
320  void handleBrowse( Flag flag, const std::string& service, const std::string& regtype, const std::string& domain, int interface, bool moreComing );
321 
322  typedef std::list<ConnectionTCPClient*> ConnectionList;
323  typedef std::map<ConnectionTCPClient*, const JID> ConnectionMap;
324 
325  DNSServiceRef m_publishRef;
326  DNSServiceRef m_browseRef;
327 
328  ServiceList m_tmpServices;
329 // ServiceList m_services;
330 
331  std::string m_user;
332  std::string m_host;
333  std::string m_domain;
334  unsigned m_interface;
335  int m_port;
336 
337  const LogSink& m_logInstance;
338 
339  int m_browseFd;
340 
341  StringMap m_txtData;
342 
343  ConnectionTCPServer m_server;
344 
345  Handler* m_linkLocalHandler;
346  ConnectionHandler* m_connectionHandler;
347 
348  };
349 
350  }
351 
352 }
353 
354 #endif // HAVE_MDNS
355 
356 #endif // LINKLOCALMANAGER_H___