RRP/Server.pm

filter: Statementstmt 91.1% 31 / 34 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 91.6% 11 / 12 Podpod n/a totaltotal 91.3% 42 / 46 CC 1 SCAR 1.0
CC
1
Cov
91%
getName
2.0
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« RRP/Response/n420.pm ↑ Summary RRP.pm »
1 package Net::RRP::Request::Timeout;
2  
3 0 ▶sub getName { 'Timeout' };
Subroutine getName: not called CC=1 SCAR=6.9
4  
5 1;
6  
7 package Net::RRP::Server;
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Net::RRP::Codec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Net::RRP::Protocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Net::RRP::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Net::RRP::Exception::ServerError;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Net::RRP::Exception::InvalidCommandSequence;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Net::RRP::Response::n420;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Net::RRP::Response::n200;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 0 ▶use Proc::Application::Daemon;
Subroutine BEGIN: called CC=1 SCAR=2.6
22 use base qw(Proc::Application::Daemon);
23  
24 $Net::RRP::Server::VERSION = '0.1';
25  
26 # |
27 # |
28 # v
29 # +-----------------+ Timeout
30 # | Waiting for |-------------------+
31 # Authentication Succeeded | Client | |
32 # +---------| Authentication | Authentication |
33 # | | (PRE) |-----+ Failed |
34 # | +-----------------+ | |
35 # | | |
36 # V V |
37 # +-----------+ Succeeded +--------------------+ |
38 # |Waiting for|<-----------------| Waiting for | |
39 # | Command |----------+ |Authentication Retry| |
40 # | (WFC) | Timeout | | (WFR) | |
41 # +-----------+ | +--------------------+ |
42 # | ^ | | | |
43 # | | | Timeout | | Failed |
44 # Request V |Response | | | |
45 # +-----------+ | V V V
46 # | Executing | | +--------------------+
47 # | Command | +--------->| Disconnected |
48 # | (EXE) |-------------------->| (DIS) |
49 # +-----------+ QUIT +--------------------+
50 # PRE Waiting for client connection and authentication
51 # WFR Waiting for authentication retry
52 # WFC Waiting for a command from an authenticated client
53 # EXE Executing a command
54 # DIS Disconnected
55  
56  
57 use constant INIT_STATE => 'PRE';
58 use constant DONE_STATE => 'DIS';
59 use constant STATES => { PRE => { Session => { 1 => 'WFC', 0 => 'WFR' },
60 Quit => { 1 => 'DIS', 0 => 'DIS' }, },
61 WFR => { Session => { 1 => 'WFC', 0 => 'DIS' },
62 Quit => { 1 => 'DIS', 0 => 'DIS' }, },
63 WFC => { Quit => { 1 => 'DIS', 0 => 'DIS' },
64 Add => { 1 => 'WFC', 0 => 'WFC' },
65 Check => { 1 => 'WFC', 0 => 'WFC' },
66 Del => { 1 => 'WFC', 0 => 'WFC' },
67 Describe => { 1 => 'WFC', 0 => 'WFC' },
68 Mod => { 1 => 'WFC', 0 => 'WFC' },
69 Quit => { 1 => 'DIS', 0 => 'DIS' },
70 Renew => { 1 => 'WFC', 0 => 'WFC' },
71 Session => { 1 => 'WFC', 0 => 'WFR' },
72 Status => { 1 => 'WFC', 0 => 'WFC' },
73 Transfer => { 1 => 'WFC', 0 => 'WFC' } },
74 DIS => {}, # there are virtual states
75 EXE => {} };
76  
77 use constant EXCEPTION_STATES => { 'PRE' => 'WFR',
78 'WFR' => 'DIS' };
79 sub setStates
80 {
81 my $this = shift;
82 $this->{states} = STATES;
83 }
84  
85 sub getStates
86 {
87 my $this = shift;
88 return $this->{states};
89 }
90  
91 sub execute
92 {
93 warn "Net::RRP::Server::execute() must be overwriten at child class, return defaults n200 response";
94 my $response = new Net::RRP::Response::n200 ();
95 $response->setDescription ( "ok" );
96 $response;
97 }
98  
99 sub getHelloInfo
100 {
101 warn "Net::RRP::Server::getHelloInfo() must be overwriten at child class, return defaults";
102 ( registryName => "RU",
103 version => '1.1.0',
104 buildDate => 'Mon Jun 19 14:04:00 MSK 2000' );
105 }
106  
107 sub _logException
108 {
109 my ( $this, $exception ) = @_;
110 $this->Log ( 'debug', "$$: file " . $exception->file . " line " . $exception->line );
111 $this->Log ( 'debug', "$$: trace " . $exception->stacktrace );
112 $this->Log ( 'debug', "$$: catch exception $exception with code " . $exception->value() ) if $exception->value();
113 $this->Log ( 'debug', "$$: exception object " . Data::Dumper->Dump ( [ $exception->object ], [ 'exception information' ] ) ) if $exception->object;
114 }
115  
116 sub getProtocol
117 {
118 my $this = shift;
119 $this->{protocol};
120 }
121  
122 sub handler
123 {
124 my $this = shift;
125  
126 my $socket = $this->socket();
127 $this->Log ( 'notice', "$$: connecttion from: %s", $socket->peerhost );
128  
129 $this->setStates ();
130  
131 my $protocol = new Net::RRP::Protocol ( socket => $socket );
132 $protocol->sendHello ( $this->getHelloInfo() );
133
134 my $state = INIT_STATE;
135 my $states = $this->getStates ();
136  
137 $this->{protocol} = $protocol;
138  
139 while ( 1 )
140 {
141 my $response;
142  
143 $this->Log ( 'debug', "$$: rrp state at start: $state" );
144  
145 try
146 {
147 #local $Error::Debug = 1 if $this->{options}->{debug};
148 my $subState = undef;
149 my $request = undef;
150 my $prevState;
151  
152 try
153 {
154 $prevState = $state;
155  
156 my $requestBuffer = $protocol->_getLinesFromSocket();
157 $request = $protocol->{codec}->decodeRequest ( $requestBuffer );
158 #$request = $protocol->getRequest ();
159 $this->Debug ( "rrp request buffer is\n$requestBuffer" );
160  
161 my $requestName = $request->getName();
162 $this->Log ( 'notice', "$$: get %s request", $requestName );
163 $subState = $states->{ $state }->{ $requestName };
164 throw Net::RRP::Exception::InvalidCommandSequence () unless $subState;
165 $state = 'EXE';
166 $response = $this->execute ( $request );
167 $state = $subState->{ $request->isSuccessResponse ( $response ) || 0 };
168 }
169 catch Net::RRP::Exception with
170 {
171 my $exception = shift;
172 $this->_logException ( $exception );
173 last if ( $exception->isa ( 'Net::RRP::Exception::IOError') );
174 $response = Net::RRP::Response->newFromException ( $exception );
175 $this->Log ( 'debug', "$$: rrp state before exception: $state" );
176 if ( $subState )
177 {
178 $state = $subState->{ $request->isSuccessResponse ( $response ) || 0 };
179 }
180 else
181 {
182 my $checkState = EXCEPTION_STATES->{ $prevState };
183 $state = $checkState ? $checkState : $prevState;
184 }
185 $this->Log ( 'debug', "$$: rrp state after exception: $state" );
186 };
187  
188 }
189 otherwise
190 {
191 my $exception = shift;
192 $this->_logException ( $exception );
193 $response = new Net::RRP::Response::n420;
194 $response->setDescription ( 'internal server error' );
195 $state = DONE_STATE;
196 };
197  
198 $this->Log ( 'notice', "$$: send response: %s", $response->getCode );
199 $this->Log ( 'debug', "$$: rrp state at end: $state" );
200  
201 $protocol->sendResponse ( $response );
202  
203 last if $state eq DONE_STATE;
204 }
205  
206 $this->Log ( 'notice', "$$: done connection" );
207 }
208  
209 __END__
« RRP/Response/n420.pm ↑ Summary RRP.pm »