Sprocket/Spread.pm untested

filter: Statementstmt 0.0% 0 / 219 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 75 MC/DCmcdc 0.0% 0 / 44 Subroutinesub 0.0% 0 / 34 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 473 CC 42 SCAR 37.6
CC
42
Cov
0%
input
156.0
publish
56.0
connect
42.0
CRAP 43.1
1 package Sprocket::Spread;
2  
3 use strict;
4 use warnings;
5  
6 use Sprocket qw( ChannelManager );
7 use POE qw( Driver::SysRW Wheel::ReadWrite );
8 use Spread;
9 use Carp qw( croak );
10 use Symbol qw( gensym );
11  
12 # XXX
13 use Data::Dumper;
14  
15 our $sprocket_spread;
16  
17  
18 sub import {
19 my ( $class, $args ) = @_;
20 my $package = caller();
21  
22 croak "Sprocket::Spread expects its arguments in a hash ref"
23 if ( $args && ref( $args ) ne 'HASH' );
24  
25 unless ( delete $args->{no_auto_export} ) {
26 {
27 no strict 'refs';
28 *{ $package . '::sprocket_spread' } = \$sprocket_spread;
29 # XXX push Spread consts into their namespace?
30 }
31 }
32  
33 return if ( delete $args->{no_auto_bootstrap} );
34  
35 # bootstrap
36 __PACKAGE__->new( %$args );
37
38 return;
39 }
40  
41  
42 sub new {
43 my $class = shift;
44 return $sprocket_spread if ( $sprocket_spread );
45  
46 my %args = &adjust_params;
47
48 my $host = delete $args{host};
49 my $port = delete $args{port};
50  
51 warn 'Unknown params passed to Sprocket::Spread: '.join(',', keys %args)."\n"
52 if ( keys %args );
53  
54 $sprocket_spread = bless( {
55 host => $host,
56 port => $port,
57 cm => Sprocket::ChannelManager->new(),
58 }, ref $class || $class);
59
60 $sprocket_spread->{session_id} =
61 POE::Session->create(
62 object_states => [
63 $sprocket_spread => [qw(
64 _start
65 _stop
66  
67 input
68 error
69  
70 disconnect
71 connect
72 )]
73 ]
74 )->ID();
75  
76 return $sprocket_spread;
77 }
78  
79  
80 sub connect {
81 my ( $self, %args );
82 if ( ref $_[ KERNEL ] ) {
83 ( $self, %args ) = @_[ OBJECT, ARG0 .. $#_ ];
84 } else {
85 $self = shift;
86 return $poe_kernel->call( $self->{session_id} => 'connect' => @_ );
87 }
88
89 return 1 if ( $self->{connected} );
90
91 $args{host} = $self->{host}
92 if ( $self->{host} && !$args{host} );
93
94 $args{port} = $self->{port}
95 if ( $self->{port} && !$args{port} );
96
97 $self->{spread_name} = ( $args{port} || '4803' ) . '@' . ( $args{host} || 'localhost' );
98 $self->{private_name} = 'sp-' . $$;
99 # names can't be too long, because it chops it and finds that the first part is not unique
100 # ARGGG!
101 #$self->{private_name} = $poe_kernel->ID();
102  
103 @{$self}{qw( mbox private_group )} = Spread::connect( {
104 spread_name => $self->{spread_name},
105 private_name => $self->{private_name},
106 } );
107  
108 if ( $@ || !defined( $self->{mbox} ) || !defined( $self->{private_group} ) ) {
109 warn "Spread connect failed: $@\n";
110  
111 $self->{connected} = 0;
112 return 0;
113 } else {
114 # XXX retry?
115 my $fh = $self->{fh} = gensym();
116 open( $fh, "<&=$self->{mbox}" ) or die $!;
117  
118 $self->{wheel} = POE::Wheel::ReadWrite->new(
119 Handle => $fh,
120 Driver => Sprocket::Spread::Driver->new( mbox => $self->{mbox} ),
121 Filter => Sprocket::Spread::Filter->new(),
122 InputEvent => 'input',
123 ErrorEvent => 'error',
124 );
125 warn "spread connected with mbox: $self->{mbox} priv: $self->{private_name} and spread_name $self->{spread_name}";
126  
127 $self->{connected} = 1;
128 return 1;
129 }
130  
131 }
132  
133 sub _start {
134 warn "Spread started\n";
135 $_[KERNEL]->yield( 'connect' );
136 }
137  
138 sub _stop {
139 warn "Spread stopped\n";
140 }
141  
142 sub error {
143 warn "Spread error\n";
144 $_[OBJECT]->disconnect();
145 }
146  
147 sub input {
148 my ( $self, $input ) = @_[ OBJECT, ARG0 ];
149 my ( $type, $sender, $groups, $mess_type, $endian, $message ) = @{$input};
150  
151 return $self->disconnect()
152 unless( defined( $type ) );
153  
154 if ( $type & REGULAR_MESS ) {
155  
156 if ( defined( $endian ) && $endian ) {
157 warn "Spread: endian mismatch!";
158 }
159  
160 $self->deliver( 'message', $self->{private_name}, {
161 type => 'message',
162 message => $message,
163 group => $sender,
164 members => $groups,
165 index => $mess_type,
166 } );
167  
168 return;
169 }
170  
171 if ( $type & TRANSITION_MESS ) {
172  
173 $self->deliver( 'admin', $self->{private_name}, {
174 'type' => 'transitional',
175 'group' => $sender
176 } );
177  
178 } elsif ( $type & CAUSED_BY_LEAVE && !( $type & REG_MEMB_MESS ) ) {
179  
180 $self->deliver( 'admin', $self->{private_name}, {
181 'type' => 'self_leave',
182 'group' => $sender
183 } );
184  
185 } elsif ( $type & REG_MEMB_MESS ) {
186  
187 my ( @gids, $nummem, $member );
188 eval {
189 @gids = unpack( 'IIIIa*', $message );
190 ( $nummem, $member ) = delete @gids[ 3, 4 ];
191 };
192  
193 if ( $@ ) {
194 $self->deliver( 'error', 'receive', $@ );
195 return;
196 }
197  
198 if ( $type & CAUSED_BY_DISCONNECT ) {
199
200 $self->deliver( 'admin', $self->{private_name}, {
201 type => 'disconnect',
202 who => $member,
203 group => $sender,
204 members => $groups,
205 index => $mess_type,
206 gid => \@gids,
207 } );
208  
209 } elsif ( $type & CAUSED_BY_NETWORK ) {
210
211 $self->deliver( 'admin', $self->{private_name}, {
212 type => 'network',
213 message => $message,
214 group => $sender,
215 members => $groups,
216 index => $mess_type,
217 gid => \@gids,
218 } );
219  
220 } elsif ( $type & CAUSED_BY_JOIN ) {
221
222 $self->deliver( 'admin', $self->{private_name}, {
223 type => 'join',
224 who => $member,
225 group => $sender,
226 members => $groups,
227 index => $mess_type,
228 gid => \@gids,
229 } );
230  
231 } elsif ( $type & CAUSED_BY_LEAVE ) {
232  
233 $self->deliver( 'admin', $self->{private_name}, {
234 type => 'leave',
235 who => $member,
236 group => $sender,
237 members => $groups,
238 index => $mess_type,
239 gid => \@gids,
240 } );
241  
242 } else {
243 $self->deliver( 'error', 'receive', 'unknown packet type' );
244 }
245  
246 } else {
247 $self->deliver( 'error', 'receive', 'unknown packet type' );
248 }
249  
250 return;
251 }
252  
253 sub deliver {
254 my ( $self, $type ) = ( shift, shift );
255 my ( $errtype, $privname, $msg );
256  
257 if ( $type eq 'error' ) {
258 ( $errtype, $msg ) = @_;
259 warn "error: $errtype $msg\n";
260 } else {
261 ( $privname, $msg ) = @_;
262 $self->{cm}->deliver( $type, $privname, $msg );
263 }
264
265 # XXX
266 print STDERR 'msg:'.$privname.' '.Data::Dumper->Dump([$msg]);
267
268 return;
269 }
270  
271 sub disconnect {
272 my ( $self );
273 if ( ref $_[ KERNEL ] ) {
274 $self = $_[ OBJECT ];
275 } else {
276 $self = shift;
277 return $poe_kernel->call( $self->{session_id} => 'disconnect' => @_ );
278 }
279 # TODO
280 warn "Spread disconnect\n";
281 $self->{connected} = 0;
282 }
283  
284 sub publish {
285 my ( $self, $groups, $message, $mess_type, $flag ) = @_;
286  
287 unless ( $self->{mbox} ) {
288 warn "not connected when trying to publish to spread";
289 return;
290 }
291  
292 $flag = SAFE_MESS unless( defined( $flag ) );
293
294 $mess_type = 0 unless( defined( $mess_type ) );
295  
296 $groups = $groups->[0]
297 if ( ref( $groups ) && ref( $groups ) eq 'ARRAY' && $#{$groups} == 0 );
298
299 require Data::Dumper;
300 warn "groups:".Data::Dumper->Dump([$groups]);
301
302 my $ret;
303 eval {
304 $ret = Spread::multicast( $self->{mbox}, $flag, $groups, $mess_type, $message );
305 };
306  
307 if ( $@ || !defined( $ret ) || $ret < 0 ) {
308 $self->disconnect()
309 if ( defined $sperrno && $sperrno == CONNECTION_CLOSED );
310 return 0;
311 }
312  
313 return 1;
314 }
315  
316 sub subscribe {
317 my ( $self, $groups ) = @_;
318  
319 unless ( $self->{connected} ) {
320 unless ( $self->connect() ) {
321 return 0;
322 }
323 }
324  
325 $groups = $groups->[0]
326 if ( ref( $groups ) && ref( $groups ) eq 'ARRAY' && $#{$groups} == 0 );
327  
328 my $ret;
329 eval {
330 $ret = Spread::join( $self->{mbox}, $groups );
331 };
332 if ( $@ && !$ret ) {
333 $self->disconnect()
334 if ( defined $sperrno && $sperrno == CONNECTION_CLOSED );
335 return 0;
336 }
337
338 return 1;
339 }
340  
341 sub unsubscribe {
342 my ( $self, $groups ) = @_;
343  
344 unless ( $self->{connected} ) {
345 unless ( $self->connect() ) {
346 return 0;
347 }
348 }
349  
350 $groups = $groups->[0]
351 if ( ref( $groups ) && ref( $groups ) eq 'ARRAY' && $#{$groups} == 0 );
352  
353 my $ret;
354 eval {
355 $ret = Spread::leave( $self->{mbox}, $groups );
356 };
357 if ( $@ && !$ret ) {
358 $self->disconnect()
359 if ( defined $sperrno && $sperrno == CONNECTION_CLOSED );
360 return 0;
361 }
362  
363 return 1;
364 }
365  
366 sub plugin_subscribe {
367 my ( $self, $plugin, $groups ) = @_;
368
369 return $self->subscribe( $self->{cm}->subscribe( $plugin, $groups ) );
370 }
371  
372 sub plugin_unsubscribe {
373 my ( $self, $plugin, $groups ) = @_;
374
375 return $self->unsubscribe( $self->{cm}->unsubscribe( $plugin, $groups ) );
376 }
377  
378 sub plugin_publish {
379 my ( $self, $plugin, $groups ) = ( shift, shift, shift );
380
381 my $pub = $self->{cm}->grouplist( $plugin, $groups );
382 require Data::Dumper;
383 warn "plugin publish: $pub ".Data::Dumper->Dump([$pub]);
384 return $self->publish( $pub, @_ );
385 }
386  
387  
388 1;
389  
390  
391 package Sprocket::Spread::Driver;
392  
393 use strict;
394 use warnings;
395  
396 sub new {
397 my $class = shift;
398 my %args = @_;
399  
400 my $mbox = delete $args{mbox};
401  
402 warn "bad Spread param: ".join( ',', keys %args ) if ( keys %args );
403  
404 bless( [ $mbox ], ref $class || $class );
405 }
406  
407  
408 sub get {
409 my ( $self, $fh ) = @_;
410  
411 my ( $type, $sender, $groups, $messt, $endian, $message ) = Spread::receive( $self->[ 0 ] );
412  
413 if ( !defined( $type ) ) {
414 warn "Spread: Unknown error";
415 return [];
416 }
417  
418 return [ $type, $sender, $groups, $messt, $endian, $message ];
419 }
420  
421 1;
422  
423 package Sprocket::Spread::Filter;
424  
425 use strict;
426 use warnings;
427  
428 sub new {
429 my $class = shift;
430  
431 bless( [], ref $class || $class );
432 }
433  
434 sub get {
435 shift;
436 return [ @_ ];
437 }
438  
439 1;
440