| 1 |
NAME |
| 2 |
Object::Pluggable - A base class for creating plugin-enabled objects |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
# A simple POE Component that sends ping events to registered sessions |
| 6 |
# and plugins every second. |
| 7 |
|
| 8 |
{ |
| 9 |
package SimplePoCo; |
| 10 |
|
| 11 |
use strict; |
| 12 |
use warnings; |
| 13 |
use base qw(Object::Pluggable); |
| 14 |
use POE; |
| 15 |
use Object::Pluggable::Constants qw(:ALL); |
| 16 |
|
| 17 |
sub spawn { |
| 18 |
my ($package, %opts) = @_; |
| 19 |
my $self = bless \%opts, $package; |
| 20 |
|
| 21 |
$self->_pluggable_init( |
| 22 |
prefix => 'simplepoco_', |
| 23 |
types => [qw(EXAMPLE)], |
| 24 |
debug => 1, |
| 25 |
); |
| 26 |
|
| 27 |
POE::Session->create( |
| 28 |
object_states => [ |
| 29 |
$self => { shutdown => '_shutdown' }, |
| 30 |
$self => [qw(_send_ping _start register unregister __send_event)], |
| 31 |
], |
| 32 |
); |
| 33 |
|
| 34 |
return $self; |
| 35 |
} |
| 36 |
|
| 37 |
sub shutdown { |
| 38 |
my ($self) = @_; |
| 39 |
$poe_kernel->post($self->{session_id}, 'shutdown'); |
| 40 |
} |
| 41 |
|
| 42 |
sub _pluggable_event { |
| 43 |
my ($self) = @_; |
| 44 |
$poe_kernel->post($self->{session_id}, '__send_event', @_); |
| 45 |
} |
| 46 |
|
| 47 |
sub _start { |
| 48 |
my ($kernel, $self) = @_[KERNEL, OBJECT]; |
| 49 |
$self->{session_id} = $_[SESSION]->ID(); |
| 50 |
|
| 51 |
if ($self->{alias}) { |
| 52 |
$kernel->alias_set($self->{alias}); |
| 53 |
} |
| 54 |
else { |
| 55 |
$kernel->refcount_increment($self->{session_id}, __PACKAGE__); |
| 56 |
} |
| 57 |
|
| 58 |
$kernel->delay(_send_ping => $self->{time} || 300); |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
sub _shutdown { |
| 63 |
my ($kernel, $self) = @_[KERNEL, OBJECT]; |
| 64 |
|
| 65 |
$self->_pluggable_destroy(); |
| 66 |
$kernel->alarm_remove_all(); |
| 67 |
$kernel->alias_remove($_) for $kernel->alias_list(); |
| 68 |
$kernel->refcount_decrement($self->{session_id}, __PACKAGE__) if !$self->{alias}; |
| 69 |
$kernel->refcount_decrement($_, __PACKAGE__) for keys %{ $self->{sessions} }; |
| 70 |
|
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
sub register { |
| 75 |
my ($kernel, $sender, $self) = @_[KERNEL, SENDER, OBJECT]; |
| 76 |
my $sender_id = $sender->ID(); |
| 77 |
$self->{sessions}->{$sender_id}++; |
| 78 |
|
| 79 |
if ($self->{sessions}->{$sender_id} == 1) { |
| 80 |
$kernel->refcount_increment($sender_id, __PACKAGE__); |
| 81 |
$kernel->yield(__send_event => 'simplepoco_registered', $sender_id); |
| 82 |
} |
| 83 |
|
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
sub unregister { |
| 88 |
my ($kernel, $sender, $self) = @_[KERNEL, SENDER, OBJECT]; |
| 89 |
my $sender_id = $sender->ID(); |
| 90 |
my $record = delete $self->{sessions}->{$sender_id}; |
| 91 |
|
| 92 |
if ($record) { |
| 93 |
$kernel->refcount_decrement($sender_id, __PACKAGE__); |
| 94 |
$kernel->yield(__send_event => 'simplepoco_unregistered', $sender_id); |
| 95 |
} |
| 96 |
|
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
sub __send_event { |
| 101 |
my ($kernel, $self, $event, @args) = @_[KERNEL, OBJECT, ARG0..$#_]; |
| 102 |
|
| 103 |
return 1 if $self->_pluggable_process(EXAMPLE => $event, \@args) == PLUGIN_EAT_ALL; |
| 104 |
$kernel->post($_, $event, @args) for keys %{ $self->{sessions} }; |
| 105 |
} |
| 106 |
|
| 107 |
sub _send_ping { |
| 108 |
my ($kernel, $self) = @_[KERNEL, OBJECT]; |
| 109 |
|
| 110 |
$kernel->yield(__send_event => 'simplepoco_ping', 'Wake up sleepy'); |
| 111 |
$kernel->delay(_send_ping => $self->{time} || 1); |
| 112 |
return; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
{ |
| 117 |
package SimplePoCo::Plugin; |
| 118 |
use strict; |
| 119 |
use warnings; |
| 120 |
use Object::Pluggable::Constants qw(:ALL); |
| 121 |
|
| 122 |
sub new { |
| 123 |
my $package = shift; |
| 124 |
return bless { @_ }, $package; |
| 125 |
} |
| 126 |
|
| 127 |
sub plugin_register { |
| 128 |
my ($self, $pluggable) = splice @_, 0, 2; |
| 129 |
print "Plugin added\n"; |
| 130 |
$pluggable->plugin_register($self, 'EXAMPLE', 'all'); |
| 131 |
return 1; |
| 132 |
} |
| 133 |
|
| 134 |
sub plugin_unregister { |
| 135 |
print "Plugin removed\n"; |
| 136 |
return 1; |
| 137 |
} |
| 138 |
|
| 139 |
sub EXAMPLE_ping { |
| 140 |
my ($self, $pluggable) = splice @_, 0, 2; |
| 141 |
my $text = ${ $_[0] }; |
| 142 |
print "Plugin got '$text'\n"; |
| 143 |
return PLUGIN_EAT_NONE; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
use strict; |
| 148 |
use warnings; |
| 149 |
use POE; |
| 150 |
|
| 151 |
my $pluggable = SimplePoCo->spawn( |
| 152 |
alias => 'pluggable', |
| 153 |
time => 1, |
| 154 |
); |
| 155 |
|
| 156 |
POE::Session->create( |
| 157 |
package_states => [ |
| 158 |
main => [qw(_start simplepoco_registered simplepoco_ping)], |
| 159 |
], |
| 160 |
); |
| 161 |
|
| 162 |
$poe_kernel->run(); |
| 163 |
|
| 164 |
sub _start { |
| 165 |
my $kernel = $_[KERNEL]; |
| 166 |
$kernel->post(pluggable => 'register'); |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
sub simplepoco_registered { |
| 171 |
print "Main program registered for events\n"; |
| 172 |
my $plugin = SimplePoCo::Plugin->new(); |
| 173 |
$pluggable->plugin_add('TestPlugin', $plugin); |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
sub simplepoco_ping { |
| 178 |
my ($heap, $text) = @_[HEAP, ARG0]; |
| 179 |
print "Main program got '$text'\n"; |
| 180 |
$heap->{got_ping}++; |
| 181 |
$pluggable->shutdown() if $heap->{got_ping} == 3; |
| 182 |
return; |
| 183 |
} |
| 184 |
|
| 185 |
DESCRIPTION |
| 186 |
Object::Pluggable is a base class for creating plugin enabled objects. |
| 187 |
It is a generic port of POE::Component::IRC's plugin system. |
| 188 |
|
| 189 |
If your object dispatches events to listeners, then Object::Pluggable |
| 190 |
may be a good fit for you. |
| 191 |
|
| 192 |
Basic use would involve subclassing Object::Pluggable, then overriding |
| 193 |
"_pluggable_event()" and inserting "_pluggable_process()" wherever you |
| 194 |
dispatch events from. |
| 195 |
|
| 196 |
Users of your object can then load plugins using the plugin methods |
| 197 |
provided to handle events generated by the object. |
| 198 |
|
| 199 |
You may also use plugin style handlers within your object as |
| 200 |
"_pluggable_process()" will attempt to process any events with local |
| 201 |
method calls first. The return value of these handlers has the same |
| 202 |
significance as the return value of 'normal' plugin handlers. |
| 203 |
|
| 204 |
PRIVATE METHODS |
| 205 |
Subclassing Object::Pluggable gives your object the following 'private' |
| 206 |
methods: |
| 207 |
|
| 208 |
"_pluggable_init" |
| 209 |
This should be called on your object after initialisation, but before |
| 210 |
you want to start processing plugins. It accepts a number of |
| 211 |
argument/value pairs: |
| 212 |
|
| 213 |
'types', an arrayref of the types of events that your poco will support, |
| 214 |
OR a hashref with the event types as keys and their abbrevations |
| 215 |
(used as plugin event method prefixes) as values. This argument is |
| 216 |
mandatory. |
| 217 |
|
| 218 |
'prefix', the prefix for your events (default: 'pluggable_'); |
| 219 |
'reg_prefix', the prefix for the register()/unregister() plugin methods |
| 220 |
(default: 'plugin_'); |
| 221 |
'debug', a boolean, if true, will cause a warning to be printed every time a |
| 222 |
plugin call fails. |
| 223 |
|
| 224 |
Notes: 'prefix' should probably end with a '_'. The types specify the |
| 225 |
prefixes for plugin handlers. You can specify as many different types as |
| 226 |
you require. |
| 227 |
|
| 228 |
"_pluggable_destroy" |
| 229 |
This should be called from any shutdown handler that your poco has. The |
| 230 |
method unloads any loaded plugins. |
| 231 |
|
| 232 |
"_pluggable_process" |
| 233 |
This should be called before events are dispatched to interested |
| 234 |
sessions. This gives pluggable a chance to discard events if requested |
| 235 |
to by a plugin. |
| 236 |
|
| 237 |
The first argument is a type, as specified to "_pluggable_init()". |
| 238 |
|
| 239 |
sub _dispatch { |
| 240 |
my ($self, $event, $type, @args) = @_; |
| 241 |
|
| 242 |
# stuff |
| 243 |
|
| 244 |
my $type = ... |
| 245 |
|
| 246 |
return 1 if $self->_pluggable_process($type, $event, \@args)) == PLUGIN_EAT_ALL; |
| 247 |
|
| 248 |
# dispatch event to interested sessions. |
| 249 |
} |
| 250 |
|
| 251 |
A reference to the argument array is passed. This allows the plugin |
| 252 |
system to mangle the arguments or even add new ones. |
| 253 |
|
| 254 |
"_pluggable_event" |
| 255 |
This method should be overridden in your class so that pipeline can |
| 256 |
dispatch events through your event dispatcher. Pipeline sends a prefixed |
| 257 |
'plugin_add' and 'plugin_del' event whenever plugins are added or |
| 258 |
removed, respectively. A prefixed 'plugin_error' event will be sent if a |
| 259 |
plugin a) raises an exception, b) fails to return a true value from its |
| 260 |
register/unregister methods, or c) fails to return a valid EAT constant |
| 261 |
from a handler. |
| 262 |
|
| 263 |
sub _pluggable_event { |
| 264 |
my $self = shift; |
| 265 |
$poe_kernel->post($self->{session_id}, '__send_event', @_); |
| 266 |
} |
| 267 |
|
| 268 |
There is an example of this in the SYNOPSIS. |
| 269 |
|
| 270 |
PUBLIC METHODS |
| 271 |
Subclassing Object::Pluggable gives your object the following public |
| 272 |
methods: |
| 273 |
|
| 274 |
"pipeline" |
| 275 |
Returns the Object::Pluggable::Pipeline object. |
| 276 |
|
| 277 |
"plugin_add" |
| 278 |
Accepts two arguments: |
| 279 |
|
| 280 |
The alias for the plugin |
| 281 |
The actual plugin object |
| 282 |
Any number of extra arguments |
| 283 |
|
| 284 |
The alias is there for the user to refer to it, as it is possible to |
| 285 |
have multiple plugins of the same kind active in one Object::Pluggable |
| 286 |
object. |
| 287 |
|
| 288 |
This method goes through the pipeline's "push()" method, which will call |
| 289 |
"$plugin->plugin_register($pluggable, @args)". |
| 290 |
|
| 291 |
Returns the number of plugins now in the pipeline if plugin was |
| 292 |
initialized, "undef"/an empty list if not. |
| 293 |
|
| 294 |
"plugin_del" |
| 295 |
Accepts the following arguments: |
| 296 |
|
| 297 |
The alias for the plugin or the plugin object itself |
| 298 |
Any number of extra arguments |
| 299 |
|
| 300 |
This method goes through the pipeline's "remove()" method, which will |
| 301 |
call "$plugin->plugin_unregister($pluggable, @args)". |
| 302 |
|
| 303 |
Returns the plugin object if the plugin was removed, "undef"/an empty |
| 304 |
list if not. |
| 305 |
|
| 306 |
"plugin_get" |
| 307 |
Accepts the following arguments: |
| 308 |
|
| 309 |
The alias for the plugin |
| 310 |
|
| 311 |
This method goes through the pipeline's "get()" method. |
| 312 |
|
| 313 |
Returns the plugin object if it was found, "undef"/an empty list if not. |
| 314 |
|
| 315 |
"plugin_list" |
| 316 |
Takes no arguments. |
| 317 |
|
| 318 |
Returns a hashref of plugin objects, keyed on alias, or an empty list if |
| 319 |
there are no plugins loaded. |
| 320 |
|
| 321 |
"plugin_order" |
| 322 |
Takes no arguments. |
| 323 |
|
| 324 |
Returns an arrayref of plugin objects, in the order which they are |
| 325 |
encountered in the pipeline. |
| 326 |
|
| 327 |
"plugin_register" |
| 328 |
Accepts the following arguments: |
| 329 |
|
| 330 |
The plugin object |
| 331 |
The type of the hook (the hook types are specified with _pluggable_init()'s 'types') |
| 332 |
The event name[s] to watch |
| 333 |
|
| 334 |
The event names can be as many as possible, or an arrayref. They |
| 335 |
correspond to the prefixed events and naturally, arbitrary events too. |
| 336 |
|
| 337 |
You do not need to supply events with the prefix in front of them, just |
| 338 |
the names. |
| 339 |
|
| 340 |
It is possible to register for all events by specifying 'all' as an |
| 341 |
event. |
| 342 |
|
| 343 |
Returns 1 if everything checked out fine, "undef"/an empty list if |
| 344 |
something is seriously wrong. |
| 345 |
|
| 346 |
"plugin_unregister" |
| 347 |
Accepts the following arguments: |
| 348 |
|
| 349 |
The plugin object |
| 350 |
The type of the hook (the hook types are specified with _pluggable_init()'s 'types') |
| 351 |
The event name[s] to unwatch |
| 352 |
|
| 353 |
The event names can be as many as possible, or an arrayref. They |
| 354 |
correspond to the prefixed events and naturally, arbitrary events too. |
| 355 |
|
| 356 |
You do not need to supply events with the prefix in front of them, just |
| 357 |
the names. |
| 358 |
|
| 359 |
It is possible to register for all events by specifying 'all' as an |
| 360 |
event. |
| 361 |
|
| 362 |
Returns 1 if all the event name[s] was unregistered, undef if some was |
| 363 |
not found. |
| 364 |
|
| 365 |
PLUGINS |
| 366 |
The basic anatomy of a pluggable plugin is: |
| 367 |
|
| 368 |
# Import the constants, of course you could provide your own |
| 369 |
# constants as long as they map correctly. |
| 370 |
use Object::Pluggable::Constants qw( :ALL ); |
| 371 |
|
| 372 |
# Our constructor |
| 373 |
sub new { |
| 374 |
... |
| 375 |
} |
| 376 |
|
| 377 |
# Required entry point for pluggable plugins |
| 378 |
sub plugin_register { |
| 379 |
my($self, $pluggable) = @_; |
| 380 |
|
| 381 |
# Register events we are interested in |
| 382 |
$pluggable->plugin_register($self, 'SERVER', qw(something whatever)); |
| 383 |
|
| 384 |
# Return success |
| 385 |
return 1; |
| 386 |
} |
| 387 |
|
| 388 |
# Required exit point for pluggable |
| 389 |
sub plugin_unregister { |
| 390 |
my($self, $pluggable) = @_; |
| 391 |
|
| 392 |
# Pluggable will automatically unregister events for the plugin |
| 393 |
|
| 394 |
# Do some cleanup... |
| 395 |
|
| 396 |
# Return success |
| 397 |
return 1; |
| 398 |
} |
| 399 |
|
| 400 |
sub _default { |
| 401 |
my($self, $pluggable, $event) = splice @_, 0, 3; |
| 402 |
|
| 403 |
print "Default called for $event\n"; |
| 404 |
|
| 405 |
# Return an exit code |
| 406 |
return PLUGIN_EAT_NONE; |
| 407 |
} |
| 408 |
|
| 409 |
As shown in the example above, a plugin's "_default" subroutine (if |
| 410 |
present) is called if the plugin receives an event for which it has no |
| 411 |
handler. |
| 412 |
|
| 413 |
The special exit code CONSTANTS are documented in |
| 414 |
Object::Pluggable::Constants. You could provide your own as long as the |
| 415 |
values match up, though. |
| 416 |
|
| 417 |
TODO |
| 418 |
Better documentation >:] |
| 419 |
|
| 420 |
AUTHOR |
| 421 |
Chris 'BinGOs' Williams <[email protected]> |
| 422 |
|
| 423 |
LICENSE |
| 424 |
Copyright "(c)" Chris Williams, Apocalypse, Hinrik Örn Sigurðsson and |
| 425 |
Jeff Pinyan |
| 426 |
|
| 427 |
This module may be used, modified, and distributed under the same terms |
| 428 |
as Perl itself. Please see the license that came with your Perl |
| 429 |
distribution for details. |
| 430 |
|
| 431 |
KUDOS |
| 432 |
APOCAL for writing the original POE::Component::IRC plugin system. |
| 433 |
|
| 434 |
japhy for writing POE::Component::IRC::Pipeline which improved on it. |
| 435 |
|
| 436 |
All the happy chappies who have contributed to POE::Component::IRC over |
| 437 |
the years (yes, it has been years) refining and tweaking the plugin |
| 438 |
system. |
| 439 |
|
| 440 |
The initial idea was heavily borrowed from X-Chat, BIG thanks go out to |
| 441 |
the genius that came up with the EAT_* system :) |
| 442 |
|
| 443 |
SEE ALSO |
| 444 |
POE::Component::IRC |
| 445 |
|
| 446 |
Object::Pluggable::Pipeline |
| 447 |
|
| 448 |
Both POE::Component::Client::NNTP and POE::Component::Server::NNTP use |
| 449 |
this module as a base, examination of their source may yield further |
| 450 |
understanding. |
| 451 |
|