glooxd  0.3-svn
c2s.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 C2S_H__
15 #define C2S_H__
16 
17 #include "componentbase.h"
18 #include "glooxd.h"
19 #include "semafore.h"
20 
21 #include <gloox/connectionhandler.h>
22 #include <gloox/gloox.h>
23 #include <gloox/mutex.h>
24 
25 #include <string>
26 #include <list>
27 
28 namespace gloox
29 {
30  class ConnectionBase;
31  class ConnectionTCPServer;
32  class JID;
33 }
34 
35 namespace glooxd
36 {
37 
38  class ConfigManager;
39  class Router;
40  class StreamBase;
41  class SM;
42 
52  class GLOOXD_API C2S : public gloox::ConnectionHandler, ComponentBase
53  {
54 
55  friend class StreamBase;
56 
57  public:
69  C2S( Router& router, ConfigManager& cm, const gloox::LogSink& logInstance,
70  SM& sm, bool threading = false,
71  const gloox::JID& name = gloox::JID( "c2s." + GLOOXD_DOMAIN )
72  );
73 
77  virtual ~C2S();
78 
83  void run();
84 
93  void registerStreamFeature( StreamFeature* feature )
94  {
95  m_features.push_back( feature );
96  }
97 
104  void setStrictXMPP( bool enable ) { m_strictXMPP = enable; }
105 
116  void setSupportRecipientUnavailable( bool value )
117  {
118  m_supportRecipientUnavailable = value;
119  }
120 
128  bool supportRecipientUnavailable() const
129  {
130  return m_supportRecipientUnavailable;
131  }
132 
133  // reimplemented from gloox::ConnectionHandler
134  virtual void handleIncomingConnection( gloox::ConnectionBase* server,
135  gloox::ConnectionBase* connection );
136 
137  // reimplemented from TagHandler
138  virtual void handleIncomingTag( gloox::Tag* tag );
139 
140  // reimplemented from TagHandler
141  virtual void handleOutgoingTag( gloox::Tag* tag );
142 
143  // reimplemented from ConfigHandler
144  virtual bool handleC2SInterfaceAdded( const std::string& interface,
145  int port );
146 
147  // reimplemented from ConfigHandler
148  virtual bool handleC2SInterfaceRemoved( const std::string& interface,
149  int port );
150  // reimplemented from ConfigHandler
151  virtual void handleDomainAdded( const std::string& domain );
152 
153  // reimplemented from ConfigHandler
154  virtual void handleDomainRemoved( const std::string& domain );
155 
156  // reimplemented from ConfigHandler
157  virtual void handleShutdown();
158 
159  private:
165  void disconnect( StreamBase* client );
166 
172  gloox::ConnectionError recv( int timeout = -1 );
173 
179  gloox::ConnectionError receive();
180 
181  void handleTag( gloox::Tag* tag );
182 
183  typedef std::list<gloox::ConnectionTCPServer*> ServerList;
184  typedef std::list<StreamBase*> StreamBaseList;
185 
186  ServerList m_servers;
187  StreamBaseList m_clients;
188  StreamBaseList m_obsoleteClients;
189  FeatureList m_features;
190 
191  gloox::util::Mutex m_clientMutex;
192  gloox::util::Mutex m_obsoleteClientMutex;
193  gloox::util::Mutex m_serverMutex;
194 
195  SM& m_sm;
196 
197  gloox::TagList m_queue;
198  glooxd::util::Semaphore m_queueSemaphore;
199  gloox::util::Mutex m_queueMutex;
200 
201  volatile bool m_threading;
202  bool m_strictXMPP;
203  bool m_running;
204  bool m_supportRecipientUnavailable;
205 
206  };
207 }
208 
209 #endif // C2S_H__