#! /usr/bin/perl

sub udpmonCook
{

    my ( @filenames ) = @_;

  
    print "date\tsource\tsink\tsink_port\tpacket_size\tfraction_lost\tpkts_sent\tpkts_rcvd\tpkts_lost\tpkts_badorder\tpkts_timeout_zero\tpkts_timeout\tduration\ttime_per_frame\treq_interval\tinterval\ttime_rcv_pkt\tdata_rate\trecv_data_rate\trecv_wire_rate\n";


    foreach my $file (@filenames) {

	
	my $udpPort;
	my $pktSize;


	my ( $temp, $date, $source, $sink ) = split /_/, $file;
	
	open ( UDPMON, "<$file");
	
	while (<UDPMON>) {
	    
	    if ( /destination UDP port is\s+(\d+)/ ) {
		$udpPort = $1;
		
		#   print "FOUND UDPPORT\n";
	    }
	    elsif ( /(\d+) bytes/ ) {
		$pktSize = $1;
	    }
	    elsif ( /\s+$pktSize/ ) {
		my $line = $_;
		@values = split /\s+\;\s+/, $line;
		
		#  print "FOUND DATA\n";
	    }
	 
	} #end while
	close ( UPDMON);
	
	my ( $pkt_len, $loop_count, $num_timeout_zero, $num_timeout, $time_frame, $wait_time, $send_time, $data_rate, $num_recv, $num_lost, $num_bad_order, $time_1, $time_recv_pkt, $recv_data_rate, $recv_wire_rate ) = @values;
	
	$pkt_len =~ s/\s//;
	
	my $perc_lost;
	if ( $loop_count > 0 ) {
	    $perc_lost = ($num_lost / $loop_count);
	    $perc_lost = sprintf "%.3f", $perc_lost;
	}

	my ( $sourceAdd, $sinkAdd ) = &udpmonGetIPs( $source, $sink );
	
	my $output = sprintf( "$date\t$source:$sourceAdd\t$sink:$sinkAdd\t$udpPort\t$pkt_len\t%.2f\t$loop_count\t$num_recv\t$num_lost\t$num_bad_order\t$num_timeout_zero\t$num_timeout\t$time_1\t$time_frame\t$wait_time\t$send_time\t$time_recv_pkt\t%.3f\t%.3f\t%.3f" , $perc_lost, $data_rate, $recv_data_rate, $recv_wire_rate);

	    print $output;

	print "\n";

    }
    
}




sub udpmonGetIPs {

    local ( $source, $sink ) = @_;

    # find out the IP addresses of source and destination
    my ($true_name,$aliases,$addrtype,$addrlength,@addrs) = gethostbyname($source);
    if (!defined $true_name) {
	print STDERR "+++Data_GetIPs: Error! Can not resolve $source to IP address\n";
    }
    else {
	($a, $b, $c, $d) = unpack('C4', $addrs[0]);
	$src_ip = "$a.$b.$c.$d";
    }
    
    my ($true_name,$aliases,$addrtype,$addrlength,@addrs) = gethostbyname($sink);
    if (!defined $true_name) {
	print STDERR "+++Data_GetIPs: Error! Can not resolve $sink to IP address\n";
    }
    else {
	($a, $b, $c, $d) = unpack('C4', $addrs[0]);
	$sink_ip = "$a.$b.$c.$d";
    }
    
    if ( $_debug eq 10 ) {
	print "+Data_GetIPs: source ip: " . $src_ip . "\t" . "sink ip: " . $sink_ip . "\n";;
    }
    
    return my @ips = ($src_ip, $sink_ip);
    
}




1;
