Gnats/Session.pm

filter: Statementstmt 89.5% 137 / 153 Branchbran 74.3% 58 / 78 Conditioncond 44.4% 4 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 90.9% 30 / 33 Podpod 73.6% 14 / 19 totaltotal 82.1% 244 / 297 CC 43 SCAR 16.2
CC
43
Cov
83%
gconnect
14.1
database
8.8
issue
6.0
CRAP 5.0
« Gnats/Response.pm ↑ Summary Gnats/Command/FTYP.pm »
1 package Net::Gnats::Session;
2 40 ▶use v5.10.00;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 40 ▶use strictures;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 $Net::Gnats::Session::VERSION = '0.22';
6 }
7 40 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 40 ▶use Net::Gnats qw(verbose_level);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use Net::Gnats::Command qw(user quit);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 40 ▶use Net::Gnats::Constants qw(LF CODE_GREETING CODE_PR_READY CODE_SEND_PR CODE_SEND_TEXT CODE_SEND_CHANGE_REASON CODE_INFORMATION);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 40 ▶use Net::Gnats::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 40 $| = 1;
16  
17 =head1 NAME
18  
19 Net::Gnats::Session
20  
21 =head1 DESCRIPTION
22  
23 Represents a specific connection to Gnats.
24  
25 When constructing a new session, it resets $Net::Gnats::current_session.
26  
27 =cut
28  
29 44 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
30 44 my ($class, %o ) = @_;
31 44 my ($self);
32 44 ▶ $self = bless {}, $class if not %o;
Branch: 1 branch
BranchTrueFalseTotal
unless %o 2 42 44
33 44 $self = bless \%o, $class;
34  
35 #set the current session to Net::Gnats so we can ref it throughout
36 44 Net::Gnats->current_session($self);
37  
38 44 return $self;
39 }
40  
41 =head1 ACCESSORS
42  
43 =head2 name
44  
45 The name is a combination of database and username, a friendly handle.
46  
47 It does not mean anything to GNATS.
48  
49 =cut
50  
51 0 ▶sub name {
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
52 0 my $self = shift;
53 0 return $self->hostname . '-' . $self->username;
54 }
55  
56  
57 =head2 access
58  
59 Retrieves the access for the current database.
60  
61 =cut
62  
63 82 ▶sub access { shift->{access}; }
Subroutine access: called CC=1 SCAR=0.0
Pod documented
64  
65 =head2 database
66  
67 Sets and retrieves the current database. If a value is given then
68 a change to the given database is made.
69  
70 =cut
71  
72 23 ▶sub database {
Subroutine database: called CC=5 SCAR=21.7
Pod documented
73 23 my ($self, $value) = @_;
74 23 ▶ $self->{database} = 'default' if not defined $self->{database};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"database"} 12 11 23
75 23 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 23 23
76 0 ▶ return $self->{database} if $self->{database} eq $value;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"database"} eq $value 0 0 0
77 0 ▶ $self->{database} = $value if
Branch: 1 branch
BranchTrueFalseTotal
if $self->issue("Net::Gnats::Command"->chdb("database", $value))->is_ok 0 0 0
78 $self->issue(Net::Gnats::Command->chdb( database => $value))
79 ->is_ok;
80  
81 # initialize schema for changed database
82 0 $self->{schema} = Net::Gnats::Schema->new($self);
83 }
84 23 return $self->{database};
85 }
86  
87 =head2 hostname
88  
89 The hostname of the Gnats daemon process.
90  
91 Default: localhost
92  
93 =cut
94  
95 90 ▶sub hostname {
Subroutine hostname: called CC=3 SCAR=11.0
Pod documented
96 90 my ( $self, $value ) = @_;
97 90 ▶ $self->{hostname} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 1 89 90
98 90 ▶ $self->{hostname} = 'localhost' if not defined $self->{hostname};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"hostname"} 28 62 90
99 90 $self->{hostname};
100 }
101  
102 45 ▶sub is_authenticated {
Subroutine is_authenticated: called CC=2 SCAR=6.9
Pod undocumented
103 45 my ( $self ) = @_;
104 45 ▶ $self->{authenticated} = 0 if not defined $self->{authenticated};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"authenticated"} 16 29 45
105 45 $self->{authenticated};
106 }
107  
108 16 ▶sub is_connected {
Subroutine is_connected: called CC=2 SCAR=7.1
Pod undocumented
109 16 my ( $self ) = @_;
110 16 ▶ $self->{connected} = 0 if not defined $self->{connected};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"connected"} 0 16 16
111 16 $self->{connected};
112 }
113  
114  
115  
116 =head2 password
117  
118 The password for the user connecting to the Gnats daemon process.
119  
120 Most commands require authentication.
121  
122 =cut
123  
124 51 ▶sub password {
Subroutine password: called CC=2 SCAR=6.9
Pod documented
125 51 my ( $self, $value ) = @_;
126 51 ▶ $self->{password} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 12 39 51
127 51 $self->{password};
128 }
129  
130 =head2 port
131  
132 The port of the Gnats daemon process.
133  
134 Default: 1529
135  
136 =cut
137  
138 90 ▶sub port {
Subroutine port: called CC=3 SCAR=11.0
Pod documented
139 90 my ( $self, $value ) = @_;
140 90 ▶ $self->{port} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 90 90
141 90 ▶ $self->{port} = 1529 if not defined $self->{port};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"port"} 29 61 90
142 90 $self->{port};
143 }
144  
145 =head2 schema
146  
147 Get the schema for this session. Readonly.
148  
149 =cut
150  
151 184 ▶sub schema { shift->{schema} }
Subroutine schema: called CC=1 SCAR=0.0
Pod documented
152  
153 =head2 skip_version
154  
155 Set skip_version to override Gnats version checking. By default,
156 Net::Gnats supports v4 only.
157  
158 You use this at your own risk.
159  
160 =cut
161  
162 89 ▶sub skip_version {
Subroutine skip_version: called CC=3 SCAR=11.0
Pod documented
163 89 my ($self, $value) = @_;
164 89 ▶ $self->{skip_version} = 0 if not defined $self->{skip_version};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"skip_version"} 44 45 89
165 89 ▶ $self->{skip_version} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 1 88 89
166 89 $self->{skip_version};
167 }
168  
169 =head2 username
170  
171 The user connecting to the Gnats daemon process.
172  
173 Most commands require authentication.
174  
175 =cut
176  
177 62 ▶sub username {
Subroutine username: called CC=2 SCAR=6.9
Pod documented
178 62 my ( $self, $value ) = @_;
179 62 ▶ $self->{username} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 12 50 62
180 62 $self->{username};
181 }
182  
183 =head2 version
184  
185 The Gnats daemon process version. The version will only be set after connecting.
186  
187 =cut
188  
189 86 ▶sub version { return shift->{version} }
Subroutine version: called CC=1 SCAR=0.0
Pod documented
190  
191 =head1 METHODS
192  
193  
194 =head2 authenticate
195  
196 Return:
197  
198 0 if failue
199 1 if success
200  
201 =cut
202  
203 38 ▶sub authenticate {
Subroutine authenticate: called CC=3 SCAR=11.1
Pod documented
204 38 my ( $self ) = @_;
205 38 my ($c);
206  
207 38 $c = $self->issue(Net::Gnats::Command->user( username => $self->username,
208 password => $self->password ));
209 38 $self->{authenticated} = $c->is_ok;
210 38 ▶ return $self if not $c->is_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->is_ok 0 38 38
211  
212 38 ▶ $self->{schema} = Net::Gnats::Schema->new($self) if not defined $self->schema;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->schema 38 0 38
213  
214 38 _trace('AUTH: ' . $c->is_ok);
215  
216 38 $c->is_ok;
217 }
218  
219 =head2 gconnect
220  
221 Connects to Gnats. If the username and password is set, it will
222 attempt authentication.
223  
224 Connecting an already connected session infers reconnect.
225  
226 =cut
227  
228 44 ▶sub gconnect {
Subroutine gconnect: called CC=13 SCAR=26.5
Pod documented
229 44 my ( $self ) = @_;
230 44 my ( $sock, $iaddr, $paddr, $proto );
231  
232 44 _trace ('disconnecting sock if it exists');
233 44 ▶ $self->disconnect if defined $self->{gsock};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"gsock"} 0 44 44
234  
235 44 _trace ('constructing socket');
236 44 _trace ('host: ' . $self->hostname);
237 44 _trace ('port: ' . $self->port);
238  
239 44 $self->{gsock} = IO::Socket::INET->new( PeerAddr => $self->hostname,
240 PeerPort => $self->port,
241 Proto => 'tcp');
242  
243 44 ▶ return $self if not defined $self->{gsock};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"gsock"} 0 44 44
244  
245 44 my $response = $self->_read;
246  
247 44 _trace('Connection response: ' . $response->as_string);
248  
249 44 ▶ return undef if not defined $response->code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $response->code 0 44 44
250 44 ▶ return undef if $response->code != CODE_GREETING;
Branch: 1 branch
BranchTrueFalseTotal
if $response->code != 200 0 44 44
251  
252 44 _trace('Is Connected.');
253 44 $self->{connected} = 1;
254  
255 # Grab the gnatsd version
256 44 $self->gnatsd_version( $response->as_string );
257  
258 44 ▶ print "? Error: GNATS Daemon version $self->{version} at $self->{hostname} $self->{port} is not supported by Net::Gnats\n" if not $self->check_gnatsd_version;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_gnatsd_version 1 43 44
259 44 ▶ if ( not $self->check_gnatsd_version ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->check_gnatsd_version) 1 43 44
260 1 $self->issue(Net::Gnats::Command->quit);
261 1 $self->{connected} = 0;
262 1 return undef;
263 }
264  
265 # issue USER to get current access level
266 43 $self->{access} = $self->issue(Net::Gnats::Command->user)->level;
267  
268 43 ▶ $self->authenticate if defined $self->{username} and defined $self->{password};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"username"} and defined $self->{"password"} 27 16 43
Condition66%2/3
defined $self->{"username"} and defined $self->{"password"}
!ll&&!rl&&r
16027
MC/DC50%1/2
defined $self->{"username"} and defined $self->{"password"}
defined $self->{"username"} defined $self->{"password"}
Truth table66%2/3
A and B
A= defined $self->{"username"}B= defined $self->{"password"}
ABresult
0X0
100
111
269  
270 43 ▶ return $self if not $self->is_authenticated;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_authenticated 16 27 43
271  
272 27 ▶ return $self if $self->access eq 'none' or $self->access eq 'deny' or $self->access eq 'listdb';
Branch: 1 branch
BranchTrueFalseTotal
if $self->access eq "none" or $self->access eq "deny" or $self->access eq "listdb" 0 27 27
Condition33%2/6
$self->access eq "none" or $self->access eq "deny" or $self->access eq "listdb"
l!l&&r!l&&!r
0027
$self->access eq "none" or $self->access eq "deny"
l!l&&r!l&&!r
0027
MC/DC0%0/3
$self->access eq "none" or $self->access eq "deny" or $self->access eq "listdb"
$self->access eq "none" $self->access eq "deny" $self->access eq "listdb"
Truth table25%1/4
A or B or C
A= $self->access eq "none"B= $self->access eq "deny"C= $self->access eq "listdb"
ABCresult
1XX1
01X1
0011
0000
273  
274 27 return $self;
275 }
276  
277 =head2 disconnect
278  
279 Disconnects from the current session, either authenticated or not.
280  
281 =cut
282  
283 0 ▶sub disconnect {
Subroutine disconnect: not called CC=1 SCAR=6.9
Pod documented
284 0 my ( $self ) = @_;
285 0 $self->issue( Net::Gnats::Command->quit );
286 0 $self->{connected} = 0;
287 0 $self->{authenticated} = 0;
288 0 $self->{schema} = undef;
289 }
290  
291 =head2 issue
292  
293 Issues a command using a Command object. The Command object is
294 returned to the caller.
295  
296 The Command object composes a Response, whose value(s) carry error
297 codes and the literal values retrived from Gnats.
298  
299 =cut
300  
301 535 ▶sub issue {
Subroutine issue: called CC=6 SCAR=17.9
Pod documented
302 535 my ( $self, $command ) = @_;
303  
304 # if the command cannot be formed, the as_string method will return
305 # undef.
306 535 ▶ return $command if not defined $command->as_string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $command->as_string 27 508 535
307  
308 508 $command->response( $self->_run( $command->as_string ) );
309  
310 # In case we received the an undefined response code, return here.
311 # This could happen when the network response gets broken.
312 508 ▶ return $command if not defined $command->response->code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $command->response->code 0 508 508
313  
314 # Check CODE_SEND_TEXT or CODE_SEND_PR
315 # This will be a field object value.
316 508 ▶ if ($command->response->code == CODE_SEND_TEXT) {
Branch: 2 branches
BranchTrueFalseTotal
if ($command->response->code == 212) { } 8 500 508
elsif ($command->response->code == 211) { } 5 495 500
317 8 $command->response( $self->_run( $command->field->value . "\n." ) );
318 8 ▶ $command->response( $self->_run( $command->field_change_reason->value . "\n." ))
Branch: 1 branch
BranchTrueFalseTotal
if $command->response->code == 213 3 5 8
319 if $command->response->code == CODE_SEND_CHANGE_REASON;
320 }
321 # This will be a whole serialized PR.
322 elsif ($command->response->code == CODE_SEND_PR) {
323 5 $command->response( $self->_run( $command->pr->asString . "\n." ) );
324 }
325 508 return $command;
326 }
327  
328 =head2 run
329  
330 Runs a RAW command using this session. Returns RAW output.
331  
332 =cut
333  
334  
335 # PRIVATE METHODS HERE - DO NOT EXPORT
336  
337 44 ▶sub gnatsd_version {
Subroutine gnatsd_version: called CC=2 SCAR=7.0
Pod undocumented
338 44 my ($self, $value) = @_;
339 44 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 44 0 44
340 44 $value =~ s/.*(\d+.\d+.\d+).*/$1/;
341 44 $self->{version} = $1;
342 }
343 44 return $self->{version};
344 }
345  
346 # "legally" use v4 daemon only
347 88 ▶sub check_gnatsd_version {
Subroutine check_gnatsd_version: called CC=4 SCAR=13.9
Pod undocumented
348 88 my ($self) = @_;
349 88 my $rmajor = 4;
350 88 my $min_minor = 1;
351 88 ▶ return 1 if $self->skip_version;
Branch: 1 branch
BranchTrueFalseTotal
if $self->skip_version 2 86 88
352  
353 86 my ($majorv, $minorv, $patchv) = split /\./, $self->version;
354  
355 86 ▶ return 0 if $majorv != $rmajor;
Branch: 1 branch
BranchTrueFalseTotal
if $majorv != $rmajor 2 84 86
356 84 ▶ return 0 if $minorv < $min_minor;
Branch: 1 branch
BranchTrueFalseTotal
if $minorv < $min_minor 0 84 84
357 84 return 1;
358 }
359  
360  
361 524 ▶sub _run {
Subroutine _run: called CC=1 SCAR=0.0
362 524 my ( $self, $cmd ) = @_;
363  
364 #$self->_clear_error();
365  
366 524 _trace('SENDING: [' . $cmd . ']');
367  
368 524 $self->{gsock}->print( $cmd . LF );
369  
370 524 return $self->_read;
371 }
372  
373 568 ▶sub _read {
Subroutine _read: called CC=3 SCAR=11.0
374 568 my ( $self ) = @_;
375 568 my $response = Net::Gnats::Response->new(type => 0);
376  
377 568 ▶ until ( $response->is_finished == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_finished == 1) 6015 568 6583
378 6015 my $line = $self->_read_clean($self->{gsock}->getline);
379  
380 # We didn't get anyting from the socket, it could mean a broken
381 # connection or malformed response.
382 6015 ▶ last if not defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 6015 6015
383  
384 # Process the line normally.
385 6015 $response->raw( $line );
386 6015 _trace('RECV: [' . $line . ']');
387 }
388 568 return $response;
389 }
390  
391 6015 ▶sub _read_clean {
Subroutine _read_clean: called CC=2 SCAR=7.4
392 6015 my ( $self, $line ) = @_;
393 0 ▶ if ( not defined $line ) { return; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 0 6015 6015
394  
395 6015 $line =~ s/\r|\n//gsm;
396 # $line =~ s/^[.][.]/./gsm;
397 6015 return $line;
398 }
399  
400 0 ▶sub _read_decompose {
Subroutine _read_decompose: not called CC=1 SCAR=6.9
401 0 my ( $self, $raw ) = @_;
402 0 my @result = $raw =~ /^(\d\d\d)([- ]?)(.*$)/sxm;
403 0 return \@result;
404 }
405  
406 6841 ▶sub _trace {
Subroutine _trace: called CC=2 SCAR=9.2
407 6841 my ( $message ) = @_;
408 6841 ▶ return if Net::Gnats->verbose_level() != 3;
Branch: 1 branch
BranchTrueFalseTotal
if "Net::Gnats"->verbose_level != 3 6841 0 6841
409 0 print 'TRACE: [' . $message . ']' . LF;
410 0 return;
411 }
412  
413 40 1;
« Gnats/Response.pm ↑ Summary Gnats/Command/FTYP.pm »