Skip to content

Commit

Permalink
0.1.1: use logging facade
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonHartley committed Dec 16, 2019
1 parent ffdd000 commit 2edbbc4
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 30 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion coerce-rt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "coerce-rt"
description = "Async actor runtime"
license = "Apache-2.0"
version = "0.1.0"
version = "0.1.1"
authors = ["Leon Hartley <ljph@outlook.com>"]
edition = "2018"

Expand All @@ -15,3 +15,4 @@ async-trait = { version = "0.1" }
lazy_static = "1.4.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
log = "0.4"
10 changes: 6 additions & 4 deletions coerce-rt/src/actor/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub async fn actor_loop<A: Actor>(
) where
A: 'static + Send + Sync,
{
println!("[{}] actor starting", &id);
trace!(target: "ActorLoop", "[{}] starting", &id);
let mut ctx = ActorHandlerContext::new(Starting);

actor.started(&mut ctx).await;
Expand All @@ -61,9 +61,11 @@ pub async fn actor_loop<A: Actor>(
on_start.send(true);
}

println!("[{}] actor begin handling msgs", &id);
trace!(target: "ActorLoop", "[{}] ready", &id);

while let Some(mut msg) = rx.recv().await {
println!("[{}] actor recv", &id);
trace!(target: "ActorLoop", "[{}] recv", &id);

msg.handle(&mut actor, &mut ctx).await;

match ctx.get_status() {
Expand All @@ -72,7 +74,7 @@ pub async fn actor_loop<A: Actor>(
}
}

println!("[{}] actor stopping", &id);
trace!(target: "ActorLoop", "[{}] stopping", &id);

ctx.set_status(Stopping);

Expand Down
4 changes: 2 additions & 2 deletions coerce-rt/src/actor/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ where

let sender = self.sender.take();
match sender.unwrap().send(result) {
Ok(_) => println!("send OK"),
Err(e) => println!("send err"),
Ok(_) => trace!(target: "ActorMessage", "sent result successfully"),
Err(e) => trace!(target: "ActorMessage", "failed to send result"),
}
}
}
Expand Down
13 changes: 5 additions & 8 deletions coerce-rt/src/actor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::actor::context::{ActorContext, ActorHandlerContext, ActorStatus};
use crate::actor::lifecycle::{Status, Stop};
use crate::actor::message::{ActorMessage, Exec, Handler, Message, MessageHandler};
use crate::actor::scheduler::{GetActor, RegisterActor};
use log::error;
use std::any::Any;
use uuid::Uuid;

Expand All @@ -14,13 +15,9 @@ pub type ActorId = Uuid;

#[async_trait]
pub trait Actor {
async fn started(&mut self, _ctx: &mut ActorHandlerContext) {
println!("actor started");
}
async fn started(&mut self, _ctx: &mut ActorHandlerContext) {}

async fn stopped(&mut self, _ctx: &mut ActorHandlerContext) {
println!("actor stopped");
}
async fn stopped(&mut self, _ctx: &mut ActorHandlerContext) {}
}

pub async fn new_actor<A: Actor>(actor: A) -> Result<ActorRef<A>, ActorRefError>
Expand Down Expand Up @@ -111,12 +108,12 @@ where
Ok(_) => match rx.await {
Ok(res) => Ok(res),
Err(e) => {
println!("got error {}", e);
error!(target: "ActorRef", "error receiving result");
Err(ActorRefError::ActorUnavailable)
}
},
Err(e) => {
println!("got error 1 {}", e);
error!(target: "ActorRef", "error sending message");
Err(ActorRefError::ActorUnavailable)
}
}
Expand Down
4 changes: 4 additions & 0 deletions coerce-rt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ extern crate serde_json;

#[macro_use]
extern crate lazy_static;

#[macro_use]
extern crate log;

extern crate tokio;
extern crate uuid;

Expand Down
22 changes: 13 additions & 9 deletions coerce-rt/src/remote/actor.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use crate::actor::context::ActorHandlerContext;
use crate::actor::message::{Handler, Message};
use crate::actor::{new_actor, Actor, ActorRef};
use crate::remote::handler::RemoteMessageHandler;
use std::collections::HashMap;
use crate::actor::message::{Message, Handler};
use crate::actor::context::ActorHandlerContext;

pub(crate) type BoxedHandler = Box<dyn RemoteMessageHandler + Send + Sync>;

pub struct RemoteRegistry {}

pub struct RemoteHandler {
handlers: HashMap<String, Box<dyn RemoteMessageHandler + Send + Sync>>,
handlers: HashMap<String, BoxedHandler>,
}

impl Actor for RemoteRegistry {}
Expand All @@ -21,25 +23,27 @@ impl RemoteRegistry {
}

impl RemoteHandler {
pub async fn new(
handlers: HashMap<String, Box<dyn RemoteMessageHandler + Send + Sync>>,
) -> ActorRef<RemoteHandler> {
pub async fn new(handlers: HashMap<String, BoxedHandler>) -> ActorRef<RemoteHandler> {
new_actor(RemoteHandler { handlers }).await.unwrap()
}
}

pub struct GetHandler(pub String);

impl Message for GetHandler {
type Result = Option<Box<dyn RemoteMessageHandler + Send + Sync>>;
type Result = Option<BoxedHandler>;
}

#[async_trait]
impl Handler<GetHandler> for RemoteHandler {
async fn handle(&mut self, message: GetHandler, ctx: &mut ActorHandlerContext) -> Option<Box<dyn RemoteMessageHandler + Send + Sync>> {
async fn handle(
&mut self,
message: GetHandler,
ctx: &mut ActorHandlerContext,
) -> Option<BoxedHandler> {
match self.handlers.get(&message.0) {
Some(handler) => Some(handler.new_boxed()),
None => None
None => None,
}
}
}
2 changes: 1 addition & 1 deletion coerce-rt/src/remote/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::actor::message::{Handler, Message};
use crate::actor::{Actor, ActorId, ActorRef};
use crate::remote::actor::{RemoteHandler, RemoteRegistry, GetHandler};
use crate::remote::actor::{GetHandler, RemoteHandler, RemoteRegistry};
use crate::remote::handler::{RemoteActorMessageHandler, RemoteMessageHandler};
use serde::de::DeserializeOwned;
use serde::Serialize;
Expand Down
12 changes: 8 additions & 4 deletions coerce-rt/src/remote/handler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::actor::message::{Handler, Message};
use crate::actor::{get_actor, Actor, ActorId, ActorRef};
use crate::remote::actor::BoxedHandler;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::collections::HashMap;
Expand All @@ -14,7 +15,7 @@ pub trait RemoteMessageHandler {
res: tokio::sync::oneshot::Sender<Vec<u8>>,
);

fn new_boxed(&self) -> Box<dyn RemoteMessageHandler + Send + Sync>;
fn new_boxed(&self) -> BoxedHandler;
}

pub struct RemoteActorMessageHandler<A: Actor, M: Message>
Expand Down Expand Up @@ -64,12 +65,15 @@ where
res.send(serde_json::to_string(&result).unwrap().as_bytes().to_vec());
}
}
Err(e) => println!("decode failed"),
Err(e) => error!(target: "RemoteHandler", "failed to decode message, {}", e),
};
}
}

fn new_boxed(&self) -> Box<dyn RemoteMessageHandler + Send + Sync> {
Box::new(Self { _a: PhantomData, _m: PhantomData })
fn new_boxed(&self) -> BoxedHandler {
Box::new(Self {
_a: PhantomData,
_m: PhantomData,
})
}
}

0 comments on commit 2edbbc4

Please sign in to comment.