Open a path from the root, returning a communication handle Note: This function spawns a thread which will then attempt to open the socket. It is possible that this function succeeds but then opening the socket fails.
(path: &str)
| 242 | /// to open the socket. It is possible that this function |
| 243 | /// succeeds but then opening the socket fails. |
| 244 | fn open_path(path: &str) -> Result<CommHandle, ()> { |
| 245 | if let Some(ind) = path.find('/') { |
| 246 | // Split and copy into Strings which can be moved to a new thread |
| 247 | let host_str = String::from(&path[..ind]); |
| 248 | |
| 249 | let port: u16 = ((&path[(ind+1)..]) |
| 250 | .trim_matches(|c:char| c == '/' || |
| 251 | c.is_whitespace())) |
| 252 | .parse().map_err(|e| {println!("[tcp] Invalid port: {:?}", e);})?; |
| 253 | |
| 254 | // Make a new communication handle pair |
| 255 | let (handle, client_handle) = syscalls::new_rendezvous() |
| 256 | .map_err(|e| {println!("[tcp] Couldn't create Rendezvous {:?}", e);})?; |
| 257 | |
| 258 | // Start a thread with one of the handles |
| 259 | thread::spawn(move || { |
| 260 | // Get the IP address |
| 261 | let ip = match IpAddress::from_str(&host_str) { |
| 262 | Ok(ip) => ip, |
| 263 | Err(_) => { |
| 264 | // Not an IP address, so assume it's a host name to be resolved |
| 265 | match dns::resolve(&host_str) { |
| 266 | Ok(addr) => { |
| 267 | println!("[tcp] {} has address {}", &host_str, addr); |
| 268 | addr |
| 269 | } |
| 270 | Err(e) => { |
| 271 | println!("[tcp] Could not resolve host {}: {:?}", &host_str, e); |
| 272 | return; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | }; |
| 277 | |
| 278 | open_socket(ip, port, handle); |
| 279 | }); |
| 280 | |
| 281 | // Return the other handle to the client |
| 282 | return Ok(client_handle); |
| 283 | } |
| 284 | println!("[tcp] Error: open_path '{}' doesn't contain '/'", path); |
| 285 | Err(()) |
| 286 | } |
| 287 | |
| 288 | /// Returns a port number in the range 49152–65535. |
| 289 | /// |
no test coverage detected