1- use std:: any:: Any ;
2- use std:: { collections:: HashMap , str:: FromStr } ;
3- use std:: fmt:: Display ;
41use anyhow:: anyhow;
5- use serde:: { Deserialize , Serialize } ;
6- use wg_internal:: { network:: NodeId , packet:: Packet } ;
72use crossbeam_channel:: Sender ;
3+ use serde:: { Deserialize , Serialize } ;
4+ use std:: any:: Any ;
5+ use std:: fmt:: Display ;
6+ use std:: { collections:: HashMap , str:: FromStr } ;
87use uuid:: Uuid ;
8+ use wg_internal:: { network:: NodeId , packet:: Packet } ;
99pub type Bytes = Vec < u8 > ;
1010
1111#[ derive( Debug , Clone , Serialize , Deserialize , Hash , PartialEq , Eq ) ]
1212pub struct MediaReference {
1313 location : NodeId ,
14- pub id : Uuid
14+ pub id : Uuid ,
1515}
1616
1717impl MediaReference {
1818 #[ must_use]
1919 pub fn new ( location : NodeId ) -> Self {
2020 Self {
2121 location,
22- id : Uuid :: new_v4 ( )
22+ id : Uuid :: new_v4 ( ) ,
2323 }
2424 }
2525
@@ -43,30 +43,32 @@ impl FromStr for MediaReference {
4343 if let Some ( c) = value. chars ( ) . position ( |c| c == '/' ) {
4444 c
4545 } else {
46- return Err ( anyhow ! ( "Cannot parse media reference" ) )
46+ return Err ( anyhow ! ( "Cannot parse media reference" ) ) ;
4747 }
4848 } ) ;
49- Ok ( Self { location : u8:: from_str ( location) ?, id : Uuid :: from_str ( id) ? } )
49+ Ok ( Self {
50+ location : u8:: from_str ( location) ?,
51+ id : Uuid :: from_str ( id) ?,
52+ } )
5053 }
5154}
5255
5356#[ derive( Debug , Clone , Serialize , Deserialize , Hash , PartialEq , Eq ) ]
54- pub struct TextFile {
57+ pub struct TextFile {
5558 pub id : Uuid ,
5659 pub title : String ,
5760 pub content : String ,
58- pub media_refs : Vec < MediaReference >
61+ pub media_refs : Vec < MediaReference > ,
5962}
6063
61-
6264impl TextFile {
6365 #[ must_use]
6466 pub fn new ( title : String , content : String , media_refs : Vec < MediaReference > ) -> Self {
6567 Self {
6668 title,
6769 id : Uuid :: new_v4 ( ) ,
6870 content,
69- media_refs
71+ media_refs,
7072 }
7173 }
7274
@@ -75,14 +77,12 @@ impl TextFile {
7577 self . media_refs . clone ( )
7678 }
7779
78-
7980 #[ must_use]
8081 pub fn get_media_ids ( & self ) -> Vec < Uuid > {
8182 self . media_refs . iter ( ) . map ( |m| m. id ) . collect ( )
8283 }
8384}
8485
85-
8686#[ derive( Debug , Clone , Serialize , Deserialize , Hash , PartialEq , Eq ) ]
8787
8888pub struct MediaFile {
@@ -104,10 +104,7 @@ impl MediaFile {
104104 #[ must_use]
105105 pub fn from_u8 ( filename : String , data : & [ u8 ] ) -> Self {
106106 let chunk_size = 1024 ;
107- let content: Vec < Bytes > = data
108- . chunks ( chunk_size)
109- . map ( <[ u8 ] >:: to_vec)
110- . collect ( ) ;
107+ let content: Vec < Bytes > = data. chunks ( chunk_size) . map ( <[ u8 ] >:: to_vec) . collect ( ) ;
111108
112109 Self :: new ( filename, content)
113110 }
@@ -132,22 +129,20 @@ impl MediaFile {
132129pub struct File {
133130 pub id : Uuid ,
134131 pub text_file : TextFile ,
135- media_files : Vec < MediaFile >
132+ media_files : Vec < MediaFile > ,
136133}
137134
138-
139135impl File {
140136 #[ must_use]
141137 pub fn new ( text_file : TextFile , media_files : Vec < MediaFile > ) -> Self {
142138 Self {
143139 id : text_file. id ,
144140 text_file,
145- media_files
141+ media_files,
146142 }
147143 }
148144}
149145
150-
151146#[ derive( Serialize , Deserialize , Debug ) ]
152147#[ serde( tag = "request_type" ) ]
153148pub enum WebRequest {
@@ -168,9 +163,9 @@ impl WebRequest {
168163 #[ must_use]
169164 pub fn get_file_id ( & self ) -> Option < String > {
170165 match self {
171- Self :: FileQuery { file_id} => Some ( file_id. clone ( ) ) ,
166+ Self :: FileQuery { file_id } => Some ( file_id. clone ( ) ) ,
172167 Self :: MediaQuery { media_id } => Some ( media_id. clone ( ) ) ,
173- _ => None
168+ _ => None ,
174169 }
175170 }
176171}
@@ -197,7 +192,7 @@ pub enum WebResponse {
197192 BadUuid ( String ) ,
198193}
199194
200- #[ derive( Serialize , Deserialize , Debug ) ]
195+ #[ derive( Clone , Serialize , Deserialize , Debug ) ]
201196#[ serde( tag = "request_type" ) ]
202197pub enum ChatRequest {
203198 #[ serde( rename = "server_type?" ) ]
@@ -226,7 +221,7 @@ pub enum ChatResponse {
226221 MessageFrom { client_id : NodeId , message : String } ,
227222
228223 #[ serde( rename = "error_wrong_client_id!" ) ]
229- ErrorWrongClientId ,
224+ ErrorWrongClientId { wrong_id : NodeId } ,
230225
231226 // Custom response for successful registration
232227 #[ serde( rename = "registration_success" ) ]
@@ -250,7 +245,6 @@ impl Message {
250245pub trait Command : Send {
251246 fn as_any ( & self ) -> & dyn Any ;
252247 fn into_any ( self : Box < Self > ) -> Box < dyn Any > ;
253-
254248}
255249
256250impl < T : ' static + Send > Command for T {
@@ -262,7 +256,6 @@ impl<T: 'static + Send> Command for T {
262256 }
263257}
264258
265-
266259pub trait Event : Send {
267260 fn as_any ( & self ) -> & dyn Any ;
268261 fn into_any ( self : Box < Self > ) -> Box < dyn Any > ;
@@ -281,18 +274,49 @@ impl<T: 'static + Send> Event for T {
281274pub enum ChatCommand {
282275 GetChatsHistory ,
283276 GetRegisteredClients ,
284- SendMessage ( Message )
277+ SendMessage ( Message ) ,
285278}
286279
287-
288280#[ derive( Debug , Clone ) ]
289281pub enum ChatEvent {
290- ChatHistory ( HashMap < NodeId , Vec < Message > > ) ,
291- RegisteredClients ( Vec < NodeId > ) ,
292- MessageSent ,
293- MessageReceived ( Message ) ,
294- ClientRegistered ( NodeId , NodeId ) , // client_id, server_id
295- ClientListQueried ( NodeId , NodeId ) , // requester_id, server_id
282+ ChatHistory {
283+ notification_from : NodeId ,
284+ history : HashMap < NodeId , Vec < Message > > ,
285+ } ,
286+ RegisteredClients {
287+ notification_from : NodeId ,
288+ list : Vec < NodeId > ,
289+ } ,
290+ MessageSent {
291+ notification_from : NodeId ,
292+ to : NodeId ,
293+ } ,
294+ MessageReceived {
295+ notification_from : NodeId ,
296+ msg : Message ,
297+ } ,
298+ ClientRegistered {
299+ client : NodeId ,
300+ server : NodeId ,
301+ } , // client_id, server_id
302+ ClientListQueried {
303+ notification_from : NodeId ,
304+ from : NodeId ,
305+ } , // requester_id, server_id
306+ ClientNotInList {
307+ notification_from : NodeId ,
308+ id : NodeId ,
309+ } ,
310+ ErrorClientNotFound {
311+ notification_from : NodeId ,
312+ location : NodeId ,
313+ not_found : NodeId ,
314+ } ,
315+
316+ RegistrationSucceeded {
317+ notification_from : NodeId ,
318+ to : NodeId ,
319+ } ,
296320}
297321
298322#[ derive( Debug , Clone ) ]
@@ -311,36 +335,92 @@ pub enum WebCommand {
311335 RemoveMediaFile ( Uuid ) ,
312336}
313337
314-
315338#[ derive( Debug , Clone ) ]
316339pub enum WebEvent {
317- CachedFiles ( Vec < File > ) ,
318- File ( File ) ,
319- TextFiles ( Vec < TextFile > ) ,
320- TextFile ( TextFile ) ,
321- MediaFiles ( Vec < MediaFile > ) ,
322- MediaFile ( MediaFile ) ,
323- FileNotFound ( Uuid ) ,
324- TextFileAdded ( Uuid ) ,
325- MediaFileAdded ( Uuid ) ,
326- TextFileRemoved ( Uuid ) ,
327- MediaFileRemoved ( Uuid ) ,
328- FileOperationError ( String ) ,
329- FileRequested ( NodeId , String ) , // requester_id, file_id
330- FileServed ( NodeId , String ) , // server_id, file_id
331- FilesListQueried ( NodeId , NodeId ) , // requester_id, server_id
332- BadUid ( NodeId , NodeId , String ) , // requester_id, server_id, uuid
340+ CachedFiles {
341+ notification_from : NodeId ,
342+ files : Vec < File > ,
343+ } ,
344+ File {
345+ notification_from : NodeId ,
346+ file : File ,
347+ } ,
348+ TextFiles {
349+ notification_from : NodeId ,
350+ files : Vec < TextFile > ,
351+ } ,
352+ TextFile {
353+ notification_from : NodeId ,
354+ file : TextFile ,
355+ } ,
356+ MediaFiles {
357+ notification_from : NodeId ,
358+ files : Vec < MediaFile > ,
359+ } ,
360+ MediaFile {
361+ notification_from : NodeId ,
362+ file : MediaFile ,
363+ } ,
364+ FileNotFound {
365+ notification_from : NodeId ,
366+ uuid : Uuid ,
367+ } ,
368+ TextFileAdded {
369+ notification_from : NodeId ,
370+ uuid : Uuid ,
371+ } ,
372+ MediaFileAdded {
373+ notification_from : NodeId ,
374+ uuid : Uuid ,
375+ } ,
376+ TextFileRemoved {
377+ notification_from : NodeId ,
378+ uuid : Uuid ,
379+ } ,
380+ MediaFileRemoved {
381+ notification_from : NodeId ,
382+ uuid : Uuid ,
383+ } ,
384+ FileOperationError {
385+ notification_from : NodeId ,
386+ msg : String ,
387+ } ,
388+ FileRequested {
389+ notification_from : NodeId ,
390+ uuid : String ,
391+ } , // requester_id, file_id
392+ FileServed {
393+ notification_from : NodeId ,
394+ file : String ,
395+ } , // server_id, file_id
396+ FilesListQueried {
397+ notification_from : NodeId ,
398+ to : NodeId ,
399+ } , // requester_id, server_id
400+ BadUuid {
401+ notification_from : NodeId ,
402+ from : NodeId ,
403+ uuid : String ,
404+ } , // requester_id, server_id, uuid
333405}
334406
335-
336407#[ derive( Debug , Clone ) ]
337408pub enum NodeEvent {
338409 PacketSent ( Packet ) ,
339410 FloodStarted ( u64 , NodeId ) ,
340411 NodeRemoved ( NodeId ) ,
341- MessageReceived ( NodeId , NodeId ) , // from, to
342- MessageSent ( NodeId , NodeId ) , // from, to
343- ServerTypeQueried ( NodeId , NodeId ) , // requester_id, server_id
412+ MessageReceived {
413+ notification_from : NodeId ,
414+ from : NodeId ,
415+ } , // from, to
416+ MessageSent {
417+ notification_from : NodeId ,
418+ to : NodeId ,
419+ } , // from, to
420+ ServerTypeQueried {
421+ notification_from : NodeId ,
422+ to : NodeId ,
423+ } , // requester_id, server_id
344424}
345425
346426#[ derive( Debug , Clone ) ]
@@ -350,7 +430,6 @@ pub enum NodeCommand {
350430 Shutdown ,
351431}
352432
353-
354433impl NodeCommand {
355434 #[ must_use]
356435 pub fn as_add_sender ( self ) -> Option < ( NodeId , Sender < Packet > ) > {
0 commit comments