glooxd  0.3-svn
router.h
1 /*
2  Copyright (c) 2008-2009 by Jakob Schroeter <js@camaya.net>
3  This file is part of the glooxd library. http://camaya.net/glooxd
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 
14 #ifndef ROUTER_H__
15 #define ROUTER_H__
16 
17 #include "confighandler.h"
18 #include "glooxd.h"
19 #include "semafore.h"
20 #include "taghandler.h"
21 
22 #include <gloox/mutex.h>
23 #include <gloox/mutexguard.h>
24 #include <gloox/tag.h>
25 
26 #include <string>
27 
28 namespace gloox
29 {
30  class LogSink;
31 }
32 
33 namespace glooxd
34 {
35 
36  class ConfigManager;
37 
43  class GLOOXD_API Router : public TagHandler, public ConfigHandler
44  {
45  public:
52  Router( ConfigManager& cm, const gloox::LogSink& logInstance, bool threading = false );
53 
57  ~Router();
58 
63  void run();
64 
73  void registerDomain( TagHandler* th, const std::string& domain, bool node );
74 
77  void removeDomain( TagHandler* th, const std::string& domain, bool node );
78 
79  // reimplemented from TagHandler
80  virtual void handleIncomingTag( gloox::Tag* tag );
81 
82  // reimplemented from TagHandler
83  virtual void handleOutgoingTag( gloox::Tag* ) {}
84 
85  // reimplemented from ConfigHandler
86  virtual void handleDomainAdded( const std::string& ) {}
87 
88  // reimplemented from ConfigHandler
89  virtual void handleDomainRemoved( const std::string& ) {}
90 
91  // reimplemented from ConfigHandler
92  virtual void handleShutdown();
93 
94  private:
95  void handleTag( gloox::Tag* tag );
96  void routeBySender( gloox::Tag* tag );
97  void routeByReceipient( gloox::Tag* tag );
98  bool isLocal( const std::string& domain );
99 
100  typedef std::map<std::string, TagHandler*> RoutingTable;
101  RoutingTable m_domainRouting;
102  RoutingTable m_userRouting;
103 
104  ConfigManager& m_cm;
105 
106  const gloox::LogSink& m_logInstance;
107  gloox::util::Mutex m_domainRoutingMutex;
108  gloox::util::Mutex m_userRoutingMutex;
109 
110  gloox::TagList m_queue;
111  glooxd::util::Semaphore m_queueSemaphore;
112  gloox::util::Mutex m_queueMutex;
113 
114  volatile bool m_threading;
115 
116  };
117 
118 }
119 
120 #endif // ROUTER_H__