Hello,
Below is a sample example code which has a point structure. Run function spawns a socket and listens to the stream of data using stream.map. While there is an tokio based http server listening for any incoming connections.
The stream.map contains data that needs to be passed to http server. To Accomplish the task, I am using this point structure.
use errors::*;
quick_main!(run);
#[derive(Clone, Debug)]
struct Point {
x: u32,
y: u32,
result: String,
status: bool
}
impl Point {
fn update_results(&mut self, resultp: String){
self.result = resultp;
println!("Update Struct : {:?}", self.result);
}
fn update_status(&mut self, x: bool) {
self.status = x;
}
}
struct ExampleServer <'a> {
wpa_cmd: mpsc::Sender<String>,
wpa_db: &'a Point,
}
impl<'a> ExampleServer<'a> {
fn new(wpa_cmd: mpsc::Sender<String>, P0: &'a Point) -> ExampleServer {
ExampleServer {
wpa_cmd: wpa_cmd,
wpa_db: P0,
}
}
}
impl <'a> Service for ExampleServer <'a>{
type Request = Request;
type Response = Response;
type Error = hyper::Error;
type Future = Box<Future<Item=Self::Response, Error=Self::Error>>;
fn call(&self, req: Request) -> Self::Future {
match (req.method(), req.path()) {
(&Method::Get, "/") => {
println!("{:?}", self.wpa_db.result); // need the stream.map data here
Box::new(futures::future::ok(response))
},
}
}
}
fn run() -> Result<()> {
let (cmd_send, cmd_recv) = mpsc::channel::<String>(5); // extra buffer
let mut p0 = Point { x: 5, y: 10, result: String::from("Hello World"), status: false};
let p2 = p0.clone();
let http_server = {
let wpa_data = wpa_data.clone();
let cmd_send = cmd_send.clone();
Http::new().bind(
&addr,
move || Ok(ExampleServer::new(cmd_send.clone(), &p0))
)?
};
let wpa_data = wpa_data.clone();
let sock = UnixDatagram::bind(SCAN_AND_CONNECT_PATH, &handle).unwrap().framed(StringDatagramCodec);
let f = sock.send("TEST".to_string()).and_then(move |sock| {
let (sink, stream) = sock.split();
let mapped_stream = stream.map(move |msg| {
let wpa_data = wpa_data.clone();
let mut ret = "".to_string();
if msg == "OK\n" {
} else if msg == "<3>RECEIVED DATA " {
println!("Recvd msg notification {}", msg);
ret = "TEST_RESULT".to_string();
p0.update_results(ret.clone()); // NEED THIS DATA To be available in HTTP
}
ret
}).select(cmd_recv.map_err(|()| {
use std::io::{Error, ErrorKind};
Error::new(ErrorKind::Other, "end of stream")
}));
sink.send_all(mapped_stream)
});
handle.spawn(f.map(|_| ()).map_err(|_| ()));
http_server.run()?;
Ok(())
}
If I pass p0 directly to the new of http server, I get below error.
If I pass p2 to the http server, since it is a clone, any update to p0 structure would not be reflected in the httpserver.
i.e., p0.update_results(ret.clone()); would not be reflected in the self.wpa_db structure.
How could we make this structure accessible in the http server?
Thanks a Ton in Advance.