diff --git a/src/runtime2/connector2.rs b/src/runtime2/connector2.rs index 37ecca56cc66c87b0c6ec74d7295cc0d4bb1a350..14858c64491958cfd90137cd6b01b7c8e12740bd 100644 --- a/src/runtime2/connector2.rs +++ b/src/runtime2/connector2.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; /// connector.rs /// /// Represents a component. A component (and the scheduler that is running it) @@ -62,7 +63,7 @@ pub(crate) enum ConnectorScheduling { Immediate, // Run again, immediately Later, // Schedule for running, at some later point in time NotNow, // Do not reschedule for running - Exit, // Connector has exited + Exit, // Connector has exited } pub(crate) struct ConnectorPDL { @@ -70,28 +71,51 @@ pub(crate) struct ConnectorPDL { consensus: Consensus, } -struct ConnectorRunContext {} +struct ConnectorRunContext<'a> { + branch_id: BranchId, + consensus: &'a Consensus, + received: &'a HashMap, + scheduler: SchedulerCtx<'a>, + prepared_channel: Option<(Value, Value)>, +} + impl RunContext for ConnectorRunContext{ fn did_put(&mut self, port: PortId) -> bool { - todo!() + let port_id = PortIdLocal::new(port.0.u32_suffix); + let annotation = self.consensus.get_annotation(self.branch_id, port_id); + return annotation.registered_id.is_some(); } fn get(&mut self, port: PortId) -> Option { - todo!() + let port_id = PortIdLocal::new(port.0.u32_suffix); + match self.received.get(&port_id) { + Some(data) => Some(data.clone()), + None => None, + } } fn fires(&mut self, port: PortId) -> Option { - todo!() + let port_id = PortIdLocal::new(port.0.u32_suffix); + let annotation = self.consensus.get_annotation(self.branch_id, port_id); + return annotation.expected_firing.map(|v| Value::Bool(v)); } fn get_channel(&mut self) -> Option<(Value, Value)> { - todo!() + return self.prepared_channel.take(); } } impl Connector for ConnectorPDL { fn run(&mut self, sched_ctx: SchedulerCtx, comp_ctx: &mut ComponentCtxFancy) -> ConnectorScheduling { - todo!() + self.handle_new_messages(comp_ctx); + if self.tree.is_in_sync() { + let scheduling = self.run_in_sync_mode(sched_ctx, comp_ctx); + self.consensus.handle_new_finished_sync_branches(); + return scheduling; + } else { + let scheduling = self.run_in_deterministic_mode(sched_ctx, comp_ctx); + return scheduling; + } } } @@ -143,7 +167,7 @@ impl ConnectorPDL { // --- Running code - pub fn run_in_sync_mode(&mut self, sched_ctx: &mut SchedulerCtx, comp_ctx: &mut ComponentCtxFancy) -> ConnectorScheduling { + pub fn run_in_sync_mode(&mut self, sched_ctx: SchedulerCtx, comp_ctx: &mut ComponentCtxFancy) -> ConnectorScheduling { // Check if we have any branch that needs running debug_assert!(self.tree.is_in_sync() && self.consensus.is_in_sync()); let branch_id = self.tree.pop_from_queue(QueueKind::Runnable); @@ -155,7 +179,13 @@ impl ConnectorPDL { let branch_id = branch_id.unwrap(); let branch = &mut self.tree[branch_id]; - let mut run_context = ConnectorRunContext{}; + let mut run_context = ConnectorRunContext{ + branch_id, + consensus: &self.consensus, + received: &branch.inbox, + scheduler: *sched_ctx, + prepared_channel: branch.prepared_channel.take(), + }; let run_result = branch.code_state.run(&mut run_context, &sched_ctx.runtime.protocol_description); // Handle the returned result. Note that this match statement contains @@ -266,13 +296,19 @@ impl ConnectorPDL { } } - pub fn run_in_deterministic_mode(&mut self, sched_ctx: &SchedulerCtx, comp_ctx: &mut ComponentCtxFancy) -> ConnectorScheduling { + pub fn run_in_deterministic_mode(&mut self, sched_ctx: SchedulerCtx, comp_ctx: &mut ComponentCtxFancy) -> ConnectorScheduling { debug_assert!(!self.tree.is_in_sync() && !self.consensus.is_in_sync()); let branch = self.tree.base_branch_mut(); debug_assert!(branch.sync_state == SpeculativeState::RunningNonSync); - let mut run_context = ConnectorRunContext{}; + let mut run_context = ConnectorRunContext{ + branch_id, + consensus: &self.consensus, + received: &branch.inbox, + scheduler: *sched_ctx, + prepared_channel: branch.prepared_channel.take(), + }; let run_result = branch.code_state.run(&mut run_context, &sched_ctx.runtime.protocol_description); match run_result {