#!/usr/bin/perl -w

use strict;
use CGI qw(:standard);

use vars qw( $start_time $time_start );
use vars qw( $data_dir );
use vars qw( $link_cc_sum $link_ci_sum $sample_total );

require "Common.pl";

my $DURATION_THRESHOLD = 400;
my $MAX_LINE = 100;
my $MAX_INDEX = 20923;
my $WIN_WIDTH = 400;

my $hide_snd = param( 'hide_snd' );
my $hide_rcv = param( 'hide_rcv' );
my $hide_stt = param( 'hide_stt' );

if ( ! $hide_snd )
{
    $WIN_WIDTH += 225;
}
if ( ! $hide_rcv )
{
    $WIN_WIDTH += 225;
}
if ( ! $hide_stt )
{
    $WIN_WIDTH += 250;
}

print "Content-type: text/html\n\n";
print <<EOM;
<HTML>
<HEAD>
<style type="text/css">
<!--
.style1 {
	font-size: 16px;
	font-family: "Times New Roman", Times, serif;
	background: "#DDDDDD";
}
.style2 {
	font-size: 16px;
	font-family: "Times New Roman", Times, serif;
	font-weight: normal;
}
.style3 {
	font-size: 32px;
	font-family: "Times New Roman", Times, serif;
	font-weight: bold;
}
-->
</style>
</HEAD>
<BODY>
EOM

my $hop_info_head;
my $receiver;

my $start_index = param( 'start_index' );
if ( ! $start_index )
{
    $start_index = 1;
}

my $goto_index = param( 'goto' );
if ( $goto_index )
{
    if ( $goto_index eq "+100" )
    {
        $start_index += 100;
    }
    elsif ( $goto_index eq "+1000" )
    {
        $start_index += 1000;
    }
    elsif ( $goto_index eq "+10000" )
    {
        $start_index += 10000;
    }
    elsif ( $goto_index eq "-100" )
    {
        $start_index -= 100;
    }
    elsif ( $goto_index eq "-1000" )
    {
        $start_index -= 1000;
    }
    elsif ( $goto_index eq "-10000" )
    {
        $start_index -= 10000;
    }
}
if ( $start_index < 1 )
{
    $start_index = 1;
}
elsif( $start_index > $MAX_INDEX - $MAX_LINE + 1 )
{
    $start_index = $MAX_INDEX - $MAX_LINE + 1;
}

print start_form;
print "<TABLE ALIGN=\"center\" WIDTH=700><TR ALIGN=\"center\">",
      "<TD><A HREF=\"Readme.php\" TARGET=\"_blank\">Help</A></TD>",
      "<TD>", submit( -name => "goto", -value=>'-10000'  ),
      "&nbsp;&nbsp;", submit( -name => "goto", -value=>'-1000'  ),
      "&nbsp;&nbsp;", submit( -name => "goto", -value=>'-100'  ),
      "&nbsp;&nbsp;Start from index: <input type=\"text\" name=\"start_index\" style=\"width: 40\" value = $start_index> of $MAX_INDEX",
      "&nbsp;&nbsp;", submit( -name => "goto", -value => "Update" ),
      "&nbsp;&nbsp;", submit( -name => "goto", -value=>'+100'  ),
      "&nbsp;&nbsp;", submit( -name => "goto", -value=>'+1000'  ),
      "&nbsp;&nbsp;", submit( -name => "goto", -value=>'+10000'  ),
      "</TD></TR>",
      "</TABLE>";
print "<TABLE BORDER=3 CELLPADDING=\"3\" STYLE=\"BORDER-COLLAPSE: COLLAPSE\" ALIGN=\"center\" WIDTH=$WIN_WIDTH>\n",
      "<TR ALIGN=\"center\" BGCOLOR=#FFFF00><TD CLASS=\"style3\" COLSPAN=11>Measurement Data List",
      "<DIV CLASS=\"style2\">(Hide columns:",
      "&nbsp;&nbsp;", checkbox( -name => 'hide_snd', -label => 'Sender', -onClick => 'submit()' ),
      "&nbsp;&nbsp;", checkbox( -name => 'hide_rcv', -label => 'Receiver', -onClick => 'submit()' ),
      "&nbsp;&nbsp;", checkbox( -name => 'hide_stt', -label => 'Start time', -onClick => 'submit()' ),
      ")</DIV>",
      "</TD></TR>\n";
print "<TR ALIGN=\"center\" BGCOLOR=#FFFF00 CLASS=\"style1\">",
      "<TD><A HREF=\"Readme.php?title_in=Index#Index\" TARGET=\"_blank\">Index</A></TD>",
      "<TD><A HREF=\"Readme.php?title_in=Path#Path\" TARGET=\"_blank\">Path</A></TD>",
      "<TD><A HREF=\"Readme.php?title_in=Duration#Duration\" TARGET=\"_blank\">Duration</A></TD>",
      "<TD><A HREF=\"Readme.php?title_in=Samples#Samples\" TARGET=\"_blank\">Samples</A></TD>",
      "<TD><A HREF=\"Readme.php?title_in=WCC#WCC\" TARGET=\"_blank\">WCC</A></TD>",
      "<TD><A HREF=\"Readme.php?title_in=WCF#WCF\" TARGET=\"_blank\">WCF</A></TD>";
if ( ! $hide_snd )
{
    print "<TD><A HREF=\"Readme.php?title_in=Sender#Sender\" TARGET=\"_blank\">Sender</A></TD>";
}
if ( ! $hide_rcv )
{
    print "<TD><A HREF=\"Readme.php?title_in=Receiver#Receiver\" TARGET=\"_blank\">Receiver</A></TD>";
}
if ( ! $hide_stt )
{
    print "<TD><A HREF=\"Readme.php?title_in=Start_time#Start_time\" TARGET=\"_blank\">Start time</A></TD>";
}
print "<TD><A HREF=\"Readme.php?title_in=ER#ER\" TARGET=\"_blank\">E.R.</A></TD></TR>\n";

if ( open( SUMMARY, "<", "history/summary_snd" ) )
{
    my $index = 1;
    my $remain_line = $MAX_LINE;
    while ( <SUMMARY> )
    {
        if ( $index >= $start_index )
        {
            chomp;
            my ( $nID, $st_time, $duration, $path_id, $end_reason, $host ) = split( /\t/ );
            my $data_id = "sender/$nID/$st_time";
            $data_dir = "history/$data_id";
            if ( $duration > $DURATION_THRESHOLD && GetStartTime() && GetHopInfoHead() && GetPongSummary() )
            {
                my $host_dir = "history/sender/$nID";
                my ( $null, $rcv_id ) = split( /-/, $path_id );
                my $receiver_dir = "history/receiver/$rcv_id";
                my $href_para = "?data_id=$data_id&no_refresh=on";
                my $c_c = sprintf( "%.2lf", $link_cc_sum * 1000 / $sample_total );
                my $c_i = sprintf( "%.2lf", $link_ci_sum * 1000 / $sample_total );
                if ( $c_i > 1000 || $c_i < 0 )
                {
                    $c_i = "(nan)";
                }
                print "<TR CLASS=\"style2\"><TD ALIGN=\"center\"><A HREF=\"$data_dir\" TARGET=\"_blank\">$index</A></TD>\n",
                      "<TD ALIGN=\"center\"><A HREF=\"ShowLCD.pl$href_para\" TARGET=\"_blank\">$path_id</A></TD>\n",
                      "<TD ALIGN=\"right\">$duration</TD>\n",
                      "<TD ALIGN=\"right\">$sample_total</TD>\n",
                      "<TD ALIGN=\"right\"><A HREF=\"ShowLinkCC.pl$href_para\" TARGET=\"_blank\">$c_c</A></TD>\n",
                      "<TD ALIGN=\"right\"><A HREF=\"ShowLinkCC.pl$href_para&show_ci=on\" TARGET=\"_blank\">$c_i</A></TD>\n";
                if ( ! $hide_snd )
                {
                    print "<TD><A HREF=\"$host_dir\" TARGET=\"_blank\">$host</A></TD>\n";
                }
                if ( ! $hide_rcv )
                {
                    print "<TD><A HREF=\"$receiver_dir\" TARGET=\"_blank\">$receiver</A></TD>\n";
                }
                if ( ! $hide_stt )
                {
                    print "<TD>$start_time</TD>\n";
                }
                print "<TD ALIGN=\"center\">$end_reason</TD></TR>\n";
            }
            $remain_line --;
            if ( $remain_line <= 0 )
            {
                last;
            }
        }
        $index ++;
    }
    close SUMMARY;
}

print "</TABLE>\n";

print end_form;

print <<EOM;
</BODY>
</HTML>
EOM


sub GetHopInfoHead
{
    if ( ! open( FILE, "<", "$data_dir/hop_info_head" ) )
    {
        return 0;
    }
    $hop_info_head = <FILE>;
    close FILE;
    chomp( $hop_info_head );
    $hop_info_head =~ s/<TD CLASS="style2" BGCOLOR=#FFFF00 COLSPAN=2>//;
    $hop_info_head =~ s/ \(<A HREF="#hop_info">Hop information<\/A>\)<\/TD>//;
    $receiver = $hop_info_head;
    $receiver =~ s/From .* to <U>//;
    $receiver =~ s/<\/U>//;
    return 1;
}


1;