Skip to content

Commit

Permalink
Split into modules #1
Browse files Browse the repository at this point in the history
  • Loading branch information
jonbinney committed Nov 29, 2014
1 parent 4a6167a commit 6f57a23
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ authors = [ "Jon Binney <jon.binney@gmail.com>" ]
[lib]
name = "ros_rust"

[[bin]]
name = "ros_rust_demo"

17 changes: 17 additions & 0 deletions src/bin/ros_rust_demo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#[allow(dead_code)]
fn main() {
println!("Starting node");

//let master_uri = "localhost:11311";
//let node_name = "foo_node";
//let caller_api = "localhost:12345";

//let node = MasterProxy {
// master_uri: String::from_str(master_uri),
// caller_id: String::from_str(node_name),
// caller_api: String::from_str(caller_api)
// };

//let response = node.get_published_topics();
//println!("response: {}", response);
}
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
extern crate regex;

pub mod xmlrpc;

48 changes: 48 additions & 0 deletions src/master_proxy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::fmt;
use std::string;

struct MasterProxy {
master_uri: String,
caller_id: String,
caller_api: String
}


impl MasterProxy {
fn get_published_topics(&self) -> String
{
let request = format!(
"<?xml version=\"1.0\"?>\n\
<methodCall>\n\
<methodName>getPublishedTopics</methodName>\n\
<params>\n\
<param>\n\
<value><string>{}</string></value>\n\
<value><string></string></value>\n\
</param>\n\
</params>\n\
</methodCall>\n", self.caller_id);

execute_xmlrpc_request(self.master_uri.as_slice(), request.as_slice())
}

fn register_subscriber(&self, topic: &str, topic_type: &str) {
let request = format!(
"<?xml version=\"1.0\"?>\n\
<methodCall>\n\
<methodName>registerSubscriber</methodName>\n\
<params>\n\
<param>\n\
<value><string>{}</string></value>\n\
<value><string>{}</string></value>\n\
<value><string>{}</string></value>\n\
<value><string>{}</string></value>\n\
</param>\n\
</params>\n\
</methodCall>\n", self.caller_id, topic, topic_type, self.caller_api);

execute_xmlrpc_request(self.master_uri.as_slice(), request.as_slice());
}
}


75 changes: 75 additions & 0 deletions src/xmlrpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use std::io::TcpStream;
use regex::Regex;

pub struct XMLRPCProxy {
server_uri: String
}

impl XMLRPCProxy {
pub fn execute_request(&self, request: &str) -> Vec<String> {

let mut stream = TcpStream::connect(self.server_uri.as_slice()).unwrap();

let header = format!(
"POST /RPC2 HTTP/1.0\n\
User-Agent: RosRust/0.0\n\
Host: localhost\n\
Content-Type: text/xml\n\
Content-length: {content_length}\n\n", content_length=request.len());

let message = header + request.to_string();

println!("request: {}", message);

// Send request to server
match stream.write(message.as_bytes()) {
Ok(_) => (),
Err(err) => panic!("{}", err),
}

// Read response from server
let response_str = match stream.read_to_string() {
Ok(response_str) => response_str,
Err(err) => panic!("{}", err),
};

// Parse reponse
parse_response(response_str.as_slice())
}
}

fn parse_response(response_str: &str) -> Vec<String> {
let param_re = match Regex::new(r"<value><string>([^<]*)</string></value>") {
Ok(re) => re,
Err(err) => panic!("{}", err),
};

let mut params: Vec<String> = vec![];
for cap in param_re.captures_iter(response_str) {
params.push(cap.at(1).to_string());
println!("{}", cap.at(1));
}
params
}

#[test]
fn test_parse_good_response() {
let response_str =
"HTTP/1.1 200 OK\n\
Content-Length: 158\n\
Content-Type: text/xml\n\n\
<?xml version=\"1.0\"?>\n\
<methodResponse>\n\
<params>\n\
<param>\n\
<value><string>param1</string></value>\n\
</param>\n\
</params>\n\
</methodResponse>\n";

let params = parse_response(response_str);
let mut correct_result: Vec<String> = vec![];
correct_result.push("param1".to_string());
assert_eq!(params, correct_result);
}

0 comments on commit 6f57a23

Please sign in to comment.