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

WIP: Trying to resurrect the "basic_tests" file #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ homepage = "https://github.com/sjames/cyclonedds-rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cyclonedds-sys = "0.2"
cyclonedds-sys = { git = "https://github.com/sjames/cyclonedds-sys" }
cdr = "0.2.4"
serde = "1"
serde_derive = "1"
Expand Down
4 changes: 2 additions & 2 deletions src/dds_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ where

let ret = unsafe {
if take {
dds_take(entity.entity(), voidpp , info_ptr as *mut _, buf.len() as size_t, buf.len() as u32)
dds_take(entity.entity(), voidpp, info_ptr as *mut _, buf.len() as size_t, buf.len() as u32)
} else {
dds_read(entity.entity(), voidpp , info_ptr as *mut _, buf.len() as size_t, buf.len() as u32)
dds_read(entity.entity(), voidpp, info_ptr as *mut _, buf.len() as size_t, buf.len() as u32)
}
};
if ret > 0 {
Expand Down
2 changes: 1 addition & 1 deletion src/serdes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl<T> Drop for SampleStorage<T> {
}
}


// TODO: THIS ISN'T SUITABLE FOR PASSING AS A SAMPLE POLINTER TO dds_take I reckon!
pub struct Sample<T> {
//Serdata is used for incoming samples. We hold a reference to the ddsi_serdata which contains
// the sample
Expand Down
109 changes: 42 additions & 67 deletions tests/basic_tests.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
/*
use cyclonedds_rs::{
self, dds_api, dds_topic::DdsTopic, DdsListener, DdsQos, DdsReader, DdsStatus, DdsWriter,
Entity,
};
use cdds_derive::Topic;
use cyclonedds_rs::serdes::SampleStorage;
use cyclonedds_rs::*;

use helloworld_data;
use serde_derive::{Deserialize, Serialize};

use std::ffi::{CStr, CString};
#[repr(C)]
#[derive(PartialEq, Debug, Serialize, Deserialize, Topic, Clone)]
pub struct HelloWorldData {
pub userID: i64,
// pub message: String,
}

use std::sync::mpsc;
use std::sync::mpsc::{Receiver, Sender};
use std::sync::Arc;

/// Simple hello world test. Sending and receiving one message
#[test]
fn hello_world_idl_test() {
let receiver = std::thread::spawn(|| subscriber());

let message_string = CString::new("Hello from DDS Cyclone Rust")
.expect("Unable to create CString")
.into_raw();
let message_string = "Hello from DDS Cyclone Rust";

let participant = cyclonedds_rs::DdsParticipant::create(None, None, None).unwrap();

// The topic is typed by the generated types in the IDL crate.
let topic: DdsTopic<helloworld_data::HelloWorldData::Msg> =
let topic: DdsTopic<HelloWorldData> =
DdsTopic::create(&participant, "HelloWorldData_Msg", None, None)
.expect("Unable to create topic");

Expand All @@ -34,7 +35,7 @@ fn hello_world_idl_test() {
);
qos.set_resource_limits(10, 1, 10);

let mut writer = DdsWriter::create(&participant, &topic, Some(&qos), None).unwrap();
let mut writer = DdsWriter::create(&participant, topic, Some(qos), None).unwrap();

let mut count = 0;

Expand Down Expand Up @@ -62,24 +63,24 @@ fn hello_world_idl_test() {
panic!("Unable to set status mask");
}

let msg = helloworld_data::HelloWorldData::Msg {
let msg = HelloWorldData {
userID: 1,
message: message_string,
// message: message_string.to_string(),
};
println!("Writing: {}", msg.userID);
writer.write(&msg).unwrap();
writer.write(Arc::new(msg)).unwrap();

receiver.join().unwrap();
}

fn subscriber() {
let participant = cyclonedds_rs::DdsParticipant::create(None, None, None).unwrap();
// The topic is typed by the generated types in the IDL crate.
let topic: DdsTopic<helloworld_data::HelloWorldData::Msg> =
let topic: DdsTopic<HelloWorldData> =
DdsTopic::create(&participant, "HelloWorldData_Msg", None, None)
.expect("Unable to create topic");

let (tx, rx): (Sender<i32>, Receiver<i32>) = mpsc::channel();
let (tx, rx) = mpsc::channel::<Option<SampleStorage<HelloWorldData>>>();

let mut qos = DdsQos::create().unwrap();
qos.set_history(
Expand All @@ -93,58 +94,32 @@ fn subscriber() {
})
.on_data_available(move |entity| {
println!("Data on reader");
tx.send(0).unwrap();
// you could call read here, but then you need to use the unsafe read function exported
// by cyclonedds-sys.
/*
// cyclonedds_sys::read is unsafe.
unsafe {
if let Ok(msg) =
cyclonedds_sys::read::<helloworld_data::HelloWorldData::Msg>(&entity)
{
let msg = msg.as_slice();
println!("Received {} messages", msg.len());

println!("Received message : {}", msg[0].userID);
assert_eq!(1, msg[0].userID);
assert_eq!(
CStr::from_ptr(msg[0].message),
CStr::from_bytes_with_nul("Hello from DDS Cyclone Rust\0".as_bytes())
.unwrap()
);
tx.send(0).unwrap();
} else {
println!("Error reading");
let mut buf: SampleBuffer<HelloWorldData> = SampleBuffer::new(1);
let res = DdsReader::readn_from_entity_now(&entity, &mut buf, true);
match res {
Ok(count) => {
let mmsg = buf.get(0).get_sample();
println!("Received {} messages", count);
tx.send(mmsg).unwrap();
}
Err(e) => {
println!("Error reading: {:?}", e);
}
}
*/
})
.hook();

if let Ok(mut reader) = DdsReader::create(&participant, &topic, Some(&qos), None) {
reader
.set_listener(listener)
.expect("Unable to set listener");

let id = rx.recv().unwrap();
if let Ok(msg) = reader.take() {
let msg = msg.as_slice();
println!("Received {} messages", msg.len());

println!("Received message : {}", msg[0].userID);
assert_eq!(1, msg[0].userID);
assert_eq!(
unsafe { CStr::from_ptr(msg[0].message) },
CStr::from_bytes_with_nul("Hello from DDS Cyclone Rust\0".as_bytes()).unwrap()
);
} else {
println!("Error reading");
}
println!("Received :{} completed", id);
let ten_millis = std::time::Duration::from_millis(100);
std::thread::sleep(ten_millis);
} else {
panic!("Unable to create reader");
};
let _reader = DdsReader::create(&participant, topic, Some(qos), Some(listener)).unwrap();

let value = rx.recv().unwrap();

assert!(value.is_some());

if let Some(msg) = value {
assert_eq!(msg.userID, 1);
// assert_eq!(
// msg.message,
// "Hello from DDS Cyclone Rust"
// );
}
}
*/