#!/usr/bin/perl

$event_filter = "r";
@ptype_filter = ("tcp", "cbr", "exp", "tcpFriend");

#@flow_name = ("HTTP", "FTP", "Status Report", "Streaming Media");

# trace MAC instead of AGT
# (ns doesn't install trace objects at the ports of all Mobile Nodes!)
$ttype_filter = "MAC";

if (@ARGV != 2) {
    printf "usage plot_flows.pl <trace file> <node>\n";
    exit;
}

$max_tp = 0; # max. throughput for scaling of plot

$exponential_y = 0;

$granularity = 1.0;
@p_data = ();
@p_time = ();
@trace_list = ();
@trace_type = ();

$tracename = shift;
$tracenode = shift;
$outname = "flow_n$tracenode.ps";

open(infile, $tracename) or die "couldn't open $tracename";
while ($line = <infile>) {
    ++$line_no;

    next if ($line !~ /^$event_filter/);
    $cont = 0;
    foreach $t (@ptype_filter) {
        if ($line =~ /$t/) {
	    $cont = 1;
	    last;
	}
    }
    next unless ($cont);

    @entry = split(/[\s\(\)\[\]]+/, $line);

    $event = $entry[0];
    $time = $entry[1];

    # check for trace format and parse entry
    if ($entry[2] =~ /_+(\d+)_+/) { # mobile
	$to = $1;
	next if ($to != $tracenode);
	next if ($entry[3] ne $ttype_filter);
	$pktid = $entry[5];
	$type = $entry[6];
	$size = $entry[7];
	$flow = $entry[13];
    } else {                        # standard
	$to = $entry[3];
	next if ($to != $tracenode);
	$pktid = $entry[11];
	$from = $entry[2];
	$type = $entry[4];
	$size = $entry[5];
	$flow = $entry[7];
    }

    if ($outfile[$flow] eq "") {
	$outfile[$flow] = $fh++;
	open($outfile[$flow], ">flow$flow.log") or die "couldn't open flow$flow.log";
	push @trace_list, $flow;
	$trace_type[$flow] = "$type";
	$p_time[$flow] = 0.0;
	$p_data[$flow] = 0;
	printf { $outfile[$flow] } "0.0 0\n";

	if ((not defined $flow_name[$flow]) || ($flow_name[$flow] eq "")) {
	    $flow_name[$flow] = "$type $flow";
	}
    }

    while ($time > $p_time[$flow] + $granularity) {
	$p_time[$flow] += $granularity;
	#printf "%08.4f $flow %i\n", $p_time[$flow], $p_data[$flow] / $granularity;
	printf { $outfile[$flow] } "%08.4f %i\n", $p_time[$flow], $p_data[$flow] / $granularity;
	$max_tp = $p_data[$flow] if ($max_tp < $p_data[$flow]);
	$p_data[$flow] = 0;
    }

    $p_data[$flow] += $size;
}
close(infile);
$max_tp *= 8.0 / 1000.0 / $granularity; # adjust to KBit/s and granularity

foreach $flow (@trace_list) {
    while ($time >= $p_time[$flow] + $granularity) { # fill rest with 0
	$p_time[$flow] += $granularity;
	printf { $outfile[$flow] } "%08.4f %i\n", $p_time[$flow], $p_data[$flow] / $granularity;
	$p_data[$flow] = 0;
    }
    close($outfile[$flow]);
}

@plot_list = sort { $a <=> $b } @trace_list;
die "no nodes to plot" if (@plot_list == 0);

open(cmd, ">cmd.tmp");
print cmd "set term post eps color solid 14\n";
#print cmd "set term post color solid 14\n";
print cmd "set output \"$outname\"\n";
print cmd "set key top left outside\n";
print cmd "set size 1.0,0.6\n";
print cmd "set xlabel \"Time (s)\"\n";
print cmd "set xrange [0:$time]\n";
print cmd "set ylabel \"Throughput (KBit/s)\"\n";
if ($exponential_y == 0) {
    print cmd "set yrange [0:". ($max_tp * 1.1) . "]\n";
} else {
    print cmd "set yrange [1:". ($max_tp * 2) . "]\n";
    print cmd "set logscale y\n";
}

$nr = 0;
print cmd "plot ";
foreach $flow (@plot_list) {
    print cmd ", " if ($nr > 0);
    if ((defined $flow_name[$flow]) && ($flow_name[$flow] ne "")) {
	print cmd "\"flow$flow.log\" u 1:(\$2*8/1000) t \"$flow_name[$flow]\" w l";
    } else {
	print cmd "\"flow$flow.log\" u 1:(\$2*8/1000) t \"flow $flow\" w l";
    }
    ++$nr;
}
print cmd "\n";
close(cmd);
system "gnuplot cmd.tmp; rm -f cmd.tmp";
