Skip to content

Commit 152aaac

Browse files
committed
Parse connection headers and String messages
1 parent 2556b15 commit 152aaac

File tree

6 files changed

+83
-6
lines changed

6 files changed

+83
-6
lines changed

Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,3 @@ git = "https://github.com/rust-lang/regex"
1212
[dependencies.log]
1313
git = "https://github.com/rust-lang/log"
1414

15-
[dependencies.rustc-serialize]
16-
git = "https://github.com/rust-lang/rustc-serialize"
17-

src/bin/tcpros_subscriber.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,46 @@ extern crate ros_rust;
33
use std::mem;
44
use std::os;
55
use std::old_io::TcpStream;
6+
use std::old_io::MemReader;
7+
use std::collections::HashMap;
8+
9+
use ros_rust::msg::std_msgs;
10+
11+
/// Read a TCPROS connection header into a HashMap of key and value strings
12+
fn read_connection_header<R: Reader>(stream: &mut R) -> Result<HashMap<String, String>, String> {
13+
let header_length = match stream.read_le_u32() {
14+
Ok(header_length) => header_length,
15+
Err(_) => return Err("Failed to read connection header length".to_string()),
16+
};
17+
18+
// Read the connection header fields
19+
let mut header_bytes_read = 0u32;
20+
let mut fields: HashMap<String, String> = HashMap::new();
21+
while header_bytes_read < header_length {
22+
// Read the length of this field
23+
let field_length = match stream.read_le_u32() {
24+
Ok(field_length) => field_length,
25+
Err(_) => return Err("Failed to read connection header length".to_string()),
26+
};
27+
header_bytes_read += 4;
28+
29+
// Read the field itself
30+
let field_bytes = match stream.read_exact(field_length as usize) {
31+
Ok(field_bytes) => field_bytes,
32+
Err(_) => return Err("Failed to read connection header field".to_string()),
33+
};
34+
35+
let field_string = match String::from_utf8(field_bytes) {
36+
Ok(s) => s,
37+
Err(_) => return Err("Failed to interpret connection header field as string".to_string()),
38+
};
39+
40+
println!("{:?}", field_string);
41+
42+
header_bytes_read += field_length;
43+
}
44+
Ok(fields)
45+
}
646

747
#[allow(dead_code)]
848
fn main() {
@@ -47,10 +87,16 @@ fn main() {
4787
Err(_) => panic!("Unable to send data to server".to_string()),
4888
};
4989

90+
// Read the connection header that the server sends
91+
let connection_header_fields = match read_connection_header(&mut stream) {
92+
Ok(fields) => fields,
93+
Err(err) => panic!(err),
94+
};
95+
5096
println!("Reading data");
5197
loop {
52-
match stream.read_byte() {
53-
Ok(b) => println!("{}", b.to_string()),
98+
match std_msgs::String::from_stream(&mut stream) {
99+
Ok(s) => println!("{}", s.data),
54100
Err(_) => panic!("Read failed!"),
55101
}
56102
}

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ extern crate regex;
99
#[macro_use]
1010
extern crate log;
1111

12-
pub mod xml;
12+
pub mod msg;
1313
pub mod http;
14+
pub mod xml;
1415
pub mod xmlrpc;
1516

src/msg/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod std_msgs;

src/msg/std_msgs/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mod string;
2+
3+
pub use msg::std_msgs::string::String;

src/msg/std_msgs/string.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use std::string::String as std_String;
2+
use std::old_io::Reader;
3+
4+
pub struct String {
5+
pub data: std_String,
6+
}
7+
8+
impl String {
9+
pub fn from_stream<R: Reader>(stream : &mut R) -> Result<String, std_String> {
10+
let string_size = match stream.read_le_u32() {
11+
Ok(string_size) => string_size,
12+
Err(_) => return Err("Failed to read string length".to_string()),
13+
};
14+
15+
// Read the string data
16+
let string_data_bytes = match stream.read_exact(string_size as usize) {
17+
Ok(string_data_bytes) => string_data_bytes,
18+
Err(_) => return Err("Failed to read string data field".to_string()),
19+
};
20+
21+
let data = match std_String::from_utf8(string_data_bytes) {
22+
Ok(data) => data,
23+
Err(_) => return Err("Failed to interpret string data as utf8 string".to_string()),
24+
};
25+
26+
Ok(String {data: data})
27+
}
28+
}
29+

0 commit comments

Comments
 (0)