Skip to main content

radar_server/plugins/
publish_data.rs

1use zmq;
2use crate::plugins::radar_packet::*;
3use rmp_serde::{to_vec_named};
4
5/// Contains the context and socket for a ZMQ connection
6pub struct Connection {
7    #[allow(dead_code)]
8    context: zmq::Context,
9    socket: zmq::Socket,
10}
11
12/// A connection that sends dummy data on the specified ip.
13pub trait Server {
14    /// Creates a new Dummy Server.
15    fn new_broadcast(ip:&str) -> Self;
16    /// Sends simulated radar data on a loop.
17    fn broadcast(&mut self,packet: ComPacket );
18}
19
20/// A connection that subscribes to the specified ip.
21pub trait Subscriber {
22    /// Creates a new Subscriber.
23    fn new_subscription(ip:&str) -> Self;
24    /// Checks for new packets on the connection.
25    fn subscribe_check(&mut self) -> Option<ComPacket>;
26}
27
28/// A connection to communicate settings over
29pub trait SettingsChannel {
30    fn new_router(ip:&str) -> Self;
31    fn new_dealer(ip:&str) -> Self;
32    fn send_settings(&mut self,dealer_id:&[u8], settings: SettingsPacket);
33    fn check_settings(&mut self) -> Option<Vec<Vec<u8>>>;
34}
35
36impl Server for Connection {
37    fn new_broadcast(ip:&str) -> Connection {
38        let context = zmq::Context::new();
39        let socket = context.socket(zmq::PUB).unwrap();
40        socket.bind(ip).expect("Could not bind socket.");
41        Connection {context, socket}
42    }
43
44    fn broadcast(&mut self, packet: ComPacket) {
45        let pak = to_vec_named(&packet).expect("Could not serialize packet.");
46        self.socket.send(pak,0).expect("Failed to send packet.");
47    }
48}
49
50impl Subscriber for Connection {
51    fn new_subscription(ip:&str) -> Connection {
52        let context = zmq::Context::new();
53        let socket = context.socket(zmq::SUB).unwrap();
54        socket.connect(ip).expect("Failed to connect to socket");
55        socket.set_subscribe(b"").expect("Failed to subscribe socket");
56        println!("Subscribe complete");
57        Connection {context, socket}
58    }
59    fn subscribe_check(&mut self) -> Option<ComPacket>{
60        match zmq::poll(&mut [self.socket.as_poll_item(zmq::POLLIN)], 0) {
61            Ok(event) if event > 0 =>
62                match rmp_serde::from_slice(&self.socket.recv_msg(0).unwrap()){
63                    Ok(packet) => Some(packet),
64                    _ => None
65                }
66            _ => None
67        }
68    }
69}
70
71impl SettingsChannel for Connection {
72    fn new_router(ip:&str) -> Connection {
73        let context = zmq::Context::new();
74        let socket = context.socket(zmq::ROUTER).unwrap();
75        socket.bind(ip).expect("Could not bind socket.");
76        println!("Settings channel router bound to {}",ip);
77        Connection {context, socket }
78    }
79    fn new_dealer(ip:&str) -> Connection {
80        let context = zmq::Context::new();
81        let socket = context.socket(zmq::DEALER).unwrap();
82        socket.connect(ip).expect("Failed to connect to socket");
83        //socket.send("".as_bytes(),0).expect("Failed to send packet.");
84        println!("Settings channel dealer connected to {}",ip);
85        Connection {context, socket}
86    }
87    fn send_settings(&mut self, dealer_id:&[u8], settings: SettingsPacket) {
88        if dealer_id != "".as_bytes(){
89            self.socket.send(dealer_id, zmq::SNDMORE).expect("failed to target dealer");
90        }
91        let settings_json = serde_json::to_string(&settings).expect("Failed to serialize settings");
92        self.socket.send(&settings_json, 0)
93            .expect("Failed to send packet");
94        println!("To Dealer_id: {:?}\nSent: {settings_json}",dealer_id);
95    }
96
97    fn check_settings(&mut self) -> Option<Vec<Vec<u8>>>{
98        match zmq::poll(&mut [self.socket.as_poll_item(zmq::POLLIN)], 0) {
99            Ok(event) if event > 0 => {
100                Some(self.socket.recv_multipart(0).unwrap())
101            }
102            _ => None
103        }
104    }
105}