Portal.pm

filter: Statementstmt 33.5% 59 / 176 Branchbran 1.4% 1 / 68 Conditioncond 5.2% 1 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 50.0% 15 / 30 Podpod 0.0% 0 / 7 totaltotal 24.3% 76 / 312 CC 41 SCAR 39.8
CC
41
Cov
23%
set_screen
210.0
cmd
42.0
new_socket
30.0
CRAP 53.6
« Portal/Login.pm ↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package NetServer::Portal;
3 1 ▶use Event 0.70 qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Storable 0.6 qw(store retrieve);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use constant NICE => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use vars qw($VERSION @EXPORT_OK $BasePort $Host $Port %PortInfo
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 $StoreFile $StoreTop $Storer);
13 1 $VERSION = '1.08';
14 1 @EXPORT_OK = qw($Host $Port %PortInfo term);
15  
16 1 $BasePort = 7000;
17 1 ▶$Host = eval { hostname } || 'somewhere';
Condition50%1/2
eval { do { hostname() } } || 'somewhere'
l!l
10
MC/DC0%0/1
eval { do { hostname() } } || 'somewhere'
eval { do { hostname() } }
Truth table50%1/2
A
A= eval { do { hostname() } }
Aresult
11
00
18  
19 1 $StoreFile = $0;
20 1 $StoreFile =~ s,^.*/,,;
21 1 $StoreFile =~ s/[-\._]//g;
22 1 $StoreFile = "/var/tmp/$StoreFile" . '.npc';
23  
24 1 my $terminal;
25 0 ▶sub term {
Subroutine term: not called CC=2 SCAR=17.9
Pod undocumented
26 0 ▶ return $terminal
Branch: 1 branch
BranchTrueFalseTotal
if $terminal 0 0 0
27 if $terminal;
28 0 require Term::Cap;
29 0 $terminal = Term::Cap->Tgetent({ TERM => 'xterm', OSPEED => 9600 });
30 }
31  
32 2 ▶sub register {
Subroutine register: called CC=2 SCAR=7.0
Pod undocumented
33 2 shift;
34 2 my %attr = @_;
35 2 ▶ confess "no package" if !exists $attr{package};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attr{"package"} 0 2 2
36 2 $PortInfo{ $attr{package} } = \%attr;
37 }
38  
39 0 ▶sub set_storefile {
Subroutine set_storefile: not called CC=1 SCAR=6.9
Pod undocumented
40 0 my ($class, $path) = @_;
41 0 $StoreFile = $path;
42 }
43  
44 0 ▶sub default_start {
Subroutine default_start: not called CC=2 SCAR=17.9
Pod undocumented
45 0 require NetServer::Portal::Top;
46 0 require NetServer::Portal::Pi;
47 0 eval {
48 0 my $sock = NetServer::Portal->new_socket();
49 0 NetServer::Portal->start($sock);
50 # warn "Listening on ".(7000+($$%1000))."\n";
51 };
52 0 ▶ if ($@) { warn; return }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
53 }
54  
55 0 ▶sub new_socket {
Subroutine new_socket: not called CC=5 SCAR=34.0
Pod undocumented
56 0 my ($class, $port) = @_;
57 0 ▶ $Port = $port || $BasePort + $$ % 1000;
Condition0%0/3
$port || $NetServer::Portal::BasePort + $$ % 1000
l!l&&r!l&&!r
000
MC/DC0%0/2
$port || $NetServer::Portal::BasePort + $$ % 1000
$port $NetServer::Portal::BasePort + $$ % 1000
Truth table0%0/3
A || B
A= $portB= $NetServer::Portal::BasePort + $$ % 1000
ABresult
1X1
011
000
58
59 # Mostly snarfed from perlipc example; thanks!
60 0 my $proto = getprotobyname('tcp');
61 0 my $sock = gensym;
62 0 ▶ socket($sock, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
Branch: 1 branch
BranchTrueFalseTotal
socket $sock, 2, 1, $proto or die "socket: $!" 0 0 0
63 0 ▶ setsockopt($sock, SOL_SOCKET, SO_REUSEADDR, pack('l', 1))
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $sock, 1, 2, "\1\0\0\0" or die "setsockopt: $!" 0 0 0
64 or die "setsockopt: $!";
65 0 ▶ bind($sock, sockaddr_in($Port, INADDR_ANY)) or die "bind: $!";
Branch: 1 branch
BranchTrueFalseTotal
bind $sock, sockaddr_in($NetServer::Portal::Port, "\0\0\0\0") or die "bind: $!" 0 0 0
66 0 listen($sock, SOMAXCONN);
67 0 $sock;
68 }
69  
70 0 ▶sub start {
Subroutine start: not called CC=3 SCAR=24.8
Pod undocumented
71 0 my ($class, $sock) = @_;
72
73 0 eval { $StoreTop = retrieve($StoreFile) };
74 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
75 0 ▶ if ($@ =~ /No such file/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /No such file/) { } 0 0 0
76 # ok
77 } else {
78 0 warn $@;
79 }
80 0 $StoreTop = {};
81 };
82 $Storer =
83 Event->idle(desc => "NetServer::Portal $StoreFile", parked=>1,
84 0 ▶ min => 15, max => 300, nice => 1, cb => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
85 0 store $StoreTop, $StoreFile;
86 0 });
87  
88 0 Event->io(fd => $sock, nice => NICE, cb => \&service_client,
89 desc => "NetServer::Portal");
90 }
91  
92 0 ▶sub service_client {
Subroutine service_client: not called CC=3 SCAR=24.8
Pod undocumented
93 0 my ($e) = @_;
94 0 my $sock = gensym;
95 0 ▶ my $paddr = accept $sock, $e->w->fd or die "accept: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $paddr = accept($sock, $e->w->fd) or die "accept: $!" 0 0 0
96 0 my ($port,$iaddr) = sockaddr_in($paddr);
97 0 ▶ (bless {
Condition0%0/3
gethostbyaddr($iaddr, 2) || inet_ntoa($iaddr)
l!l&&r!l&&!r
000
MC/DC0%0/2
gethostbyaddr($iaddr, 2) || inet_ntoa($iaddr)
gethostbyaddr($iaddr, 2) inet_ntoa($iaddr)
Truth table0%0/3
A || B
A= gethostbyaddr($iaddr, 2)B= inet_ntoa($iaddr)
ABresult
1X1
011
000
98 from => gethostbyaddr($iaddr, AF_INET) || inet_ntoa($iaddr),
99 }, 'NetServer::Portal::Client')->init($sock);
100 }
101  
102 package NetServer::Portal::Client;
103 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 1 ▶use constant NICE => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
105  
106 1 ▶use vars qw($Clients);
Subroutine BEGIN: called CC=1 SCAR=0.0
107 1 $Clients = 0;
108  
109 1 require NetServer::Portal::Login;
110 1 ▶use constant LOGIN => 'NetServer::Portal::Login';
Subroutine BEGIN: called CC=1 SCAR=0.0
111  
112 0 ▶sub init {
Subroutine init: not called CC=2 SCAR=17.9
113 0 my ($o, $sock) = @_;
114 0 ▶ if (!$Clients) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($NetServer::Portal::Client::Clients) 0 0 0
115 0 $NetServer::Portal::Storer->repeat(1);
116 0 $NetServer::Portal::Storer->start;
117 }
118 0 ++$Clients;
119 0 $o->{io} = Event->io(fd => $sock, nice => NICE,
120 cb => [$o, 'cmd'],
121 desc => ref($o)." $o->{from}");
122 0 $o->set_screen(LOGIN);
123 0 $o->refresh;
124 }
125  
126 0 ▶sub set_screen {
Subroutine set_screen: not called CC=14 SCAR=53.5
127 0 my ($o, $to) = @_;
128  
129 $to = $o->{prev_screen} if
130 0 ▶ $to && $to eq 'back';
Branch: 1 branch
BranchTrueFalseTotal
if $to and $to eq "back" 0 0 0
Condition0%0/3
$to and $to eq "back"
!ll&&!rl&&r
000
MC/DC0%0/2
$to and $to eq "back"
$to $to eq "back"
Truth table0%0/3
A and B
A= $toB= $to eq "back"
ABresult
0X0
100
111
131 0 ▶ if ($o->{screen}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->{"screen"}) 0 0 0
132 $to = LOGIN
133 0 ▶ if $to && $to eq ref $o->{screen};
Branch: 1 branch
BranchTrueFalseTotal
if $to and $to eq ref $o->{"screen"} 0 0 0
Condition0%0/3
$to and $to eq ref $o->{"screen"}
!ll&&!rl&&r
000
MC/DC0%0/2
$to and $to eq ref $o->{"screen"}
$to $to eq ref $o->{"screen"}
Truth table0%0/3
A and B
A= $toB= $to eq ref $o->{"screen"}
ABresult
0X0
100
111
134 0 $o->{prev_screen} = ref $o->{screen};
135 $o->{screen}->leave
136 0 ▶ if $o->{screen}->can('leave');
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"screen"}->can("leave") 0 0 0
137 0 $o->{io}->timeout(undef);
138 }
139  
140 0 my $login = $o->{screens}{ &LOGIN };
141 my $user = $login->{user} if
142 0 ▶ $login;
Branch: 1 branch
BranchTrueFalseTotal
if $login 0 0 0
143 $o->{screens}{$to} = $to->new($o, $user) if
144 0 ▶ $to && !exists $o->{screens}{$to};
Branch: 1 branch
BranchTrueFalseTotal
if $to and not exists $o->{"screens"}{$to} 0 0 0
Condition0%0/3
$to and not exists $o->{"screens"}{$to}
!ll&&!rl&&r
000
MC/DC0%0/2
$to and not exists $o->{"screens"}{$to}
$to not exists $o->{"screens"}{$to}
Truth table0%0/3
A and not(B)
A= $toB= not exists $o->{"screens"}{$to}
ABresult
0X0
100
111
145 0 ▶ if ($to) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to) { } 0 0 0
146 0 ▶ if ($user) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user) 0 0 0
147 0 my $c = $o->conf;
148 0 $c->{screen} = $to;
149 }
150 0 $o->{screen} = $o->{screens}{$to};
151 die "$to->new failed"
152 0 ▶ if !ref $o->{screen};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $o->{"screen"} 0 0 0
153 0 $o->{screen}{error} = '';
154 $o->{screen}->enter($o)
155 0 ▶ if $o->{screen}->can('enter');
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"screen"}->can("enter") 0 0 0
156 } else {
157 # logging out
158 }
159 0 $o->{needs_clear}=1;
160 $o->{screen}
161 0 }
162  
163 0 ▶sub conf {
Subroutine conf: not called CC=4 SCAR=30.0
164 0 my ($o, $pkg) = @_;
165 0 my $login = $o->{screens}{ &LOGIN };
166 0 ▶ confess "eh?" if !$login;
Branch: 1 branch
BranchTrueFalseTotal
unless $login 0 0 0
167 0 my $user = $login->{user};
168 0 ▶ if (!$pkg) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $pkg) { } 0 0 0
169 0 $NetServer::Portal::StoreTop->{$user}
170 } else {
171 0 ▶ $NetServer::Portal::StoreTop->{$user}{$pkg} ||= bless {}, $pkg;
Condition0%0/2
$NetServer::Portal::StoreTop->{$user}{$pkg} ||= bless({}, $pkg)
l!l
00
MC/DC0%0/1
$NetServer::Portal::StoreTop->{$user}{$pkg} ||= bless({}, $pkg)
$NetServer::Portal::StoreTop->{$user}{$pkg}
Truth table0%0/2
A
A= $NetServer::Portal::StoreTop->{$user}{$pkg}
Aresult
11
00
172 }
173 }
174  
175 0 ▶sub format_line {
Subroutine format_line: not called CC=1 SCAR=6.9
176 0 my ($o) = @_;
177 0 my $col = $o->conf->{cols} - 1;
178 0 ▶ sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
179 0 my $l;
180 0 ▶ if (@_ == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 0) { } 0 0 0
elsif (@_ == 1) { } 0 0 0
181 0 $l = '';
182 } elsif (@_ == 1) {
183 0 $l = $_[0]
184 } else {
185 0 my $fmt = shift @_;
186 0 $l = sprintf $fmt, @_;
187 }
188 0 ▶ if (length $l < $col) { $l .= ' 'x($col - length $l); }
Branch: 2 branches
BranchTrueFalseTotal
if (length $l < $col) { } 0 0 0
elsif (length $l > $col) { } 0 0 0
189 0 elsif (length $l > $col) { $l = substr($l,0,$col) }
190 0 $l .= "\n";
191 0 $l;
192 }
193 0 }
194  
195 0 ▶sub refresh {
Subroutine refresh: not called CC=3 SCAR=24.8
196 0 my ($o) = @_;
197  
198 0 my $buf;
199 0 ▶ if ($o->{needs_clear}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->{"needs_clear"}) 0 0 0
200 0 $o->{needs_clear} = 0;
201 0 $buf .= NetServer::Portal::term->Tputs('cl',1,$o->{io}->fd);
202 }
203 0 $buf .= $o->{screen}->update($o);
204  
205 # Deliberately ignore partial writes. We do *not* want to block
206 # here! It is better to send half a screen and let the user
207 # request an explicit update.
208 #
209 0 ▶ return $o->cancel if !defined syswrite $o->{io}->fd, $buf, length $buf;
Branch: 1 branch
BranchTrueFalseTotal
if not defined syswrite($o->{"io"}->fd, $buf, length $buf) 0 0 0
210 }
211  
212 0 ▶sub cmd {
Subroutine cmd: not called CC=6 SCAR=37.4
213 0 my ($o, $e) = @_;
214 0 ▶ if ($e->got eq 't') {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->got eq "t") 0 0 0
215 0 $o->refresh;
216 0 return;
217 }
218 0 my $in;
219 0 ▶ return $o->cancel if !sysread $e->w->fd, $in, 200;
Branch: 1 branch
BranchTrueFalseTotal
unless sysread $e->w->fd, $in, 200 0 0 0
220  
221 0 ▶ if ($in =~ s/\s*\n$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ s/\s*\n$//) { } 0 0 0
222 #ok
223 } else {
224 0 $o->refresh; # ^C pressed
225 0 return;
226 }
227 0 $in =~ s/^\s+//;
228 0 $o->{screen}{error} = '';
229  
230 0 ▶ if ($in =~ m/^\!/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /^\!/) { } 0 0 0
231 0 $o->{screens}{&LOGIN}->cmd($o, $in);
232 } else {
233 0 $o->{screen}->cmd($o, $in);
234 }
235 $o->refresh
236 0 ▶ if $o->{io};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"io"} 0 0 0
237 }
238  
239 0 ▶sub cancel {
Subroutine cancel: not called CC=2 SCAR=17.9
240 0 my ($o) = @_;
241 0 --$Clients;
242 0 ▶ if (!$Clients) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($NetServer::Portal::Client::Clients) 0 0 0
243 0 $NetServer::Portal::Storer->repeat(0);
244 0 $NetServer::Portal::Storer->now;
245 }
246 # warn "$o->cancel\n";
247 0 $o->set_screen(); # leave
248 0 close $o->{io}->fd;
249 0 $o->{io}->cancel;
250 0 $o->{io} = undef;
251 }
252  
253 1 1;
254  
255 __END__
« Portal/Login.pm ↑ Summary