use std::rc::Rc;
use std::cell::RefCell;
use crate::allocator::thread::ThreadBuilder;
use crate::allocator::{Allocate, AllocateBuilder, Exchangeable, Thread, Process, ProcessBuilder};
use crate::allocator::zero_copy::allocator::{TcpBuilder, TcpAllocator};
use crate::{Push, Pull};
pub enum Allocator {
Thread(Thread),
Process(Process),
Tcp(TcpAllocator),
}
impl Allocator {
pub fn index(&self) -> usize {
match self {
Allocator::Thread(t) => t.index(),
Allocator::Process(p) => p.index(),
Allocator::Tcp(z) => z.index(),
}
}
pub fn peers(&self) -> usize {
match self {
Allocator::Thread(t) => t.peers(),
Allocator::Process(p) => p.peers(),
Allocator::Tcp(z) => z.peers(),
}
}
pub fn allocate<T: Exchangeable>(&mut self, identifier: usize) -> (Vec<Box<dyn Push<T>>>, Box<dyn Pull<T>>) {
match self {
Allocator::Thread(t) => t.allocate(identifier),
Allocator::Process(p) => p.allocate(identifier),
Allocator::Tcp(z) => z.allocate(identifier),
}
}
pub fn broadcast<T: Exchangeable+Clone>(&mut self, identifier: usize) -> (Box<dyn Push<T>>, Box<dyn Pull<T>>) {
match self {
Allocator::Thread(t) => t.broadcast(identifier),
Allocator::Process(p) => p.broadcast(identifier),
Allocator::Tcp(z) => z.broadcast(identifier),
}
}
pub fn receive(&mut self) {
match self {
Allocator::Thread(t) => t.receive(),
Allocator::Process(p) => p.receive(),
Allocator::Tcp(z) => z.receive(),
}
}
pub fn release(&mut self) {
match self {
Allocator::Thread(t) => t.release(),
Allocator::Process(p) => p.release(),
Allocator::Tcp(z) => z.release(),
}
}
pub fn events(&self) -> &Rc<RefCell<Vec<usize>>> {
match self {
Allocator::Thread(ref t) => t.events(),
Allocator::Process(ref p) => p.events(),
Allocator::Tcp(ref z) => z.events(),
}
}
pub fn await_events(&self, duration: Option<std::time::Duration>) {
match self {
Allocator::Thread(t) => t.await_events(duration),
Allocator::Process(p) => p.await_events(duration),
Allocator::Tcp(z) => z.await_events(duration),
}
}
pub fn pipeline<T: 'static>(&mut self, identifier: usize) ->
(crate::allocator::thread::ThreadPusher<T>,
crate::allocator::thread::ThreadPuller<T>)
{
crate::allocator::thread::Thread::new_from(identifier, Rc::clone(self.events()))
}
}
impl Allocate for Allocator {
fn index(&self) -> usize { self.index() }
fn peers(&self) -> usize { self.peers() }
fn allocate<T: Exchangeable>(&mut self, identifier: usize) -> (Vec<Box<dyn Push<T>>>, Box<dyn Pull<T>>) {
self.allocate(identifier)
}
fn broadcast<T: Exchangeable+Clone>(&mut self, identifier: usize) -> (Box<dyn Push<T>>, Box<dyn Pull<T>>) {
self.broadcast(identifier)
}
fn receive(&mut self) { self.receive(); }
fn release(&mut self) { self.release(); }
fn events(&self) -> &Rc<RefCell<Vec<usize>>> { self.events() }
fn await_events(&self, _duration: Option<std::time::Duration>) {
match self {
Allocator::Thread(t) => t.await_events(_duration),
Allocator::Process(p) => p.await_events(_duration),
Allocator::Tcp(z) => z.await_events(_duration),
}
}
}
pub enum AllocatorBuilder {
Thread(ThreadBuilder),
Process(ProcessBuilder),
Tcp(TcpBuilder),
}
impl AllocateBuilder for AllocatorBuilder {
type Allocator = Allocator;
fn build(self) -> Allocator {
match self {
AllocatorBuilder::Thread(t) => Allocator::Thread(t.build()),
AllocatorBuilder::Process(p) => Allocator::Process(p.build()),
AllocatorBuilder::Tcp(z) => Allocator::Tcp(z.build()),
}
}
}