Skip to content

Commit

Permalink
Fix encoding codec
Browse files Browse the repository at this point in the history
  • Loading branch information
Mallets committed Feb 21, 2024
1 parent 1e1eef3 commit 6d774ff
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 121 deletions.
4 changes: 3 additions & 1 deletion commons/zenoh-codec/src/core/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use zenoh_protocol::core::{Encoding, EncodingPrefix};

impl LCodec<&Encoding> for Zenoh080 {
fn w_len(self, x: &Encoding) -> usize {
1 + self.w_len(x.suffix())
self.w_len(x.prefix()) + self.w_len(x.suffix())
}
}

Expand All @@ -34,6 +34,7 @@ where
fn write(self, writer: &mut W, x: &Encoding) -> Self::Output {
let zodec = Zenoh080Bounded::<EncodingPrefix>::new();
zodec.write(&mut *writer, x.prefix())?;
let zodec = Zenoh080Bounded::<u8>::new();
zodec.write(&mut *writer, x.suffix())?;
Ok(())
}
Expand All @@ -48,6 +49,7 @@ where
fn read(self, reader: &mut R) -> Result<Encoding, Self::Error> {
let zodec = Zenoh080Bounded::<EncodingPrefix>::new();
let prefix: EncodingPrefix = zodec.read(&mut *reader)?;
let zodec = Zenoh080Bounded::<u8>::new();
let suffix: String = zodec.read(&mut *reader)?;

let mut encoding: Encoding = Encoding::new(prefix);
Expand Down
18 changes: 9 additions & 9 deletions plugins/zenoh-plugin-rest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::sync::Arc;
use tide::http::Mime;
use tide::sse::Sender;
use tide::{Request, Response, Server, StatusCode};
use zenoh::encoding::EncodingMapping;
use zenoh::encoding::{DefaultEncodingMapping, EncodingMapping};
use zenoh::plugins::{RunningPluginTrait, ZenohPlugin};
use zenoh::prelude::r#async::*;
use zenoh::properties::Properties;
Expand All @@ -50,19 +50,19 @@ const RAW_KEY: &str = "_raw";
fn value_to_json(value: Value) -> String {
// @TODO: transcode to JSON when implemented in Value
match &value.encoding {
p if p.starts_with(DefaultEncoder::TEXT_PLAIN)
|| p.starts_with(DefaultEncoder::APP_XWWW_FORM_URLENCODED) =>
p if p.starts_with(DefaultEncoding::TEXT_PLAIN)
|| p.starts_with(DefaultEncoding::APP_XWWW_FORM_URLENCODED) =>
{
// convert to Json string for special characters escaping
serde_json::json!(value.to_string()).to_string()
}
p if p.starts_with(DefaultEncoder::APP_PROPERTIES) => {
p if p.starts_with(DefaultEncoding::APP_PROPERTIES) => {
// convert to Json string for special characters escaping
serde_json::json!(*Properties::from(value.to_string())).to_string()
}
p if p.starts_with(DefaultEncoder::APP_JSON)
|| p.starts_with(DefaultEncoder::APP_INTEGER)
|| p.starts_with(DefaultEncoder::APP_FLOAT) =>
p if p.starts_with(DefaultEncoding::APP_JSON)
|| p.starts_with(DefaultEncoding::APP_INTEGER)
|| p.starts_with(DefaultEncoding::APP_FLOAT) =>
{
value.to_string()
}
Expand Down Expand Up @@ -404,7 +404,7 @@ async fn query(mut req: Request<(Arc<Session>, String)>) -> tide::Result<Respons
let mut query = req.state().0.get(&selector).consolidation(consolidation);
if !body.is_empty() {
let encoding = match req.content_type() {
Some(m) => match DefaultEncoder::parse(m.to_string().as_str()) {
Some(m) => match DefaultEncodingMapping::parse(m.to_string().as_str()) {
Ok(e) => e,
Err(e) => {
return Ok(response(
Expand Down Expand Up @@ -453,7 +453,7 @@ async fn write(mut req: Request<(Arc<Session>, String)>) -> tide::Result<Respons
};

let encoding = match req.content_type() {
Some(m) => match DefaultEncoder::parse(m.to_string().as_str()) {
Some(m) => match DefaultEncodingMapping::parse(m.to_string().as_str()) {
Ok(e) => e,
Err(e) => {
return Ok(response(
Expand Down
5 changes: 3 additions & 2 deletions plugins/zenoh-plugin-storage-manager/src/replica/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::collections::{HashMap, HashSet};
use std::str::{self, FromStr};
use std::time::{SystemTime, UNIX_EPOCH};
use zenoh::buffers::ZBuf;
use zenoh::encoding::EncodingMapping;
use zenoh::encoding::{DefaultEncodingMapping, EncodingMapping};
use zenoh::prelude::r#async::*;
use zenoh::query::ConsolidationMode;
use zenoh::time::{Timestamp, NTP64};
Expand Down Expand Up @@ -694,7 +694,8 @@ fn construct_update(data: String) -> Update {
for slice in result.3 {
payload.push_zslice(slice.to_vec().into());
}
let value = Value::new(payload).encoding(DefaultEncoder::parse(result.2.as_str()).unwrap()); // @TODO: remove the unwrap()
let value =
Value::new(payload).encoding(DefaultEncodingMapping::parse(result.2.as_str()).unwrap()); // @TODO: remove the unwrap()
let data = StoredData {
value,
timestamp: Timestamp::from_str(&result.1).unwrap(), // @TODO: remove the unwrap()
Expand Down
6 changes: 3 additions & 3 deletions zenoh/src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// ZettaScale Zenoh Team, <[email protected]>
//
use crate::{
encoding::DefaultEncoder,
encoding::DefaultEncoding,
keyexpr,
prelude::sync::{KeyExpr, Locality, SampleKind},
queryable::Query,
Expand Down Expand Up @@ -143,7 +143,7 @@ impl TransportMulticastEventHandler for Handler {
let expr = WireExpr::from(&(*KE_PREFIX / own_zid / *KE_TRANSPORT_UNICAST / zid))
.to_owned();
let info = DataInfo {
encoding: Some(DefaultEncoder::APP_JSON),
encoding: Some(DefaultEncoding::APP_JSON),
..Default::default()
};
self.session.handle_data(
Expand Down Expand Up @@ -189,7 +189,7 @@ impl TransportPeerEventHandler for PeerHandler {
let mut s = DefaultHasher::new();
link.hash(&mut s);
let info = DataInfo {
encoding: Some(DefaultEncoder::APP_JSON),
encoding: Some(DefaultEncoding::APP_JSON),
..Default::default()
};
self.session.handle_data(
Expand Down
Loading

0 comments on commit 6d774ff

Please sign in to comment.