@@ -20,6 +20,7 @@ use super::protocol::{HandlerFn, RobotMessage, ViewerMessage};
2020
2121const LINGER_DURATION : Duration = Duration :: from_secs ( 2 ) ;
2222const CONNECTION_ATTEMPT_DELAY : Duration = Duration :: from_secs ( 5 ) ;
23+ const CONNECTION_ATTEMPTS : usize = 3 ;
2324
2425pub struct ControlViewer {
2526 address : SocketAddrV4 ,
@@ -61,7 +62,7 @@ impl ControlViewer {
6162 let handle = app. clone ( ) ;
6263
6364 tokio:: spawn ( async move {
64- loop {
65+ for attempt in 1 ..= CONNECTION_ATTEMPTS {
6566 let socket = Socket :: new (
6667 Domain :: for_address ( app. address . into ( ) ) ,
6768 Type :: STREAM ,
@@ -80,7 +81,13 @@ impl ControlViewer {
8081 app. handle_connection ( stream) . await ;
8182 }
8283 Err ( error) => {
83- tracing:: error!( ?error, "failed to connect to {}" , app. address) ;
84+ tracing:: debug!(
85+ ?error,
86+ "failed to connect to {}, attempt [{}/{}]" ,
87+ app. address,
88+ attempt,
89+ CONNECTION_ATTEMPTS
90+ ) ;
8491 }
8592 }
8693
@@ -93,7 +100,6 @@ impl ControlViewer {
93100 }
94101
95102 async fn handle_connection ( & self , socket : TcpStream ) {
96- tracing:: info!( "connected with app: {}" , self . address) ;
97103 let ( read_half, write_half) = socket. split ( ) ;
98104
99105 // Spawn tasks to handle read and write
@@ -116,7 +122,7 @@ impl ControlViewer {
116122 // is completed.
117123 writer_task. abort ( ) ;
118124
119- tracing:: warn!( "connection termintaed with app: {}" , self . address) ;
125+ tracing:: warn!( "connection terminated with app: {}" , self . address) ;
120126 }
121127
122128 async fn global_message_handler (
@@ -133,7 +139,7 @@ impl ControlViewer {
133139 }
134140 notify. notify_one ( ) ;
135141 }
136- tracing:: info !( "Global message channel closed" ) ;
142+ tracing:: debug !( "Global message channel closed" ) ;
137143 }
138144
139145 async fn handle_read (
@@ -223,6 +229,11 @@ pub struct ControlViewerHandle {
223229}
224230
225231impl ControlViewerHandle {
232+ #[ must_use]
233+ pub fn addr ( & self ) -> SocketAddrV4 {
234+ self . app . address
235+ }
236+
226237 pub fn send ( & self , msg : ViewerMessage ) -> Result < ( ) > {
227238 self . app . tx . unbounded_send ( msg) . into_diagnostic ( )
228239 }
0 commit comments