Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protocol #1

Merged
merged 1 commit into from Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 22 additions & 13 deletions device/src/main.rs
@@ -1,20 +1,29 @@
use std::net::TcpStream;
use protocol::{Message, send_msg};
use std::{net::TcpStream, io::{stdin, BufRead}};
use protocol::{Message, send_msg, read_msg};

fn handler(s: TcpStream) -> std::io::Result<()> {
loop {
match read_msg(&s) {
Ok(msg) => {
use Message::*;
match msg {
ReadInput => {
println!("Server requested a line, send something:");
let line = stdin().lock().lines().next().unwrap().unwrap();
send_msg(&s, Line(line))?;
}
_ => panic!("Unexpected message")
};
}
Err(e) => println!("Failed to read message: {:?}", e),
}
}
}

fn main() -> std::io::Result<()> {
let conn = TcpStream::connect("127.0.0.1:8000")?;

use Message::*;
send_msg(&conn, HELLO)?;
println!("Said hello");
// read_msg(&conn)?; // ACK
// println!("got ACK");
send_msg(&conn, MSG("TEST".to_string()))?;
println!("sent TEST");
// read_msg(&conn)?; // ACK
// println!("got ACK");
send_msg(&conn, GOODBYE)?;
println!("sent goodbye");
handler(conn)?;

Ok(())
}
48 changes: 44 additions & 4 deletions protocol/src/lib.rs
@@ -1,12 +1,52 @@
use serde::{Deserialize, Serialize};
use std::net::TcpStream;

#[derive(Serialize, Deserialize)]
pub enum Suit {
HEART,
DIAMOND,
SPADE,
CLUB,
}

#[derive(Serialize, Deserialize)]
pub enum Rank {
ACE,
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING
}

#[derive(Serialize, Deserialize)]
pub struct Card(Suit, Rank);

#[derive(Serialize, Deserialize)]
pub enum Message {
HELLO,
MSG(String),
GOODBYE,
ACK,

// Server -> Device
Print(String),
Clear,
ReadInput,
PrintCard(Card),
RequestScan,

// Device -> Server
Line(String),
DetectedCards(Vec<Card>),

// Misc
DEBUG(String),
ERR(String),
ACK
}

// These functions seem kind of silly, but since this is a shared library by both the server and
Expand Down
19 changes: 6 additions & 13 deletions server/src/main.rs
Expand Up @@ -4,23 +4,16 @@ use protocol::{read_msg, send_msg, Message};

fn handler(s: TcpStream) -> std::io::Result<()> {
loop {
use Message::*;
send_msg(&s, ReadInput)?;

match read_msg(&s) {
Ok(msg) => {
use Message::*;
match msg {
HELLO => {
println!("Device said hello!");
send_msg(&s, ACK)?;
}
MSG(m) => {
println!("Got a message: {}", m);
send_msg(&s, ACK)?;
}
GOODBYE => {
println!("Got goodbye, detatching");
return Ok(());
Line(str) => {
println!("Got a message: {}", str);
}
ACK => panic!("Unexpected ACK"),
_ => panic!("Unexpected message")
};
}
Err(e) => println!("Failed to read message: {:?}", e),
Expand Down