Skip to content

Instantly share code, notes, and snippets.

@jdu
Created July 24, 2018 09:40
Show Gist options
  • Save jdu/1e4ec84769172b89532f0f82a4577313 to your computer and use it in GitHub Desktop.
Save jdu/1e4ec84769172b89532f0f82a4577313 to your computer and use it in GitHub Desktop.
pub fn handle_api(&mut self, ctx: &mut actix::Context<Self>, id: String, cmd: String, args: Value) {
eprintln!("Called handle api");
let cloned_id = id.clone();
let result = self.addr.send(ApiCall {
id: cloned_id,
cmd: cmd,
args: args,
});
eprintln!("SEND request to actor");
ctx.spawn(
result
.map_err(|_| {
eprintln!("HELLO");
})
.map(|res| {
eprintln!("Got async response from actor");
self.framed.write(res);
}),
);
}
pub struct ApiCall {
pub id: String,
pub cmd: String,
pub args: Value,
}
impl actix::Message for ApiCall {
type Result = PeerResponse;
}
impl<A, M> MessageResponse<A, M> for PeerResponse
where
A: Actor,
M: actix::Message<Result = PeerResponse>,
{
fn handle<R: ResponseChannel<M>>(self, _: &mut A::Context, tx: Option<R>) {
if let Some(tx) = tx {
tx.send(self);
}
}
}
impl Handler<ApiCall> for SonomaServer {
type Result = PeerResponse;
fn handle(&mut self, msg: ApiCall, ctx: &mut Context<Self>) -> Self::Result {
let result: PeerResponse = api_handler(self.app.clone(), msg.id, msg.cmd, msg.args).unwrap();
result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment