@@ -19,9 +19,11 @@ use std::fmt::Write;
1919use std:: net:: ToSocketAddrs ;
2020use std:: str:: FromStr ;
2121use std:: { cmp:: min, net:: SocketAddr , sync:: Arc , time:: Duration } ;
22+ use tokio:: sync:: RwLock ;
2223use tokio:: time:: sleep;
2324use tracing:: debug;
2425
26+ pub mod cli;
2527pub mod config;
2628pub mod messages;
2729pub mod routes;
@@ -95,7 +97,15 @@ impl NetworkLayer {
9597 }
9698
9799 /// Request the server to coordinate a direct connection to the given agent.
98- pub async fn direct_connect ( & self , agent : InstanceId , port : Option < u16 > ) { }
100+ pub async fn direct_connect ( & self , agent : InstanceId , port : Option < u16 > ) {
101+ todo ! ( )
102+ }
103+
104+ pub async fn run ( & self ) {
105+ for server in self . servers . iter ( ) {
106+ server. run ( ) . await ;
107+ }
108+ }
99109}
100110
101111/// Convenience type to be used as return of request handler.
@@ -133,35 +143,6 @@ pub struct ConnectionData {
133143 disconnected : Option < DateTime < Utc > > ,
134144}
135145
136- /// Request the server for a new direct connection to an agent.
137- // #[from = "client"]
138- // #[to = "server"]
139- #[ derive( Serialize , Deserialize ) ]
140- pub struct AgentConnectionRequest {
141- // The requested node
142- id : InstanceId ,
143-
144- // An optional listener port. If specified, the requested node will attempt
145- // a connection on this port. Otherwise, the server will coordinate the connection.
146- port : Option < u16 > ,
147- }
148-
149- // Request that the recieving instance establish a new connection to the given
150- // host. message RQ_CoordinateConnection {
151-
152- // // The host IP address
153- // string host = 1;
154-
155- // // The port
156- // int32 port = 2;
157-
158- // // The transport protocol type
159- // string transport = 3;
160-
161- // // The initial encryption key for the new connection.
162- // bytes encryption_key = 4;
163- // }
164-
165146/// A direct connection to an agent from a client.
166147pub struct AgentConnection { }
167148
@@ -207,7 +188,9 @@ impl Default for ConnectionCooldown {
207188 }
208189}
209190
210- pub struct ServerConnectionData { }
191+ pub struct ServerConnectionData {
192+ iterations : u64 ,
193+ }
211194
212195/// A connection to a server from any other instance (including another server).
213196///
@@ -230,9 +213,9 @@ pub struct ServerConnectionData {}
230213pub struct ServerConnection {
231214 pub address : ServerAddress ,
232215 group : GroupName ,
233- iterations : u64 , // TODO Data?
234216 cooldown : ConnectionCooldown ,
235217 pub client : reqwest:: Client ,
218+ pub data : RwLock < ServerConnectionData > ,
236219}
237220
238221impl ServerConnection {
@@ -243,7 +226,7 @@ impl ServerConnection {
243226 ) -> Result < Self > {
244227 Ok ( Self {
245228 group : cert. name ( ) ?,
246- iterations : 0 ,
229+ data : RwLock :: new ( ServerConnectionData { iterations : 0 } ) ,
247230 cooldown,
248231 client : ClientBuilder :: new ( )
249232 . add_root_certificate ( cert. ca ( ) ?)
@@ -256,10 +239,10 @@ impl ServerConnection {
256239 }
257240
258241 /// Run the connection routine forever.
259- pub async fn run ( mut self ) {
242+ pub async fn run ( & self ) {
260243 loop {
261- if self . iterations > 0 {
262- sleep ( self . cooldown . next ( self . iterations ) ) . await ;
244+ if self . data . read ( ) . await . iterations > 0 {
245+ sleep ( self . cooldown . next ( self . data . read ( ) . await . iterations ) ) . await ;
263246 }
264247
265248 debug ! ( "Attempting server connection" ) ;
@@ -281,7 +264,7 @@ impl ServerConnection {
281264 } ,
282265 Err ( e) => debug ! ( error = ?e, "Connection failed" ) ,
283266 }
284- self . iterations += 1 ;
267+ self . data . write ( ) . await . iterations += 1 ;
285268 }
286269 }
287270}
0 commit comments