use tokio::net::UnixListener;
use tokio::stream::StreamExt;
use tokio::io::AsyncWriteExt;
use tokio::io::AsyncReadExt;

#[tokio::main]
async fn main() {
    let mut listener = UnixListener::bind("sampleunix.sock").unwrap();
    while let Some(stream) = listener.next().await {
        match stream {
            Ok(stream) => {
                println!("New client: {:?}", stream);
                let (mut read, mut write) = stream.into_split();
                let s = tokio::spawn(async move {
                    let mut buf = [0; 1024];
                    match read.read(&mut buf[..]).await {
                        Err(e) => println!("Read err: {}", e),
                        Ok(n) => {
                            println!("Read: {:?}", &buf[0..n]);
                            println!("Read: {}", String::from_utf8_lossy(&buf[0..n]));
                        }
                    }
                });
                s.await.ok();
                write.write(b"hello").await.ok();
            }
            Err(e) => {
                println!("Unix listern error: {}", e);
            }
        }
    }
}