Skip to content

Commit

Permalink
Added support for sending messages to another child and hide Uuids in…
Browse files Browse the repository at this point in the history
…side a BastionID struct
  • Loading branch information
r3v2d0g committed Oct 10, 2019
1 parent 9e9acc6 commit 719b3c5
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
8 changes: 8 additions & 0 deletions bastion/src/broadcast.rs
Expand Up @@ -173,6 +173,14 @@ impl BastionMessage {
false
}
}

pub(super) fn into_msg(self) -> Option<Box<dyn Message>> {
if let BastionMessage::Message(msg) = self {
Some(msg)
} else {
None
}
}
}

impl Stream for Broadcast {
Expand Down
19 changes: 16 additions & 3 deletions bastion/src/context.rs
@@ -1,17 +1,30 @@
use crate::broadcast::Sender;
use crate::bastion::REGISTRY;
use crate::broadcast::{BastionMessage, Sender};
use crate::children::Message;
use uuid::Uuid;

#[derive(Debug, Clone)]
pub struct BastionID(Uuid);

pub struct BastionContext {
id: Uuid,
id: BastionID,
parent: Sender,
}

impl BastionContext {
pub(super) fn new(id: Uuid, parent: Sender) -> Self {
let id = BastionID(id);

BastionContext { id, parent }
}

pub fn id(&self) -> &Uuid {
pub fn id(&self) -> &BastionID {
&self.id
}

pub fn send_msg(&self, id: &BastionID, msg: Box<dyn Message>) -> Result<(), Box<dyn Message>> {
let msg = BastionMessage::msg(msg);

REGISTRY.send_child(&id.0, msg).map_err(|msg| msg.into_msg().unwrap())
}
}
57 changes: 56 additions & 1 deletion bastion/src/registry.rs
@@ -1,4 +1,4 @@
use crate::broadcast::Sender;
use crate::broadcast::{BastionMessage, Sender};
use crate::children::{Child, Children};
use crate::supervisor::Supervisor;
use chashmap::CHashMap;
Expand All @@ -13,6 +13,7 @@ struct Registrant {
ty: RegistrantType,
}

#[derive(Eq, PartialEq)]
enum RegistrantType {
Supervisor,
Children,
Expand Down Expand Up @@ -53,6 +54,48 @@ impl Registry {

self.registered.insert(id, registrant);
}

pub(super) fn send_supervisor(&self, id: &Uuid, msg: BastionMessage) -> Result<(), BastionMessage> {
let registrant = if let Some(registrant) = self.registered.get(id) {
registrant
} else {
return Err(msg);
};

if !registrant.is_supervisor() {
return Err(msg);
}

registrant.sender.unbounded_send(msg).map_err(|err| err.into_inner())
}

pub(super) fn send_children(&self, id: &Uuid, msg: BastionMessage) -> Result<(), BastionMessage> {
let registrant = if let Some(registrant) = self.registered.get(id) {
registrant
} else {
return Err(msg);
};

if !registrant.is_children() {
return Err(msg);
}

registrant.sender.unbounded_send(msg).map_err(|err| err.into_inner())
}

pub(super) fn send_child(&self, id: &Uuid, msg: BastionMessage) -> Result<(), BastionMessage> {
let registrant = if let Some(registrant) = self.registered.get(id) {
registrant
} else {
return Err(msg);
};

if !registrant.is_child() {
return Err(msg);
}

registrant.sender.unbounded_send(msg).map_err(|err| err.into_inner())
}
}

impl Registrant {
Expand All @@ -73,4 +116,16 @@ impl Registrant {

Registrant { sender, ty }
}

fn is_supervisor(&self) -> bool {
self.ty == RegistrantType::Supervisor
}

fn is_children(&self) -> bool {
self.ty == RegistrantType::Children
}

fn is_child(&self) -> bool {
self.ty == RegistrantType::Child
}
}

0 comments on commit 719b3c5

Please sign in to comment.