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
#![allow(unused_must_use)]
use futures::{Async,Poll};
use std::sync::Arc;
use std::sync::Mutex;
use tokio_io::{AsyncRead};
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;
#[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_ping(&self) -> Box<Future<Item = (), Error = io::Error>> {
if let Ok(mut transport) = self.transport.lock() {
transport.send_ping()
.and_then(|_| {
Ok(Box::new(future::ok(())))
}).unwrap()
} else {
panic!("")
}
}
pub fn send_presence(&self) -> Box<Future<Item = (), Error = io::Error>> {
if let Ok(mut transport) = self.transport.lock() {
transport.send_presence()
.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 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
}))
} else {
panic!("")
}
}
}
#[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)
}
}))
}