#! /usr/bin/perl

###########################################################################
# This script executes a series of udpmon transfers with varying
# test pkt numbers
###########################################################################
#
# Author: Yee-Ting Li, UCL HEP
# 
# Change History:
#  19-07-02: initial release
#
###########################################################################

require 'udpmon-cook.pl';

$DEBUG = 1;

if ( $#ARGV < 0 ) {
    &printHelp;
}


# parse through the args
for ( $i=0; $i<$#ARGV+1; $i++ ) {

    if( $ARGV[$i] =~ /^-(.*)/ ) {

	$switch = $1;

	# connection host
	if ( $switch eq 'd' ) {
	    $destip = $ARGV[$i+1];
	}
	elsif ( $switch eq 'p' ) {
	    $dest_port = $ARGV[$i+1];
	}
	elsif ( $switch eq 'n' ) {
#	    @pktss = split( /,/, $ARGV[$i+1] );
	    $pkts = $ARGV[$i+1];
	}
	elsif ( $switch eq 's' ) {
	    @sizes = split( /,/, $ARGV[$i+1] );
	    $size = $ARGV[$i+1] ;
	}
	elsif ( $switch eq 'f' ) {
	    $outfile = $ARGV[$i+1];
	}
	elsif ( $switch eq 'i' ) {
	    $interval = $ARGV[$i+1];
	}
	elsif ( $switch eq 'h' ) {
	    &printHelp;
	}


    }


}


# go through an put default values

if ( $dest_port eq '' ) { $dest_port = 14233; }
if ( $pkts eq '' ) { $pkts = 300; }
if ( $interval eq '' ) { $interval = 10 };
if ( $#sizes eq -1  ) { 
    @sizes = ( '100', '200', '300', '400', '500', '600' , '700', '800', '900', '1000', '1100', '1200' , '1300', '1400' ); 
}


if ( $DEBUG ) { print "\ndest: $destip, port: $dest_port, packets: $pkts, pkt interval: $interval\npacket sizes: @sizes\n" ; }




$sourceip = `hostname`; chomp($sourceip);
$UDPMON = `which udp_bw_mon`; chomp( $UDPMON );


### Setup to go

# Get the time and date
my ($sec, $minutes, $hour, $mday, $month, $year, $wday, $yday, $isdst) = gmtime();

# create a directory based on dd-mm-yy in working directory
$dir = sprintf "%2.2d-%2.2d-%4d", $mday, $month+1, $year+1900;
`mkdir $dir \&> /dev/null`;

$count = 1;
$total = $#sizes + 1;

@files;

print STDERR '=' x 72 . "\n";

# get the ip address
my ( $temp, $destAdd ) = &udpmonGetIPs( $destip, $destip );



foreach my $size (@sizes) {

    print STDERR "Conducting test packet size $size bytes ($count/$total)\n";

    # get time again
    ($sec, $minutes, $hour, $mday, $month, $year, $wday, $yday, $isdst) = gmtime();

    # create a filename to store output
    $filename = sprintf "$dir\/udpmon_%4d%2.2d%2.2d-%2.2d%2.2d%2.2d_" . "$sourceip" . "_" . "$destip" . "_n" . "$pkts" . "_i" . $interval . '_s' . $size . ".log", $year+1900, $month+1, $mday, $hour, $minutes, $sec;


    # add filename to array
    push ( @files, $filename );
    $udpmon_cmd = "$UDPMON -d $destAdd -u $dest_port -l $pkts -p $size -w $interval \&> $filename";
    
#    print $udpmon_cmd;

    # execute iperf
    system( $udpmon_cmd );

    $count++ ;
}

print STDERR '=' x 72 . "\n";
print STDERR "Tests complete!\n";
print STDERR "\nCooking...\n";
print STDERR '=' x 72 . "\n";



if ( $outfile eq '' ) {
    &udpmonCook( @files );
}
else {
    
    open(SAVEOUT, ">&STDOUT");
    open (STDOUT,">$outfile") || warn "SSH_Open: Could not redirect STDOUT to $filename: $!\n";
    select(STDOUT); $| = 1;	
    &udpmonCook( @files );   
    # redirect stdout to stdout
    open(STDOUT, ">&SAVEOUT");
    open ( OUT, "<$outfile" );
    while ( <OUT> ) {
	print $_;
    }

}
print STDERR '=' x 72 . "\n";

print STDERR "\nComplete!\n";


sub printHelp{
    print "Usage: do_packetsize.pl -d <sink> [-p <port>] [-i <pkt interval>(msec)] [-n <number of pkts>] [-s <s1,s2,s3...>(bytes)]\n";
exit;
}



