#!/usr/bin/perl -I/server/GoogleProxy/GoogleProxy

#########################################
##### Author:		Sebastian Enger / B.Sc
##### CopyRight:	BitJoe GmbH
##### LastModified	16.07.2006
##### Function:		IO spezifisches Modul
##### Todo:			
########################################

package GoogleProxy::IO;

$| = 1;

use IO::Socket;
use strict;
no strict 'subs';

my $MaxCreateSocketTry			= 1;	# Anzahl Versuche für Aufbau eines Sockets
my $MaxCreateSocketRandSleep	= 1;	# Anzahl max Sekunden Wartezeit zwischen Aufbau des Sockets


sub new(){
	
	my $self = bless {}, shift;
	return $self;
		
}; # new()


sub readSocket() {
	
	my $self	= shift;
	my $socket	= shift;

	binmode($socket);

	# old working
	my $STREAM = <$socket>;
	return $STREAM;

}; # sub readSocket(){}


sub writeSocket(){
	
	my $self		= shift;
	my $socket		= shift;
	my $refContent	= shift;
	my $delay		= shift || 0;
	
#	eval { 
#
#		# select(undef, undef, undef, $delay );
#		binmode($socket);
#		
#	}; # eval { 
#
#	### print "ERROR : '$@' \n";


	if ( ref($refContent) eq 'ARRAY' ) {

		for ( my $i=0; $i<=$#{$refContent}; $i++ ) {
			syswrite($socket, $refContent->[$i], length($refContent->[$i]) );	
		}; # for ( my $i=0; ... 

	} elsif ( ref($refContent) eq 'HASH' ) {

		my $keys = keys( %{$refContent} );
		for ( my $i=0; $i<=$keys; $i++ ) {
			syswrite($socket, $refContent->{$i}, length($refContent->{$i}) );
		}; # for ( my $i=0; ...	

	} elsif ( ref($refContent) eq 'SCALAR' ) {
		syswrite($socket, ${$refContent}, length(${$refContent}) );

	} elsif ( ref($refContent) eq '' ) {	# normaler scalar senden
		syswrite($socket, $refContent, length($refContent) );

	} else {

		# code oder glob ref
		warn "GoogleProxy::IO->writeSocket(): CODE or GLOB Ref - not supported\n";
		return -1;

	}; # if
	
	return 1;	# always false

}; # sub sub writeSocket(){}

# Handy <-> Paris Distributed ?
sub CreateHandyDistributedSocket(){

	my $self			= shift;
	my $PhexServerIP	= shift;
	my $port			= shift;

	my $socket			= IO::Socket::INET->new(
				LocalPort	=> $port,
				Proto		=> 'tcp',
				Listen		=> SOMAXCONN,
				Reuse		=> 1 ) or warn "GoogleProxy::IO->CreateHandyDistributedSocket($port): Can't create listen socket: $!\n";

	if ( ref($socket) ne "IO::Socket::INET" ) {

	###	$ICQ->SendICQMessage("FrontendSocket for BitJoe Paris is down");
	#	$MAIL->SendNoHandyClientSocketMail( 3383 );
	#	$SMS->SendHandyClientSocketDownSMS($PhexServerIP);

	}; # if ( ref($socket) ne "IO::Socket::INET" ) {

	return $socket;

}; # sub CreateHandyDistributedSocket(){


sub readRandomBytes(){
	
	srand();

	my $self   = shift;
	my $length = shift || int(rand(8096))+1;
	my $result;

	if (-r RANDOM_DEVICE && open(RAND,RANDOM_DEVICE)) {
		read(RAND,$result,$length);
		close RAND;
	} else {
		return GoogleProxy::CryptoLibrary->SimpleURandom( $length );	# $length ist die länge des seeds
	};
	
	return $result;

}; # sub readRandomBytes {}


sub ReadFileIntoArray(){

	my $self = shift;
	my $File = shift || warn "GoogleProxy::IO->ReadFileIntoArray(): Error Nof File given for reading: $!\n";

	my @content = ();
	open( RH, "<$File");
		flock(RH, 2);
		@content = <RH>;
	close RH;

	return \@content;

}; # sub ReadFileIntoArray(){}


sub ReadFileIntoScalar(){

	my $self = shift;
	my $File = shift || return -1;

	my $FileContent;
	open(FILE, "<$File" ) or sub { warn "GoogleProxy::IO->ReadFileIntoScalar(): IO ERROR: $!\n"; return -1; };
		flock(FILE, 2);
		{ 	local $/ = undef;			# Read entire file at once
			$FileContent = <FILE>;   # Return file as one single `line'
        };  
	close FILE;

	return \$FileContent;

}; # sub ReadFileIntoScalar(){}


sub WriteFile(){

#	my $URandom			= GoogleProxy::CryptoLibrary->URandom( rand(100) );
#	my $URandomMD5		= GoogleProxy::CheckSum->MD5ToHEX( \$URandom ); 
#	my $RandStringPart	= time() . $URandomMD5;

	my $self			= shift;
	my $File			= shift || warn "GoogleProxy::IO->WriteFile(): NO FILE FOR WRITING\n";
	my $refContent		= shift;

	open(FILE, ">$File") or warn "GoogleProxy::IO->WriteFile(): Open Error: $!\n";
		binmode(FILE);
		flock(FILE, 2);
		if ( ref($refContent) eq 'ARRAY' ) {

			for ( my $i=0; $i<=$#{$refContent}; $i++ ) {
				print FILE $refContent->[$i];
			}; # for ( my $i=0; ... 

		} elsif ( ref($refContent) eq 'HASH' ) {

			my $keys = keys( %{$refContent} );
			for ( my $i=0; $i<=$keys; $i++ ) {
				print FILE $refContent->{$i};
			}; # for ( my $i=0; ...	

		} elsif ( ref($refContent) eq 'SCALAR' ) {
			print FILE ${$refContent};

		} elsif ( ref($refContent) eq '' ) {	# normaler scalar senden
			print FILE $refContent;

		} else {

			# code oder glob ref
			warn "GoogleProxy::IO->WriteFile(): CODE or GLOB Ref - not supported\n";
			close FILE;
			return -1;

		}; # if ( ref($refContent) eq 'ARRAY' ) {

	close FILE;

	return $File;

}; # sub WriteFile(){}


sub readSocketBM() {

	my $self	= shift;
	my $socket	= shift;
	
	my $IO;
	my $STREAM;
	my $StreamCount = 0;

	while (sysread($socket , $IO, 1) == 1) {
				
		$STREAM = $STREAM . $IO;
		$StreamCount++;

    }; # while (sysread($socket , $IO, 1) == 1) {}

	return $STREAM;

}; # sub _readSocketBM() {


return 1;