Skip to content

Commit

Permalink
init adding ws client
Browse files Browse the repository at this point in the history
  • Loading branch information
besok committed Oct 31, 2023
1 parent 5c17749 commit c59496a
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 7 deletions.
5 changes: 3 additions & 2 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ parsit = "0.2.0"
graphviz-rust = "0.6.2"
logos = "0.13.0"
itertools = "0.11.0"
strum = "0.25.0"
strum = { version = "0.25.0", features = [] }
strum_macros = "0.25.1"
log = "0.4"
env_logger = "0.10.0"
Expand All @@ -30,6 +30,7 @@ chrono = "0.4.26"
quick-xml = "0.31.0"

tungstenite = "0.20.1"
url = "2.4.1"

[dev-dependencies]
wiremock = "0.5.19"
Expand Down
1 change: 0 additions & 1 deletion src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use std::str::ParseBoolError;
use std::string::FromUtf8Error;
use std::sync::{MutexGuard, PoisonError};
use quick_xml::events::attributes::AttrError;
use rustdds::dds::CreateError;

/// The major type of every result in Forester.
pub type RtResult<T> = Result<T, RuntimeError>;
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/builder/ros_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ pub fn ros_actions_file() -> String {
// The actions are accessible using the import 'import "ros::core"'
// Publish message to the topic
impl publish(topic:string, type:string, value:any, qos_policy:object);
impl publish(topic:string, value:any, url:string);
"#)
}
2 changes: 1 addition & 1 deletion src/runtime/dds.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Provide a DDS implementation for the runtime.

// ros2 launch rosbridge_server rosbridge_websocket_launch.xml
pub mod ws_client;

use crate::runtime::action::{Impl, Tick};
Expand Down
49 changes: 49 additions & 0 deletions src/runtime/dds/ws_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use serde::{Deserialize, Serialize};
use tungstenite::{connect, Message};
use url::Url;


#[derive(Serialize, Deserialize)]
pub struct PubMes<D: Serialize> {
pub topic: String,
pub msg: D,
}

impl<D: Serialize> PubMes<D> {
pub fn new(topic: String, msg: D) -> Self {
Self { topic, msg }
}
}

pub fn publish<D: Serialize>(mes: PubMes<D>, url: String) {
let (mut socket, response) =
connect(Url::parse(url.as_str()).unwrap()).expect("Can't connect");
println!("Connected to the server");
println!("Response HTTP code: {}", response.status());
println!("Response contains the following headers:");
for (ref header, _value) in response.headers() {
println!("* {}", header);
}
let js = serde_json::to_string_pretty(&mes).unwrap();
socket.send(Message::text(js)).unwrap();
}


#[cfg(test)]
mod tests {
use std::collections::HashMap;
use crate::runtime::args::RtValue;
use crate::runtime::dds::ws_client::{publish, PubMes};

#[test]
fn smoke() {
let value = RtValue::Object(HashMap::from_iter(vec![("a".to_string(), RtValue::int(10))]));
publish(PubMes::new(
"test".to_string(),
value,
), "ws://localhost:9090".to_string());
}
}



2 changes: 1 addition & 1 deletion tree/tests/flow/parallel/simple_w_retry/main.tree
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "std::actions"

cond fail_before_tick();
cond incr(key:string,default:num);
cond incr(key:string,default:any);

root main repeat(3) parallel {
r_fallback {
Expand Down

0 comments on commit c59496a

Please sign in to comment.