glooxd  0.3-svn
connectioncompression.cpp
1 /*
2  * Copyright (c) 2009 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 #include "connectioncompression.h"
14 
15 #include <gloox/compressiondefault.h>
16 #include <gloox/compressionbase.h>
17 #include <gloox/gloox.h>
18 #include <gloox/logsink.h>
19 
20 namespace glooxd
21 {
22 
23  ConnectionCompression::ConnectionCompression( gloox::ConnectionDataHandler* cdh, gloox::ConnectionBase* conn,
24  const gloox::LogSink& log )
25  : gloox::ConnectionBase( cdh ),
26  m_connection( conn ), m_compression( 0 ), m_log( log )
27  {
28  if( m_connection )
29  m_connection->registerConnectionDataHandler( this );
30  }
31 
32  ConnectionCompression::ConnectionCompression( gloox::ConnectionBase* conn, const gloox::LogSink& log )
33  : gloox::ConnectionBase( 0 ),
34  m_connection( conn ), m_compression( 0 ), m_log( log )
35  {
36  if( m_connection )
37  m_connection->registerConnectionDataHandler( this );
38  }
39 
41  {
42  delete m_connection;
43  delete m_compression;
44  }
45 
46  void ConnectionCompression::setConnectionImpl( gloox::ConnectionBase* connection )
47  {
48  if( m_connection )
49  m_connection->registerConnectionDataHandler( 0 );
50 
51  m_connection = connection;
52 
53  if( m_connection )
54  m_connection->registerConnectionDataHandler( this );
55  }
56 
57  gloox::ConnectionError ConnectionCompression::connect()
58  {
59  if( !m_connection )
60  return gloox::ConnNotConnected;
61 
62  if( m_state == gloox::StateConnected )
63  return gloox::ConnNoError;
64 
65  if( !m_compression )
66  m_compression = new gloox::CompressionDefault( this );
67 
68  if( !m_compression )
69  return gloox::ConnCompressionFailed;
70 
71  if( !m_compression->init() )
72  return gloox::ConnCompressionFailed;
73 
74  m_state = gloox::StateConnected;
75 
76  if( m_connection->state() != gloox::StateConnected )
77  return m_connection->connect();
78 
79  return gloox::ConnNoError;
80  }
81 
82  gloox::ConnectionError ConnectionCompression::recv( int timeout )
83  {
84  if( m_connection->state() == gloox::StateConnected )
85  return m_connection->recv( timeout );
86 
87  return gloox::ConnNotConnected;
88  }
89 
90  bool ConnectionCompression::send( const std::string& data )
91  {
92  if( !m_compression )
93  return false;
94 
95  m_compression->compress( data );
96  return true;
97  }
98 
99  gloox::ConnectionError ConnectionCompression::receive()
100  {
101  if( m_connection )
102  return m_connection->receive();
103  else
104  return gloox::ConnNotConnected;
105  }
106 
107  void ConnectionCompression::disconnect()
108  {
109  if( m_connection )
110  m_connection->disconnect();
111 
112  cleanup();
113  }
114 
115  void ConnectionCompression::cleanup()
116  {
117  if( m_connection )
118  m_connection->cleanup();
119  if( m_compression )
120  m_compression->cleanup();
121  delete m_compression;
122  m_compression = 0;
123  m_state = gloox::StateDisconnected;
124  }
125 
126  void ConnectionCompression::getStatistics( long int& totalIn, long int& totalOut )
127  {
128  if( m_connection )
129  m_connection->getStatistics( totalIn, totalOut );
130  }
131 
132  gloox::ConnectionBase* ConnectionCompression::newInstance() const
133  {
134  gloox::ConnectionBase* newConn = 0;
135  if( m_connection )
136  newConn = m_connection->newInstance();
137  return new ConnectionCompression( m_handler, newConn, m_log );
138  }
139 
140  void ConnectionCompression::handleReceivedData( const gloox::ConnectionBase* /*connection*/,
141  const std::string& data )
142  {
143  if( m_compression )
144  m_compression->decompress( data );
145  }
146 
147  void ConnectionCompression::handleConnect( const gloox::ConnectionBase* /*connection*/ )
148  {
149  if( m_handler )
150  m_handler->handleConnect( this );
151  }
152 
153  void ConnectionCompression::handleDisconnect( const gloox::ConnectionBase* /*connection*/, gloox::ConnectionError reason )
154  {
155  if( m_handler )
156  m_handler->handleDisconnect( this, reason );
157 
158  cleanup();
159  }
160 
161  void ConnectionCompression::handleCompressedData( const std::string& data )
162  {
163  if( m_connection )
164  m_connection->send( data );
165  }
166 
167  void ConnectionCompression::handleDecompressedData( const std::string& data )
168  {
169  if( m_handler )
170  m_handler->handleReceivedData( this, data );
171  }
172 
173 }