clean up serializatio ntest code

This commit is contained in:
Robin Müller 2024-05-25 13:08:32 +02:00
parent a710b30013
commit 4c67bcdde1
5 changed files with 1135 additions and 27 deletions

View File

@ -10,5 +10,6 @@ members = [
exclude = [ exclude = [
"satrs-example-stm32f3-disco", "satrs-example-stm32f3-disco",
"serialization-prototyping",
] ]

File diff suppressed because it is too large Load Diff

View File

@ -6,6 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
satrs-minisim = { version = "0.1", path = "../satrs-minisim" }
satrs = { version = "0.2", path = "../satrs" }
rmp-serde = "1" rmp-serde = "1"
rmpv = { version = "1", features = ["with-serde"] } rmpv = { version = "1", features = ["with-serde"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }

View File

@ -2,21 +2,31 @@
from socket import AF_INET, SOCK_DGRAM, socket from socket import AF_INET, SOCK_DGRAM, socket
import msgpack import msgpack
nadine = {
TEST_PERSON = {
"age": 24, "age": 24,
"name": "Nadine", "name": "Nadine",
} }
msg_pack_stuff = msgpack.packb(nadine)
assert msg_pack_stuff is not None
server_socket = socket(AF_INET, SOCK_DGRAM)
target_address = "localhost", 7301
bytes_sent = server_socket.sendto(msg_pack_stuff, target_address)
recv_back = server_socket.recv(4096)
print(f"recv back: {recv_back}")
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} # 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)
def main():
server_socket = socket(AF_INET, SOCK_DGRAM)
target_address = "localhost", 7301
msg_pack_stuff = msgpack.packb(TEST_PERSON)
assert msg_pack_stuff is not None
_ = server_socket.sendto(msg_pack_stuff, target_address)
recv_back = server_socket.recv(4096)
print(f"recv back: {recv_back}")
pass
if __name__ == "__main__":
main()

View File

@ -1,6 +1,10 @@
use rmp_serde::{Deserializer, Serializer}; use rmp_serde::{Deserializer, Serializer};
use satrs_minisim::eps::SwitchMap;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{collections::HashMap, net::UdpSocket}; use std::{
collections::HashMap,
net::{SocketAddr, UdpSocket},
};
#[derive(Debug, Clone, Copy, PartialEq, Deserialize, Serialize)] #[derive(Debug, Clone, Copy, PartialEq, Deserialize, Serialize)]
pub enum Color { pub enum Color {
@ -12,7 +16,6 @@ pub enum Color {
struct Human { struct Human {
age: u16, age: u16,
name: String, name: String,
} }
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
@ -29,7 +32,8 @@ struct HumanGroup {
bank: HashMap<u32, usize>, bank: HashMap<u32, usize>,
} }
fn wild_testing() { #[allow(dead_code)]
fn random_testing() {
let mut buf = Vec::new(); let mut buf = Vec::new();
let john = HumanAdvanced { let john = HumanAdvanced {
id: 0, id: 0,
@ -74,21 +78,61 @@ fn wild_testing() {
println!("human group: {}", human_group_json); println!("human group: {}", human_group_json);
println!("human group json size: {}", human_group_json.len()); println!("human group json size: {}", human_group_json.len());
let human_group_rmp_vec = rmp_serde::to_vec(&human_group_json).unwrap(); 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()); println!("human group msg pack size: {:?}", human_group_rmp_vec.len());
} }
fn main() { #[allow(dead_code)]
let socket = UdpSocket::bind("127.0.0.1:7301").expect("binding UDP socket failed"); fn send_back_weird_stuff(buf: &[u8], received: usize, socket: &UdpSocket, src: SocketAddr) {
// Receives a single datagram message on the socket. If `buf` is too small to hold
// the message, it will be cut off.
let mut buf = [0; 4096];
let (received, src) = socket.recv_from(&mut buf).expect("receive call failed");
let human_from_python: rmpv::Value = rmp_serde::from_slice(&buf[..received]).expect("blablah"); 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"); let human_attempt_2: Human = rmp_serde::from_slice(&buf[..received]).expect("blhfwhfw");
println!("human from python: {}", human_from_python); println!("human from python: {}", human_from_python);
println!("human 2 from python: {:?}", human_attempt_2); println!("human 2 from python: {:?}", human_attempt_2);
let send_back_human = rmp_serde::to_vec(&human_attempt_2).expect("k32k323k2"); 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"); socket
.send_to(&send_back_human, src)
.expect("sending back failed");
}
pub struct UdpServer {
socket: UdpSocket,
last_sender: Option<SocketAddr>,
}
impl Default for UdpServer {
fn default() -> Self {
UdpServer {
socket: UdpSocket::bind("127.0.0.1:7301").expect("binding UDP socket failed"),
last_sender: Default::default(),
}
}
}
impl UdpServer {
pub fn send_back_reply(&self, reply: &[u8]) {
self.socket
.send_to(reply, self.last_sender.expect("last sender not set"))
.expect("sending back failed");
}
}
fn main() {
let mut udp_server = UdpServer::default();
loop {
// Receives a single datagram message on the socket. If `buf` is too small to hold
// the message, it will be cut off.
let mut buf = [0; 4096];
let (received, src) = udp_server
.socket
.recv_from(&mut buf)
.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_json =
serde_json::to_string(&switch_map_off).expect("json serialization failed");
println!("sending back reply: {}", switch_map_off_json);
udp_server.send_back_reply(switch_map_off_json.as_bytes());
}
} }