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
use super::{
super::data::ipc::{IFCollectionOutputData, IPC},
sm::{
self, AdbfStateChart, Error as SMError, Events, Events::*, NewPeerData, States, UpdateData,
},
ui_data::UiData,
};
use crossbeam::channel::Receiver;
use libp2p::{
core::{
connection::{ConnectedPoint, ConnectionId},
Multiaddr, PeerId,
},
swarm::{
protocols_handler, NetworkBehaviour,
NetworkBehaviourAction::{self, GenerateEvent},
PollParameters, ProtocolsHandler,
},
};
use std::{
collections::vec_deque::VecDeque,
task::{Context, Poll},
};
#[allow(non_camel_case_types)]
#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum sm_to_net {
FoundNewPeer(String),
}
#[allow(non_camel_case_types)]
#[derive(Serialize, Deserialize, Debug)]
pub enum SMOutEvents {
ForwardSM(sm_to_net),
ForwardIPC(IPC),
}
pub struct SMBehaviour {
sm: sm::StateMachine<AdbfStateChart>,
own_peer: PeerId,
send_buffer: VecDeque<SMOutEvents>,
ipc_receiver: Receiver<IPC>,
}
impl SMBehaviour {
pub fn new(ipc_receiver: Receiver<IPC>, own_peer: PeerId, ui_data: UiData) -> Self {
Self {
sm: AdbfStateChart::init(AdbfStateChart::new(ui_data)),
own_peer,
send_buffer: VecDeque::new(),
ipc_receiver,
}
}
pub fn own_peer(&self) -> PeerId {
self.own_peer.clone()
}
pub fn mdns_new_peer(&mut self, peer_id: &PeerId, multi_addr: &Multiaddr) {
let new_peer_event = GotANewPeer(NewPeerData {
id: peer_id.clone(),
addr: multi_addr.clone(),
});
self.process_and_react(new_peer_event);
}
pub fn mdns_remove(&mut self, peer_id: &PeerId) {
let remove_peer_event = HaveToRemovePeer(peer_id.clone());
self.process_and_react(remove_peer_event);
}
pub fn update_peer_data(&mut self, peer_id: &PeerId, data: IFCollectionOutputData) {
let to_update_peer = UpdatePeer(UpdateData {
id: peer_id.clone(),
data: data.clone(),
});
self.process_and_react(to_update_peer);
}
fn process_and_react(&mut self, event: Events) {
let return_state = self.sm.process_event(event);
match return_state {
Ok(good_state) => match good_state {
States::Start => (),
States::WaitingForPeerAction => (),
},
Err(bad_state) => {
match bad_state {
SMError::InvalidEvent => warn!("unexpected event transition"),
SMError::GuardFailed => (),
}
}
}
}
}
impl NetworkBehaviour for SMBehaviour {
type ProtocolsHandler = protocols_handler::DummyProtocolsHandler;
type OutEvent = SMOutEvents;
fn new_handler(&mut self) -> Self::ProtocolsHandler {
protocols_handler::DummyProtocolsHandler::default()
}
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
Vec::new()
}
fn inject_connected(&mut self, _: &PeerId) {}
fn inject_disconnected(&mut self, _: &PeerId) {}
fn inject_connection_established(&mut self, _: &PeerId, _: &ConnectionId, _: &ConnectedPoint) {}
fn inject_connection_closed(&mut self, _: &PeerId, _: &ConnectionId, _: &ConnectedPoint) {}
fn inject_event(
&mut self,
_: PeerId,
_: ConnectionId,
_: <Self::ProtocolsHandler as ProtocolsHandler>::OutEvent,
) {
}
fn poll(
&mut self,
_: &mut Context,
_: &mut impl PollParameters,
) -> Poll<
NetworkBehaviourAction<
<Self::ProtocolsHandler as ProtocolsHandler>::InEvent,
Self::OutEvent,
>,
> {
match self.ipc_receiver.try_recv() {
Ok(ipc_msg) => {
self.send_buffer.push_back(SMOutEvents::ForwardIPC(ipc_msg))
}
Err(_) => (),
}
if let Some(item) = self.send_buffer.pop_front() {
Poll::Ready(GenerateEvent(item))
} else {
Poll::Pending
}
}
}