-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,6 @@ authors = [ "Jon Binney <jon.binney@gmail.com>" ] | |
[lib] | ||
name = "ros_rust" | ||
|
||
[[bin]] | ||
name = "ros_rust_demo" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
extern crate regex; | ||
|
||
pub mod xmlrpc; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|