1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#![allow(dead_code)]
use super::*;

use self::setup_teardown::*;
use crate::channel::sender::*;

/// The connect method creates a machine, implementing an instruction set.
/// The machine has a bound communication channel of a default size receiving those instructions.
pub fn connect<T, P>(
    machine: T,
) -> (
    Arc<Mutex<T>>,
    Sender<<<P as MachineImpl>::Adapter as MachineBuilder>::InstructionSet>,
)
where
    T: 'static + Machine<P> + Machine<<<P as MachineImpl>::Adapter as MachineBuilder>::InstructionSet>,
    P: MachineImpl,
    <P as MachineImpl>::Adapter: MachineBuilder,
{
    let channel_max = default_channel_max.load();
    let (machine, sender, collective_adapter) =
        <<P as MachineImpl>::Adapter as MachineBuilder>::build_raw(machine, channel_max);
    Server::assign_machine(collective_adapter);
    (machine, sender)
}

/// The and_connect method adds an additional instruction set and communication channel to the machine.
/// The communicate channel ib bound to a default size.
pub fn and_connect<T, P>(
    machine: &Arc<Mutex<T>>,
) -> Sender<<<P as MachineImpl>::Adapter as MachineBuilder>::InstructionSet>
where
    T: 'static + Machine<P> + Machine<<<P as MachineImpl>::Adapter as MachineBuilder>::InstructionSet>,
    P: MachineImpl,
    <P as MachineImpl>::Adapter: MachineBuilder,
{
    let channel_max = default_channel_max.load();
    let (sender, collective_adapter) =
        <<P as MachineImpl>::Adapter as MachineBuilder>::build_addition(machine, channel_max);
    Server::assign_machine(collective_adapter);
    sender
}

/// The connect_with_capacity method creates a machine with a bounded queue of the specified size.
pub fn connect_with_capacity<T, P>(
    machine: T,
    capacity: usize,
) -> (
    Arc<Mutex<T>>,
    Sender<<<P as MachineImpl>::Adapter as MachineBuilder>::InstructionSet>,
)
where
    T: 'static + Machine<P> + Machine<<<P as MachineImpl>::Adapter as MachineBuilder>::InstructionSet>,
    P: MachineImpl,
    <P as MachineImpl>::Adapter: MachineBuilder,
{
    let (machine, sender, collective_adapter) =
        <<P as MachineImpl>::Adapter as MachineBuilder>::build_raw(machine, capacity);
    Server::assign_machine(collective_adapter);
    (machine, sender)
}

/// The and_connect_with_capacity method adds an additional instruction set and sender to the machine.
/// The communication channel is bound to the specified size.
pub fn and_connect_with_capacity<T, P>(
    machine: &Arc<Mutex<T>>,
    capacity: usize,
) -> Sender<<<P as MachineImpl>::Adapter as MachineBuilder>::InstructionSet>
where
    T: 'static + Machine<P> + Machine<<<P as MachineImpl>::Adapter as MachineBuilder>::InstructionSet>,
    P: MachineImpl,
    <P as MachineImpl>::Adapter: MachineBuilder,
{
    let (sender, collective_adapter) =
        <<P as MachineImpl>::Adapter as MachineBuilder>::build_addition(machine, capacity);
    Server::assign_machine(collective_adapter);
    sender
}

/// The connect_unbounded method creates a machine with an unbounded queue. It can result
/// in a panic if system resources become exhausted.
pub fn connect_unbounded<T, P>(
    machine: T,
) -> (
    Arc<Mutex<T>>,
    Sender<<<P as MachineImpl>::Adapter as MachineBuilder>::InstructionSet>,
)
where
    T: 'static + Machine<P> + Machine<<<P as MachineImpl>::Adapter as MachineBuilder>::InstructionSet>,
    P: MachineImpl,
    <P as MachineImpl>::Adapter: MachineBuilder,
{
    let (machine, sender, collective_adapter) =
        <<P as MachineImpl>::Adapter as MachineBuilder>::build_unbounded(machine);
    Server::assign_machine(collective_adapter);
    (machine, sender)
}

/// The and_connect_unbounded method adds an additional instruction set and sender to the machine.
/// The communication channel is unbound.
pub fn and_connect_unbounded<T, P>(
    machine: &Arc<Mutex<T>>,
) -> Sender<<<P as MachineImpl>::Adapter as MachineBuilder>::InstructionSet>
where
    T: 'static + Machine<P> + Machine<<<P as MachineImpl>::Adapter as MachineBuilder>::InstructionSet>,
    P: MachineImpl,
    <P as MachineImpl>::Adapter: MachineBuilder,
{
    let (sender, collective_adapter) =
        <<P as MachineImpl>::Adapter as MachineBuilder>::build_addition_unbounded(machine);
    Server::assign_machine(collective_adapter);
    sender
}

/// CHANNEL_MAX is the default size for bound communication channels.
pub const CHANNEL_MAX: usize = 250;

#[allow(dead_code)]
#[allow(non_upper_case_globals)]
/// The default_channel_max static is the default used for creating bound channels.
pub static default_channel_max: AtomicCell<usize> = AtomicCell::new(CHANNEL_MAX);
/// The get_default_channel_capacity function returns the default value.
#[allow(dead_code)]
pub fn get_default_channel_capacity() -> usize { default_channel_max.load() }
/// The set_default_channel_capacity function sets a new default value.
/// setting should be performed before starting the server.
#[allow(dead_code)]
pub fn set_default_channel_capacity(new: usize) { default_channel_max.store(new); }