~ubuntu-branches/ubuntu/vivid/gloox/vivid-proposed

« back to all changes in this revision

Viewing changes to src/examples/reconnect_example.cpp

  • Committer: Package Import Robot
  • Author(s): Vincent Cheng
  • Date: 2014-03-16 17:34:43 UTC
  • mfrom: (12.1.2 experimental)
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: package-import@ubuntu.com-20140316173443-4s177dovzaz5dm8o
Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#define CLIENT_TEST
 
2
#define CLIENTBASE_TEST
 
3
#include "../client.h"
 
4
#include "../messagesessionhandler.h"
 
5
#include "../messageeventhandler.h"
 
6
#include "../messageeventfilter.h"
 
7
#include "../chatstatehandler.h"
 
8
#include "../chatstatefilter.h"
 
9
#include "../connectionlistener.h"
 
10
#include "../disco.h"
 
11
#include "../message.h"
 
12
#include "../gloox.h"
 
13
#include "../lastactivity.h"
 
14
#include "../loghandler.h"
 
15
#include "../logsink.h"
 
16
#include "../connectiontcpclient.h"
 
17
#include "../connectionsocks5proxy.h"
 
18
#include "../connectionhttpproxy.h"
 
19
#include "../messagehandler.h"
 
20
using namespace gloox;
 
21
 
 
22
#ifndef _WIN32
 
23
# include <unistd.h>
 
24
#endif
 
25
 
 
26
#include <stdio.h>
 
27
#include <string>
 
28
 
 
29
#include <cstdio> // [s]print[f]
 
30
 
 
31
#if defined( WIN32 ) || defined( _WIN32 )
 
32
# include <windows.h>
 
33
#endif
 
34
 
 
35
class MessageTest : public MessageSessionHandler, ConnectionListener, LogHandler,
 
36
                    MessageEventHandler, MessageHandler, ChatStateHandler
 
37
{
 
38
  public:
 
39
    MessageTest() : m_session( 0 ), m_reconnect( false ) {}
 
40
 
 
41
    virtual ~MessageTest() {}
 
42
 
 
43
    void start()
 
44
    {
 
45
 
 
46
      JID jid( "admin@jabba.us/gloox" );
 
47
      j = new Client( jid, "test" );
 
48
      j->registerConnectionListener( this );
 
49
      j->registerMessageSessionHandler( this, 0 );
 
50
      j->disco()->setVersion( "reconnectTest", GLOOX_VERSION, "Linux" );
 
51
      j->disco()->setIdentity( "client", "bot" );
 
52
      j->disco()->addFeature( XMLNS_CHAT_STATES );
 
53
//       j->setTls( TLSDisabled );
 
54
      j->setCompression( false );
 
55
      j->setStreamManagement( true, true );
 
56
      StringList ca;
 
57
      ca.push_back( "/path/to/cacert.crt" );
 
58
      j->setCACerts( ca );
 
59
 
 
60
      j->logInstance().registerLogHandler( LogLevelDebug, LogAreaAll, this );
 
61
 
 
62
//
 
63
// this code connects to a jabber server through a SOCKS5 proxy
 
64
//
 
65
//       ConnectionSOCKS5Proxy* conn = new ConnectionSOCKS5Proxy( j,
 
66
//                                   new ConnectionTCP( j->logInstance(),
 
67
//                                                      "sockshost", 1080 ),
 
68
//                                   j->logInstance(), "example.net" );
 
69
//       conn->setProxyAuth( "socksuser", "sockspwd" );
 
70
//       j->setConnectionImpl( conn );
 
71
 
 
72
//
 
73
// this code connects to a jabber server through a HTTP proxy through a SOCKS5 proxy
 
74
//
 
75
//       ConnectionTCP* conn0 = new ConnectionTCP( j->logInstance(), "old", 1080 );
 
76
//       ConnectionSOCKS5Proxy* conn1 = new ConnectionSOCKS5Proxy( conn0, j->logInstance(), "old", 8080 );
 
77
//       conn1->setProxyAuth( "socksuser", "sockspwd" );
 
78
//       ConnectionHTTPProxy* conn2 = new ConnectionHTTPProxy( j, conn1, j->logInstance(), "jabber.cc" );
 
79
//       conn2->setProxyAuth( "httpuser", "httppwd" );
 
80
//       j->setConnectionImpl( conn2 );
 
81
 
 
82
 
 
83
      ConnectionError ce = ConnNoError;
 
84
      if( j->connect( false ) )
 
85
      {
 
86
        while( ce == ConnNoError )
 
87
        {
 
88
          ce = j->recv();
 
89
        }
 
90
        printf( "ce: %d\n", ce );
 
91
      }
 
92
 
 
93
      m_reconnect = true;
 
94
      ce = ConnNoError;
 
95
      if( j->connect( false ) )
 
96
      {
 
97
        while( ce == ConnNoError )
 
98
        {
 
99
          ce = j->recv();
 
100
        }
 
101
        printf( "ce: %d\n", ce );
 
102
      }
 
103
 
 
104
      delete( j );
 
105
    }
 
106
 
 
107
    virtual void onConnect()
 
108
    {
 
109
      printf( "connected!!!\n" );
 
110
    }
 
111
 
 
112
    virtual void onDisconnect( ConnectionError e )
 
113
    {
 
114
      printf( "message_test: disconnected: %d\n", e );
 
115
      if( e == ConnAuthenticationFailed )
 
116
        printf( "auth failed. reason: %d\n", j->authError() );
 
117
    }
 
118
 
 
119
    virtual bool onTLSConnect( const CertInfo& info )
 
120
    {
 
121
      time_t from( info.date_from );
 
122
      time_t to( info.date_to );
 
123
 
 
124
      printf( "status: %d\nissuer: %s\npeer: %s\nprotocol: %s\nmac: %s\ncipher: %s\ncompression: %s\n"
 
125
              "from: %s\nto: %s\n",
 
126
              info.status, info.issuer.c_str(), info.server.c_str(),
 
127
              info.protocol.c_str(), info.mac.c_str(), info.cipher.c_str(),
 
128
              info.compression.c_str(), ctime( &from ), ctime( &to ) );
 
129
      return true;
 
130
    }
 
131
 
 
132
    virtual void handleMessage( const Message& msg, MessageSession * /*session*/ )
 
133
    {
 
134
      printf( "type: %d, subject: %s, message: %s, thread id: %s\n", msg.subtype(),
 
135
              msg.subject().c_str(), msg.body().c_str(), msg.thread().c_str() );
 
136
 
 
137
 
 
138
      if( msg.body().substr( 0, 3 ) == "ack" ) // using substr() to work around some stupid clients
 
139
        j->ackStreamManagement();
 
140
      else if( msg.body().substr( 0, 3 ) == "req" )
 
141
        j->reqStreamManagement();
 
142
      else if( msg.body().substr( 0, 4 ) == "quit" )
 
143
        j->disconnect();
 
144
      else
 
145
      {
 
146
        std::string re = "You said:\n> " + msg.body() + "\nI like that statement.";
 
147
        m_session->send( re, gloox::EmptyString );
 
148
      }
 
149
    }
 
150
 
 
151
    virtual void handleMessageEvent( const JID& from, MessageEventType event )
 
152
    {
 
153
      printf( "received event: %d from: %s\n", event, from.full().c_str() );
 
154
    }
 
155
 
 
156
    virtual void handleChatState( const JID& from, ChatStateType state )
 
157
    {
 
158
      printf( "received state: %d from: %s\n", state, from.full().c_str() );
 
159
    }
 
160
 
 
161
    virtual void handleMessageSession( MessageSession *session )
 
162
    {
 
163
      printf( "got new session\n");
 
164
      // this example can handle only one session. so we get rid of the old session
 
165
      j->disposeMessageSession( m_session );
 
166
      m_session = session;
 
167
      m_session->registerMessageHandler( this );
 
168
    }
 
169
 
 
170
    virtual void handleLog( LogLevel level, LogArea area, const std::string& message )
 
171
    {
 
172
      printf("log: level: %d, area: %d, %s\n", level, area, message.c_str() );
 
173
 
 
174
      if( message.substr( 0, 10 ) == "<stream:er" )
 
175
        printf( "something's foul\n" );
 
176
 
 
177
      if( !m_reconnect && j->m_smHandled > 10 )
 
178
        j->disconnect( ConnTlsFailed ); // fake disconnect reason so that no </stream:stream> is sent
 
179
    }
 
180
 
 
181
  private:
 
182
    Client *j;
 
183
    MessageSession *m_session;
 
184
    bool m_reconnect;
 
185
};
 
186
 
 
187
int main( int /*argc*/, char** /*argv*/ )
 
188
{
 
189
  MessageTest *r = new MessageTest();
 
190
  r->start();
 
191
  delete( r );
 
192
  return 0;
 
193
}