| 1 | package XAS::Lib::POE::PubSub; | ||
| 2 | |||
| 3 | 1 | our $VERSION = '0.01'; | |
| 4 | |||
| 5 | 1 | ▶ | use POE; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | use XAS::Class | ||
| 8 | 1 | debug => 0, | |
| 9 | version => $VERSION, | ||
| 10 | base => 'XAS::Singleton', | ||
| 11 | 1 | ▶ | ; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | #use Data::Dumper; | ||
| 14 | |||
| 15 | # ---------------------------------------------------------------------- | ||
| 16 | # Public Methods | ||
| 17 | # ---------------------------------------------------------------------- | ||
| 18 | |||
| 19 | sub subscribe { | ||
| 20 | 0 | ▶ | my $self = shift; |
|
Subroutine
subscribe: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 21 | 0 | my ($session, $channel) = $self->validate_params(\@_, [ | |
| 22 | 1, | ||
| 23 | { optional => 1, default => 'default' } | ||
| 24 | ]); | ||
| 25 | |||
| 26 | 0 | $self->{registry}->{$channel}->{$session} = $session; | |
| 27 | |||
| 28 | } | ||
| 29 | |||
| 30 | sub unsubscribe { | ||
| 31 | 0 | ▶ | my $self = shift; |
|
Subroutine
unsubscribe: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 32 | 0 | my ($session, $channel) = $self->validate_params(\@_, [ | |
| 33 | 1, | ||
| 34 | { optional => 1, default => 'default' } | ||
| 35 | ]); | ||
| 36 | |||
| 37 | 0 | delete $self->{registry}->{$channel}->{$session}; | |
| 38 | |||
| 39 | } | ||
| 40 | |||
| 41 | sub publish { | ||
| 42 | 0 | ▶ | my $self = shift; |
|
Subroutine
publish: not called CC=2 SCAR=17.9
Pod
documented
| |||
| 43 | 0 | my $p = $self->validate_params(\@_, { | |
| 44 | -event => 1, | ||
| 45 | -args => { optional => 1, default => undef }, | ||
| 46 | -channel => { optional => 1, default => 'default' }, | ||
| 47 | }); | ||
| 48 | |||
| 49 | 0 | my $event = $p->{event}; | |
| 50 | 0 | my $channel = $p->{channel}; | |
| 51 | 0 | my $args = $p->{args}; | |
| 52 | |||
| 53 | 0 | foreach my $session (keys %{$self->{registry}->{$channel}}) { | |
| 54 | |||
| 55 | 0 | $poe_kernel->post($session, $event, $args); | |
| 56 | |||
| 57 | } | ||
| 58 | |||
| 59 | } | ||
| 60 | |||
| 61 | # ---------------------------------------------------------------------- | ||
| 62 | # Private Methods | ||
| 63 | # ---------------------------------------------------------------------- | ||
| 64 | |||
| 65 | sub init { | ||
| 66 | 0 | ▶ | my $class = shift; |
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 67 | |||
| 68 | 0 | my $self = $class->SUPER::init(@_); | |
| 69 | |||
| 70 | 0 | $self->{registry} = {}; | |
| 71 | |||
| 72 | 0 | return $self; | |
| 73 | |||
| 74 | } | ||
| 75 | |||
| 76 | 1 | 1; | |
| 77 | |||
| 78 | __END__ | ||
| 79 | |||
| 80 | =head1 NAME | ||
| 81 | |||
| 82 | XAS::Lib::POE::PubSub - A publish/subscribe class for POE sessions | ||
| 83 | |||
| 84 | =head1 SYNOPSIS | ||
| 85 | |||
| 86 | use POE; | ||
| 87 | use XAS::Lib::POE::PubSub; | ||
| 88 | |||
| 89 | my $pubsub = XAS::Lib::POE::PubSub->new(); | ||
| 90 | |||
| 91 | $pubsub->subscribe('session', 'channel'); | ||
| 92 | $pubusb->publish( | ||
| 93 | -event => 'event', | ||
| 94 | -channel => 'channel', | ||
| 95 | ); | ||
| 96 | |||
| 97 | =head1 DESCRIPTION | ||
| 98 | |||
| 99 | This is a very simple channel based publish/subscribe framework for POE. It | ||
| 100 | is implemented as a singleton. It allows you to publish events to all | ||
| 101 | interested sessions. | ||
| 102 | |||
| 103 | =head1 METHODS | ||
| 104 | |||
| 105 | =head2 subscribe($session, $channel); | ||
| 106 | |||
| 107 | This method allows you to subscribe to the specified channel. | ||
| 108 | |||
| 109 | =over 4 | ||
| 110 | |||
| 111 | =item B<$session> | ||
| 112 | |||
| 113 | The session name. | ||
| 114 | |||
| 115 | =item B<$channel> | ||
| 116 | |||
| 117 | The optional channel to subscribe too. Defaults to 'default'. | ||
| 118 | |||
| 119 | =back | ||
| 120 | |||
| 121 | =head2 unsubscribe($session, $channel); | ||
| 122 | |||
| 123 | This method allows you to unsubscribe from the specified channel. | ||
| 124 | |||
| 125 | =over 4 | ||
| 126 | |||
| 127 | =item B<$session> | ||
| 128 | |||
| 129 | The session name. | ||
| 130 | |||
| 131 | =item B<$channel> | ||
| 132 | |||
| 133 | The optional channel to unsubscribe from. Defaults to 'default'. | ||
| 134 | |||
| 135 | =back | ||
| 136 | |||
| 137 | =head2 publish | ||
| 138 | |||
| 139 | This method allows you to publish an event to a specified channel. Additional | ||
| 140 | arguuments can be supplied. It takes the following parameters: | ||
| 141 | |||
| 142 | =over 4 | ||
| 143 | |||
| 144 | =item B<-event> | ||
| 145 | |||
| 146 | The event to send. | ||
| 147 | |||
| 148 | =item B<-channel> | ||
| 149 | |||
| 150 | Optional channel. Defaults to 'default'. | ||
| 151 | |||
| 152 | =item B<-args> | ||
| 153 | |||
| 154 | Optional additional arguments to send with the event. The context of the | ||
| 155 | arguments is determined by the method that is handling the event. For example: | ||
| 156 | |||
| 157 | -args => ['this', 'is', 'neat'] | ||
| 158 | -args => { this => 'is neat' } | ||
| 159 | -args => 'this is neat' | ||
| 160 | |||
| 161 | Are all valid arguments. | ||
| 162 | |||
| 163 | =back | ||
| 164 | |||
| 165 | =head1 SEE ALSO | ||
| 166 | |||
| 167 | =over 4 | ||
| 168 | |||
| 169 | =item L<XAS|XAS> | ||
| 170 | |||
| 171 | =back | ||
| 172 | |||
| 173 | =head1 AUTHOR | ||
| 174 | |||
| 175 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||
| 176 | |||
| 177 | =head1 COPYRIGHT AND LICENSE | ||
| 178 | |||
| 179 | Copyright (C) 2014 Kevin L. Esteb | ||
| 180 | |||
| 181 | This is free software; you can redistribute it and/or modify it under | ||
| 182 | the terms of the Artistic License 2.0. For details, see the full text | ||
| 183 | of the license at http://www.perlfoundation.org/artistic_license_2_0. | ||
| 184 | |||
| 185 | =cut | ||