Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use tokio::prelude::*;
- use tokio::timer::Interval;
- use std::time::{Duration, Instant};
- use std::net::SocketAddr;
- extern crate tokio_threadpool;
- extern crate futures;
- use tokio_threadpool::ThreadPool;
- use tokio::net::TcpStream;
- extern crate tokio;
- use tokio::io;
- fn delay_future() -> Box<Future<Item = (), Error = io::Error> + Send> {
- println!("Sleeping ... ");
- let wait_time = Duration::from_millis(2000);
- let now = Instant::now();
- let task = Interval::new(now, wait_time)
- .map_err(|e| panic!("interval errored; err={:?}", e))
- .for_each(|instant| {
- println!("fire; instant={:?}", instant);
- Ok(())
- });
- println!("Resuming Future");
- Box::new(task)
- }
- fn connect_future() -> Box<Future<Item = (), Error = io::Error> + Send> {
- println!("Executing connect future...");
- let server_addr = "94.130.182.25:443".parse::<SocketAddr>().unwrap();
- let server_conn = TcpStream::connect(&server_addr);
- let task = server_conn
- .map_err(|err| eprintln!("{}", err))
- .then(|socket| {
- println!("conn successful on {:?}", socket);
- Ok(())
- });
- println!("End Future");
- Box::new(task)
- }
- fn main() -> Result<(),String> {
- let pool = ThreadPool::new();
- pool.spawn(delay_future().map_err(|e| eprintln!("future error {}", e)));
- pool.spawn(connect_future().map_err(|e| eprintln!("future error {}", e)));
- // kills the future thread
- // pool.shutdown().wait().expect("shutdown error!");
- pool.shutdown_on_idle().wait().expect("shutdown error!");
- Ok(())
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement