Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating fallback ip address - please explain "die ... unless $sock"

Status
Not open for further replies.

Andyfives

Programmer
Feb 22, 2002
46
0
0
DK
I have managed to write a working process that connects to a given IP address, sends a very basic xml string onto the host and receives a response back.

All well and good. Now I would like to add a fallback ip address that the process can call upon if it has a problem with the normal ip.

I thought about controlling this from my calling process (template-toolkit), but to do that I should return something worthwhile from here if the process fails... I guess I just don't know enough about the die command...

Please can someone help me amend this so I can get it to work? I have included my code so far...

the timeout property in my IO::Socket::INET does nothing.

Thanks in advance, here is the code...

Code:
package plugin::SterlingDCC;

use base qw( Template::Plugin );
use Template::Plugin;
use IO::Socket;

use lib::SkyObject;

use vars qw( @ISA );
@ISA		= qw( Template::Plugin lib::SkyObject );

#! /usr/bin/perl -w
#----------------

sub getResp {
	my $self			= shift;
	my %params			= (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
	# important adding new line char to reqXML
	my $reqXML = $params{myReqXML} . \n;
	#open socket
	my $sock = new IO::Socket::INET (PeerAddr => $params{myURL},
					 PeerPort => $params{myPort},
					 Proto => 'tcp',
					 Timeout   => 3,);

	die "Could not create socket: $!\n" unless $sock;
	$resp="";
	# send xml request
	print $sock "$reqXML";
	#receive and print response
	$sock->autoflush(1); # so output gets there right away
	while (defined ($myline = <$sock>)) {
	   # print $myline;
	   $resp = $resp . $myline;
	}
       close($sock);
       return $resp;
};
1;
 
instad of "die" use "return(0);" to return a value of 0 (zero) to the calling script. If the caller gets back 0 it should do something else, like try the fallback IP address.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Ok, I will give that a try, thanks for your help. As you can see I'm no "raw" perl developer, but am having to learn some due to the simple non "webservice" host I have to connect with. Cheers
 
You're welcme, glad you got it working.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi again. I do have one more question....

Although the previous return(0) worked and I could use the template to call on my fallback IP there is a time delay of 30 seconds. This is too long.

I am tyring to set a timeout of around 2-3 seconds and notice that the IO::Socket::INET timeout value is doing nothing.

Reading up I found some detail on using an alarm method, but my small knowledge of perl is hindering me yet again. (incorrect) syntax below,

:-( :-

Code:
package plugin::SterlingDCC;

use base qw( Template::Plugin );
use Template::Plugin;
use IO::Socket;

use lib::SkyObject;

use vars qw( @ISA );
@ISA		= qw( Template::Plugin lib::SkyObject );

#! /usr/bin/perl -w
#----------------

sub getResp {
	my $timeout = 3;
	my $self			= shift;
	my %params			= (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
	my $reqXML = $params{myReqXML} . \n;
	my $resp="";

	$SIG{ALRM} = \&timed_out;
	eval {
		alarm ($timeout);
		#open socket
		my $sock = new IO::Socket::INET (PeerAddr => $params{myURL},
			PeerPort => $params{myPort},
			Proto => 'tcp',
			);
	
		print $sock "$reqXML";
		$sock->autoflush(1); # so output gets there right away
		while (defined ($myline = <$sock>)) {
			$resp = $resp . $myline;
		}
		alarm (0);
		close($sock);
	};
	$resp;
};

sub timed_out {
  return (0);
}

1;

I am sure I have been close to getting it to work somewhere along the line, but not sure where... might be the parameter variables are not being recognised in the eval...


Any help again with this would be really great.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top