#!/usr/bin/perl
$tracefile=$ARGV[0];
$granularity=$ARGV[1];

$ofile="simulation_result.csv";
open OUT, ">$ofile" or die "$0 cannot open output file $ofile: $!";

open (DR,STDIN);
$gclock=0;


$dataSent = 0;
$dataRecv = 0;
$routerDrop = 0;


$aodvSent = 0;
$aodvRecv = 0;

$aodvSentRequest = 0;
$aodvRecvRequest = 0;
$aodvDropRequest = 0;

$aodvSentReply = 0;
$aodvRecvReply = 0;
$aodvDropReply = 0;

if ($granularity==0) {$granularity=30;}

while(<>){
	chomp;
	if (/^s/){
		if (/^s.*AODV/) {
			$aodvSent++;
			if (/^s.*REQUEST/) {
				$aodvSendRequest++;
			}
			elsif (/^s.*REPLY/) {
				$aodvSendReply++;
			}
		}
		elsif (/^s.*RTR/) {
			$dataSent++;			
		}		
		
	} elsif (/^r/){
		if (/^r.*AODV/) {
			$aodvRecv++;
			if (/^r.*REQUEST/) {
				$aodvRecvRequest++;
			}
			elsif (/^r.*REPLY/) {
				$aodvRecvReply++;
			}
			
		}
		elsif (/^r.*cbr/) {
			$dataRecv++;			
		}
		
	} elsif (/^D/) {
		if (/^D.*AODV/) {
			if (/^D.*REQUEST/) {
				$aodvDropRequest++;
			}
			elsif (/^D.*REPLY/) {
				$aodvDropReply++;
			}
			
		}
		if (/^D.*RTR/) {
			$routerDrop++;
		}
	}
	 
}

close DR;

$Drop_ratio = ($dataRecv/$dataSent)/100;
$Throughput = (($dataRecv/100)*(8/1000));
$PDR = $dataRecv/$dataSent;
$Delay       = (150-5)/$dataRecv;
$Overhead    = (($aodvSent+$dataSent)/$dataRecv)+4.0;

print "AODV Sent		: $aodvSent\n";
print "AODV Recv		: $aodvRecv\n";
print "Data Sent		: $dataSent\n";
print "Data Recv		: $dataRecv\n";
print "Router Drop		: $routerDrop\n";
print "Drop Ratio		: $Drop_ratio \n";
print "Throughput		: $Throughput \n";
print "PDR			: $PDR \n";
print "Delay			: $Delay \n";
print "Overhead			: $Overhead \n";

print OUT "Messages Sent,$dataSent\n";
print OUT "Messages Recieved,$dataRecv\n";
print OUT "Messages Dropped,$routerDrop\n";
print OUT "Drop Rate,$Drop_ratio\n";
print OUT "Drop Rate,$Throughput\n";

close OUT;

