this actually works!

This commit is contained in:
Robin Müller 2024-05-25 13:46:14 +02:00
parent a6d9bee5df
commit 2eaa78dfbc
5 changed files with 152 additions and 91 deletions
serialization-prototyping

@ -266,6 +266,12 @@ dependencies = [
"allocator-api2", "allocator-api2",
] ]
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.3.9" version = "0.3.9"
@ -657,9 +663,11 @@ dependencies = [
"fern", "fern",
"humantime", "humantime",
"log", "log",
"num_enum",
"satrs", "satrs",
"serde", "serde",
"serde_json", "serde_json",
"strum",
"thiserror", "thiserror",
] ]
@ -788,6 +796,28 @@ dependencies = [
"loom", "loom",
] ]
[[package]]
name = "strum"
version = "0.26.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d8cec3501a5194c432b2b7976db6b7d10ec95c253208b45f83f7136aa985e29"
dependencies = [
"strum_macros",
]
[[package]]
name = "strum_macros"
version = "0.26.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c6cf59daf282c0a494ba14fd21610a0325f9f90ec9d1231dea26bcb1d696c946"
dependencies = [
"heck",
"proc-macro2",
"quote",
"rustversion",
"syn 2.0.60",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.109" version = "1.0.109"

@ -1,5 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import enum
from socket import AF_INET, SOCK_DGRAM, socket from socket import AF_INET, SOCK_DGRAM, socket
from pydantic import BaseModel
import msgpack import msgpack
@ -9,10 +11,26 @@ TEST_PERSON = {
} }
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): def msg_pack_unloading(recv_back: bytes):
unpacked = msgpack.unpackb(recv_back) unpacked = msgpack.unpackb(recv_back)
print(f"unpacked: {unpacked}") print(f"unpacked: {unpacked}")
# human_test = {:x for x in unpacked}
loaded_back = msgpack.loads(recv_back) loaded_back = msgpack.loads(recv_back)
print(loaded_back) print(loaded_back)
@ -25,7 +43,8 @@ def main():
_ = server_socket.sendto(msg_pack_stuff, target_address) _ = server_socket.sendto(msg_pack_stuff, target_address)
recv_back = server_socket.recv(4096) recv_back = server_socket.recv(4096)
print(f"recv back: {recv_back}") print(f"recv back: {recv_back}")
pass switch_map = SwitchMap.model_validate_json(recv_back)
print(f"switch map: {switch_map}")
if __name__ == "__main__": if __name__ == "__main__":

@ -1 +1,2 @@
msgpack==1.0.8 msgpack==1.0.8
pydantic==2.7

@ -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");
}

@ -1,97 +1,16 @@
#![allow(unused_imports)]
use rmp_serde::{Deserializer, Serializer}; use rmp_serde::{Deserializer, Serializer};
use satrs_minisim::eps::SwitchMap; use satrs_minisim::eps::{SwitchMap, SwitchMapWrapper};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use std::{
collections::HashMap, collections::HashMap,
net::{SocketAddr, UdpSocket}, net::{SocketAddr, UdpSocket},
}; };
#[derive(Debug, Clone, Copy, PartialEq, Deserialize, Serialize)] #[derive(Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum Color { pub struct SwitchSet {
Red = 0, pub valid: bool,
Green = 1, pub switch_map: SwitchMap,
}
#[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");
} }
pub struct UdpServer { pub struct UdpServer {
@ -129,9 +48,13 @@ fn main() {
.expect("receive call failed"); .expect("receive call failed");
udp_server.last_sender = Some(src); udp_server.last_sender = Some(src);
println!("received {} bytes from {:?}", received, 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 = 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); println!("sending back reply: {}", switch_map_off_json);
udp_server.send_back_reply(switch_map_off_json.as_bytes()); udp_server.send_back_reply(switch_map_off_json.as_bytes());
} }