From: Dobrica Pavlinusic Date: Wed, 25 May 2011 09:34:10 +0000 (+0200) Subject: refactor to proper Moose object X-Git-Url: http://git.rot13.org/?a=commitdiff_plain;h=713cf4eff364eb7bfeb0df97f6e1cea5be46a43b;p=APKPM.git refactor to proper Moose object --- diff --git a/lib/H1/ZTEDSLAM.pm b/lib/H1/ZTEDSLAM.pm index 6308a5c..2d2d575 100755 --- a/lib/H1/ZTEDSLAM.pm +++ b/lib/H1/ZTEDSLAM.pm @@ -2,14 +2,33 @@ package H1::ZTEDSLAM; use warnings; use strict; +use Moose; + use Net::Telnet; use Data::Dump qw(dump); +use Moose::Util::TypeConstraints; +use Regexp::Common qw(net); + +subtype IPAddr +=> as Str +=> where {/^$RE{net}{IPv4}$/} +=> message { 'invalid IP address'}; + +subtype Port +=> as Str +=> where { m{\d+(/\d+)+} } +=> message { 'invalid port' }; + +has 'ip' => ( is => 'rw', isa => 'IPAddr' ); +has 'port' => ( is => 'rw', isa => 'Port' ); our $telnet; sub telnet { - my ($ip) = @_; + my $self = shift; + + my $ip = $self->ip; return $telnet->{$ip} if exists $telnet->{$ip}; @@ -17,7 +36,7 @@ sub telnet { $t->dump_log('/tmp/log') if $ENV{DEBUG}; - warn "open"; + warn "open $ip"; $t->open( $ip ); $t->print(""); @@ -36,17 +55,12 @@ sub telnet { return $telnet->{$ip} = $t; } -sub hash { - my ($class,$ip,$port) = @_; - - warn "# hash $ip $port"; - - my $t = telnet($ip); - sub command { - my $command = shift; + my ($self,$command) = @_; - $command .= ' ' . $port; + $command .= ' ' . $self->port; + + my $t = $self->telnet; warn "# $command\n"; $t->print($command); @@ -73,11 +87,19 @@ sub command { } - warn "## $ip $command ",dump $hash; + warn "## ", $self->ip, " $command ",dump $hash; return $hash; } +sub hash { + my ($self) = @_; + + my $ip = $self->ip; + my $port = $self->port; + + warn "# hash $ip $port"; + our ( $row, $hash ); sub copy { @@ -87,19 +109,19 @@ sub copy { } } -$hash = command 'show interface'; +$hash = $self->command('show interface'); copy qw( AdminStatus LinkStatus LastLinkUpTime ); -$hash = command 'show adsl status'; +$hash = $self->command('show adsl status'); copy qw( LineConfProfile ); -$hash = command 'show adsl physical'; +$hash = $self->command('show adsl physical'); copy qw( AtucCurrSnrMgn AtucCurrAtn @@ -123,7 +145,8 @@ return $row; sub logout { - my ($ip) = @_; + my $self = shift; + my $ip = $self->ip; my $t = $telnet->{$ip} || die "no $ip telnet in ",dump($telnet); warn "logout $ip"; @@ -134,8 +157,9 @@ sub logout { } sub DESTROY { - warn "# telnet = ",dump($telnet); - logout($_) foreach keys %$telnet; + my $self = shift; + warn "# DESTROY telnet = ",dump( keys %$telnet ); + $self->logout($_) foreach keys %$telnet; } 1; diff --git a/t/ZTEDSLAM.t b/t/ZTEDSLAM.t index 3954f06..e57443c 100755 --- a/t/ZTEDSLAM.t +++ b/t/ZTEDSLAM.t @@ -9,6 +9,6 @@ use lib 'lib'; use_ok 'H1::ZTEDSLAM'; -ok my $hash = H1::ZTEDSLAM->hash( '10.99.123.16', '3/39' ), 'new'; +ok my $hash = H1::ZTEDSLAM->new( ip => '10.99.123.16', port => '3/39' )->hash, 'new'; diag dump $hash;