Skip to content

Commit

Permalink
automatically put version in packets
Browse files Browse the repository at this point in the history
  • Loading branch information
braden-godley committed May 11, 2024
1 parent 67b1452 commit 0cc707d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
9 changes: 4 additions & 5 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::net::TcpStream;
use std::io::{Read, Write, self};
use clap::crate_version;

pub struct Client {
version: String,
stream: TcpStream,
}

Expand All @@ -12,15 +12,13 @@ enum EchoType {
}

impl Client {
pub fn new(host: &str, port: u16, version: &str) -> Self {
pub fn new(host: &str, port: u16) -> Self {
let connection_url = format!("{}:{}", host, port);
let stream = TcpStream::connect(&connection_url)
.expect(&format!("Failed to connect to {}", &connection_url));
let version = version.to_string();

Client {
stream,
version,
}
}

Expand Down Expand Up @@ -103,7 +101,8 @@ impl Client {
}

pub fn send(&mut self, command: &str, lines: Vec<&str>) -> io::Result<()> {
let version_line = format!("Rustis {}", self.version);
let version = crate_version!();
let version_line = format!("Rustis {}", version);
let message = format!("{}\n{}\n{}\n\n", version_line, command, lines.join("\n"));

let bytes = message.as_bytes();
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn main() -> io::Result<()> {
let host = matches.get_one::<String>("host").unwrap();
let port = matches.get_one::<u16>("port").unwrap();

let mut client = client::Client::new(host, *port, "1.0.0");
let mut client = client::Client::new(host, *port);

if let Some(matches) = matches.subcommand_matches("publish") {
let channel = matches.get_one::<String>("channel").unwrap();
Expand Down
20 changes: 12 additions & 8 deletions src/packetreader.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use regex::Regex;

const VERSION: &'static str = "1.0.0";
use clap::crate_version;

/* Packet format:
Expand Down Expand Up @@ -49,7 +48,7 @@ impl RequestPacket {
pub fn new(buf: String) -> Self {
let lines: Vec<_> = buf.split("\n").collect();

let version = if let Some(line1) = lines.get(0) {
let packet_version = if let Some(line1) = lines.get(0) {
let re = Regex::new(r"^Rustis (\d{1,3}\.\d{1,4}\.\d{1,4})$").unwrap();

if let Some(caps) = re.captures(line1) {
Expand All @@ -61,8 +60,10 @@ impl RequestPacket {
None
};

if let Some(version) = version {
if version != VERSION {
let current_version = crate_version!();

if let Some(packet_version) = packet_version {
if packet_version != current_version {
return RequestPacket::Invalid {
error: String::from("version mismatch"),
}
Expand Down Expand Up @@ -189,7 +190,8 @@ mod test {
#[test]
fn publish_packet() {
let mut buf = String::new();
buf.push_str("Rustis 1.0.0\n");
let version = crate_version!();
buf.push_str(&format!("Rustis {}\n", version));
buf.push_str("publish\n");
buf.push_str("channel\n");
buf.push_str("message");
Expand All @@ -205,7 +207,8 @@ mod test {
#[test]
fn invalid_version() {
let mut buf = String::new();
buf.push_str("Rustis 99.99.99\n");
let version = "999.999.999";
buf.push_str(&format!("Rustis {}\n", version));
buf.push_str("publish\n");
buf.push_str("channel\n");
buf.push_str("message\n");
Expand All @@ -221,7 +224,8 @@ mod test {
#[test]
fn unknown_command() {
let mut buf = String::new();
buf.push_str("Rustis 1.0.0\n");
let version = crate_version!();
buf.push_str(&format!("Rustis {}\n", version));
buf.push_str("heebee\n");

let packet = RequestPacket::new(buf);
Expand Down

0 comments on commit 0cc707d

Please sign in to comment.