forked from emma/liminality
		
	initial client code
This commit is contained in:
		
							parent
							
								
									59a26742ce
								
							
						
					
					
						commit
						8f2850445b
					
				
							
								
								
									
										32
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										32
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							@ -20,7 +20,7 @@ dependencies = [
 | 
			
		||||
 "regex",
 | 
			
		||||
 "rustc-hash",
 | 
			
		||||
 "shlex",
 | 
			
		||||
 "syn 1.0.109",
 | 
			
		||||
 "syn",
 | 
			
		||||
 "which",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
@ -100,8 +100,6 @@ dependencies = [
 | 
			
		||||
name = "liminality"
 | 
			
		||||
version = "0.0.1"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "serde",
 | 
			
		||||
 "serde_derive",
 | 
			
		||||
 "yacexits",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
@ -187,23 +185,6 @@ version = "1.1.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "serde"
 | 
			
		||||
version = "1.0.164"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "9e8c8cf938e98f769bc164923b06dce91cea1751522f46f8466461af04c9027d"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "serde_derive"
 | 
			
		||||
version = "1.0.164"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "d9735b638ccc51c28bf6914d90a2e9725b377144fc612c49a611fddd1b631d68"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "proc-macro2",
 | 
			
		||||
 "quote",
 | 
			
		||||
 "syn 2.0.12",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "shlex"
 | 
			
		||||
version = "1.1.0"
 | 
			
		||||
@ -221,17 +202,6 @@ dependencies = [
 | 
			
		||||
 "unicode-ident",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "syn"
 | 
			
		||||
version = "2.0.12"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "79d9531f94112cfc3e4c8f5f02cb2b58f72c97b7efd85f70203cc6d8efda5927"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "proc-macro2",
 | 
			
		||||
 "quote",
 | 
			
		||||
 "unicode-ident",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "unicode-ident"
 | 
			
		||||
version = "1.0.8"
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										10
									
								
								Cargo.toml
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								Cargo.toml
									
									
									
									
									
								
							@ -6,6 +6,12 @@ license = "AGPL-3.0-or-later"
 | 
			
		||||
authors = [ "Emma Tebibyte <emma@tebibyte.media>" ]
 | 
			
		||||
 | 
			
		||||
[dependencies]
 | 
			
		||||
serde = "1.0.164"
 | 
			
		||||
serde_derive = "1.0.164"
 | 
			
		||||
yacexits = "0.1.5"
 | 
			
		||||
 | 
			
		||||
[[bin]]
 | 
			
		||||
name = "server"
 | 
			
		||||
path = "src/server.rs"
 | 
			
		||||
 | 
			
		||||
[[bin]]
 | 
			
		||||
name = "client"
 | 
			
		||||
path = "src/client.rs"
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										52
									
								
								src/client.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								src/client.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,52 @@
 | 
			
		||||
use std::{
 | 
			
		||||
    env::args,
 | 
			
		||||
    io::{ BufRead, BufReader, stdin, Write },
 | 
			
		||||
    net::TcpStream,
 | 
			
		||||
    thread,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
use yacexits::exit;
 | 
			
		||||
 | 
			
		||||
fn main() {
 | 
			
		||||
    let args = args().collect::<Vec<String>>();
 | 
			
		||||
    let argv = args.iter().map(String::as_str).collect::<Vec<&str>>();
 | 
			
		||||
 | 
			
		||||
    if !argv.clone().get(1).is_some() || !argv.clone().get(2).is_some() {
 | 
			
		||||
        exit(1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let user = argv[2];
 | 
			
		||||
 | 
			
		||||
    let mut stream = TcpStream::connect(argv[1]).unwrap_or_else(|err| {
 | 
			
		||||
        eprintln!("{:?}", err);
 | 
			
		||||
        exit(1);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    let from_message = format!("FROM {}\n", user);
 | 
			
		||||
 | 
			
		||||
    let _ = stream.write(from_message.as_bytes());
 | 
			
		||||
 | 
			
		||||
    let stream_recv = stream.try_clone().unwrap();
 | 
			
		||||
 | 
			
		||||
    let _ = thread::spawn(move || {
 | 
			
		||||
        let reader = BufReader::new(stream_recv);
 | 
			
		||||
 | 
			
		||||
        for line in reader.lines() {
 | 
			
		||||
            if let Ok(message) = line {
 | 
			
		||||
                let words: Vec<&str> = message.split_whitespace().collect();
 | 
			
		||||
                match words[0] {
 | 
			
		||||
                    "FROM" => print!("{}: ", words[1..].join(" ")),
 | 
			
		||||
                    "BODY" => println!("{}", words[1..].join(" ")),
 | 
			
		||||
                    _ => {},
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    loop {
 | 
			
		||||
        let mut buf = String::new();
 | 
			
		||||
        let _ = stdin().read_line(&mut buf);
 | 
			
		||||
        let out = format!("BODY {}\n", buf);
 | 
			
		||||
        let _ = stream.write(out.as_bytes());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -19,9 +19,8 @@
 | 
			
		||||
use std::{
 | 
			
		||||
    collections::HashMap,
 | 
			
		||||
    io::{ BufRead, BufReader, Write },
 | 
			
		||||
    net::{ TcpListener, TcpStream },
 | 
			
		||||
    net::TcpStream,
 | 
			
		||||
    sync::{ Arc, Mutex },
 | 
			
		||||
    thread,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct Message {
 | 
			
		||||
@ -29,7 +28,7 @@ struct Message {
 | 
			
		||||
    body: Option<String>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn handle_client(
 | 
			
		||||
pub fn handle_client(
 | 
			
		||||
    client: usize,
 | 
			
		||||
    clients: Arc<Mutex<HashMap<usize, TcpStream>>>
 | 
			
		||||
) {
 | 
			
		||||
@ -107,25 +106,3 @@ fn handle_client(
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn main() {
 | 
			
		||||
    let listener = TcpListener::bind("0.0.0.0:8080")
 | 
			
		||||
        .expect("Failed to bind to port 8080.");
 | 
			
		||||
    println!("Server listening on port 8080.");
 | 
			
		||||
 | 
			
		||||
    let connections: Arc<Mutex<HashMap<usize, TcpStream>>> = Arc::new(
 | 
			
		||||
        Mutex::new(HashMap::new())
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    for (id, stream) in listener.incoming().enumerate() {
 | 
			
		||||
        match stream {
 | 
			
		||||
            Ok(stream) => {
 | 
			
		||||
                let clients = connections.clone();
 | 
			
		||||
                clients.lock().unwrap().insert(id.clone(), stream);
 | 
			
		||||
                thread::spawn(move || handle_client(id, clients));
 | 
			
		||||
            }
 | 
			
		||||
            Err(err) => {
 | 
			
		||||
                eprintln!("Error accepting client connection: {}", err);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										49
									
								
								src/server.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								src/server.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,49 @@
 | 
			
		||||
/* 
 | 
			
		||||
 * Copyright (c) 2023 Emma Tebibyte
 | 
			
		||||
 * SPDX-License-Identifier: AGPL-3.0-or-later
 | 
			
		||||
 *
 | 
			
		||||
 * This program is free software: you can redistribute it and/or modify it under
 | 
			
		||||
 * the terms of the GNU Affero General Public License as published by the Free
 | 
			
		||||
 * Software Foundation, either version 3 of the License, or (at your option) any
 | 
			
		||||
 * later version.
 | 
			
		||||
 * 
 | 
			
		||||
 * This program is distributed in the hope that it will be useful, but WITHOUT
 | 
			
		||||
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 | 
			
		||||
 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 | 
			
		||||
 * details.
 | 
			
		||||
 *
 | 
			
		||||
 * You should have received a copy of the GNU Affero General Public License
 | 
			
		||||
 * along with this program. If not, see https://www.gnu.org/licenses/.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
use std::{
 | 
			
		||||
    collections::HashMap,
 | 
			
		||||
    net::{ TcpListener, TcpStream },
 | 
			
		||||
    sync::{ Arc, Mutex },
 | 
			
		||||
    thread,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
use liminality::handle_client;
 | 
			
		||||
 | 
			
		||||
fn main() {
 | 
			
		||||
    let listener = TcpListener::bind("0.0.0.0:8080")
 | 
			
		||||
        .expect("Failed to bind to port 8080.");
 | 
			
		||||
    println!("Server listening on port 8080.");
 | 
			
		||||
 | 
			
		||||
    let connections: Arc<Mutex<HashMap<usize, TcpStream>>> = Arc::new(
 | 
			
		||||
        Mutex::new(HashMap::new())
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    for (id, stream) in listener.incoming().enumerate() {
 | 
			
		||||
        match stream {
 | 
			
		||||
            Ok(stream) => {
 | 
			
		||||
                let clients = connections.clone();
 | 
			
		||||
                clients.lock().unwrap().insert(id.clone(), stream);
 | 
			
		||||
                thread::spawn(move || handle_client(id, clients));
 | 
			
		||||
            }
 | 
			
		||||
            Err(err) => {
 | 
			
		||||
                eprintln!("Error accepting client connection: {}", err);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user