Skip to content

Commit

Permalink
Parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
Adriano Santos committed Sep 18, 2023
1 parent 25f0c9d commit 2999a78
Show file tree
Hide file tree
Showing 14 changed files with 213 additions and 57 deletions.
50 changes: 43 additions & 7 deletions Cargo.lock

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

7 changes: 6 additions & 1 deletion spawn-examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ version = "0.1.0"

[dependencies]
env_logger = "0.10.0"
prost-types = "0.11"
log = {version = "0.4.8", features = ["std"]}
prost = "0.12.1"
prost-types = "0.12.1"
rocket = "=0.5.0-rc.3"
spawn-rs = {path = "../spawn-rs"}

[build-dependencies]
tonic-build = "0.8"
7 changes: 7 additions & 0 deletions spawn-examples/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub(crate) fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_build::configure()
.build_server(false)
.out_dir("src/domain")
.compile(&["proto/domain.proto"], &["proto"])?;
Ok(())
}
15 changes: 15 additions & 0 deletions spawn-examples/proto/domain.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
syntax = "proto3";

package domain;

message State {
repeated string languages = 1;
}

message Request {
string language = 1;
}

message Reply {
string response = 1;
}
18 changes: 18 additions & 0 deletions spawn-examples/src/domain/domain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct State {
#[prost(string, repeated, tag = "1")]
pub languages: ::prost::alloc::vec::Vec<::prost::alloc::string::String>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Request {
#[prost(string, tag = "1")]
pub language: ::prost::alloc::string::String,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Reply {
#[prost(string, tag = "1")]
pub response: ::prost::alloc::string::String,
}
1 change: 1 addition & 0 deletions spawn-examples/src/domain/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod domain;
30 changes: 23 additions & 7 deletions spawn-examples/src/joe.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
use prost_types::Any;

use spawn_examples::domain::domain::{Reply, Request};
use spawn_rs::{context::Context, value::Value, Message};

pub fn sum(_msg: Message, ctx: Context) -> Value {
return Value::new()
.state(ctx.state().clone())
.response(Any::default())
.to_owned();
use log::info;

pub fn set_language(msg: Message, ctx: Context) -> Value {
info!("Actor msg: {:?}", msg);
let value: Value = match msg.body::<Request>() {
Ok(request) => {
let lang = request.language;
info!("Setlanguage To: {:?}", lang);
let reply = Reply::default();

Value::new()
.state(ctx.state().clone())
.response(&Reply::default())
.to_owned()
}
Err(e) => Value::new()
.state(ctx.state().clone())
//.response(Any::default())
.to_owned(),
};

return value;
}
5 changes: 5 additions & 0 deletions spawn-examples/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[macro_use]
extern crate log;
extern crate prost_types;

pub mod domain;
6 changes: 2 additions & 4 deletions spawn-examples/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ extern crate rocket;

mod joe;

use joe::sum;
use joe::set_language;
use spawn_rs::actor::{ActorDefinition, ActorSettings, Kind};
use spawn_rs::spawn::Spawn;

#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("debug"));

Spawn::new()
.create("spawn-system".to_string())
.with_actor(
Expand All @@ -25,7 +23,7 @@ async fn main() -> Result<(), rocket::Error> {
.snapshot_timeout(10000)
.to_owned(),
)
.with_action("sum".to_owned(), sum),
.with_action("sum".to_owned(), set_language),
)
.start()
.await?;
Expand Down
5 changes: 3 additions & 2 deletions spawn-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ version = "0.1.0"

[dependencies]
env_logger = "0.10.0"
prost = "0.11"
prost-types = "0.11"
log = {version = "0.4.8", features = ["std"]}
prost = "0.12.1"
prost-types = "0.12.1"
rocket = "=0.5.0-rc.3"

[build-dependencies]
Expand Down
55 changes: 55 additions & 0 deletions spawn-rs/src/handler/actor_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ use std::collections::HashMap;

use crate::actor::ActorDefinition;

use crate::context::Context as ActorContext;
use crate::eigr::spawn::actor_invocation::Payload;
use crate::eigr::spawn::{ActorId, ActorInvocation, ActorInvocationResponse, Context, Noop};
use crate::value::Value;
use crate::Message as ActorMessage;

use log::{debug, info};
use prost_types::Any;

#[derive()]
pub struct Handler {
actors: HashMap<String, ActorDefinition>,
Expand Down Expand Up @@ -35,4 +44,50 @@ impl Handler {
pub fn get_actors(&mut self) -> &mut HashMap<String, ActorDefinition> {
&mut self.actors
}

pub fn handle(&mut self, request: ActorInvocation) -> ActorInvocationResponse {
info!("Received ActorInvocation request.");
debug!(
"Handle ActorInvocation with incoming request: {:?}",
request
);

let actor_id: ActorId = request.actor.unwrap();
let action: String = request.action_name;
let context: Context = request.current_context.unwrap();

let response = ActorInvocationResponse::default();

if self.actors.contains_key(actor_id.name.as_str()) {
debug!(
"Forward ActorInvocation to Actor: {:?}",
actor_id.name.as_str()
);
// handle response
let mut actor_def = self.actors.get(actor_id.name.as_str()).unwrap().clone();

if actor_def.get_actions().contains_key(action.as_str()) {
let function: &fn(ActorMessage, ActorContext) -> Value =
actor_def.get_actions().get(action.as_str()).unwrap();

let payload = match request.payload {
Some(Payload::Value(value)) => value,
Some(Payload::Noop(_)) => Any::default(),
None => Any::default(),
};

let mut msg: ActorMessage = ActorMessage::new();
msg.set_body(payload);

let ctx: ActorContext = ActorContext::new();

let result: Value = (function)(msg, ctx);

// TODO: build correct response
return response;
}
}

return response;
}
}
32 changes: 26 additions & 6 deletions spawn-rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[macro_use]
extern crate rocket;
extern crate log;
extern crate prost_types;

mod eigr;
Expand All @@ -11,18 +12,34 @@ pub mod serializer;
pub mod spawn;
pub mod value;

use prost::DecodeError;
use prost_types::Any;

// fn to_any<T>(message: &T) -> Any
// where
// T: prost::Message,
// {
// Any {
// type_url: T::type_url().to_string(),
// value: message.encode_to_vec(),
// }
// }

fn from_any<T>(message: &Any) -> Result<T, DecodeError>
where
T: prost::Message + Default,
{
T::decode(message.value.as_slice())
}

#[derive(Debug, Clone)]
pub struct Message {
action: String,
body: Any,
}

impl Default for Message {
fn default() -> Message {
Message {
action: String::from(""),
body: Any::default(),
}
}
Expand All @@ -33,11 +50,14 @@ impl Message {
Default::default()
}

pub fn action(&self) -> &str {
&self.action
pub fn body<T>(&self) -> Result<T, DecodeError>
where
T: prost::Message + Default,
{
from_any(&self.body)
}

pub fn body(&self) -> &Any {
&self.body
pub fn set_body(&mut self, message: Any) {
self.body = message
}
}
Loading

0 comments on commit 2999a78

Please sign in to comment.