Read graph from file
This commit is contained in:
parent
f2798a14d4
commit
9fc1ff2f8c
4 changed files with 46 additions and 7 deletions
10
README.md
10
README.md
|
|
@ -53,6 +53,16 @@ Path found, destination <0e1fb61e3b38e332868b331740642f41> is 4 hops away via <1
|
||||||
|
|
||||||
For now, UDP ports are allocated to links in order from `42000` to `65535`.
|
For now, UDP ports are allocated to links in order from `42000` to `65535`.
|
||||||
|
|
||||||
|
The graph can also be read from a text file listing edges, using option `-G <path>`. An edge is a space-separated list of two node indices. Separation can be any whitespace character. Example:
|
||||||
|
|
||||||
|
```
|
||||||
|
0 1
|
||||||
|
1 2
|
||||||
|
2 0
|
||||||
|
1 3
|
||||||
|
3 4
|
||||||
|
```
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
[Support me via LiberaPay](https://liberapay.com/tuxmain/donate)
|
[Support me via LiberaPay](https://liberapay.com/tuxmain/donate)
|
||||||
|
|
|
||||||
0
mitm.py
Normal file
0
mitm.py
Normal file
23
src/main.rs
23
src/main.rs
|
|
@ -1,10 +1,11 @@
|
||||||
mod generator;
|
mod generator;
|
||||||
|
mod reader;
|
||||||
mod render;
|
mod render;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
collections::{HashMap, hash_map::Entry},
|
collections::{HashMap, hash_map::Entry},
|
||||||
io::Write,
|
io::Write,
|
||||||
path::PathBuf,
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Generate configuration for local Reticulum networks
|
/// Generate configuration for local Reticulum networks
|
||||||
|
|
@ -16,6 +17,9 @@ struct Cli {
|
||||||
/// Generate graph and create Reticulum configs
|
/// Generate graph and create Reticulum configs
|
||||||
#[argp(switch, short = 'g', long = "gen")]
|
#[argp(switch, short = 'g', long = "gen")]
|
||||||
generate: bool,
|
generate: bool,
|
||||||
|
/// Read graph file instead of generating random (one edge per line, node numbers separated by space)
|
||||||
|
#[argp(option, short = 'G')]
|
||||||
|
graph: Option<String>,
|
||||||
/// Number of nodes
|
/// Number of nodes
|
||||||
#[argp(option, short = 'n', default = "5")]
|
#[argp(option, short = 'n', default = "5")]
|
||||||
nodes: usize,
|
nodes: usize,
|
||||||
|
|
@ -35,13 +39,17 @@ fn main() {
|
||||||
let cli: Cli = argp::parse_args_or_exit(argp::DEFAULT);
|
let cli: Cli = argp::parse_args_or_exit(argp::DEFAULT);
|
||||||
|
|
||||||
if cli.generate {
|
if cli.generate {
|
||||||
let mut rng = rand::thread_rng();
|
let edges = if let Some(graph_file) = cli.graph {
|
||||||
|
reader::read_graph_file(Path::new(&graph_file))
|
||||||
|
} else {
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
|
||||||
assert!(
|
assert!(
|
||||||
cli.proba >= 0.0 && cli.proba <= 1.0,
|
cli.proba >= 0.0 && cli.proba <= 1.0,
|
||||||
"Probability should be between 0 and 1."
|
"Probability should be between 0 and 1."
|
||||||
);
|
);
|
||||||
let edges = generator::generate(cli.nodes, cli.proba, &mut rng);
|
generator::generate(cli.nodes, cli.proba, &mut rng)
|
||||||
|
};
|
||||||
|
|
||||||
let mut ports = HashMap::new();
|
let mut ports = HashMap::new();
|
||||||
let mut port: u16 = 42000;
|
let mut port: u16 = 42000;
|
||||||
|
|
@ -49,6 +57,7 @@ fn main() {
|
||||||
|
|
||||||
#[cfg(feature = "render")]
|
#[cfg(feature = "render")]
|
||||||
if let Some(out_svg) = &cli.render {
|
if let Some(out_svg) = &cli.render {
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
render::render_to_svg(cli.nodes, &edges, out_svg, &mut rng);
|
render::render_to_svg(cli.nodes, &edges, out_svg, &mut rng);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
20
src/reader.rs
Normal file
20
src/reader.rs
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
use std::{
|
||||||
|
io::{BufRead, BufReader},
|
||||||
|
path::Path,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn read_graph_file(path: &Path) -> Vec<(usize, usize)> {
|
||||||
|
let file = std::fs::OpenOptions::new().read(true).open(path).unwrap();
|
||||||
|
let reader = BufReader::new(file);
|
||||||
|
let mut edges = Vec::new();
|
||||||
|
for line in reader.lines() {
|
||||||
|
let line = line.unwrap();
|
||||||
|
let mut cols = line.split_whitespace();
|
||||||
|
if let (Some(n1), Some(n2)) = (cols.next(), cols.next()) {
|
||||||
|
if let (Ok(n1), Ok(n2)) = (n1.parse(), n2.parse()) {
|
||||||
|
edges.push((n1, n2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
edges
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue