Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. use tokio::prelude::*;
  2. use tokio::timer::Interval;
  3.  
  4. use std::time::{Duration, Instant};
  5. use std::net::SocketAddr;
  6.  
  7. extern crate tokio_threadpool;
  8. extern crate futures;
  9. use tokio_threadpool::ThreadPool;
  10. use tokio::net::TcpStream;
  11.  
  12. extern crate tokio;
  13.  
  14. use tokio::io;
  15.  
  16. fn delay_future() -> Box<Future<Item = (), Error = io::Error> + Send> {
  17. println!("Sleeping ... ");
  18. let wait_time = Duration::from_millis(2000);
  19. let now = Instant::now();
  20.  
  21. let task = Interval::new(now, wait_time)
  22. .map_err(|e| panic!("interval errored; err={:?}", e))
  23. .for_each(|instant| {
  24. println!("fire; instant={:?}", instant);
  25. Ok(())
  26. });
  27. println!("Resuming Future");
  28. Box::new(task)
  29. }
  30.  
  31. fn connect_future() -> Box<Future<Item = (), Error = io::Error> + Send> {
  32. println!("Executing connect future...");
  33. let server_addr = "94.130.182.25:443".parse::<SocketAddr>().unwrap();
  34. let server_conn = TcpStream::connect(&server_addr);
  35. let task = server_conn
  36. .map_err(|err| eprintln!("{}", err))
  37. .then(|socket| {
  38. println!("conn successful on {:?}", socket);
  39. Ok(())
  40. });
  41. println!("End Future");
  42. Box::new(task)
  43. }
  44.  
  45.  
  46. fn main() -> Result<(),String> {
  47. let pool = ThreadPool::new();
  48. pool.spawn(delay_future().map_err(|e| eprintln!("future error {}", e)));
  49. pool.spawn(connect_future().map_err(|e| eprintln!("future error {}", e)));
  50. // kills the future thread
  51. // pool.shutdown().wait().expect("shutdown error!");
  52. pool.shutdown_on_idle().wait().expect("shutdown error!");
  53. Ok(())
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement