@@ -15,7 +15,15 @@ pub struct Agent<L: LifeCycle, C: Connection> {
15
15
inner : L ,
16
16
pub ( crate ) controller : Arc < Controller > ,
17
17
handlers : HashMap < TypeId , MessageHandlerFn < C > > ,
18
- transport : Transport < C > ,
18
+ pub ( crate ) transport : Transport < C > ,
19
+ }
20
+
21
+ pub struct RunningAgent < C : Connection > {
22
+ pub name : Option < String > ,
23
+ pub ( crate ) task : JoinHandle < Box < dyn RuntimeAgent < C > > > ,
24
+ pub ( crate ) controller : Arc < Controller > ,
25
+ pub ( crate ) outbound_connections : Arc < Mutex < HashMap < C :: Address , C :: Sender > > > ,
26
+ pub ( crate ) sender : C :: Sender ,
19
27
}
20
28
21
29
pub struct Controller {
@@ -86,11 +94,6 @@ impl<L: LifeCycle, C: Connection> Agent<L, C> {
86
94
87
95
pub fn clear_name ( & mut self ) { self . name = None ; }
88
96
89
- // Access to inner L, requires locking.
90
- pub fn inner_mut ( & mut self ) -> & mut L { & mut self . inner }
91
-
92
- pub fn inner ( & self ) -> & L { & self . inner }
93
-
94
97
pub fn with_handler < M > ( mut self ) -> Self
95
98
where
96
99
M : Message ,
@@ -137,10 +140,8 @@ pub trait RuntimeAgent<C: Connection>: Send + Sync + Any {
137
140
// Methods to signal the agent's desired state
138
141
fn signal_start ( & self ) ;
139
142
fn signal_stop ( & self ) ;
140
- fn current_loop_state ( & self ) -> State ;
141
143
142
- fn process ( self ) -> JoinHandle < Self >
143
- where Self : Sized ;
144
+ fn process ( self ) -> RunningAgent < C > ;
144
145
145
146
#[ cfg( test) ]
146
147
fn inner_as_any ( & self ) -> & dyn Any ;
@@ -175,12 +176,16 @@ where C::Payload: Package<L::StartMessage> + Package<L::StopMessage>
175
176
}
176
177
}
177
178
178
- fn current_loop_state ( & self ) -> State { * self . controller . state . lock ( ) . unwrap ( ) }
179
+ fn process ( mut self ) -> RunningAgent < C > {
180
+ let name = self . name . clone ( ) ;
181
+ let outbound_connections = self . transport . outbound_connections . clone ( ) ;
182
+ let controller = self . controller . clone ( ) ;
183
+ let ( _, sender) = self . transport . create_inbound_connection ( ) ;
179
184
180
- fn process ( mut self ) -> JoinHandle < Self > {
181
- std:: thread:: spawn ( move || {
185
+ let handle = std:: thread:: spawn ( move || {
182
186
let start_message = self . inner . on_start ( ) ;
183
187
self . transport . broadcast ( Envelope :: package ( start_message) ) ;
188
+ println ! ( "Agent {}: Sent start message." , self . address( ) ) ;
184
189
185
190
loop {
186
191
println ! ( "Agent {}: Loop iteration." , self . address( ) ) ;
@@ -269,8 +274,9 @@ where C::Payload: Package<L::StartMessage> + Package<L::StopMessage>
269
274
270
275
self . inner . on_stop ( ) ;
271
276
println ! ( "Agent {}: Executed on_stop. Main loop finished." , self . address( ) ) ;
272
- self
273
- } )
277
+ Box :: new ( self ) as Box < dyn RuntimeAgent < C > >
278
+ } ) ;
279
+ RunningAgent { name, task : handle, controller, outbound_connections, sender }
274
280
}
275
281
276
282
#[ cfg( test) ]
@@ -287,20 +293,21 @@ mod tests {
287
293
fn test_agent_lifecycle ( ) {
288
294
let logger = Logger { name : "TestLogger" . to_string ( ) , message_count : 0 } ;
289
295
let agent = Agent :: < Logger , InMemory > :: new ( logger) ;
290
- assert_eq ! ( agent. current_loop_state ( ) , State :: Stopped ) ;
296
+ assert_eq ! ( * agent. controller . state . lock ( ) . unwrap ( ) , State :: Stopped ) ;
291
297
292
298
// Start agent
293
299
let controller = agent. controller . clone ( ) ;
294
- let handle = agent. process ( ) ;
300
+ let running_agent = agent. process ( ) ;
295
301
296
302
controller. signal_start ( ) ;
297
303
std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 50 ) ) ; // Give time for agent to start
298
304
assert_eq ! ( controller. state. lock( ) . unwrap( ) . clone( ) , State :: Running ) ;
299
305
300
306
controller. signal_stop ( ) ;
301
- let joined_agent = handle. join ( ) . unwrap ( ) ;
302
- assert_eq ! ( joined_agent. inner( ) . message_count, 0 ) ;
303
- assert_eq ! ( joined_agent. current_loop_state( ) , State :: Stopped ) ;
307
+ let joined_agent = running_agent. task . join ( ) . unwrap ( ) ;
308
+ let agent = joined_agent. inner_as_any ( ) . downcast_ref :: < Logger > ( ) . unwrap ( ) ;
309
+ assert_eq ! ( agent. message_count, 0 ) ;
310
+ assert_eq ! ( * running_agent. controller. state. lock( ) . unwrap( ) , State :: Stopped ) ;
304
311
}
305
312
306
313
#[ test]
@@ -310,7 +317,7 @@ mod tests {
310
317
let sender = agent. transport . inbound_connection . sender . clone ( ) ;
311
318
312
319
let controller = agent. controller . clone ( ) ;
313
- let handle = agent. process ( ) ;
320
+ let running_agent = agent. process ( ) ;
314
321
315
322
controller. signal_start ( ) ;
316
323
std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 50 ) ) ; // Allow agent to start and enter wait
@@ -321,9 +328,9 @@ mod tests {
321
328
assert_eq ! ( controller. state. lock( ) . unwrap( ) . clone( ) , State :: Running ) ;
322
329
323
330
controller. signal_stop ( ) ;
324
- let result_agent = handle . join ( ) . unwrap ( ) ;
325
-
326
- assert_eq ! ( result_agent . inner ( ) . message_count, 1 ) ;
331
+ let result_agent = running_agent . task . join ( ) . unwrap ( ) ;
332
+ let agent = result_agent . inner_as_any ( ) . downcast_ref :: < Logger > ( ) . unwrap ( ) ;
333
+ assert_eq ! ( agent . message_count, 1 ) ;
327
334
}
328
335
329
336
#[ test]
@@ -335,10 +342,10 @@ mod tests {
335
342
agent_struct = agent_struct. with_handler :: < TextMessage > ( ) . with_handler :: < NumberMessage > ( ) ;
336
343
let sender = agent_struct. transport . inbound_connection . sender . clone ( ) ;
337
344
338
- assert_eq ! ( agent_struct. current_loop_state ( ) , State :: Stopped ) ;
345
+ assert_eq ! ( * agent_struct. controller . state . lock ( ) . unwrap ( ) , State :: Stopped ) ;
339
346
340
347
let controller = agent_struct. controller . clone ( ) ;
341
- let handle = agent_struct. process ( ) ;
348
+ let running_agent = agent_struct. process ( ) ;
342
349
343
350
controller. signal_start ( ) ;
344
351
std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 50 ) ) ;
@@ -352,7 +359,8 @@ mod tests {
352
359
std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 50 ) ) ;
353
360
354
361
controller. signal_stop ( ) ;
355
- let result_agent = handle. join ( ) . unwrap ( ) ;
356
- assert_eq ! ( result_agent. inner( ) . message_count, 2 ) ;
362
+ let result_agent = running_agent. task . join ( ) . unwrap ( ) ;
363
+ let agent = result_agent. inner_as_any ( ) . downcast_ref :: < Logger > ( ) . unwrap ( ) ;
364
+ assert_eq ! ( agent. message_count, 2 ) ;
357
365
}
358
366
}
0 commit comments