1use 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#[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 Ok(())
72 }
73}