///
/// ref: https://docs.rs/mio/0.6.19/mio/
/// 
/// 

use mio::*;
use mio::net::{TcpListener, TcpStream};


fn main(){
    // Setup some tokens to allow us to identify which event is
    // for which socket.
    const SERVER: Token = Token(0);
    const CLIENT: Token = Token(1);

    let addr = "127.0.0.1:13265".parse().unwrap();

    // Setup the server socket
    let server = TcpListener::bind(&addr).unwrap();

    // Create a poll instance
    let poll = Poll::new().unwrap();

    // Start listening for incoming connections
    poll.register(&server, SERVER, Ready::readable(),
                PollOpt::edge()).unwrap();

    // Setup the client socket
    let sock = TcpStream::connect(&addr).unwrap();

    // Register the socket
    poll.register(&sock, CLIENT, Ready::readable(),
                PollOpt::edge()).unwrap();

    // Create storage for events
    let mut events = Events::with_capacity(1<<12);

    println!("\t loop \t begin");
    
    loop {
        poll.poll(&mut events, None).unwrap();

        for event in events.iter() {
            match event.token() {
                SERVER => {
                    println!("\t match event.token() \t SERVER
                    Accept and drop the socket immediately, this will close 
                    the socket and notify the client of the EOF.
                    ");
                    let _ = server.accept();
                }
                CLIENT => {
                    println!("\t match event.token() \t CLIENT
                    The server just shuts down the socket, let's just exit
                    from our event loop.
                    ");
                    continue;
                }
                _ => unreachable!(),
            }
        }
    }
    
    println!("\t loop \t end");
}