bla
This commit is contained in:
parent
9f072717bc
commit
d7a988e46b
1 changed files with 46 additions and 11 deletions
55
src/main.rs
55
src/main.rs
|
@ -1,33 +1,68 @@
|
|||
use clap::Parser;
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
use std::{
|
||||
io::{Read, Result, Write},
|
||||
net::{TcpListener, TcpStream},
|
||||
os::unix::net::SocketAddr,
|
||||
};
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
struct Args {
|
||||
#[arg(short, long, default_value_t = 758)]
|
||||
#[arg(short, long, default_value_t = 1743)]
|
||||
port: u32,
|
||||
|
||||
#[arg(short, long)]
|
||||
daemon: bool,
|
||||
// #[arg(long)]
|
||||
// inst: String,
|
||||
|
||||
// #[arg(long)]
|
||||
// val: String,
|
||||
}
|
||||
|
||||
fn handle_client(stream: TcpStream) {
|
||||
// ...
|
||||
fn handle_client(mut stream: TcpStream) {
|
||||
let mut message: Vec<u8> = vec![0; 100];
|
||||
if let Ok(m) = stream.read(&mut message[..]) {
|
||||
if let Ok(s) = String::from_utf8(message) {
|
||||
println!("{}", s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
let listener = TcpListener::bind("127.0.0.1:758")?;
|
||||
|
||||
let args = Args::parse();
|
||||
|
||||
println!("Daemon enabled: {}", args.daemon);
|
||||
if args.daemon {
|
||||
println!("Listening on port: {}", args.daemon);
|
||||
if args.daemon {}
|
||||
|
||||
let is_daemon = args.daemon;
|
||||
println!("Daemon enabled: {}", is_daemon);
|
||||
|
||||
if is_daemon {
|
||||
println!("Listening on port: {}", args.port);
|
||||
run_daemon(args.port);
|
||||
} else {
|
||||
lower_volume(args.port);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// accept connections and process them serially
|
||||
fn run_daemon(port: u32) -> Result<()> {
|
||||
let address = format!("127.0.0.1:{}", port);
|
||||
let listener = TcpListener::bind(address)?;
|
||||
|
||||
for stream in listener.incoming() {
|
||||
handle_client(stream?);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn lower_volume(port: u32) {
|
||||
let address = format!("127.0.0.1:{}", port);
|
||||
if let Ok(mut stream) = TcpStream::connect(address) {
|
||||
println!("Connected!");
|
||||
let _ = stream.write_all(b"Remote Message!");
|
||||
} else {
|
||||
println!("Could not connect!");
|
||||
}
|
||||
println!("Lowering Volume");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue