Read graph from file

This commit is contained in:
Pascal Engélibert 2025-10-17 16:11:43 +02:00
commit 9fc1ff2f8c
4 changed files with 46 additions and 7 deletions

View file

@ -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`.
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
[Support me via LiberaPay](https://liberapay.com/tuxmain/donate)

0
mitm.py Normal file
View file

View file

@ -1,10 +1,11 @@
mod generator;
mod reader;
mod render;
use std::{
collections::{HashMap, hash_map::Entry},
io::Write,
path::PathBuf,
path::{Path, PathBuf},
};
/// Generate configuration for local Reticulum networks
@ -16,6 +17,9 @@ struct Cli {
/// Generate graph and create Reticulum configs
#[argp(switch, short = 'g', long = "gen")]
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
#[argp(option, short = 'n', default = "5")]
nodes: usize,
@ -35,13 +39,17 @@ fn main() {
let cli: Cli = argp::parse_args_or_exit(argp::DEFAULT);
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!(
cli.proba >= 0.0 && cli.proba <= 1.0,
"Probability should be between 0 and 1."
);
let edges = generator::generate(cli.nodes, cli.proba, &mut rng);
assert!(
cli.proba >= 0.0 && cli.proba <= 1.0,
"Probability should be between 0 and 1."
);
generator::generate(cli.nodes, cli.proba, &mut rng)
};
let mut ports = HashMap::new();
let mut port: u16 = 42000;
@ -49,6 +57,7 @@ fn main() {
#[cfg(feature = "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);
}

20
src/reader.rs Normal file
View 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
}