|
| 1 | +use super::*; |
| 2 | +use crate::callback::Callback; |
| 3 | +use crate::scheduler::{scheduler, Runnable, Shared}; |
| 4 | +use std::cell::RefCell; |
| 5 | +use std::fmt; |
| 6 | +use std::rc::Rc; |
| 7 | + |
| 8 | +/// Defines communication from Worker to Consumers |
| 9 | +pub(crate) trait Responder<AGN: Agent> { |
| 10 | + /// Implementation for communication channel from Worker to Consumers |
| 11 | + fn respond(&self, id: HandlerId, output: AGN::Output); |
| 12 | +} |
| 13 | + |
| 14 | +/// Link to agent's scope for creating callbacks. |
| 15 | +pub struct AgentLink<AGN: Agent> { |
| 16 | + scope: AgentScope<AGN>, |
| 17 | + responder: Rc<dyn Responder<AGN>>, |
| 18 | +} |
| 19 | + |
| 20 | +impl<AGN: Agent> AgentLink<AGN> { |
| 21 | + /// Create link for a scope. |
| 22 | + pub(crate) fn connect<T>(scope: &AgentScope<AGN>, responder: T) -> Self |
| 23 | + where |
| 24 | + T: Responder<AGN> + 'static, |
| 25 | + { |
| 26 | + AgentLink { |
| 27 | + scope: scope.clone(), |
| 28 | + responder: Rc::new(responder), |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + /// Send response to an agent. |
| 33 | + pub fn respond(&self, id: HandlerId, output: AGN::Output) { |
| 34 | + self.responder.respond(id, output); |
| 35 | + } |
| 36 | + |
| 37 | + /// Create a callback which will send a message to the agent when invoked. |
| 38 | + pub fn callback<F, IN>(&self, function: F) -> Callback<IN> |
| 39 | + where |
| 40 | + F: Fn(IN) -> AGN::Message + 'static, |
| 41 | + { |
| 42 | + let scope = self.scope.clone(); |
| 43 | + let closure = move |input| { |
| 44 | + let output = function(input); |
| 45 | + scope.send(AgentLifecycleEvent::Message(output)); |
| 46 | + }; |
| 47 | + closure.into() |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl<AGN: Agent> fmt::Debug for AgentLink<AGN> { |
| 52 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 53 | + f.write_str("AgentLink<_>") |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl<AGN: Agent> Clone for AgentLink<AGN> { |
| 58 | + fn clone(&self) -> Self { |
| 59 | + AgentLink { |
| 60 | + scope: self.scope.clone(), |
| 61 | + responder: self.responder.clone(), |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | +/// This struct holds a reference to a component and to a global scheduler. |
| 66 | +pub(crate) struct AgentScope<AGN: Agent> { |
| 67 | + shared_agent: Shared<AgentRunnable<AGN>>, |
| 68 | +} |
| 69 | + |
| 70 | +impl<AGN: Agent> fmt::Debug for AgentScope<AGN> { |
| 71 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 72 | + f.write_str("AgentScope<_>") |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl<AGN: Agent> Clone for AgentScope<AGN> { |
| 77 | + fn clone(&self) -> Self { |
| 78 | + AgentScope { |
| 79 | + shared_agent: self.shared_agent.clone(), |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl<AGN: Agent> AgentScope<AGN> { |
| 85 | + /// Create agent scope |
| 86 | + pub fn new() -> Self { |
| 87 | + let shared_agent = Rc::new(RefCell::new(AgentRunnable::new())); |
| 88 | + AgentScope { shared_agent } |
| 89 | + } |
| 90 | + /// Schedule message for sending to agent |
| 91 | + pub fn send(&self, update: AgentLifecycleEvent<AGN>) { |
| 92 | + let envelope = AgentEnvelope { |
| 93 | + shared_agent: self.shared_agent.clone(), |
| 94 | + update, |
| 95 | + }; |
| 96 | + let runnable: Box<dyn Runnable> = Box::new(envelope); |
| 97 | + scheduler().push(runnable); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +impl<AGN: Agent> Default for AgentScope<AGN> { |
| 102 | + fn default() -> Self { |
| 103 | + Self::new() |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +struct AgentRunnable<AGN> { |
| 108 | + agent: Option<AGN>, |
| 109 | + // TODO(#939): Use agent field to control create message this flag |
| 110 | + destroyed: bool, |
| 111 | +} |
| 112 | + |
| 113 | +impl<AGN> AgentRunnable<AGN> { |
| 114 | + fn new() -> Self { |
| 115 | + AgentRunnable { |
| 116 | + agent: None, |
| 117 | + destroyed: false, |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +/// Local Agent messages |
| 123 | +#[derive(Debug)] |
| 124 | +pub(crate) enum AgentLifecycleEvent<AGN: Agent> { |
| 125 | + /// Request to create link |
| 126 | + Create(AgentLink<AGN>), |
| 127 | + /// Internal Agent message |
| 128 | + Message(AGN::Message), |
| 129 | + /// Client connected |
| 130 | + Connected(HandlerId), |
| 131 | + /// Received mesasge from Client |
| 132 | + Input(AGN::Input, HandlerId), |
| 133 | + /// Client disconnected |
| 134 | + Disconnected(HandlerId), |
| 135 | + /// Request to destroy agent |
| 136 | + Destroy, |
| 137 | +} |
| 138 | + |
| 139 | +struct AgentEnvelope<AGN: Agent> { |
| 140 | + shared_agent: Shared<AgentRunnable<AGN>>, |
| 141 | + update: AgentLifecycleEvent<AGN>, |
| 142 | +} |
| 143 | + |
| 144 | +impl<AGN> Runnable for AgentEnvelope<AGN> |
| 145 | +where |
| 146 | + AGN: Agent, |
| 147 | +{ |
| 148 | + fn run(self: Box<Self>) { |
| 149 | + let mut this = self.shared_agent.borrow_mut(); |
| 150 | + if this.destroyed { |
| 151 | + return; |
| 152 | + } |
| 153 | + match self.update { |
| 154 | + AgentLifecycleEvent::Create(link) => { |
| 155 | + this.agent = Some(AGN::create(link)); |
| 156 | + } |
| 157 | + AgentLifecycleEvent::Message(msg) => { |
| 158 | + this.agent |
| 159 | + .as_mut() |
| 160 | + .expect("agent was not created to process messages") |
| 161 | + .update(msg); |
| 162 | + } |
| 163 | + AgentLifecycleEvent::Connected(id) => { |
| 164 | + this.agent |
| 165 | + .as_mut() |
| 166 | + .expect("agent was not created to send a connected message") |
| 167 | + .connected(id); |
| 168 | + } |
| 169 | + AgentLifecycleEvent::Input(inp, id) => { |
| 170 | + this.agent |
| 171 | + .as_mut() |
| 172 | + .expect("agent was not created to process inputs") |
| 173 | + .handle_input(inp, id); |
| 174 | + } |
| 175 | + AgentLifecycleEvent::Disconnected(id) => { |
| 176 | + this.agent |
| 177 | + .as_mut() |
| 178 | + .expect("agent was not created to send a disconnected message") |
| 179 | + .disconnected(id); |
| 180 | + } |
| 181 | + AgentLifecycleEvent::Destroy => { |
| 182 | + let mut agent = this |
| 183 | + .agent |
| 184 | + .take() |
| 185 | + .expect("trying to destroy not existent agent"); |
| 186 | + agent.destroy(); |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments