@@ -4,6 +4,7 @@ use std::error::Error;
4
4
use std:: collections:: HashMap ;
5
5
use crate :: networking:: NetworkManager ;
6
6
use crate :: networking:: address:: Address ;
7
+ use crate :: networking:: NetworkSender ;
7
8
8
9
#[ cfg( test) ]
9
10
use std:: sync:: atomic:: AtomicUsize ;
@@ -31,102 +32,8 @@ static WARN_DEPRECATED_CALLS: AtomicUsize = AtomicUsize::new(0);
31
32
///
32
33
/// _**Note:** Try to avoid the use of .unwrap() in actual production code, this is just an example_
33
34
///
34
- ///
35
- /// ```
36
- /// use ipv8::community::peer::Peer;
37
- /// use ipv8::community::Community;
38
- /// use ipv8::serialization::header::Header;
39
- /// use ipv8::serialization::{PacketDeserializer, Packet};
40
- /// use std::net::{Ipv4Addr, SocketAddr, IpAddr};
41
- /// use ipv8::networking::address::Address;
42
- /// use std::error::Error;
43
- /// use ipv8::IPv8;
44
- /// use ipv8::configuration::Config;
45
- /// use ipv8::serialization::header::HeaderVersion::PyIPV8Header;
46
- /// use ipv8::crypto::keytypes::PublicKey;
47
- /// use ipv8::networking::NetworkManager;
48
- /// use rust_sodium::crypto::sign::ed25519;
49
- ///
50
- /// pub struct TestCommunity{
51
- /// peer: Peer
52
- /// }
53
- ///
54
- /// impl TestCommunity{
55
- /// }
56
- ///
57
- /// impl Community for TestCommunity{
58
- /// fn new(endpoint: &NetworkManager) -> Result<Self, Box<dyn Error>> {
59
- /// // Use the highest available key
60
- /// let seed = ed25519::Seed::from_slice(&[
61
- /// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
62
- /// 23, 24, 25, 26, 27, 28, 29, 30, 31,
63
- /// ])
64
- /// .unwrap();
65
- /// let (pkey1, _) = ed25519::keypair_from_seed(&seed);
66
- /// let (pkey2, _) = ed25519::keypair_from_seed(&seed);
67
- /// let pk = PublicKey(pkey1, pkey2);
68
- /// // Actually create the community
69
- /// Ok(TestCommunity {
70
- /// peer: Peer::new(
71
- /// pk,
72
- /// Address(SocketAddr::new(
73
- /// IpAddr::V4(Ipv4Addr::new(42, 42, 42, 42)),
74
- /// 8000,
75
- /// )),
76
- /// true,
77
- /// ),
78
- /// })
79
- /// }
80
- ///
81
- /// // Returns the hash of our master peer
82
- /// fn get_mid(&self) -> Vec<u8> {
83
- /// self.peer.get_sha1().0
84
- /// }
85
- ///
86
- /// // The function which will be called when the community receives a packet
87
- /// fn on_receive(&self, header: Header, deserializer: PacketDeserializer, address: Address) -> Result<(),Box<dyn Error>>{
88
- /// # assert_eq!(header.mid_hash.unwrap(), self.get_mid());
89
- /// # assert_eq!(header.version, PyIPV8Header);
90
- /// # assert_eq!(header.message_type, Some(42));
91
- /// // Do some stuff here like to distribute the message based on it's message_type (in the header)
92
- /// // and check it's signature
93
- /// Ok(())
94
- /// }
95
- /// }
96
- ///
97
- /// let mut config = Config::default();
98
- /// let mut ipv8 = IPv8::new(config).unwrap();
99
- ///
100
- /// let community = TestCommunity::new(&ipv8.networkmanager).unwrap();
101
- /// let mid = community.get_mid();
102
- /// ipv8.communities.add_community(Box::new(community));
103
- ///
104
- /// // now simulate a packet coming in
105
- ///
106
- /// // Create a packet to test the community with
107
- /// let packet = Packet::new(Header{
108
- /// size: 23,
109
- /// version: PyIPV8Header,
110
- /// mid_hash: Some(mid),
111
- /// message_type: Some(42),
112
- /// }).unwrap();
113
- ///
114
- /// // Normally you would want to sign the packet here
115
- ///
116
- /// // Send the packet
117
- /// ipv8.communities
118
- /// .forward_message(
119
- /// packet,
120
- /// Address(SocketAddr::new(
121
- /// IpAddr::V4(Ipv4Addr::new(42, 42, 42, 42)),
122
- /// 42,
123
- /// )),
124
- /// )
125
- /// .unwrap();
126
- ///
127
- /// ```
128
35
pub trait Community {
129
- fn new ( endpoint : & NetworkManager ) -> Result < Self , Box < dyn Error > >
36
+ fn new ( endpoint : & NetworkSender ) -> Result < Self , Box < dyn Error > >
130
37
where
131
38
Self : Sized ;
132
39
@@ -204,15 +111,6 @@ pub trait Community {
204
111
deserializer : PacketDeserializer ,
205
112
address : Address ,
206
113
) -> Result < ( ) , Box < dyn Error > > ;
207
-
208
- fn send (
209
- & self ,
210
- endpoint : NetworkManager ,
211
- address : Address ,
212
- packet : Packet ,
213
- ) -> Result < ( ) , Box < dyn Error > > {
214
- endpoint. send ( & address, packet)
215
- }
216
114
}
217
115
218
116
/// Every different kind of community is registered here with it's MID.
@@ -221,6 +119,9 @@ pub trait Community {
221
119
/// O(1) lookup time.
222
120
pub struct CommunityRegistry {
223
121
// mid, community
122
+ #[ cfg( test) ]
123
+ pub communities : HashMap < Vec < u8 > , Box < dyn Community > > ,
124
+ #[ cfg( not( test) ) ]
224
125
communities : HashMap < Vec < u8 > , Box < dyn Community > > ,
225
126
}
226
127
@@ -270,8 +171,20 @@ mod tests {
270
171
use super :: * ;
271
172
use crate :: networking:: NetworkManager ;
272
173
use crate :: networking:: address:: Address ;
273
- use std:: net:: { SocketAddr , IpAddr } ;
174
+ use std:: net:: { SocketAddr , IpAddr , SocketAddrV4 } ;
274
175
use std:: error:: Error ;
176
+ use crate :: networking:: NetworkSender ;
177
+ use crate :: community:: peer:: Peer ;
178
+ use crate :: community:: { Community , CommunityRegistry } ;
179
+ use crate :: serialization:: header:: Header ;
180
+ use crate :: serialization:: { PacketDeserializer , Packet } ;
181
+ use std:: net:: Ipv4Addr ;
182
+ use crate :: IPv8 ;
183
+ use crate :: configuration:: Config ;
184
+ use crate :: serialization:: header:: HeaderVersion :: PyIPV8Header ;
185
+ use crate :: crypto:: keytypes:: PublicKey ;
186
+ use std:: sync:: atomic:: Ordering ;
187
+ use crate :: networking:: test_helper:: localhost;
275
188
use crate :: community:: peer:: Peer ;
276
189
use crate :: serialization:: header:: Header ;
277
190
use crate :: serialization:: { PacketDeserializer , Packet } ;
@@ -312,17 +225,18 @@ mod tests {
312
225
}
313
226
314
227
// Returns the hash of our master peer
315
- fn get_mid ( & self ) -> Vec < u8 > {
316
- self . peer . get_sha1 ( ) . 0
228
+ fn get_mid ( & self ) -> Option < Vec < u8 > > {
229
+ Some ( self . peer . get_sha1 ( ) ? . to_vec ( ) )
317
230
}
318
-
231
+
319
232
// The function which will be called when the community receives a packet
320
233
fn on_receive (
321
234
& self ,
322
235
header : Header ,
323
236
deserializer : PacketDeserializer ,
324
237
_address : Address ,
325
238
) -> Result < ( ) , Box < dyn Error > > {
239
+
326
240
assert_eq ! ( header. mid_hash. unwrap( ) , self . get_mid( ) ) ;
327
241
assert_eq ! ( header. version, PyIPV8Header ) ;
328
242
assert_eq ! ( header. message_type, Some ( 42 ) ) ;
@@ -356,17 +270,35 @@ mod tests {
356
270
let header = deser. peek_header ( ) . unwrap ( ) ;
357
271
community. receive ( header, deser, address) . unwrap ( ) ;
358
272
}
359
-
360
273
assert_eq ! ( 17 , WARN_DEPRECATED_CALLS . load( Ordering :: SeqCst ) )
361
274
}
362
275
363
276
#[ test]
364
277
fn test_networking ( ) {
365
278
let config = Config :: default ( ) ;
279
+ let ipv8 = IPv8 :: new ( config) . unwrap ( ) ;
280
+ let community = Box :: new ( TestCommunity :: new ( & ipv8. network_sender ) . unwrap ( ) ) ;
281
+ let the_same = Box :: new ( TestCommunity :: new ( & ipv8. network_sender ) . unwrap ( ) ) ;
282
+ let mid = & * community. get_mid ( ) . unwrap ( ) ;
283
+ let mut registry: CommunityRegistry = CommunityRegistry :: default ( ) ;
284
+
285
+ registry. add_community ( community) . unwrap ( ) ;
286
+
287
+ let get = registry. communities . get ( mid) . unwrap ( ) ;
288
+
289
+ assert_eq ! ( the_same. get_mid( ) , get. get_mid( ) ) ; // TODO: More thorough comparison
290
+ }
291
+
292
+ #[ test]
293
+ fn test_networking ( ) {
294
+ let mut config = Config :: default ( ) ;
295
+ config. receiving_address = localhost ( ) ;
296
+ config. sending_address = localhost ( ) ;
297
+ config. buffersize = 2048 ;
366
298
367
299
let mut ipv8 = IPv8 :: new ( config) . unwrap ( ) ;
368
300
369
- let community = TestCommunity :: new ( & ipv8. networkmanager ) . unwrap ( ) ;
301
+ let community = TestCommunity :: new ( & ipv8. network_sender ) . unwrap ( ) ;
370
302
let mid = community. get_mid ( ) ;
371
303
372
304
ipv8. communities . add_community ( Box :: new ( community) ) . unwrap ( ) ;
0 commit comments