@@ -20,7 +20,7 @@ use tokio_core::net::TcpStream;
20
20
use tokio_core:: reactor:: Handle ;
21
21
use tokio_io:: { AsyncRead , AsyncWrite } ;
22
22
use tokio_io:: codec:: { Encoder , Decoder , Framed } ;
23
- use tokio_proto:: pipeline:: { ClientProto , ClientService } ;
23
+ use tokio_proto:: pipeline:: { ClientProto , ClientService , ServerProto } ;
24
24
use tokio_proto:: TcpClient ;
25
25
use tokio_service:: Service ;
26
26
@@ -78,16 +78,27 @@ pub struct RemoteHost {
78
78
telemetry : Option < Telemetry > ,
79
79
}
80
80
81
- struct JsonCodec ;
82
- struct JsonProto ;
81
+ #[ doc( hidden) ]
82
+ pub struct JsonCodec ;
83
+ #[ doc( hidden) ]
84
+ pub struct JsonProto ;
83
85
84
86
impl RemoteHost {
85
87
/// Create a new Host connected to addr.
86
- pub fn connect ( addr : & SocketAddr , handle : & Handle ) -> Box < Future < Item = Arc < RemoteHost > , Error = Error > > {
88
+ pub fn connect ( addr : & str , handle : & Handle ) -> Box < Future < Item = Arc < RemoteHost > , Error = Error > > {
89
+ let addr: SocketAddr = match addr. parse ( ) . chain_err ( || "Invalid host address" ) {
90
+ Ok ( addr) => addr,
91
+ Err ( e) => return Box :: new ( future:: err ( e) ) ,
92
+ } ;
93
+
94
+ info ! ( "Connecting to host {}" , addr) ;
95
+
87
96
Box :: new ( TcpClient :: new ( JsonProto )
88
- . connect ( addr, handle)
97
+ . connect ( & addr, handle)
89
98
. chain_err ( || "Could not connect to host" )
90
99
. and_then ( |client_service| {
100
+ info ! ( "Connected!" ) ;
101
+
91
102
let mut host = Arc :: new ( RemoteHost {
92
103
inner : client_service,
93
104
telemetry : None ,
@@ -111,17 +122,21 @@ impl Host for RemoteHost {
111
122
fn run < D : ' static > ( & self , provider : Runnable ) -> Box < Future < Item = D , Error = Error > >
112
123
where for < ' de > D : Deserialize < ' de >
113
124
{
114
- Box :: new ( self . inner . call ( provider)
115
- . chain_err ( || "Could not run provider" )
116
- . and_then ( |v| match serde_json:: from_value :: < D > ( v) . chain_err ( || "Could not run provider" ) {
125
+ let value = match serde_json:: to_value ( provider) . chain_err ( || "Could not encode provider to send to host" ) {
126
+ Ok ( v) => v,
127
+ Err ( e) => return Box :: new ( future:: err ( e) )
128
+ } ;
129
+ Box :: new ( self . inner . call ( value)
130
+ . chain_err ( || "Error while running provider on host" )
131
+ . and_then ( |v| match serde_json:: from_value :: < D > ( v) . chain_err ( || "Could not understand response from host" ) {
117
132
Ok ( d) => future:: ok ( d) ,
118
133
Err ( e) => future:: err ( e)
119
134
} ) )
120
135
}
121
136
}
122
137
123
138
impl Service for RemoteHost {
124
- type Request = Runnable ;
139
+ type Request = serde_json :: Value ;
125
140
type Response = serde_json:: Value ;
126
141
type Error = io:: Error ;
127
142
type Future = Box < Future < Item = Self :: Response , Error = Self :: Error > > ;
@@ -152,11 +167,11 @@ impl Decoder for JsonCodec {
152
167
}
153
168
154
169
impl Encoder for JsonCodec {
155
- type Item = Runnable ;
170
+ type Item = serde_json :: Value ;
156
171
type Error = io:: Error ;
157
172
158
- fn encode ( & mut self , provider : Self :: Item , buf : & mut BytesMut ) -> io:: Result < ( ) > {
159
- let json = serde_json:: to_string ( & provider ) . unwrap ( ) ;
173
+ fn encode ( & mut self , value : Self :: Item , buf : & mut BytesMut ) -> io:: Result < ( ) > {
174
+ let json = serde_json:: to_string ( & value ) . unwrap ( ) ;
160
175
buf. reserve ( json. len ( ) + 1 ) ;
161
176
buf. extend ( json. as_bytes ( ) ) ;
162
177
buf. put_u8 ( b'\n' ) ;
@@ -166,7 +181,18 @@ impl Encoder for JsonCodec {
166
181
}
167
182
168
183
impl < T : AsyncRead + AsyncWrite + ' static > ClientProto < T > for JsonProto {
169
- type Request = Runnable ;
184
+ type Request = serde_json:: Value ;
185
+ type Response = serde_json:: Value ;
186
+ type Transport = Framed < T , JsonCodec > ;
187
+ type BindTransport = result:: Result < Self :: Transport , io:: Error > ;
188
+
189
+ fn bind_transport ( & self , io : T ) -> Self :: BindTransport {
190
+ Ok ( io. framed ( JsonCodec ) )
191
+ }
192
+ }
193
+
194
+ impl < T : AsyncRead + AsyncWrite + ' static > ServerProto < T > for JsonProto {
195
+ type Request = serde_json:: Value ;
170
196
type Response = serde_json:: Value ;
171
197
type Transport = Framed < T , JsonCodec > ;
172
198
type BindTransport = result:: Result < Self :: Transport , io:: Error > ;
0 commit comments