Skip to content

Commit

Permalink
zenoh API: improvements and updates required by zenoh-python
Browse files Browse the repository at this point in the history
  • Loading branch information
JEnoch committed Sep 17, 2020
1 parent 9c9422e commit f970e97
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
7 changes: 5 additions & 2 deletions zenoh/examples/zenoh/z_get.rs
Expand Up @@ -82,8 +82,11 @@ async fn main() {
let mut data_stream = workspace.get(&selector.try_into().unwrap()).await.unwrap();
while let Some(data) = data_stream.next().await {
println!(
">> [Reply handler] received reply data {} : {:?} with timestamp {}",
data.path, data.value, data.timestamp
" {} : {:?} (encoding: {} , timestamp: {})",
data.path,
data.value,
data.value.encoding_descr(),
data.timestamp
)
}

Expand Down
36 changes: 36 additions & 0 deletions zenoh/examples/zenoh/z_put.rs
Expand Up @@ -86,5 +86,41 @@ async fn main() {
.await
.unwrap();

// --- Examples of put with other types:

// - Integer
// workspace.put(&"/demo/example/Integer".try_into().unwrap(), 3.into())
// .await.unwrap();

// - Float
// workspace.put(&"/demo/example/Float".try_into().unwrap(), 3.14.into())
// .await.unwrap();

// - Properties (as a Dictionary with str only)
// workspace.put(
// &"/demo/example/Properties".try_into().unwrap(),
// Properties::from("p1=v1;p2=v2").into()
// ).await.unwrap();

// - Json (str format)
// workspace.put(
// &"/demo/example/Json".try_into().unwrap(),
// Value::Json(r#"{"kind"="memory"}"#.to_string()),
// ).await.unwrap();

// - Raw ('application/octet-stream' encoding by default)
// workspace.put(
// &"/demo/example/Raw".try_into().unwrap(),
// vec![0x48u8, 0x69, 0x33].into(),
// ).await.unwrap();

// - Custom
// workspace.put(
// &"/demo/example/Custom".try_into().unwrap(),
// Value::Custom {
// encoding_descr: "my_encoding".to_string(),
// data: vec![0x48u8, 0x69, 0x33].into(),
// }).await.unwrap();

zenoh.close().await.unwrap();
}
8 changes: 7 additions & 1 deletion zenoh/src/properties.rs
Expand Up @@ -24,7 +24,7 @@ const KV_SEP: char = '=';
///
/// It can be parsed from a String, using `;` as separator between each properties
/// and `=` as separator between a key and its value.
pub struct Properties(pub(crate) HashMap<String, String>);
pub struct Properties(pub HashMap<String, String>);

impl Default for Properties {
fn default() -> Self {
Expand Down Expand Up @@ -96,6 +96,12 @@ impl From<String> for Properties {
}
}

impl From<HashMap<String, String>> for Properties {
fn from(map: HashMap<String, String>) -> Self {
Self(map)
}
}

impl From<&[(&str, &str)]> for Properties {
fn from(kvs: &[(&str, &str)]) -> Properties {
let p: HashMap<String, String> = kvs
Expand Down
30 changes: 30 additions & 0 deletions zenoh/src/values.rs
Expand Up @@ -29,6 +29,36 @@ pub enum Value {
}

impl Value {
pub fn encoding(&self) -> ZInt {
use Value::*;
match self {
Raw(encoding, _) => *encoding,
Custom {
encoding_descr: _,
data: _,
} => APP_CUSTOM,
StringUTF8(_) => STRING,
Properties(_) => APP_PROPERTIES,
Json(_) => APP_JSON,
Integer(_) => APP_INTEGER,
Float(_) => APP_FLOAT,
}
}

pub fn encoding_descr(&self) -> String {
use Value::*;
match self {
Custom {
encoding_descr,
data: _,
} => encoding_descr.clone(),
_ => {
let encoding = self.encoding();
to_str(encoding).unwrap_or_else(|_| format!("Unkown encoding flag:{}", encoding))
}
}
}

pub fn encode(self) -> (ZInt, RBuf) {
use Value::*;
match self {
Expand Down

0 comments on commit f970e97

Please sign in to comment.