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