12
12
# include < boost/network/traits/string.hpp>
13
13
# include < boost/cstdint.hpp>
14
14
# include < boost/bind.hpp>
15
+ # include < boost/network/protocol/xmpp/namespaces.hpp>
15
16
# include < boost/network/protocol/xmpp/message.hpp>
16
17
# include < boost/network/protocol/xmpp/presence.hpp>
17
18
# include < boost/network/protocol/xmpp/iq.hpp>
18
19
# include < boost/asio/io_service.hpp>
20
+ # include < boost/asio/placeholders.hpp>
19
21
# include < boost/asio/ip/tcp.hpp>
22
+ # include < deque>
20
23
21
24
22
25
namespace boost {
23
26
namespace network {
24
27
namespace xmpp {
25
28
template <
26
29
class Tag ,
30
+ unsigned version_major,
31
+ unsigned version_minor,
27
32
class Handler
28
33
>
29
34
class basic_client : boost::noncopyable {
30
35
31
36
private:
32
37
33
- typedef basic_client<Tag, Handler> this_type;
38
+ typedef basic_client<Tag, version_major, version_minor, Handler> this_type;
34
39
35
40
public:
36
41
@@ -44,28 +49,30 @@ class basic_client : boost::noncopyable {
44
49
45
50
~basic_client ();
46
51
47
- void connect (const string_type &proxy_host,
48
- const string_type &proxy_port);
52
+ void set_lang (const string_type &lang);
49
53
50
- void disconnect ();
54
+ void connect (const string_type &jid,
55
+ const string_type &password);
56
+
57
+ void run ();
51
58
52
- void authenticate (const string_type &jid,
53
- const string_type &password);
59
+ void disconnect ();
54
60
55
61
void send_message (const message_type &message);
56
62
57
63
void send_presence (const presence_type &presence);
58
64
59
65
void send_iq (const iq_type &iq);
60
66
61
- void set_jid (const string_type &jid);
62
-
63
67
string_type get_jid () const ;
64
68
65
69
private:
66
70
67
- void write_stanza (const basic_stanza<Tag> &stanza);
68
- void close_socket ();
71
+ void handle_connect (const boost::system::error_code &error,
72
+ boost::asio::ip::tcp::resolver::iterator iterator);
73
+ void handle_write_stanza (const basic_stanza<Tag> &stanza);
74
+ void handle_read_stanza (const boost::system::error_code &ec);
75
+ void handle_disconnect ();
69
76
70
77
Handler &handler_;
71
78
@@ -83,123 +90,216 @@ class basic_client : boost::noncopyable {
83
90
84
91
// xml parser
85
92
93
+ // std::deque<std::string> stanza_queue_;
94
+
86
95
};
87
96
88
97
89
98
template <
90
99
class Tag ,
100
+ unsigned version_major,
101
+ unsigned version_minor,
91
102
class Handler
92
103
>
93
- basic_client<Tag, Handler>::basic_client(Handler &handler)
104
+ basic_client<Tag, version_major, version_minor, Handler>::basic_client(Handler &handler)
94
105
: handler_(handler), socket_(io_service_) {
95
- // set the handlers
106
+
96
107
}
97
108
98
109
99
110
template <
100
111
class Tag ,
112
+ unsigned version_major,
113
+ unsigned version_minor,
114
+ class Handler
115
+ >
116
+ basic_client<Tag, version_major, version_minor, Handler>::~basic_client () {
117
+
118
+ }
119
+
120
+ template <
121
+ class Tag ,
122
+ unsigned version_major,
123
+ unsigned version_minor,
101
124
class Handler
102
125
>
103
- basic_client<Tag, Handler>::~basic_client ( ) {
126
+ void basic_client<Tag, version_major, version_minor, Handler>::set_lang( const string_type &lang ) {
104
127
105
128
}
106
129
107
130
108
131
template <
109
132
class Tag ,
133
+ unsigned version_major,
134
+ unsigned version_minor,
110
135
class Handler
111
136
>
112
- void basic_client<Tag, Handler>::connect(const string_type &proxy_host,
113
- const string_type &proxy_port) {
137
+ void basic_client<Tag, version_major, version_minor, Handler>::connect(const string_type &jid,
138
+ const string_type &password) {
139
+ using boost::asio::ip::tcp;
140
+
114
141
// get the JID domain
115
- // default port is 52222
142
+ // default port is 5222
116
143
// open socket
117
144
// socket has a state
118
145
// signal connection handler
146
+
147
+ jid_ = jid;
148
+
149
+ string_type host = " 127.0.0.1" ;
150
+ string_type port = " 5222" ;
151
+
152
+ tcp::resolver resolver (io_service_);
153
+ tcp::resolver::query query (host, port);
154
+ tcp::resolver::iterator iterator = resolver.resolve (query);
155
+ socket_.async_connect (&this_type::handle_connect,
156
+ this ,
157
+ boost::asio::placeholders::error,
158
+ iterator);
119
159
}
120
160
121
161
template <
122
162
class Tag ,
163
+ unsigned version_major,
164
+ unsigned version_minor,
123
165
class Handler
124
166
>
125
- void basic_client<Tag, Handler>::disconnect() {
126
- // close socket
127
- // signal connection handler
128
- io_service_.post (
129
- boost::bind (&this_type::close_socket, this ));
167
+ void basic_client<Tag, version_major, version_minor, Handler>::run() {
168
+ io_service_.run ();
130
169
}
131
170
132
171
template <
133
172
class Tag ,
173
+ unsigned version_major,
174
+ unsigned version_minor,
134
175
class Handler
135
176
>
136
- void basic_client<Tag, Handler>::authenticate( const string_type &jid,
137
- const string_type &password) {
138
-
177
+ void basic_client<Tag, version_major, version_minor, Handler>::disconnect() {
178
+ io_service_. post (
179
+ boost::bind (&this_type::handle_disconnect, this ));
139
180
}
140
181
141
182
template <
142
183
class Tag ,
184
+ unsigned version_major,
185
+ unsigned version_minor,
143
186
class Handler
144
187
>
145
- void basic_client<Tag, Handler>::send_message(const message_type &message) {
188
+ void basic_client<Tag, version_major, version_minor, Handler>::send_message(const message_type &message) {
146
189
io_service_.post (
147
- boost::bind (&this_type::write_stanza , this , boost::ref (message)));
190
+ boost::bind (&this_type::handle_write_stanza , this , boost::ref (message)));
148
191
}
149
192
150
193
template <
151
194
class Tag ,
195
+ unsigned version_major,
196
+ unsigned version_minor,
152
197
class Handler
153
198
>
154
- void basic_client<Tag, Handler>::send_presence(const presence_type &presence) {
199
+ void basic_client<Tag, version_major, version_minor, Handler>::send_presence(const presence_type &presence) {
155
200
io_service_.post (
156
- boost::bind (&this_type::write_stanza , this , boost::ref (presence)));
201
+ boost::bind (&this_type::handle_write_stanza , this , boost::ref (presence)));
157
202
}
158
203
159
204
template <
160
205
class Tag ,
206
+ unsigned version_major,
207
+ unsigned version_minor,
161
208
class Handler
162
209
>
163
- void basic_client<Tag, Handler>::send_iq(const iq_type &iq) {
210
+ void basic_client<Tag, version_major, version_minor, Handler>::send_iq(const iq_type &iq) {
164
211
io_service_.post (
165
- boost::bind (&this_type::write_stanza , this , boost::ref (iq)));
212
+ boost::bind (&this_type::handle_write_stanza , this , boost::ref (iq)));
166
213
}
167
214
168
215
template <
169
216
class Tag ,
217
+ unsigned version_major,
218
+ unsigned version_minor,
170
219
class Handler
171
220
>
172
- typename basic_client<Tag, Handler>::string_type
173
- basic_client<Tag, Handler>::get_jid() const {
221
+ typename basic_client<Tag, version_major, version_minor, Handler>::string_type
222
+ basic_client<Tag, version_major, version_minor, Handler>::get_jid() const {
174
223
return jid_;
175
224
}
176
225
226
+ template <
227
+ class Tag ,
228
+ unsigned version_major,
229
+ unsigned version_minor,
230
+ class Handler
231
+ >
232
+ void basic_client<Tag, version_major, version_minor, Handler>::handle_connect(
233
+ const boost::system::error_code& ec,
234
+ boost::asio::ip::tcp::resolver::iterator iterator) {
235
+ if (!ec) {
236
+
237
+ // open stream
238
+ // TODO: where does "lang" come from?
239
+ string_type s =
240
+ " <?xml version=\" %d.%d\" ?>"
241
+ " <stream:stream to=\" domain\" "
242
+ " xml:lang=\" lang\" "
243
+ " version=\" %d.%d\" "
244
+ " xmlns=\" %s\" " // xmpp_ns_client or xmpp_ns_component
245
+ " xmlns:stream=\" %s\" >" ; // xmpp_ns_Streams
246
+
247
+ // boost::asio::async_read(
248
+ // socket_,
249
+ // boost::asio::buffer(read_msg_.data(), chat_message::header_length),
250
+ // boost::bind(&chat_client::handle_read_header, this,
251
+ // boost::asio::placeholders::error));
252
+ }
253
+ else if (iterator != boost::asio::ip::tcp::resolver::iterator ()) {
254
+ socket_.close ();
255
+ boost::asio::ip::tcp::endpoint endpoint = *iterator;
256
+ socket_.async_connect (
257
+ endpoint,
258
+ boost::bind (&this_type::handle_connect,
259
+ this ,
260
+ boost::asio::placeholders::error,
261
+ ++iterator));
262
+ }
263
+ else {
264
+ // unable to connect
265
+ }
266
+ }
177
267
178
268
template <
179
269
class Tag ,
270
+ unsigned version_major,
271
+ unsigned version_minor,
180
272
class Handler
181
273
>
182
- void basic_client<Tag, Handler>::write_stanza(const basic_stanza<Tag> &stanza) {
183
-
274
+ void basic_client<Tag, version_major, version_minor, Handler>::handle_write_stanza(const basic_stanza<Tag> &stanza) {
275
+ // stanza ==> string
276
+ // socket_.async_write(socket_,
277
+ // boost::asio::buffer(stanza.string()),
278
+ // boost::bind(&this_type::handle_write...))
184
279
}
185
280
186
281
187
282
template <
188
283
class Tag ,
284
+ unsigned version_major,
285
+ unsigned version_minor,
189
286
class Handler
190
287
>
191
- void basic_client<Tag, Handler>::close_socket() {
288
+ void basic_client<Tag, version_major, version_minor, Handler>::handle_disconnect() {
289
+ // close stream
290
+ string_type s = " </stream:stream>" ;
192
291
socket_.close ();
292
+ // handler_.disconnected();
193
293
}
194
294
195
295
196
296
template <
197
297
class Handler
198
298
>
199
- struct client : basic_client<tags::default_, Handler> {
299
+ struct client : basic_client<tags::default_, 1 , 0 , Handler> {
200
300
201
301
explicit client (Handler &handler)
202
- : basic_client<tags::default_, Handler>(handler) {
302
+ : basic_client<tags::default_, 1, 0, Handler>(handler) {
203
303
204
304
}
205
305
0 commit comments