Expand description
As the name implies, d3-core is the core foundation for the server. It is responsible for creating a d3 machine from a raw implementation, scheduling that machine to run and running it. Creating a machine is trivial, as the exmples illustate.
§examples
// A simple Alice machine
struct Alice {}
// Alice implements the TestMessage instruction set
impl Machine<TestMessage> for Alice {
fn receive(&self, _message: TestMessage) {}
}
// Construct Alice, and connect her
let (alice, sender) = executor::connect(Alice{});
// the returned alice is an Arc<Mutex<Alice>>
// the returned sender is a Sender<TestMessage>
// At this point, Alice is in the scheduler, but idle.
// sending a command will wake her and call her receive method.
send_cmd(&sender, TestMessage::Test);
Occasionally, a machine may implement multiple instruction sets. This is an example of Alice implementing TestMessage and StateTable.
// A simple Alice machine
struct Alice {}
// Alice implements the TestMessage instruction set
impl Machine<TestMessage> for Alice {
fn receive(&self, _message: TestMessage) {}
}
// Alice also implements the StateTable instruction set
impl Machine<StateTable> for Alice {
fn receive(&self, _message: StateTable) {}
}
// Construct Alice, and connect her, this is for the TextMessage set
let (alice, sender) = executor::connect::<_,TestMessage>(Alice{});
// the returned alice is an Arc<Mutex<Alice>>
// the returned sender is a Sender<TestMessage>
// At this point, Alice is in the scheduler, but idle.
// Now add the StateTable instruction set
let state_sender = executor::and_connect::<_,StateTable>(&alice);
// the returned sender is a Sender<StateTable>
// At this point, there are two Alice machines is in the scheduler, both idle.
// Both machines share the Alice implementation, each with their own
// sender and receiver.
// sending a command will wake her and call her receive method.
send_cmd(&sender, TestMessage::Test);
send_cmd(&state_sender, StateTable::Start);
Instruction are varients in an enum, the enum is considered an insruction set. A MachineImpl derive macro is used to convert an enum into an instruction set.
§examples
use std::sync::Arc;
use parking_lot::Mutex;
use d3_core::machine_impl::*;
use d3_derive::*;
#[derive(Debug, MachineImpl)]
pub enum StateTable {
Init,
Start,
Stop
}
// The MachineImpl provides all of the code necessary to support
// a machine sending and receiving StateTable instructions.
Modules§
Functions§
- Send an instruction to a sender and log any errors. The instruction set must implement Debug in order to use it in send_cmd()