File:  [mozdev] / bitaacj / src / schedule.pl
Revision 1.6: download - view: text, annotated - select for diffs - revision graph
Tue Apr 4 15:00:52 2006 UTC (11 years, 8 months ago) by cherian
Branches: MAIN
CVS tags: HEAD
removed , problems and close socket

#!/usr/bin/perl -w
print "\nProgram Starting...... \n";
#Lets catch the errors as soon as we can
use strict;
use warnings;
use IO::Socket;
use DBI;
use Sys::Hostname;
use Switch;
use POSIX;
use threads;
require Net::Daemon;
use Net::Ping;
# use Time::HiRes qw( usleep ualarm gettimeofday tv_interval nanosleep clock_gettime clock_getres clock_nanosleep clock );
#use Thread::Pool;
STDOUT->autoflush(1);




#for my $handle ( *STDIN, *STDOUT, *STDERR){
#	open ($handle, "+<" ,"/dev/null")
#	|| die "can't reopen $handle to /dev/null : $!";
#}


#initializations
my $sql;
my $statement;
my $st_result=0;
my $size;
my $hashes;
my $count;
#mysql connection initialization
#database design will be posted on the homepage soo. jus check it out
print "\nTrying to connect to mysql database...... ";
my $db_handle = DBI->connect("dbi:mysql:database=tracker;host=10.4.1.185;user=sprt;password=sprt")
    or die "Couldn't connect to database: $DBI::errstr\n";
print "connected \n";


#delete records greater than one day
$sql= "delete from tab_ip_tim where tim < adddate(now(), interval -1 day )"; 
$statement = $db_handle->prepare($sql)
	or die "Couldn't prepare query '$sql': $DBI::errstr\n";
$statement->execute()
	or die "Couldn't execute query '$sql': $DBI::errstr\n";

#get ip's
$sql = "select distinct ip from tab_ip_tim;";
my $statement = $db_handle->prepare($sql) 
	or die "Couldn't prepare query '$sql': $DBI::errstr\n";
$statement->execute() 
	or die "Couldn't execute query '$sql': $DBI::errstr\n";
	# my $temp=hires();
while ( my $row = $statement->fetchrow_array ) {
	my $p = Net::Ping->new("tcp");
	my ($status, $latency) = $p->ping($row);
	if (defined($status)){
		print "$row is alive.\n" ;
		print "Latency of ping for ".$row ." is ". $latency."\n"; 
		#enter into table the ip's and latency prefetching cache from the peers
		$sql= "insert into schedule_ip_lat values('$row','$latency') on duplicate key update latency='$latency'"; 
		my $statement_schedule_ip_lat = $db_handle->prepare($sql)
			or die "Couldn't prepare query '$sql': $DBI::errstr\n";
		$statement_schedule_ip_lat->execute()
			or die "Couldn't execute query '$sql': $DBI::errstr\n";
	}
	else{
		print "$row Not alive \n";
		#deleting records not tcp pingable and 2 hours old
		$sql= "delete from tab_ip_tim where ip='$row' and tim < adddate( now(),inte rval -2 hour )"; 
		my $statement_delete_tab_ip_tim = $db_handle->prepare($sql)
			or die "Couldn't prepare query '$sql': $DBI::errstr\n";
		$statement_delete_tab_ip_tim->execute()
			or die "Couldn't execute query '$sql': $DBI::errstr\n";
	}
    	$p->close();
	#icmp unix pinging now disabled due to performance issues
	#print "Going to do icmp ping on  ".$row ." \n";
	#my @args= ("ping -c 1 $row >> c");
	#my $c = system (@args);
	#print "the result is ...". $c ."\n";
	}
#code for deleting from hitcounter 
my $sql_hitcounter= "delete hitcounter,tab_ip_tim from hitcounter, tab_ip_tim where hitcounter.count<2 and hitcounter.tim< adddate(now(), interval -2 hour) and tab_ip_tim.url_hash=hitcounter.url_hash;"; 
my $statement_hitcounter = $db_handle->prepare($sql_hitcounter)
	or die "Couldn't prepare query '$sql': $DBI::errstr\n";
$statement_hitcounter->execute()
	or die "Couldn't execute query '$sql': $DBI::errstr\n";

#code for prefetching 
$sql= "select ip from schedule_ip_lat order by latency asc limit 5"; 
my $statement_select = $db_handle->prepare($sql)
	or die "Couldn't prepare query '$sql': $DBI::errstr\n";
$statement_select->execute()
	or die "Couldn't execute query '$sql': $DBI::errstr\n";
while (my $row_select = $statement_select->fetchrow_array ) {
	#the big prefetching code starts here!!!!
	print "IPs are " . $row_select ." \n";
	my $sock = new IO::Socket::INET (
                                 PeerAddr => $row_select,
                                 PeerPort => '27003',
                                  Proto => 'tcp',
                                 );
	die "Could not create socket: $!\n" unless $sock;
	$sock->send("PRE000");
	#the browser should reply back the hashes list...but since this is 2nd priority and that akhil has not coded the corresponding frame....i'll be adding  #the split functions sometime later
	$sock->recv($size, 3);
	$sock->recv($hashes,$size);
	my $leng = length($hashes);
	for($count=0; $count<$leng; $count+=32) {
	my $md5hash = substr($hashes, $count, 32);
	my $sql_prefetch= "insert into tab_ip_tim values ('$md5hash','$sock->peerhost()',now()) on duplicate key update tim=now()"; 
	my $statement_prefetch = $db_handle->prepare($sql_prefetch)
		or die "Couldn't prepare query '$sql': $DBI::errstr\n";
	$statement_prefetch->execute()
		or die "Couldn't execute query '$sql': $DBI::errstr\n";
	}
	

	}






FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>