Session/CmdQ.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« SRSRequest.pm ↑ Summary Command.pm »
1  
2 package SRS::EPP::Session::CmdQ;
3  
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use MooseX::Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use SRS::EPP::Command;
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use SRS::EPP::Response;
8  
9 has 'queue' =>
10 is => "ro",
11 isa => "ArrayRef[SRS::EPP::Command]",
12 default => sub { [] },
13 ;
14  
15 has 'responses' =>
16 is => "ro",
17 isa => "ArrayRef[Maybe[SRS::EPP::Response]]",
18 default => sub { [] },
19 ;
20  
21 has 'next' =>
22 is => "rw",
23 isa => "Num",
24 default => 0,
25 traits => ['Number'],
26 handles => {
27 add_next => 'add',
28 },
29 ;
30  
31 method next_command() {
32 my $q = $self->queue;
33 my $next = $self->next;
34 if ( my $item = $q->[$next] ) {
35 $self->add_next(1);
36 return $item;
37 }
38 else {
39 ();
40 }
41 }
42  
43 method commands_queued() {
44 my $q = $self->queue;
45 return scalar(@$q);
46 }
47  
48 method queue_command( SRS::EPP::Command $cmd ) {
49 push @{ $self->queue }, $cmd;
50 push @{ $self->responses }, undef;
51 }
52  
53 # with a command object, place a response at the same place in the queue
54 method add_command_response( SRS::EPP::Response $response, SRS::EPP::Command $cmd? )
55 {
56 my $q = $self->queue;
57 my $rs = $self->responses;
58 my $ok;
59 for ( my $i = 0; $i <= $#$q; $i++ ) {
60 if ( ($cmd and $q->[$i] == $cmd) or
61 !defined $rs->[$i] ) {
62 $rs->[$i] = $response;
63 $ok = 1;
64 last;
65 }
66 }
67 confess "Could not queue response, not found" if !$ok;
68 }
69  
70 method response_ready() {
71 defined($self->responses->[0]);
72 }
73  
74 method dequeue_response() {
75 if ( $self->response_ready ) {
76 my $cmd = shift @{ $self->queue };
77 my $response = shift @{ $self->responses };
78 $self->add_next(-1);
79 if ( wantarray ) {
80 ($response, $cmd);
81 }
82 else {
83 $response;
84 }
85 }
86 else {
87 ();
88 }
89 }
90  
91 1;
92  
93 __END__
94  
95 =head1 NAME
96  
97 SRS::EPP::Session::CmdQ - manage epp command/response queue
98  
99 =head1 SYNOPSIS
100  
101 my $q = SRS::EPP::Session::CmdQ->new( );
102  
103 # put requests on queue
104 $q->queue_command( $epp_command );
105  
106 # pull a command off the queue; mark it in progress
107 my @rq = $q->next_command;
108  
109 # put a response in
110 $q->add_command_response( $epp_response, $epp_command? );
111  
112 # if a message has had all its requests answered, it can be dequeued
113 ($epp_response, $epp_command) = $q->dequeue_response();
114  
115 # also available in scalar context
116 $epp_response = $q->dequeue_response();
117  
118 =head1 DESCRIPTION
119  
120 This class implements a simple FIFO queue, but with small
121 customizations to operation to suit the use case of the SRS EPP
122 Proxy's queue of EPP commands and responses.
123  
124 =head1 SEE ALSO
125  
126 L<SRS::EPP::Session>
127  
128 =head1 AUTHOR AND LICENCE
129  
130 Development commissioned by NZ Registry Services, and carried out by
131 Catalyst IT - L<http://www.catalyst.net.nz/>
132  
133 Copyright 2009, 2010, NZ Registry Services. This module is licensed
134 under the Artistic License v2.0, which permits relicensing under other
135 Free Software licenses.
136  
137 =cut
138  
139  
140 # Local Variables:
141 # mode:cperl
142 # indent-tabs-mode: t
143 # cperl-continued-statement-offset: 8
144 # cperl-brace-offset: 0
145 # cperl-close-paren-offset: 0
146 # cperl-continued-brace-offset: 0
147 # cperl-continued-statement-offset: 8
148 # cperl-extra-newline-before-brace: nil
149 # cperl-indent-level: 8
150 # cperl-indent-parens-as-block: t
151 # cperl-indent-wrt-brace: nil
152 # cperl-label-offset: -8
153 # cperl-merge-trailing-else: t
154 # End:
« SRSRequest.pm ↑ Summary Command.pm »