Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove z_config_client and z_config_peer #669

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,6 @@ Functions
.. doxygenfunction:: z_config_drop

.. doxygenfunction:: z_config_default
.. doxygenfunction:: z_config_client
.. doxygenfunction:: z_config_peer
.. doxygenfunction:: zc_config_from_env
.. doxygenfunction:: zc_config_from_file
.. doxygenfunction:: zc_config_from_str
Expand Down
16 changes: 0 additions & 16 deletions include/zenoh_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -1964,18 +1964,6 @@ ZENOHC_API z_result_t z_condvar_signal(const struct z_loaned_condvar_t *this_);
ZENOHC_API
z_result_t z_condvar_wait(const struct z_loaned_condvar_t *this_,
struct z_loaned_mutex_t *m);
/**
* Constructs a default, zenoh-allocated, client mode configuration.
*
* @param peers: Array with `size >= n_peers`, containing peer locators to add to the config.
* @param n_peers: Number of peers to add to the config.
*
* @return 0 in case of success, negative error code otherwise.
*/
ZENOHC_API
z_result_t z_config_client(struct z_owned_config_t *this_,
const char *const *peers,
size_t n_peers);
/**
* Clones the config into provided uninitialized memory location.
*/
Expand All @@ -1996,10 +1984,6 @@ ZENOHC_API const struct z_loaned_config_t *z_config_loan(const struct z_owned_co
* Mutably borrows config.
*/
ZENOHC_API struct z_loaned_config_t *z_config_loan_mut(struct z_owned_config_t *this_);
/**
* Constructs a default peer mode configuration.
*/
ZENOHC_API z_result_t z_config_peer(struct z_owned_config_t *this_);
/**
* Constructs and declares a key expression on the network. This reduces key key expression to a numerical id,
* which allows to save the bandwith, when passing key expression between Zenoh entities.
Expand Down
55 changes: 2 additions & 53 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
use std::{ffi::CStr, mem::MaybeUninit, slice::from_raw_parts, str::from_utf8};

use libc::{c_char, c_uint};
use zenoh::config::{Config, Locator, ValidatedMap, WhatAmI};
use zenoh::config::{Config, ValidatedMap, WhatAmI};

use crate::{
result::{self, z_result_t, Z_OK},
result::{self, Z_OK},
transmute::{LoanedCTypeRef, RustTypeRef, RustTypeRefUninit, TakeRustType},
z_internal_string_null, z_owned_string_t, z_string_copy_from_substr,
};
Expand Down Expand Up @@ -336,54 +336,3 @@ pub unsafe extern "C" fn zc_config_from_env(
}
}
}

/// Constructs a default peer mode configuration.
#[allow(clippy::missing_safety_doc)]
#[no_mangle]
pub extern "C" fn z_config_peer(this_: &mut MaybeUninit<z_owned_config_t>) -> result::z_result_t {
this_
.as_rust_type_mut_uninit()
.write(Some(zenoh::config::peer()));
Z_OK
}

/// Constructs a default, zenoh-allocated, client mode configuration.
///
/// @param peers: Array with `size >= n_peers`, containing peer locators to add to the config.
/// @param n_peers: Number of peers to add to the config.
///
/// @return 0 in case of success, negative error code otherwise.
#[allow(clippy::missing_safety_doc)]
#[no_mangle]
pub unsafe extern "C" fn z_config_client(
this: &mut MaybeUninit<z_owned_config_t>,
peers: *const *const c_char,
n_peers: usize,
) -> z_result_t {
let mut res = result::Z_OK;
let locators = if peers.is_null() {
Vec::new()
} else if let Ok(locators) = std::slice::from_raw_parts(peers, n_peers)
.iter()
.map(|&s| CStr::from_ptr(s).to_string_lossy().parse())
.try_fold(Vec::<Locator>::new(), |mut acc, it| match it {
Err(e) => {
tracing::error!("Error parsing peer address: {}", e);
res = result::Z_EPARSE;
Err(())
}
Ok(loc) => {
acc.push(loc);
Ok(acc)
}
})
{
locators
} else {
z_internal_config_null(this);
return res;
};
this.as_rust_type_mut_uninit()
.write(Some(zenoh::config::client(locators)));
res
}
20 changes: 8 additions & 12 deletions tests/z_api_config_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,25 @@
#undef NDEBUG
#include <assert.h>

void config_client() {
const char *peers[] = {"tcp/127.0.0.1", "tcp/192.168.0.1", "tcp/10.0.0.1"};
void insert_get() {
z_owned_config_t config;
z_config_client(&config, peers, 3);
z_config_default(&config);
zc_config_insert_json(z_loan_mut(config), "mode", "\"client\"");
zc_config_insert_json(z_loan_mut(config), "connect/endpoints",
"[\"tcp/127.0.0.1\", \"tcp/192.168.0.1\", \"tcp/10.0.0.1\"]");
z_owned_string_t endpoints;
zc_config_get_from_str(z_loan(config), "connect/endpoints", &endpoints);
assert(strncmp(z_string_data(z_loan(endpoints)), "[\"tcp/127.0.0.1\",\"tcp/192.168.0.1\",\"tcp/10.0.0.1\"]",
z_string_len(z_loan(endpoints))) == 0);
z_drop(z_move(endpoints));
z_drop(z_move(config));
}

void config_peer() {
z_owned_config_t config;
z_config_peer(&config);
z_owned_string_t mode;
zc_config_get_from_str(z_loan(config), "mode", &mode);
assert(strncmp(z_string_data(z_loan(mode)), "\"peer\"", z_string_len(z_loan(mode))) == 0);
assert(strncmp(z_string_data(z_loan(mode)), "\"client\"", z_string_len(z_loan(mode))) == 0);
z_drop(z_move(mode));
z_drop(z_move(config));
}

int main(int argc, char **argv) {
zc_init_logging();
config_client();
config_peer();
insert_get();
}
Loading