#!/usr/bin/perl use strict; use IO::Socket; use IO::Socket::INET; package Pos::Webtop::WebtopConnection; sub new { my $this = shift; my $class = ref($this) || $this; my ($self,$tb) = @{{@_}}{qw/self tb/}; if ( not defined $self ) { $self = {}; bless($self,$class); } $self->{'tb'} = $tb; return $self; } sub DESTROY { my $self = shift; $self->disconnect; } sub connect { my $self = shift; my ($companyId, $host, $port) = @{{@_}}{qw/companyId host port/}; my $connection = undef; my $supernatHost = $self->{'tb'}->resolveHost(host => $host); my $new; if (not defined($supernatHost)) { print STDERR "WebtopConnection: could not resolve host $host.\n"; $supernatHost = $host; } if (defined($new = $self->{'tb'}->getSupernatInternalAddress(ip => $supernatHost))) { $supernatHost = $new; } # XXX: Add supernat/dns. $supernatHost =~ /(.*)/; $supernatHost = $1; if (defined($supernatHost) and defined($port)) { $connection = IO::Socket::INET->new( PeerAddr => $supernatHost, PeerPort => $port, Proto => 'tcp'); } $self->{'connection'} = $connection; return $connection; } sub readLine { my $self = shift; my $line = undef; my $conn = $self->getConnection; if (not defined($conn)) { return undef; } $line = <$conn>; return $line; } sub writeLine { my $self = shift; my ($text) = @{{@_}}{qw/text/}; my $conn = $self->getConnection; if (not defined($conn)) { return; } $text .= "\r\n"; syswrite($conn, $text); } sub getConnection { my $self = shift; return $self->{'connection'}; } sub disconnect { my $self = shift; if (defined($self->{'connection'})) { $self->writeLine(text => "QUIT"); } } 1;