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
use std::net::SocketAddr;
use tokio::sync::mpsc::Sender;
use crate::types::{Broadcast, PeerId, TransportStream, TransportType};
/// Custom apply for build a stream between nodes.
#[derive(Debug, Eq, PartialEq)]
pub enum StreamType {
/// request for build a stream, params is peer id, transport type and request custom info.
Req(PeerId, TransportType),
/// response for build a stream, params is is_ok, and response custom info.
Res(bool),
/// if response is ok, will build a stream, and return the stream to ouside.
Ok(TransportStream),
}
/// delivery message type.
#[derive(Debug, Clone)]
pub enum DeliveryType {
Data,
StableConnect,
StableResult,
}
/// main received message for outside channel, send from chamomile to outside.
#[derive(Debug)]
pub enum ReceiveMessage {
/// when peer what to stable connect, send from chamomile to outside.
/// params is `peer_id`, `socket_addr` and peer `connect_info`.
StableConnect(PeerId, Vec<u8>),
/// when peer get stable connect result.
/// params is `peer_id`, `is_ok` and `result_data`.
StableResult(PeerId, bool, Vec<u8>),
/// when peer want to response a stable result, but the session is closed,
/// if stable result is ok, then need create a result connect to sender.
/// the data type is stable result data type.
ResultConnect(PeerId, Vec<u8>),
/// when a stable connection's peer leave,
/// send from chamomile to outside.
/// params is `peer_id`.
StableLeave(PeerId),
/// when received a data from a trusted peer,
/// send to outside.
/// params is `peer_id` and `data_bytes`.
Data(PeerId, Vec<u8>),
/// (Only stable connected) Apply for build a stream between nodes.
/// params is `u32` stream symbol, and `StreamType`.
Stream(u32, StreamType, Vec<u8>),
/// (Only stable connected) Delivery feedback. include StableConnect, StableResult, Data. `id(u32) != 0`.
Delivery(DeliveryType, u64, bool, Vec<u8>),
/// when network lost all DHT network and direct stables. will tell outside.
NetworkLost,
}
/// main send message for outside channel, send from outside to chamomile.
#[derive(Debug)]
pub enum SendMessage {
/// when peer request for join, outside decide connect or not.
/// params is `delivery_feedback_id`, `peer_id`, `is_connect`, `is_force_close`, `result info`.
/// if `delivery_feedback_id = 0` will not feedback.
/// if `is_connect` is true, it will add to allow directly list.
/// we want to build a better network, add a `is_force_close`.
/// if `is_connect` is false, but `is_force_close` if true, we
/// will use this peer to build our DHT for better connection.
/// if false, we will force close it.
StableResult(u64, PeerId, bool, bool, Vec<u8>),
/// when need add a peer to stable connect, send to chamomile from outside.
/// if success connect, will start a stable connection, and add peer to kad, stables,
/// bootstraps and allowlists. if failure, will send `PeerLeave` to outside.
/// params is `delivery_feedback_id`, `peer_id`, `socket_addr` and peer `join_info`.
/// if `delivery_feedback_id = 0` will not feedback.
StableConnect(u64, PeerId, Option<SocketAddr>, Vec<u8>),
/// when outside want to close a stable connectioned peer. use it force close.
/// params is `peer_id`.
StableDisconnect(PeerId),
/// (DHT connected) when outside want to connect a peer. will try connect directly.
/// if connected, chamomile will add to kad and bootstrap.
/// params is `socket_addr`.
Connect(SocketAddr),
/// (DHT connected) when outside donnot want to connect peer. use it to force close.
/// it will remove from kad and bootstrap list.
/// params is `socket_addr`.
DisConnect(SocketAddr),
/// when need send a data to a peer, only need know the peer_id,
/// the chamomile will help you send data to there.
/// params is `delivery_feedback_id`, `peer_id` and `data_bytes`.
/// if `delivery_feedback_id = 0` will not feedback.
Data(u64, PeerId, Vec<u8>),
/// when need broadcast a data to all network,
/// chamomile support some common algorithm, use it, donnot worry.
/// params is `broadcast_type` and `data_bytes`
Broadcast(Broadcast, Vec<u8>),
/// (Only Stable connected) Apply for build a stream between nodes.
/// params is `u32` stream symbol, and `StreamType`.
Stream(u32, StreamType, Vec<u8>),
/// Request for return the network current state info.
/// params is request type, and return channel's sender (async).
NetworkState(StateRequest, Sender<StateResponse>),
/// When receive `ReceiveMessage::NetworkLost`, want to reboot network, it can use.
NetworkReboot,
}
/// Network state info response.
#[derive(Debug, Clone)]
pub enum StateRequest {
Stable,
DHT,
Seed,
}
/// Network state info response.
#[derive(Debug)]
pub enum StateResponse {
/// response is peer list and peer is relay or directly.
Stable(Vec<(PeerId, bool)>),
/// response is peer list.
DHT(Vec<PeerId>),
/// response is socket list.
Seed(Vec<SocketAddr>),
}