ant_protocol/messages.rs
1// Copyright 2024 MaidSafe.net limited.
2//
3// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
4// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
5// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
6// KIND, either express or implied. Please review the Licences for the specific language governing
7// permissions and limitations relating to use of the SAFE Network Software.
8
9//! Data messages and their possible responses.
10mod chunk_proof;
11mod cmd;
12mod connection_info;
13mod node_id;
14mod query;
15mod response;
16
17pub use self::{
18 chunk_proof::{ChunkProof, Nonce},
19 cmd::Cmd,
20 connection_info::ConnectionInfo,
21 node_id::NodeId,
22 query::Query,
23 response::{CmdResponse, QueryResponse},
24};
25
26use super::NetworkAddress;
27
28use serde::{Deserialize, Serialize};
29
30/// A request to peers in the network
31#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
32pub enum Request {
33 /// A cmd sent to peers. Cmds are writes, i.e. can cause mutation.
34 Cmd(Cmd),
35 /// A query sent to peers. Queries are read-only.
36 Query(Query),
37}
38
39/// A response to peers in the network.
40#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
41pub enum Response {
42 /// The response to a cmd.
43 Cmd(CmdResponse),
44 /// The response to a query.
45 Query(QueryResponse),
46}
47
48impl Request {
49 /// Used to send a request to the close group of the address.
50 pub fn dst(&self) -> NetworkAddress {
51 match self {
52 Request::Cmd(cmd) => cmd.dst(),
53 Request::Query(query) => query.dst(),
54 }
55 }
56}
57
58impl std::fmt::Display for Response {
59 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60 write!(f, "{self:?}")
61 }
62}