glooxd  0.3-svn
streambase.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 STREAMBASE_H__
15 #define STREAMBASE_H__
16 
17 #include "glooxd.h"
18 #include "semafore.h"
19 
20 #include <gloox/connectiondatahandler.h>
21 #include <gloox/jid.h>
22 #include <gloox/gloox.h>
23 #include <gloox/mutex.h>
24 #include <gloox/parser.h>
25 #include <gloox/taghandler.h>
26 
27 namespace gloox
28 {
29  class ConnectionBase;
30  class IQ;
31  class LogSink;
32  class TLSBase;
33 }
34 
35 namespace glooxd
36 {
37 
38  class AuthenticationHandler;
39  class C2S;
40  class ConfigManager;
41 
47  class GLOOXD_API StreamBase : public gloox::ConnectionDataHandler, gloox::TagHandler
48  {
49  public:
59  StreamBase( C2S& parent, ConfigManager& cm, gloox::ConnectionBase* connection,
60  const gloox::LogSink& logInstance );
61 
65  virtual ~StreamBase();
66 
72  void run();
73 
78  void setFeatures( const FeatureList& features ) { m_features = features; }
79 
85  gloox::ConnectionError recv( int timeout = -1 );
86 
91  void send( gloox::Tag* tag );
92 
97  void send( const std::string& xml );
98 
103  void send( const gloox::IQ& iq );
104 
112  void disconnect( gloox::ConnectionError e,
113  gloox::StreamError se = gloox::StreamErrorUndefined,
114  const std::string& text = gloox::EmptyString );
115 
120  gloox::ConnectionBase* connection() { return m_connection; }
121 
126  const gloox::LogSink& logInstance() { return m_logInstance; }
127 
132  const gloox::JID& jid() const { return m_self; }
133 
141  void setServer( const std::string& server )
142  {
143  m_self.setServer( server );
144  }
145 
151  void setUsername( const std::string& username )
152  {
153  m_self.setUsername( username );
154  }
155 
161  void setResource( const std::string& resource )
162  {
163  m_self.setResource( resource );
164  }
165 
172  void setStrictXMPP( bool enable ) { m_strictXMPP = enable; }
173 
185  void finishStreamFeature( StreamState state,
186  gloox::ConnectionBase* conn = 0 );
187 
192  void routeTag( gloox::Tag* tag );
193 
194 
200  const gloox::JID name() const;
201 
206  const std::string& id() const { return m_id; }
207 
214  void handleIncomingTag( gloox::Tag* tag );
215 
223  void handleOutgoingTag( gloox::Tag* tag );
224 
225 
226  // re-implemented from gloox::ConnectionDataHandler
227  virtual void handleReceivedData( const gloox::ConnectionBase* connection,
228  const std::string& data );
229 
230  // re-implemented from gloox::ConnectionDataHandler
231  virtual void handleConnect( const gloox::ConnectionBase* connection );
232 
233  // re-implemented from gloox::ConnectionDataHandler
234  virtual void handleDisconnect( const gloox::ConnectionBase* connection,
235  gloox::ConnectionError reason );
236 
237  // re-implemented from gloox::TagHandler
238  virtual void handleTag( gloox::Tag* tag );
239 
240  protected:
241  void sendStreamFeatures( const std::string& domain );
242  void handleStreamGreeting( const gloox::Tag* tag );
243  bool handlesDomain( const std::string& domain );
248  void stampTag( gloox::Tag* tag );
249 
250  C2S& m_parent;
251  ConfigManager& m_cm;
252 
253  gloox::ConnectionBase* m_connection;
254  const gloox::LogSink& m_logInstance;
255  gloox::Parser m_parser;
256 
257  FeatureList m_features;
258 
259  StreamFeature* m_exclusiveHandler;
260  gloox::StringList m_domains;
261 
262  std::string m_id;
263 
264  int m_state;
265 
266  gloox::JID m_self;
267 
268  gloox::TagList m_incomingQueue;
269  gloox::TagList m_outgoingQueue;
270  gloox::util::Mutex m_incomingQueueMutex;
271  gloox::util::Mutex m_outgoingQueueMutex;
272  glooxd::util::Semaphore m_queueSemaphore;
273 
274  volatile bool m_threading;
275  bool m_fakePresence;
276  bool m_strictXMPP;
277 // bool m_cancel;
278 
279  };
280 
281 }
282 
283 #endif // STREAMBASE_H__