| 1 | package Sprocket::ChannelManager; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Sprocket; | ||
| 7 | |||
| 8 | our $singleton; | ||
| 9 | |||
| 10 | sub CH () { 0 } | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my $class = shift; | ||
| 14 | return $singleton if ( $singleton ); | ||
| 15 | |||
| 16 | $singleton = bless([ | ||
| 17 | { }, # CH | ||
| 18 | ], ref $class || $class ); | ||
| 19 | } | ||
| 20 | |||
| 21 | sub grouplist { | ||
| 22 | my ( $self, $plugin, $groups ) = @_; | ||
| 23 | |||
| 24 | # XXX | ||
| 25 | #my $id = $plugin->uuid; | ||
| 26 | |||
| 27 | return [ map { | ||
| 28 | "/sp".( ( $_ =~ m!^/! ) ? $_ : '/'.$_ ) | ||
| 29 | } @$groups ]; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub subscribe { | ||
| 33 | my ( $self, $plugin, $groups ) = @_; | ||
| 34 | |||
| 35 | my $id = $plugin->uuid; | ||
| 36 | my $ch = $self->[ CH ]; | ||
| 37 | foreach ( @$groups ) { | ||
| 38 | if ( exists( $ch->{$_} ) ) { | ||
| 39 | # they could be duping their subscription if called twice | ||
| 40 | push( @{$ch->{$_}}, $id ); | ||
| 41 | } else { | ||
| 42 | $ch->{$_} = [ $id ]; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | require Data::Dumper; | ||
| 47 | warn Data::Dumper->Dump([$ch]); | ||
| 48 | |||
| 49 | return $self->grouplist( $plugin, $groups ); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub unsubscribe { | ||
| 53 | my ( $self, $plugin, $groups ) = @_; | ||
| 54 | |||
| 55 | my $id = $plugin->uuid; | ||
| 56 | my $ch = $self->[ CH ]; | ||
| 57 | foreach my $g ( @$groups ) { | ||
| 58 | next unless ( exists( $ch->{$g} ) ); | ||
| 59 | @{$ch->{$g}} = grep { $_ ne $id } @{$ch->{$g}}; | ||
| 60 | } | ||
| 61 | |||
| 62 | require Data::Dumper; | ||
| 63 | warn Data::Dumper->Dump([$ch]); | ||
| 64 | |||
| 65 | return $self->grouplist( $plugin, $groups ); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub get_plugins { | ||
| 69 | my ( $self, $groups ) = @_; | ||
| 70 | |||
| 71 | my %uuids; | ||
| 72 | foreach ( @$groups ) { | ||
| 73 | next unless ( m!/sp(/.*)! ); | ||
| 74 | if ( my $ids = $self->[ CH ]->{$1} ) { | ||
| 75 | foreach ( @$ids ) { | ||
| 76 | $uuids{$_}++; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | my $plugins = []; | ||
| 82 | foreach ( keys %uuids ) { | ||
| 83 | my $p = $sprocket->get_plugin( $_ ); | ||
| 84 | if ( $p ) { | ||
| 85 | push( @$plugins, $p ); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | require Data::Dumper; | ||
| 90 | warn "deliver to plugins:".Data::Dumper->Dump([$plugins]); | ||
| 91 | |||
| 92 | return $plugins; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub deliver { | ||
| 96 | my ( $self, $type, $privname, $message ) = @_; | ||
| 97 | |||
| 98 | warn __PACKAGE__."::deliver message: $message $message->{members}"; | ||
| 99 | |||
| 100 | unless ( ref( $message->{members} ) eq 'ARRAY' ) { | ||
| 101 | warn "members in the message is not an array, skipping: $message->{members}"; | ||
| 102 | return; | ||
| 103 | } | ||
| 104 | |||
| 105 | foreach ( @{ $self->get_plugins( $message->{members} ) } ) { | ||
| 106 | warn "handle event in channel manager $_"; | ||
| 107 | $_->handle_event( spread_message => $_ => $message ); | ||
| 108 | } | ||
| 109 | |||
| 110 | return; | ||
| 111 | } | ||
| 112 | |||
| 113 | 1; |