Skip to content

Commit

Permalink
this actually works!
Browse files Browse the repository at this point in the history
  • Loading branch information
robamu committed May 25, 2024
1 parent a6d9bee commit 2eaa78d
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 91 deletions.
30 changes: 30 additions & 0 deletions serialization-prototyping/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 21 additions & 2 deletions serialization-prototyping/python-test/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env python3
import enum
from socket import AF_INET, SOCK_DGRAM, socket
from pydantic import BaseModel
import msgpack


Expand All @@ -9,10 +11,26 @@
}


class Devices(str, enum.Enum):
MGT = "Mgt"
MGM = "Mgm"


class SwitchState(str, enum.Enum):
OFF = "Off"
ON = "On"
UNKNOWN = "Unknown"
FAULTY = "Faulty"


class SwitchMap(BaseModel):
valid: bool
switch_map: dict[Devices, SwitchState]


def msg_pack_unloading(recv_back: bytes):
unpacked = msgpack.unpackb(recv_back)
print(f"unpacked: {unpacked}")
# human_test = {:x for x in unpacked}
loaded_back = msgpack.loads(recv_back)
print(loaded_back)

Expand All @@ -25,7 +43,8 @@ def main():
_ = server_socket.sendto(msg_pack_stuff, target_address)
recv_back = server_socket.recv(4096)
print(f"recv back: {recv_back}")
pass
switch_map = SwitchMap.model_validate_json(recv_back)
print(f"switch map: {switch_map}")


if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions serialization-prototyping/python-test/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
msgpack==1.0.8
pydantic==2.7
88 changes: 88 additions & 0 deletions serialization-prototyping/src/archive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#[derive(Debug, Clone, Copy, PartialEq, Deserialize, Serialize)]
pub enum Color {
Red = 0,
Green = 1,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
struct Human {
age: u16,
name: String,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
struct HumanAdvanced {
id: u32,
age: u16,
name: String,
fav_color: Color,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
struct HumanGroup {
humans: Vec<HumanAdvanced>,
bank: HashMap<u32, usize>,
}


#[allow(dead_code)]
fn random_testing() {
let mut buf = Vec::new();
let john = HumanAdvanced {
id: 0,
age: 42,
name: "John".into(),
fav_color: Color::Green,
};

john.serialize(&mut Serializer::new(&mut buf)).unwrap();

println!("{:?}", buf);
let new_val: HumanAdvanced = rmp_serde::from_slice(&buf).expect("deserialization failed");
let rmpv_val: rmpv::Value = rmp_serde::from_slice(&buf).expect("serialization into val failed");
println!("RMPV value: {:?}", rmpv_val);
let json_str = serde_json::to_string(&rmpv_val).expect("creating json failed");
assert_eq!(john, new_val);
println!("JSON str: {}", json_str);
let val_test: HumanAdvanced = serde_json::from_str(&json_str).expect("wild");
println!("val test: {:?}", val_test);

let nadine = HumanAdvanced {
id: 1,
age: 24,
name: "Nadine".into(),
fav_color: Color::Red,
};
let mut bank = HashMap::default();
bank.insert(john.id, 1000000);
bank.insert(nadine.id, 1);

let human_group = HumanGroup {
humans: vec![john, nadine.clone()],
bank,
};
let json_str = serde_json::to_string(&nadine).unwrap();
println!("Nadine as JSON: {}", json_str);

let nadine_is_back: HumanAdvanced = serde_json::from_str(&json_str).unwrap();
println!("nadine deserialized: {:?}", nadine_is_back);

let human_group_json = serde_json::to_string(&human_group).unwrap();
println!("human group: {}", human_group_json);
println!("human group json size: {}", human_group_json.len());

let human_group_rmp_vec = rmp_serde::to_vec_named(&human_group_json).unwrap();
println!("human group msg pack size: {:?}", human_group_rmp_vec.len());
}

#[allow(dead_code)]
fn send_back_weird_stuff(buf: &[u8], received: usize, socket: &UdpSocket, src: SocketAddr) {
let human_from_python: rmpv::Value = rmp_serde::from_slice(&buf[..received]).expect("blablah");
let human_attempt_2: Human = rmp_serde::from_slice(&buf[..received]).expect("blhfwhfw");
println!("human from python: {}", human_from_python);
println!("human 2 from python: {:?}", human_attempt_2);
let send_back_human = rmp_serde::to_vec_named(&human_attempt_2).expect("k32k323k2");
socket
.send_to(&send_back_human, src)
.expect("sending back failed");
}
101 changes: 12 additions & 89 deletions serialization-prototyping/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,97 +1,16 @@
#![allow(unused_imports)]
use rmp_serde::{Deserializer, Serializer};
use satrs_minisim::eps::SwitchMap;
use satrs_minisim::eps::{SwitchMap, SwitchMapWrapper};
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
net::{SocketAddr, UdpSocket},
};

#[derive(Debug, Clone, Copy, PartialEq, Deserialize, Serialize)]
pub enum Color {
Red = 0,
Green = 1,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
struct Human {
age: u16,
name: String,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
struct HumanAdvanced {
id: u32,
age: u16,
name: String,
fav_color: Color,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
struct HumanGroup {
humans: Vec<HumanAdvanced>,
bank: HashMap<u32, usize>,
}

#[allow(dead_code)]
fn random_testing() {
let mut buf = Vec::new();
let john = HumanAdvanced {
id: 0,
age: 42,
name: "John".into(),
fav_color: Color::Green,
};

john.serialize(&mut Serializer::new(&mut buf)).unwrap();

println!("{:?}", buf);
let new_val: HumanAdvanced = rmp_serde::from_slice(&buf).expect("deserialization failed");
let rmpv_val: rmpv::Value = rmp_serde::from_slice(&buf).expect("serialization into val failed");
println!("RMPV value: {:?}", rmpv_val);
let json_str = serde_json::to_string(&rmpv_val).expect("creating json failed");
assert_eq!(john, new_val);
println!("JSON str: {}", json_str);
let val_test: HumanAdvanced = serde_json::from_str(&json_str).expect("wild");
println!("val test: {:?}", val_test);

let nadine = HumanAdvanced {
id: 1,
age: 24,
name: "Nadine".into(),
fav_color: Color::Red,
};
let mut bank = HashMap::default();
bank.insert(john.id, 1000000);
bank.insert(nadine.id, 1);

let human_group = HumanGroup {
humans: vec![john, nadine.clone()],
bank,
};
let json_str = serde_json::to_string(&nadine).unwrap();
println!("Nadine as JSON: {}", json_str);

let nadine_is_back: HumanAdvanced = serde_json::from_str(&json_str).unwrap();
println!("nadine deserialized: {:?}", nadine_is_back);

let human_group_json = serde_json::to_string(&human_group).unwrap();
println!("human group: {}", human_group_json);
println!("human group json size: {}", human_group_json.len());

let human_group_rmp_vec = rmp_serde::to_vec_named(&human_group_json).unwrap();
println!("human group msg pack size: {:?}", human_group_rmp_vec.len());
}

#[allow(dead_code)]
fn send_back_weird_stuff(buf: &[u8], received: usize, socket: &UdpSocket, src: SocketAddr) {
let human_from_python: rmpv::Value = rmp_serde::from_slice(&buf[..received]).expect("blablah");
let human_attempt_2: Human = rmp_serde::from_slice(&buf[..received]).expect("blhfwhfw");
println!("human from python: {}", human_from_python);
println!("human 2 from python: {:?}", human_attempt_2);
let send_back_human = rmp_serde::to_vec_named(&human_attempt_2).expect("k32k323k2");
socket
.send_to(&send_back_human, src)
.expect("sending back failed");
#[derive(Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct SwitchSet {
pub valid: bool,
pub switch_map: SwitchMap,
}

pub struct UdpServer {
Expand Down Expand Up @@ -129,9 +48,13 @@ fn main() {
.expect("receive call failed");
udp_server.last_sender = Some(src);
println!("received {} bytes from {:?}", received, src);
let switch_map_off = SwitchMap::default();
let switch_map_off = SwitchMapWrapper::default();
let switch_set = SwitchSet {
valid: true,
switch_map: switch_map_off.0.clone(),
};
let switch_map_off_json =
serde_json::to_string(&switch_map_off).expect("json serialization failed");
serde_json::to_string(&switch_set).expect("json serialization failed");
println!("sending back reply: {}", switch_map_off_json);
udp_server.send_back_reply(switch_map_off_json.as_bytes());
}
Expand Down

0 comments on commit 2eaa78d

Please sign in to comment.