1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#![allow(unused_must_use)]
use futures::future::poll_fn;
use futures::future::PollFn;
use futures::future::{loop_fn, LoopFn, Loop};
use futures::{Async,Poll, Sink, StartSend};
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::MutexGuard;
use tokio_io::{AsyncRead};
use futures::AsyncSink;
use std::io::{self};
use futures::Future;
use futures::future;
use xmpp_proto::transport::XMPPTransport;
use xmpp_proto::codec::XMPPCodec;
use tokio_core::net::TcpStream;
use tokio_tls::TlsConnectorExt;
use native_tls::TlsConnector;
use xmpp_proto::connection::Connection;
use xmpp_proto::stream::XMPPStream;
use xmpp_proto::config::XMPPConfig;
use futures::Stream;
use xmpp_proto::credentials::Credentials;
use xmpp_proto::events::Event;
use futures::sync::oneshot;
use futures::sync::oneshot::Receiver;
#[derive(Clone)]
pub struct Client {
transport: Arc<Mutex<XMPPTransport>>,
}
impl Client {
pub fn connect(stream: TcpStream, config: XMPPConfig, credentials: Option<Credentials>) -> Box<Future<Item=Client, Error=io::Error>> {
let connection = Connection::new(&config, credentials);
Box::new(XMPPTransport::connect(XMPPStream::Tcp(stream.framed(XMPPCodec)), connection)
.and_then(move |transport| {
let builder = TlsConnector::builder().unwrap();
let cx = builder.build().unwrap();
let connection = transport.connection;
let stream = match transport.stream {
XMPPStream::Tcp(stream) => stream.into_inner(),
XMPPStream::Tls(_) => panic!("")
};
cx.connect_async(config.get_domain(), stream).map_err(|e| {
io::Error::new(io::ErrorKind::Other, e)
}).map(|socket| (connection, socket))
})
.and_then(|(connection, s)| {
XMPPTransport::connect(XMPPStream::Tls(s.framed(XMPPCodec)), connection)
}).and_then(|transport| {
let client = Client {
transport: Arc::new(Mutex::new(transport)),
};
if let Ok(mut transport) = client.transport.lock() {
transport.handle_frames();
}
future::ok(client)
}))
}
pub fn send_presence(&self) -> Box<Future<Item = (), Error = io::Error>> {
if let Ok(mut transport) = self.transport.lock() {
transport.send_presence()
.map_err(|_| ())
.and_then(|_| {
Ok(Box::new(future::ok(())))
}).unwrap()
} else {
panic!("")
}
}
pub fn get_jid(&self) -> Credentials {
if let Ok(mut transport) = self.transport.lock() {
transport.get_credentials()
} else {
panic!("")
}
}
pub fn send(&mut self, f: Event) -> Box<Future<Item = (), Error = io::Error>> {
if let Ok(mut transport) = self.transport.lock() {
transport.send_frame(f)
} else {
panic!("")
}
}
pub fn ping(&mut self) -> Receiver<Event> {
let (tx, rx) = oneshot::channel();
let t = self.transport.clone();
if let Ok(mut transport) = self.transport.lock() {
transport.send_ping(tx);
let id = String::from("c2s1");
}
rx
}
pub fn handle(&mut self) -> Box<Future<Item = Consumer, Error = io::Error>> {
let t = self.transport.clone();
if let Ok(mut transport) = self.transport.lock() {
transport.send_frames();
transport.handle_frames();
}
let consumer = Consumer {
transport: t.clone()
};
Box::new(wait_for_answer(t.clone()).map(move |_| {
consumer
}))
}
}
#[derive(Clone)]
pub struct Consumer{
pub transport: Arc<Mutex<XMPPTransport>>,
}
impl Stream for Consumer {
type Item = Event;
type Error = io::Error;
fn poll(&mut self) -> Poll<Option<Event>, io::Error> {
if let Ok(mut transport) = self.transport.try_lock() {
transport.handle_frames();
if let Some(message) = transport.connection.next_input_frame() {
transport.stream_poll();
Ok(Async::Ready(Some(message)))
} else {
transport.stream_poll();
Ok(Async::NotReady)
}
} else {
return Ok(Async::NotReady);
}
}
}
pub fn wait_for_answer(transport: Arc<Mutex<XMPPTransport>>) -> Box<Future<Item = (), Error = io::Error>> {
Box::new(future::poll_fn(move || {
let connected = if let Ok(mut tr) = transport.try_lock() {
tr.handle_frames();
true
} else {
return Ok(Async::NotReady);
};
if connected {
Ok(Async::Ready(()))
} else {
Ok(Async::NotReady)
}
}))
}