#!/usr/bin/perl -w

# *Very* simple script to plot the logical structure of the forest generated by the
# tree formation algorithm.
# INPUT: (stdin) tossim debug log
# OUTPUT: (stdout) a ps version of the graph

# The script parses a specific type of debug message in the TreeRouting TOSSIM channel:
# DEBUG (node) ... sendBeaconTask$runTask parent: X hopcount ...
# It then plots the *last* parent seen for each node in the log.
# Requires the installation of the GraphViz perl module (just do >cpan install GraphViz)

# Author: Rodrigo Fonseca

use GraphViz;
$INVALID_ADDR = 65535;

$g = GraphViz->new();

while (<>) {
	chomp;
	if (($child,$parent) = /DEBUG \((\d+)\):.*sendBeaconTask\$runTask parent: (\d+) hopcount/) {
		if ($parent != $INVALID_ADDR) {
			$parent{$child} = $parent;
		}
	} 

}
for $node (keys %parent) {
	$g->add_node($node);
	$g->add_node($parent{$node});
	$g->add_edge($node => $parent{$node});
}
print $g->as_ps;

