glooxd  0.3-svn
featurecompression.cpp
1 /*
2  Copyright (c) 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 #include "connectioncompression.h"
15 #include "featurecompression.h"
16 #include "streambase.h"
17 
18 #include <gloox/gloox.h>
19 #include <gloox/tag.h>
20 
21 namespace glooxd
22 {
23 
25  : m_state( ZlibCanceled )
26  {
27  }
28 
29  const std::string& FeatureCompression::filterString() const
30  {
31  static const std::string filter = "/compress[@xmlns='" + gloox::XMLNS_COMPRESSION + "']";
32  return filter;
33  }
34 
35  gloox::Tag* FeatureCompression::tag( int state, const std::string& /*domain*/ )
36  {
37  gloox::Tag* t = 0;
38  if( ( state & StateTLS ) == StateTLS
39  && ( state & StateAuthentication ) != StateAuthentication
40  && ( state & StateCompression ) != StateCompression )
41  {
42  t = new gloox::Tag( "compression" );
43  t->setXmlns( gloox::XMLNS_STREAM_COMPRESS );
44  new gloox::Tag( t, "method", "zlib" );
45 
46  m_state = ZlibOffered;
47  }
48 
49  return t;
50  }
51 
52  void FeatureCompression::handleTag( gloox::Tag* _tag )
53  {
54  if( !_tag || _tag->xmlns() != gloox::XMLNS_COMPRESSION )
55  return;
56 
57  const std::string& name = _tag->name();
58 
59  if( _tag->findCData( "/compress/method" ) == "zlib" )
60  {
61  gloox::Tag* c = new gloox::Tag( "compressed" );
62  c->setXmlns( gloox::XMLNS_COMPRESSION );
63  m_parent->send( c );
64 
65  ConnectionCompression* compression = new ConnectionCompression( m_parent,
66  m_parent->connection(), m_parent->logInstance() );
67  compression->connect();
68  m_parent->finishStreamFeature( StateReset, compression );
70  m_state = ZlibEnabled;
71  }
72  else if( name == "failure" )
73  {
74  m_state = ZlibCanceled;
75  m_parent->disconnect( gloox::ConnCompressionFailed );
76  }
77  }
78 
79 }