1- mod envelope;
2- mod state;
3-
1+ pub mod envelope;
42pub mod message;
5- pub mod traits ;
3+ pub ( crate ) mod state ;
64
75use std:: sync:: atomic:: AtomicBool ;
86use std:: sync:: Arc ;
@@ -11,14 +9,14 @@ use async_channel::{unbounded, Receiver, Sender};
119
1210use crate :: error:: { BastionError , Result } ;
1311use crate :: mailbox:: envelope:: Envelope ;
12+ use crate :: mailbox:: message:: Message ;
1413use crate :: mailbox:: state:: MailboxState ;
15- use crate :: mailbox:: traits:: TypedMessage ;
1614
1715/// Struct that represents a message sender.
1816#[ derive( Clone ) ]
1917pub struct MailboxTx < T >
2018where
21- T : TypedMessage ,
19+ T : Message ,
2220{
2321 /// Indicated the transmitter part of the actor's channel
2422 /// which is using for passing messages.
3028
3129impl < T > MailboxTx < T >
3230where
33- T : TypedMessage ,
31+ T : Message ,
3432{
3533 /// Return a new instance of MailboxTx that indicates sender.
3634 pub ( crate ) fn new ( tx : Sender < Envelope < T > > ) -> Self {
@@ -57,110 +55,66 @@ where
5755#[ derive( Clone ) ]
5856pub struct Mailbox < T >
5957where
60- T : TypedMessage ,
58+ T : Message ,
6159{
62- /// User guardian sender
63- user_tx : MailboxTx < T > ,
64- /// User guardian receiver
65- user_rx : Receiver < Envelope < T > > ,
60+ /// Actor guardian sender
61+ actor_tx : MailboxTx < T > ,
62+ /// Actor guardian receiver
63+ actor_rx : Receiver < Envelope < T > > ,
6664 /// System guardian receiver
6765 system_rx : Receiver < Envelope < T > > ,
68- /// The current processing message, received from the
69- /// latest call to the user's queue
70- last_user_message : Option < Envelope < T > > ,
71- /// The current processing message, received from the
72- /// latest call to the system's queue
73- last_system_message : Option < Envelope < T > > ,
7466 /// Mailbox state machine
7567 state : Arc < MailboxState > ,
7668}
7769
7870// TODO: Add calls with recv with timeout
7971impl < T > Mailbox < T >
8072where
81- T : TypedMessage ,
73+ T : Message ,
8274{
8375 /// Creates a new mailbox for the actor.
8476 pub ( crate ) fn new ( system_rx : Receiver < Envelope < T > > ) -> Self {
85- let ( tx, user_rx) = unbounded ( ) ;
86- let user_tx = MailboxTx :: new ( tx) ;
87- let last_user_message = None ;
88- let last_system_message = None ;
77+ let ( tx, actor_rx) = unbounded ( ) ;
78+ let actor_tx = MailboxTx :: new ( tx) ;
8979 let state = Arc :: new ( MailboxState :: new ( ) ) ;
9080
9181 Mailbox {
92- user_tx ,
93- user_rx ,
82+ actor_tx ,
83+ actor_rx ,
9484 system_rx,
95- last_user_message,
96- last_system_message,
9785 state,
9886 }
9987 }
10088
101- /// Forced receive message from user queue
89+ /// Forced receive message from the actor's queue.
10290 pub async fn recv ( & mut self ) -> Envelope < T > {
103- let message = self
104- . user_rx
91+ self . actor_rx
10592 . recv ( )
10693 . await
10794 . map_err ( |e| BastionError :: ChanRecv ( e. to_string ( ) ) )
108- . unwrap ( ) ;
109-
110- self . last_user_message = Some ( message) ;
111- self . last_user_message . clone ( ) . unwrap ( )
95+ . unwrap ( )
11296 }
11397
114- /// Try receiving message from user queue
98+ /// Try receiving message from the actor's queue.
11599 pub async fn try_recv ( & mut self ) -> Result < Envelope < T > > {
116- if self . last_user_message . is_some ( ) {
117- return Err ( BastionError :: UnackedMessage ) ;
118- }
119-
120- match self . user_rx . try_recv ( ) {
121- Ok ( message) => {
122- self . last_user_message = Some ( message) ;
123- Ok ( self . last_user_message . clone ( ) . unwrap ( ) )
124- }
125- Err ( e) => Err ( BastionError :: ChanRecv ( e. to_string ( ) ) ) ,
126- }
100+ self . actor_rx
101+ . try_recv ( )
102+ . map_err ( |e| BastionError :: ChanRecv ( e. to_string ( ) ) )
127103 }
128104
129- /// Forced receive message from system queue
105+ /// Forced receive message from the internal system queue.
130106 pub async fn sys_recv ( & mut self ) -> Envelope < T > {
131- let message = self
132- . system_rx
107+ self . system_rx
133108 . recv ( )
134109 . await
135110 . map_err ( |e| BastionError :: ChanRecv ( e. to_string ( ) ) )
136- . unwrap ( ) ;
137-
138- self . last_system_message = Some ( message) ;
139- self . last_system_message . clone ( ) . unwrap ( )
111+ . unwrap ( )
140112 }
141113
142- /// Try receiving message from system queue
114+ /// Try receiving message from the internal system queue.
143115 pub async fn try_sys_recv ( & mut self ) -> Result < Envelope < T > > {
144- if self . last_system_message . is_some ( ) {
145- return Err ( BastionError :: UnackedMessage ) ;
146- }
147-
148- match self . system_rx . try_recv ( ) {
149- Ok ( message) => {
150- self . last_system_message = Some ( message) ;
151- Ok ( self . last_system_message . clone ( ) . unwrap ( ) )
152- }
153- Err ( e) => Err ( BastionError :: ChanRecv ( e. to_string ( ) ) ) ,
154- }
155- }
156-
157- /// Returns the last retrieved message from the user channel
158- pub async fn get_last_user_message ( & self ) -> Option < Envelope < T > > {
159- self . last_user_message . clone ( )
160- }
161-
162- /// Returns the last retrieved message from the system channel
163- pub async fn get_last_system_message ( & self ) -> Option < Envelope < T > > {
164- self . last_system_message . clone ( )
116+ self . system_rx
117+ . try_recv ( )
118+ . map_err ( |e| BastionError :: ChanRecv ( e. to_string ( ) ) )
165119 }
166120}
0 commit comments