Skip to content

Commit

Permalink
updated protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel-cavalcanti committed Oct 13, 2023
1 parent d01a88d commit ccb53ac
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 28 deletions.
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
pub mod log_utils;
mod remote_api_client;
mod remote_api_objects;
mod zmq_requests;
mod zmq_protocol;

pub use remote_api_client::{RemoteApiClient, RemoteApiClientInterface, RemoteApiClientParams};
pub use remote_api_objects::sim;
pub use zmq_requests::{RawRequest, ZmqRequest, LANG, VERSION};
pub use zmq_protocol::{ErrorResponse, FunctionResponse, ObjectResponse, ZmqResponse};
pub use zmq_protocol::{RawRequest, ZmqRequest, LANG, VERSION};

pub use remote_api_objects::connection_error::RemoteAPIError;

Expand Down
2 changes: 1 addition & 1 deletion src/remote_api_client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use uuid::Uuid;
use crate::remote_api_client::RemoteApiClientInterface;
use crate::sim::Sim;
use crate::sim::SimIK;
use crate::zmq_requests::{RawRequest, ZmqRequest};
use crate::zmq_protocol::{RawRequest, ZmqRequest};
use crate::{log_utils, RemoteAPIError, RemoteApiClientParams};

const ZMQ_RECV_FLAG_NONE: i32 = 0;
Expand Down
40 changes: 19 additions & 21 deletions src/remote_api_client/remote_api_client_trait.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde_json::Value as JsonValue;

use crate::{sim::Module, RawRequest, RemoteAPIError, ZmqRequest};
use crate::{sim::Module, RawRequest, RemoteAPIError, ZmqRequest, ZmqResponse};

pub trait RemoteApiClientInterface {
fn send_raw_request(&self, request: Vec<u8>) -> Result<JsonValue, RemoteAPIError>;
Expand All @@ -17,31 +17,29 @@ pub trait RemoteApiClientInterface {
Ok(())
}

fn send_request(
&self,
request: ZmqRequest,
) -> Result<serde_json::Value, crate::RemoteAPIError> {
fn send_request(&self, request: ZmqRequest) -> Result<JsonValue, crate::RemoteAPIError> {
log::trace!("request: {request:?}");
let mut json = self.send_raw_request(request.to_raw_request())?;

if let Some(err) = json.get("err") {
let error_msg = err.as_str().unwrap().to_string();
log::error!("Error:{error_msg}");
return Err(
crate::remote_api_objects::connection_error::RemoteAPIError::new(error_msg),
);
}
let json = self.send_raw_request(request.to_raw_request())?;

log::trace!("json response: {json:?}");

while let Some(func) = json.get("func") {
let function_name = func.as_str().unwrap().to_string();
let response: ZmqResponse = serde_json::from_value(json).unwrap();

if function_name == "_*wait*_" {
let wait_request = crate::zmq_requests::ZmqRequest::wait_request(self.get_uuid());

json = self.send_raw_request(wait_request.to_raw_request())?
match response {
ZmqResponse::Error(e) => {
log::error!("Error: {}", e.message);
Err(RemoteAPIError::new(e.message))
}
ZmqResponse::Object(obj) => Ok(obj.ret),
ZmqResponse::Function(fun) => {
if fun.function_name == "_*wait*_" {
let wait_request = ZmqRequest::wait_request(self.get_uuid());
self.send_request(wait_request)
} else {
Ok(serde_json::json!([]))
}
}
}

Ok(json["ret"].to_owned())
}
}
6 changes: 3 additions & 3 deletions src/remote_api_objects/requests_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ macro_rules! requests {
)*
)*

let request = crate::zmq_requests::ZmqRequest {
let request = crate::zmq_protocol::ZmqRequest {
func: format!("{}.{}",$sim_type,$function_name),
args: _args,
ver:crate::zmq_requests::VERSION,
lang:crate::zmq_requests::LANG.into(),
ver:crate::zmq_protocol::VERSION,
lang:crate::zmq_protocol::LANG.into(),
uuid:self.get_uuid(),

};
Expand Down
3 changes: 3 additions & 0 deletions src/zmq_requests/mod.rs → src/zmq_protocol/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
mod zmq_request;
mod zmq_response;

pub use zmq_request::RawRequest;
pub use zmq_request::ZmqRequest;
pub use zmq_request::{LANG, VERSION};

pub use zmq_response::{ErrorResponse, FunctionResponse, ObjectResponse, ZmqResponse};

#[cfg(test)]
mod tests;
2 changes: 1 addition & 1 deletion src/zmq_requests/tests.rs → src/zmq_protocol/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::zmq_requests::{RawRequest, ZmqRequest};
use super::{RawRequest, ZmqRequest};

use super::{LANG, VERSION};

Expand Down
File renamed without changes.
30 changes: 30 additions & 0 deletions src/zmq_protocol/zmq_response.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use serde::Deserialize;
use serde_json::Value;

#[derive(Deserialize, Debug)]
#[serde(untagged)]
pub enum ZmqResponse {
Error(ErrorResponse),
Object(ObjectResponse),
Function(FunctionResponse),
}
//#[derive(Deserialize, Debug)]
//pub struct ArrayResponse {
// #[serde(rename = "ret")]
// pub array: Vec<Value>,
//}
#[derive(Deserialize, Debug)]
pub struct ErrorResponse {
#[serde(rename = "err")]
pub message: String,
}
#[derive(Deserialize, Debug)]
pub struct ObjectResponse {
pub ret: Value,
}

#[derive(Deserialize, Debug)]
pub struct FunctionResponse {
#[serde(rename = "func")]
pub function_name: String,
}

0 comments on commit ccb53ac

Please sign in to comment.