1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use gosh_core::*;
use gut::prelude::*;
use rand::Rng;
use std::path::{Path, PathBuf};
mod base;
mod client;
mod interactive;
mod scheduler;
mod server;
mod worker;
pub mod cli;
pub mod rest;
pub mod task;
fn hostname() -> String {
let mut buf = [0u8; 512];
nix::unistd::gethostname(&mut buf)
.unwrap()
.to_str()
.unwrap()
.to_string()
}
use std::net::{SocketAddr, TcpListener, ToSocketAddrs};
pub fn address_available<A: ToSocketAddrs>(address: A) -> bool {
match TcpListener::bind(address) {
Ok(_) => true,
Err(_) => false,
}
}
pub fn get_free_tcp_address() -> Option<SocketAddr> {
let host = hostname();
TcpListener::bind(format!("{host}:0")).ok()?.local_addr().ok()
}
#[test]
fn test_addr() {
let addr = get_free_tcp_address().unwrap();
assert!(address_available(dbg!(addr)));
}
pub use base::LockFile;
#[cfg(feature = "adhoc")]
pub mod docs {
macro_rules! export_doc {
($l:ident) => {
pub mod $l {
pub use crate::$l::*;
}
};
}
export_doc!(base);
export_doc!(interactive);
export_doc!(worker);
}