DaemonDecl.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 155 CC 6 SCAR 23.6
CC
6
Cov
0%
import
42.0
ext_worker
2.0
ext_wrapper
2.0
CRAP 10.6
1 package Salvation::DaemonDecl;
2  
3 =head1 NAME
4  
5 Salvation::DaemonDecl - Сахар для создания и разработки демонов
6  
7 =head1 DESCRIPTION
8  
9 Система призвана сделать разработку демонов более простой, скрывая от
10 разработчика необходимые действия для порождения воркеров, коммуникации
11 между воркерами, обработки событий завершения воркеров и иной рутины.
12  
13 =head1 SYNOPSIS
14  
15 # TODO
16  
17 =cut
18  
19 use strict;
20 use warnings;
21  
22 use Sub::Prototype 'set_prototype';
23 use B::Hooks::EndOfScope 'on_scope_end';
24 use Salvation::DaemonDecl::Backend ();
25  
26 our $VERSION = 0.03;
27  
28 =head1 METHODS
29  
30 =cut
31  
32 my @backend_methods = (
33 'can_spawn_worker',
34 'spawn_worker',
35 'daemon_main',
36 'write_to',
37 'read_from',
38 'has_alive_workers',
39 'count_alive_workers',
40 'wait_all_workers',
41 'wait_worker_slot',
42 );
43  
44 my @sugar_methods = (
45 'worker',
46 'wrapper',
47 'name',
48 'main',
49 'max_instances',
50 'log',
51 'reap',
52 'rw',
53 'ro',
54 'wo',
55 'wait_cond',
56 );
57  
58 =head2 import
59  
60 Экспортирует функции сахара.
61  
62 =cut
63  
64 sub import {
65  
66 my ( $self, @required_methods ) = @_;
67 my $caller = caller();
68  
69 my @tmp = ();
70  
71 while( defined( my $node = shift( @required_methods ) ) ) {
72  
73 if( $node eq '-to' ) {
74  
75 $caller = shift( @required_methods );
76  
77 } else {
78  
79 push( @tmp, $node );
80 }
81 }
82  
83 @required_methods = @tmp;
84 undef @tmp;
85  
86 my %required_methods = map( { $_ => 1 } @required_methods );
87 my $import_all = ( scalar( @required_methods ) == 0 );
88  
89 foreach my $name ( @backend_methods ) {
90  
91 next unless( $import_all || exists $required_methods{ $name } );
92  
93 my $method = "${caller}::${name}";
94  
95 no strict 'refs';
96 no warnings 'once';
97  
98 *$method = sub {
99  
100 local *__ANON__ = $name;
101  
102 return $self -> backend_class() -> $name(
103 $self -> backend_class() -> get_meta( $caller ), @_
104 );
105 };
106 }
107  
108 my %prototypes = (
109 worker => '$',
110 wrapper => '&',
111 name => '$',
112 main => '&',
113 max_instances => '$',
114 log => '&',
115 reap => '&',
116 rw => '',
117 ro => '',
118 wo => '',
119 );
120  
121 foreach my $name ( @sugar_methods ) {
122  
123 next unless( $import_all || exists $required_methods{ $name } );
124  
125 my $method = "${caller}::${name}";
126 my $target = "ext_${name}";
127  
128 no strict 'refs';
129 no warnings 'once';
130  
131 my $code = sub {
132  
133 local *__ANON__ = $name;
134  
135 $self -> $target( $caller, @_ );
136 };
137  
138 set_prototype( $code, $prototypes{ $name } ) if( exists $prototypes{ $name } );
139  
140 *$method = $code;
141 }
142  
143 on_scope_end {
144  
145 my $ns = "${caller}::";
146  
147 foreach my $name ( @backend_methods, @sugar_methods ) {
148  
149 next unless( $import_all || exists $required_methods{ $name } );
150  
151 no strict 'refs';
152  
153 delete $$ns{ $name };
154 }
155 };
156  
157 return;
158 }
159  
160 =head2 worker
161  
162 Алиас для C<Salvation::DaemonDecl::Backend::add_worker>.
163  
164 =cut
165  
166 sub ext_worker {
167  
168 my ( $self, $class, $descr ) = @_;
169  
170 $self -> backend_class() -> add_worker(
171 $self -> backend_class() -> get_meta( $class ) => $descr
172 );
173  
174 return;
175 }
176  
177 =head2 wrapper
178  
179 Сахар для указания параметра C<wrapper> воркера.
180  
181 =cut
182  
183 sub ext_wrapper {
184  
185 my ( $self, $class, $code ) = @_;
186  
187 return ( wrapper => $code );
188 }
189  
190 =head2 name
191  
192 Сахар для указания параметра C<name> воркера.
193  
194 =cut
195  
196 sub ext_name {
197  
198 my ( $self, $class, $name ) = @_;
199  
200 return ( name => $name );
201 }
202  
203 =head2 main
204  
205 Сахар для указания параметра C<main> воркера.
206  
207 =cut
208  
209 sub ext_main {
210  
211 my ( $self, $class, $code ) = @_;
212  
213 return ( main => $code );
214 }
215  
216 =head2 max_instances
217  
218 Сахар для указания параметра C<max_instances> воркера.
219  
220 =cut
221  
222 sub ext_max_instances {
223  
224 my ( $self, $class, $num ) = @_;
225  
226 return ( max_instances => $num );
227 }
228  
229 =head2 log
230  
231 Сахар для указания параметра C<log> воркера.
232  
233 =cut
234  
235 sub ext_log {
236  
237 my ( $self, $class, $code ) = @_;
238  
239 return ( log => $code );
240 }
241  
242 =head2 reap
243  
244 Сахар для указания параметра C<reap> воркера.
245  
246 =cut
247  
248 sub ext_reap {
249  
250 my ( $self, $class, $code ) = @_;
251  
252 return ( reap => $code );
253 }
254  
255 =head2 rw
256  
257 Сахар для установки параметра C<transport> воркера в значение C<TRANSPORT_R | TRANSPORT_W>.
258  
259 =cut
260  
261 sub ext_rw {
262  
263 my ( $self, $class ) = @_;
264  
265 return ( transport => (
266 Salvation::DaemonDecl::Backend::TRANSPORT_R
267 | Salvation::DaemonDecl::Backend::TRANSPORT_W
268 ) );
269 }
270  
271 =head2 ro
272  
273 Сахар для установки параметра C<transport> воркера в значение C<TRANSPORT_R>.
274  
275 =cut
276  
277 sub ext_ro {
278  
279 my ( $self, $class ) = @_;
280  
281 return ( transport => Salvation::DaemonDecl::Backend::TRANSPORT_R );
282 }
283  
284 =head2 wo
285  
286 Сахар для установки параметра C<transport> воркера в значение C<TRANSPORT_W>.
287  
288 =cut
289  
290 sub ext_wo {
291  
292 my ( $self, $class ) = @_;
293  
294 return ( transport => Salvation::DaemonDecl::Backend::TRANSPORT_W );
295 }
296  
297 =head2 wait_cond( AnyEvent::CondVar cv )
298  
299 Выполняет прерываемый C<$cv -> recv()>.
300  
301 =cut
302  
303 sub ext_wait_cond {
304  
305 my ( $self, $class, $cv ) = @_;
306  
307 return $self -> backend_class() -> wait_cond( $cv );
308 }
309  
310 =head2 backend_class()
311  
312 =cut
313  
314 sub backend_class { 'Salvation::DaemonDecl::Backend' }
315  
316 1;
317  
318 __END__