gloox  1.0.1
connectionbosh.h
1 /*
2  * Copyright (c) 2007-2012 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 CONNECTIONBOSH_H__
14 #define CONNECTIONBOSH_H__
15 
16 #include "gloox.h"
17 #include "connectionbase.h"
18 #include "logsink.h"
19 #include "taghandler.h"
20 #include "parser.h"
21 
22 #include <string>
23 #include <list>
24 #include <ctime>
25 
26 namespace gloox
27 {
28 
65  {
66  public:
82  ConnectionBOSH( ConnectionBase* connection, const LogSink& logInstance, const std::string& boshHost,
83  const std::string& xmppServer, int xmppPort = 5222 );
84 
99  const LogSink& logInstance, const std::string& boshHost,
100  const std::string& xmppServer, int xmppPort = 5222 );
101 
105  virtual ~ConnectionBOSH();
106 
110  enum ConnMode
111  {
114  ModePipelining
115  };
116 
122  void setServer( const std::string& xmppHost, unsigned short xmppPort = 5222 )
123  { m_server = xmppHost; m_port = xmppPort; }
124 
130  void setPath( const std::string& path ) { m_path = path; }
131 
139  void setMode( ConnMode mode ) { m_connMode = mode; }
140 
141  // reimplemented from ConnectionBase
142  virtual ConnectionError connect();
143 
144  // reimplemented from ConnectionBase
145  virtual ConnectionError recv( int timeout = -1 );
146 
147  // reimplemented from ConnectionBase
148  virtual bool send( const std::string& data );
149 
150  // reimplemented from ConnectionBase
151  virtual ConnectionError receive();
152 
153  // reimplemented from ConnectionBase
154  virtual void disconnect();
155 
156  // reimplemented from ConnectionBase
157  virtual void cleanup();
158 
159  // reimplemented from ConnectionBase
160  virtual void getStatistics( long int& totalIn, long int& totalOut );
161 
162  // reimplemented from ConnectionDataHandler
163  virtual void handleReceivedData( const ConnectionBase* connection, const std::string& data );
164 
165  // reimplemented from ConnectionDataHandler
166  virtual void handleConnect( const ConnectionBase* connection );
167 
168  // reimplemented from ConnectionDataHandler
169  virtual void handleDisconnect( const ConnectionBase* connection, ConnectionError reason );
170 
171  // reimplemented from ConnectionDataHandler
172  virtual ConnectionBase* newInstance() const;
173 
174  // reimplemented from TagHandler
175  virtual void handleTag( Tag* tag );
176 
177  private:
178  ConnectionBOSH& operator=( const ConnectionBOSH& );
179  void initInstance( ConnectionBase* connection, const std::string& xmppServer, const int xmppPort );
180  bool sendRequest( const std::string& xml );
181  bool sendXML();
182  const std::string getHTTPField( const std::string& field );
183  ConnectionBase* getConnection();
184  ConnectionBase* activateConnection();
185  void putConnection();
186 
187  //ConnectionBase *m_connection;
188  const LogSink& m_logInstance;
189 
190  Parser m_parser; // Used for parsing XML section of responses
191  std::string m_boshHost; // The hostname of the BOSH connection manager
192  std::string m_boshedHost; // The hostname of the BOSH connection manager + : + port
193  std::string m_path; // The path part of the URL that we need to request
194 
195  // BOSH parameters
196  unsigned long m_rid;
197  std::string m_sid;
198 
199  bool m_initialStreamSent;
200  int m_openRequests;
201  int m_maxOpenRequests;
202  int m_wait;
203  int m_hold;
204 
205  bool m_streamRestart; // Set to true if we are waiting for an acknowledgement of a stream restart
206 
207  time_t m_lastRequestTime;
208  unsigned long m_minTimePerRequest;
209 
210  std::string m_buffer; // Buffer of received data
211  std::string m_bufferHeader; // HTTP header of data currently in buffer // FIXME doens't need to be member
212  std::string::size_type m_bufferContentLength; // Length of the data in the current response
213 
214  std::string m_sendBuffer; // Data waiting to be sent
215 
216  typedef std::list<ConnectionBase*> ConnectionList;
217  ConnectionList m_activeConnections;
218  ConnectionList m_connectionPool;
219  ConnMode m_connMode;
220 
221  };
222 
223 }
224 
225 #endif // CONNECTIONBOSH_H__