#!/usr/bin/perl -I/srv/server/phexproxy/PhexProxy

#########################################
##### Author:		Sebastian Enger / B.Sc
##### CopyRight:	BitJoe GmbH
##### LastModified	5.03.2007
##### Function:		Hauptdatei für PhexProxy
##### Todo:			for statt foreach: ist schneller
########################################

# todo: connection counter - nach 5 mal keine ergebnisse bemühe den result cache
# connection request daten abholen: 20s, 10s, 10s, 7s, 5s, public result cache

#### TODO: zuerst müssen alle Perl Phex Paris Server laufen und dann erst $0 starten !!! ####


###### warten auf paul encrypt server #######

my $SortedHashRef			= {};
my $SortedHashRefResults	= {};
my $MaxSourcesForOneResult	= 15;

use constant VERSION		=> "PhexDistributedProxy 0.9.1.1 - 4.10.2007 @ 17.45 Uhr";
use constant PIDFILE		=> "/srv/server/phexproxy/phexdistributedproxygzip-pid.txt";

use constant CRLF			=> "\r\n";
use constant SEPARATOR		=> "##";


use strict;
use DB_File;
use IO::Select;
use POSIX qw(floor);
use Data::Dumper;
use PhexProxy::IO;
use PhexProxy::Gzip;
use PhexProxy::Time;
use PhexProxy::Daemon;
use PhexProxy::CheckSum;
use PhexProxy::CryptoLibrary;
use PhexProxy::SortArray qw( Sort_Table );

my $IO			= PhexProxy::IO->new();
my $GZIP		= PhexProxy::Gzip->new();
my $TIME		= PhexProxy::Time->new();
my $CHECK		= PhexProxy::CheckSum->new();
my $CRYPTO		= PhexProxy::CryptoLibrary->new();

# my $ArrayRef	= $IO->ReadFileIntoArray("/srv/server/phexproxy/finalsorted.txt");
 my $ScalarRef	= $IO->ReadFileIntoScalar("/srv/server/phexproxy/finalsorted.txt");

my $SortedHashRefCount = 0;
foreach my $content ( split("\r\n", ${$ScalarRef} ) ) {
	
	my ( $RANK, $SIZE, $SHA1, $PEERHOST ) = split("##", $content );
	$SortedHashRef->{ $SortedHashRefCount }->{ 'SZ' } = $SIZE;
	$SortedHashRef->{ $SortedHashRefCount }->{ 'SH' } = $SHA1;
	$SortedHashRef->{ $SortedHashRefCount }->{ 'RK' } = $RANK;
	$SortedHashRef->{ $SortedHashRefCount }->{ 'PH' } = $PEERHOST;
	
	print "DEBUG: RANK: '$RANK' \n";
	print "DEBUG: SIZE: '$SIZE' Bytes \n";
	print "DEBUG: SHA1: '$SHA1' \n";
	print "DEBUG: HOST: '$PEERHOST' \n";
	print " ############ \n";
	sleep 1;

	$SortedHashRefCount++;

}; # foreach my $content ( @ResultContent ) {

$IO->WriteFile("/srv/server/phexproxy/tmp.txt",Dumper $SortedHashRef );
# print Dumper $SortedHashRef;


my $SortHashRefCount = keys(%{$SortedHashRef});
	
for ( my $Key=0; $Key<=$SortHashRefCount; $Key++ ) {

	my $SHA1			= %{$SortedHashRef}->{ $Key }->{ 'SH' };
	my $Size			= %{$SortedHashRef}->{ $Key }->{ 'SZ' };
	my $PeerHost		= %{$SortedHashRef}->{ $Key }->{ 'PH' };
	my $Rank			= %{$SortedHashRef}->{ $Key }->{ 'RK' };

	for ( my $NextKey=0; $NextKey<=$SortHashRefCount; $NextKey++ ) {

		my $SHA1Next		= %{$SortedHashRef}->{ $NextKey }->{ 'SH' };
		my $SizeNext		= %{$SortedHashRef}->{ $NextKey }->{ 'SZ' };
		my $PeerHostNext	= %{$SortedHashRef}->{ $NextKey }->{ 'PH' };
		my $RankNext		= %{$SortedHashRef}->{ $NextKey }->{ 'RK' };

		if ( ($SHA1 eq $SHA1Next) && length($SHA1) == 32 && length($SHA1Next) == 32 ) {


			print $PeerHostNext;
		#	my @PEERS	= split("\r\n",$PeerHostNext );
		#	print Dumper @PEERS;

		#	delete $SortedHashRef->{ $NextKey };

		}; # if ( ($SHA1 eq $SHA1Next) && length($SHA1) == 32 && length($SHA1Next) == 32 ) {

	}; # for ( my $NextKey=0; $NextKey<=$SortHashRefCount; $NextKey++ ) {

}; # for ( my $Key=0; $Key<=$SortHashRefCount; $Key++ ) {