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
use libp2p::PeerId;
use state::Storage;
use std::collections::VecDeque;
use std::sync::RwLock;
pub static FEEDREQUESTER: Storage<RwLock<FeedRequester>> = Storage::new();
pub static FEEDRESPONSER: Storage<RwLock<FeedResponser>> = Storage::new();
pub struct FeedRequest {
pub neighbour_id: PeerId,
pub feed_ids: Vec<Vec<u8>>,
}
pub struct FeedRequester {
pub to_send: VecDeque<FeedRequest>,
}
impl FeedRequester {
pub fn init() {
let feed_requester = FeedRequester {
to_send: VecDeque::new(),
};
FEEDREQUESTER.set(RwLock::new(feed_requester));
}
pub fn add(neighbour_id: &PeerId, feed_ids: &Vec<Vec<u8>>) {
let msg = FeedRequest {
neighbour_id: neighbour_id.clone(),
feed_ids: feed_ids.clone(),
};
let mut feed_requester = FEEDREQUESTER.get().write().unwrap();
feed_requester.to_send.push_back(msg);
}
}
pub struct FeedResponse {
pub neighbour_id: PeerId,
pub feeds: Vec<(Vec<u8>, Vec<u8>, String, u64)>,
}
pub struct FeedResponser {
pub to_send: VecDeque<FeedResponse>,
}
impl FeedResponser {
pub fn init() {
let feed_responser = FeedResponser {
to_send: VecDeque::new(),
};
FEEDRESPONSER.set(RwLock::new(feed_responser));
}
pub fn add(neighbour_id: &PeerId, feeds: &Vec<(Vec<u8>, Vec<u8>, String, u64)>) {
let mut msg = FeedResponse {
neighbour_id: neighbour_id.clone(),
feeds: vec![],
};
for (message_id, sender_id, content, time) in feeds {
msg.feeds.push((
message_id.clone(),
sender_id.clone(),
content.clone(),
*time,
));
}
let mut feed_responser = FEEDRESPONSER.get().write().unwrap();
feed_responser.to_send.push_back(msg);
}
}