1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use crate::import::*;


mod link;

use crate::node::link::NodeLink;
use crate::util::{RegisterRecipient, Service};
use crate::global::{Get, Global};
use crate::process::{DispatchError, Dispatcher};
use crate::process::registry::{Dispatch};
use tokio::net::TcpStream;

#[derive(Debug, Clone)]
pub struct NodeConfig {
    pub id: Uuid,
    pub listen: SocketAddr,
}

impl Default for NodeConfig {
    fn default() -> Self {
        NodeConfig {
            id: Uuid::new_v4(),
            listen: ([127, 0, 0, 1], 9090).into(),
        }
    }
}

pub struct NodeControl {
    /// Links to other nodes
    links: HashMap<Uuid, Addr<NodeLink>>,
    /// Dispatcher for unaddressed messages.
    ///
    /// Messages which are sent to process id of 00000000000000000....
    /// are considered unadressed, and are dispatched from here
    pub dispatch: HashMap<u64, NodeHandler>,
    pub listeners: HashMap<Uuid, Recipient<NodeUpdate>>,
}

impl SystemService for NodeControl {}

impl Supervised for NodeControl {
    fn restarting(&mut self, ctx: &mut Self::Context) {
        let cfg = Global::<NodeConfig>::from_registry().send(Get::default());
        let set_cfg = wrap_future(cfg)
            .then(|cfg, this: &mut Self, ctx| {
                let cfg = cfg.unwrap();
                log::warn!("Starting node listener on: {:?}", cfg);
                let cfg = cfg.unwrap();

                wrap_future(tokio::net::TcpListener::bind(cfg.listen))
            })
            .map(|listener, this: &mut Self, ctx| {
                let mut listener = listener.unwrap();
                ctx.add_stream(listener);
            });

        ctx.wait(set_cfg);
    }
}

impl Default for NodeControl {
    fn default() -> Self {
        NodeControl {
            links: HashMap::new(),
            dispatch: HashMap::new(),
            listeners: HashMap::new(),
        }
    }
}

impl Actor for NodeControl {
    type Context = Context<Self>;

    fn started(&mut self, ctx: &mut Self::Context) {
        Self::restarting(self, ctx);
    }
}

impl StreamHandler<std::io::Result<TcpStream>> for NodeControl {
    fn handle(&mut self, item: std::io::Result<TcpStream>, ctx: &mut Context<Self>) {
        let item = item.expect("Fatal error in NodeControl");

        let link = wrap_future(NodeLink::new(item));
        let fut = link
            .map(|(id, peer, link), this: &mut Self, ctx| {
                log::info!("Connected to: {:?}", id);
                this.links.insert(id, link.clone());
                this.listeners.retain(|_, l| {
                    l.do_send(NodeUpdate::Connected(id)).is_ok()
                });
            });
        ctx.spawn(fut);
    }
}

#[derive(Debug, Clone)]
pub enum NodeUpdate {
    Connected(Uuid),
    Disconnected(Uuid),
}

impl Message for NodeUpdate { type Result = (); }

impl Handler<NodeUpdate> for NodeControl {
    type Result = ();

    fn handle(&mut self, msg: NodeUpdate, ctx: &mut Self::Context) -> Self::Result {
        match msg {
            NodeUpdate::Disconnected(id) => {
                self.listeners.retain(|_, l| l.do_send(msg.clone()).is_ok());
            }
            _ => panic!("Should receive connected event yet")
        }
    }
}

impl Handler<RegisterRecipient<NodeUpdate>> for NodeControl {
    type Result = Result<Uuid, std::convert::Infallible>;

    fn handle(&mut self, msg: RegisterRecipient<NodeUpdate>, ctx: &mut Self::Context) -> Self::Result {
        let id = Uuid::new_v4();
        self.listeners.insert(id, msg.0);
        return Ok(id);
    }
}


pub struct Connect {
    pub addr: SocketAddr
}

impl Message for Connect {
    type Result = Addr<NodeLink>;
}

impl Handler<Connect> for NodeControl {
    type Result = ResponseActFuture<Self, Addr<NodeLink>>;

    fn handle(&mut self, msg: Connect, ctx: &mut Self::Context) -> Self::Result {
        log::warn!("Connecting to remote node");

        // TODO: Retrying
        let conn = async move {
            let stream = TcpStream::connect(msg.addr).await.unwrap();
            NodeLink::new(stream).await
        };

        let link = wrap_future(conn);
        Box::pin(link.map(|(id, peer, link), this: &mut Self, ctx| {
            log::info!("Connected to: {:?}", id);
            this.links.insert(id, link.clone());
            link
        }))
    }
}

pub struct SendToNode(pub Uuid, pub Dispatch);

impl Message for SendToNode {
    type Result = Result<Bytes, DispatchError>;
}

impl Handler<SendToNode> for NodeControl {
    type Result = actix::Response<Bytes, DispatchError>;

    fn handle(&mut self, msg: SendToNode, ctx: &mut Self::Context) -> Self::Result {
        if let Some(link) = self.links.get(&msg.0) {
            let link = link.clone();
            let work = async move {
                link.send(msg.1).await.unwrap()
            };
            actix::Response::fut(Box::pin(work))
        } else {
            return actix::Response::reply(Err(DispatchError::DispatchRemote));
        }
    }
}

#[derive(Debug, Clone)]
pub struct Broadcast(pub Dispatch);

impl Message for Broadcast { type Result = (); }

impl Handler<Broadcast> for NodeControl {
    type Result = ();

    fn handle(&mut self, msg: Broadcast, ctx: &mut Self::Context) -> Self::Result {
        for n in self.links.values() {
            n.do_send(msg.0.clone());
        }
    }
}

impl Handler<RecvFromNode<Dispatch>> for NodeControl {
    type Result = ();

    fn handle(&mut self, msg: RecvFromNode<Dispatch>, ctx: &mut Self::Context) -> Self::Result {
        log::info!("NodeControl dispatching unadressed message");
        match self.dispatch.get_mut(&msg.inner.method) {
            Some(m) => {
                let fut = (m)(msg.node_id, msg.inner.body);
                ctx.spawn(wrap_future(async move { fut.await.unwrap(); }));
            }
            None => {
                log::warn!("Message: {} not handled by any global handler", msg.inner.method);
            }
        }
    }
}

#[derive(Debug)]
pub struct RecvFromNode<M> {
    pub node_id: Uuid,
    pub inner: M,
}

impl<M> Message for RecvFromNode<M> {
    type Result = ();
}

pub type NodeHandler = Box<dyn FnMut(Uuid, Bytes)
    -> Pin<Box<dyn Future<Output=Result<Bytes, DispatchError>>>> + Send + 'static>;

pub struct RegisterSystemHandler {
    method: u64,
    handler: NodeHandler,
}

impl RegisterSystemHandler {
    /// Create new registration request for node-wide handler
    pub fn new<M>(rec: Recipient<RecvFromNode<M>>) -> Self
    where M: actix::Message<Result=()> + Service + Send + 'static,
    {
        let handler = move |node_id, data| -> Pin<Box<dyn Future<Output=_>>> {
            log::info!("Running global message handler");
            let rec = rec.clone();
            Box::pin(async move {
                let msg = M::read(data).map_err(|_| DispatchError::Format)?;
                let msg = RecvFromNode {
                    node_id,
                    inner: msg,
                };
                let res = rec.send(msg).await.map_err(|_| DispatchError::Format)?;
                Ok(Bytes::new())
            })
        };

        RegisterSystemHandler {
            method: M::ID,
            handler: Box::new(handler),
        }
    }
}

impl Message for RegisterSystemHandler {
    type Result = ();
}

impl Handler<RegisterSystemHandler> for NodeControl {
    type Result = ();

    fn handle(&mut self, msg: RegisterSystemHandler, ctx: &mut Context<Self>) -> Self::Result {
        self.dispatch.insert(msg.method, msg.handler);
    }
}