1- use std:: { error:: Error , sync :: Arc } ;
1+ use std:: error:: Error ;
22use axum:: Json ;
3- use tokio:: sync:: { broadcast, Mutex } ;
3+ use tokio:: sync:: broadcast;
44use wtransport:: { endpoint:: IncomingSession , Connection , Endpoint , Identity , ServerConfig } ;
5+ use crate :: datas:: EventDTO ;
6+ use crate :: events:: docker:: DockerEvent ;
7+ use crate :: events:: system:: SystemEvent ;
58use crate :: services:: docker;
9+ use crate :: events:: Event ;
610
711pub async fn start_webtransport ( ) -> Result < ( ) , Box < dyn Error + Send + Sync > > {
812 let identity = match Identity :: load_pemfiles ( "localhost.pem" , "localhost-key.pem" ) . await {
@@ -62,17 +66,18 @@ async fn handle_connection(incoming_session: IncomingSession, tx: broadcast::Sen
6266 }
6367 } ;
6468
65- log:: info!( "Accepted connection from {:?}" , connection. remote_address( ) ) ;
66-
67- let datagram_handle = tokio:: spawn ( handle_datagram ( connection. clone ( ) ) ) ;
69+ let datagram_handle = tokio:: spawn ( handle_datagram ( connection. clone ( ) , tx. clone ( ) ) ) ;
6870
6971 let bidirectional_handle = tokio:: spawn ( handle_bidirectionnal ( connection, tx) ) ;
7072
7173 let _ = tokio:: join!( datagram_handle, bidirectional_handle) ;
7274 Ok ( ( ) )
7375}
7476
75- async fn handle_datagram ( connection : Connection ) -> Result < ( ) , Box < dyn Error + Send + Sync > > {
77+ async fn handle_datagram ( connection : Connection , tx : broadcast:: Sender < String > ) -> Result < ( ) , Box < dyn Error + Send + Sync > > {
78+ log:: info!( "Accepted datagram connection from {:?}" , connection. remote_address( ) ) ;
79+ let mut rx = tx. subscribe ( ) ;
80+
7681 loop {
7782 let datagram = match connection. receive_datagram ( ) . await {
7883 Ok ( datagram) => datagram,
@@ -82,59 +87,63 @@ async fn handle_datagram(connection: Connection) -> Result<(), Box<dyn Error + S
8287 }
8388 } ;
8489 let received_message = String :: from_utf8_lossy ( & datagram) ;
85- log:: info!( "Received message: {:?}" , received_message) ;
90+ log:: info!( "Received datagram message: {:?}" , received_message) ;
8691
87- let response = b"Hello from server via datagram!" ;
88- match connection. send_datagram ( response) {
89- Ok ( _) => { } ,
92+ let event: Event = match serde_json:: from_str ( & received_message) {
93+ Ok ( event) => event,
9094 Err ( e) => {
91- log:: error!( "Failed to send datagram : {:?}" , e) ;
92- return Err ( Box :: new ( e ) ) ;
95+ log:: error!( "Failed to parse event : {:?}" , e) ;
96+ continue ;
9397 }
9498 } ;
99+
100+ match & event {
101+ Event :: Docker ( docker_event) => {
102+ match docker_event {
103+ DockerEvent :: DockerStatus => {
104+ let event = "DockerStatus" . to_string ( ) ;
105+ connection. send_datagram ( event. as_bytes ( ) ) ?;
106+ } ,
107+ DockerEvent :: DockerContainersRestart { data } => {
108+ let containers = docker:: get_containers ( ) . await ?;
109+ log:: info!( "Restarting container: {:?}" , containers. to_json( ) . as_bytes( ) . len( ) ) ;
110+ if let Err ( error) = connection. send_datagram ( containers. to_json ( ) ) {
111+ log:: error!( "Failed to send event: {:?}" , error) ;
112+ }
113+ }
114+ }
115+ } ,
116+ Event :: System ( system_event) => {
117+ match system_event {
118+ SystemEvent :: SystemStatus => {
119+ let event = "SystemStatus" . to_string ( ) ;
120+ connection. send_datagram ( event. as_bytes ( ) ) ?;
121+ }
122+ }
123+ } ,
124+ }
95125 }
96126}
97127
98128async fn handle_bidirectionnal ( connection : Connection , tx : broadcast:: Sender < String > ) -> Result < ( ) , Box < dyn Error + Send + Sync > > {
99- log:: info!( "Accepted connection from {:?}" , connection. remote_address( ) ) ;
129+ log:: info!( "Accepted bidirectional connection from {:?}" , connection. remote_address( ) ) ;
100130
101- while let Ok ( ( send_stream, mut recv_stream) ) = connection. accept_bi ( ) . await {
131+ while let Ok ( ( mut send_stream, mut recv_stream) ) = connection. accept_bi ( ) . await {
102132 log:: trace!( "Accepted bidirectional stream" ) ;
103133
104134 let mut rx = tx. subscribe ( ) ;
105- let send_stream = Arc :: new ( Mutex :: new ( send_stream) ) ;
106135
107- {
108- let send_stream = Arc :: clone ( & send_stream) ;
109- tokio:: spawn ( async move {
110- let mut buffer = vec ! [ 0 ; 1024 ] ;
111-
112- while let Ok ( Some ( bytes_read) ) = recv_stream. read ( & mut buffer) . await {
113- let received_message = String :: from_utf8_lossy ( & buffer[ ..bytes_read] ) ;
114- log:: info!( "Received message: {:?}" , received_message) ;
115-
116- let mut send_stream = send_stream. lock ( ) . await ;
117-
118- let _ = send_stream. write_all ( b"Hello from server!" ) . await ;
119- }
120- } ) ;
121- }
122-
123- {
124- let send_stream = Arc :: clone ( & send_stream) ;
125- tokio:: spawn ( async move {
126- while let Ok ( event) = rx. recv ( ) . await {
127- let mut send_stream = send_stream. lock ( ) . await ;
128- let _ = send_stream. write_all ( event. as_bytes ( ) ) . await ;
129- }
130- } ) ;
131- }
136+ tokio:: spawn ( async move {
137+ while let Ok ( event) = rx. recv ( ) . await {
138+ let _ = send_stream. write_all ( event. as_bytes ( ) ) . await ;
139+ }
140+ } ) ;
132141 }
133142
134143 Ok ( ( ) )
135144}
136145
137- pub fn handle_event ( event : String , data : Json < String > ) {
146+ fn handle_event ( event : String , data : Json < String > ) {
138147 log:: info!( "Received event: {:?}" , event) ;
139148 log:: info!( "Received data: {:?}" , data) ;
140149
0 commit comments