use std::rc::Rc;
use std::cell::RefCell;
use crate::allocator::thread::ThreadBuilder;
use crate::allocator::process::ProcessBuilder as TypedProcessBuilder;
use crate::allocator::{Allocate, AllocateBuilder, Exchangeable, Thread, Process};
use crate::allocator::zero_copy::allocator_process::{ProcessBuilder, ProcessAllocator};
use crate::allocator::zero_copy::allocator::{TcpBuilder, TcpAllocator};
use crate::{Push, Pull};
pub enum Generic {
Thread(Thread),
Process(Process),
ProcessBinary(ProcessAllocator),
ZeroCopy(TcpAllocator<Process>),
}
impl Generic {
pub fn index(&self) -> usize {
match self {
Generic::Thread(t) => t.index(),
Generic::Process(p) => p.index(),
Generic::ProcessBinary(pb) => pb.index(),
Generic::ZeroCopy(z) => z.index(),
}
}
pub fn peers(&self) -> usize {
match self {
Generic::Thread(t) => t.peers(),
Generic::Process(p) => p.peers(),
Generic::ProcessBinary(pb) => pb.peers(),
Generic::ZeroCopy(z) => z.peers(),
}
}
fn allocate<T: Exchangeable>(&mut self, identifier: usize) -> (Vec<Box<dyn Push<T>>>, Box<dyn Pull<T>>) {
match self {
Generic::Thread(t) => t.allocate(identifier),
Generic::Process(p) => p.allocate(identifier),
Generic::ProcessBinary(pb) => pb.allocate(identifier),
Generic::ZeroCopy(z) => z.allocate(identifier),
}
}
fn receive(&mut self) {
match self {
Generic::Thread(t) => t.receive(),
Generic::Process(p) => p.receive(),
Generic::ProcessBinary(pb) => pb.receive(),
Generic::ZeroCopy(z) => z.receive(),
}
}
pub fn release(&mut self) {
match self {
Generic::Thread(t) => t.release(),
Generic::Process(p) => p.release(),
Generic::ProcessBinary(pb) => pb.release(),
Generic::ZeroCopy(z) => z.release(),
}
}
fn events(&self) -> &Rc<RefCell<Vec<usize>>> {
match self {
Generic::Thread(ref t) => t.events(),
Generic::Process(ref p) => p.events(),
Generic::ProcessBinary(ref pb) => pb.events(),
Generic::ZeroCopy(ref z) => z.events(),
}
}
}
impl Allocate for Generic {
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 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 {
Generic::Thread(t) => t.await_events(_duration),
Generic::Process(p) => p.await_events(_duration),
Generic::ProcessBinary(pb) => pb.await_events(_duration),
Generic::ZeroCopy(z) => z.await_events(_duration),
}
}
}
pub enum GenericBuilder {
Thread(ThreadBuilder),
Process(TypedProcessBuilder),
ProcessBinary(ProcessBuilder),
ZeroCopy(TcpBuilder<TypedProcessBuilder>),
}
impl AllocateBuilder for GenericBuilder {
type Allocator = Generic;
fn build(self) -> Generic {
match self {
GenericBuilder::Thread(t) => Generic::Thread(t.build()),
GenericBuilder::Process(p) => Generic::Process(p.build()),
GenericBuilder::ProcessBinary(pb) => Generic::ProcessBinary(pb.build()),
GenericBuilder::ZeroCopy(z) => Generic::ZeroCopy(z.build()),
}
}
}