ng_net/actors/
probe.rs

1/*
2 * Copyright (c) 2022-2025 Niko Bonnieure, Par le Peuple, NextGraph.org developers
3 * All rights reserved.
4 * Licensed under the Apache License, Version 2.0
5 * <LICENSE-APACHE2 or http://www.apache.org/licenses/LICENSE-2.0>
6 * or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
7 * at your option. All files in the project carrying such
8 * notice may not be copied, modified, or distributed except
9 * according to those terms.
10*/
11
12use std::sync::Arc;
13
14use async_std::sync::Mutex;
15use serde::{Deserialize, Serialize};
16
17use ng_repo::errors::*;
18
19use crate::connection::NoiseFSM;
20use crate::types::{ProbeResponse, MAGIC_NG_REQUEST};
21use crate::{actor::*, types::ProtocolMessage};
22
23/// Send to probe if the server is a NextGraph broker.
24#[derive(Clone, Debug, Serialize, Deserialize)]
25pub struct Probe {}
26
27impl TryFrom<ProtocolMessage> for ProbeResponse {
28    type Error = ProtocolError;
29    fn try_from(msg: ProtocolMessage) -> Result<Self, Self::Error> {
30        if let ProtocolMessage::ProbeResponse(res) = msg {
31            Ok(res)
32        } else {
33            Err(ProtocolError::InvalidValue)
34        }
35    }
36}
37
38impl TryFrom<ProtocolMessage> for Probe {
39    type Error = ProtocolError;
40    fn try_from(msg: ProtocolMessage) -> Result<Self, Self::Error> {
41        if let ProtocolMessage::Probe(magic) = msg {
42            if magic == MAGIC_NG_REQUEST {
43                Ok(Probe {})
44            } else {
45                Err(ProtocolError::InvalidValue)
46            }
47        } else {
48            Err(ProtocolError::InvalidValue)
49        }
50    }
51}
52
53impl From<Probe> for ProtocolMessage {
54    fn from(_msg: Probe) -> ProtocolMessage {
55        ProtocolMessage::Probe(MAGIC_NG_REQUEST)
56    }
57}
58
59impl Actor<'_, Probe, ProbeResponse> {}
60
61#[async_trait::async_trait]
62impl EActor for Actor<'_, Probe, ProbeResponse> {
63    async fn respond(
64        &mut self,
65        msg: ProtocolMessage,
66        _fsm: Arc<Mutex<NoiseFSM>>,
67    ) -> Result<(), ProtocolError> {
68        let _req = Probe::try_from(msg)?;
69        //let res = ProbeResponse();
70        //fsm.lock().await.send(res.into()).await?;
71        Ok(())
72    }
73}