| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # $Id$ | 
| 2 |  |  |  |  |  |  | # | 
| 3 |  |  |  |  |  |  | # Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp> | 
| 4 |  |  |  |  |  |  | # All rights reserved. | 
| 5 |  |  |  |  |  |  | # | 
| 6 |  |  |  |  |  |  | # This program is free software: you can redistribute it and/or modify | 
| 7 |  |  |  |  |  |  | # it under the terms of the GNU General Public License as published by | 
| 8 |  |  |  |  |  |  | # the Free Software Foundation, either version 2 of the License, or | 
| 9 |  |  |  |  |  |  | # (at your option) any later version. | 
| 10 |  |  |  |  |  |  | # | 
| 11 |  |  |  |  |  |  | # This program is distributed in the hope that it will be useful, | 
| 12 |  |  |  |  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 13 |  |  |  |  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
| 14 |  |  |  |  |  |  | # GNU General Public License for more details. | 
| 15 |  |  |  |  |  |  | # | 
| 16 |  |  |  |  |  |  | # You should have received a copy of the GNU General Public License | 
| 17 |  |  |  |  |  |  | # along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | package POE::Component::MessageQueue::Statistics::Publish; | 
| 20 | 1 |  |  | 1 |  | 6 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 30 |  | 
| 21 | 1 |  |  | 1 |  | 6 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 24 |  | 
| 22 | 1 |  |  | 1 |  | 592 | use POE; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | use IO::Handle; | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | sub spawn | 
| 26 |  |  |  |  |  |  | { | 
| 27 |  |  |  |  |  |  | my $class = shift; | 
| 28 |  |  |  |  |  |  | my $self = $class->new( | 
| 29 |  |  |  |  |  |  | alias    => 'MQ-Publish', | 
| 30 |  |  |  |  |  |  | interval => 10, | 
| 31 |  |  |  |  |  |  | @_ | 
| 32 |  |  |  |  |  |  | ); | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | $self->{statistics}->add_publisher($self); | 
| 35 |  |  |  |  |  |  | $self->{session} = POE::Session->create( | 
| 36 |  |  |  |  |  |  | heap => {}, | 
| 37 |  |  |  |  |  |  | inline_states => { | 
| 38 |  |  |  |  |  |  | '_start' => sub { | 
| 39 |  |  |  |  |  |  | $_[KERNEL]->alias_set( $self->{alias} ); | 
| 40 |  |  |  |  |  |  | $_[KERNEL]->yield('publish'); | 
| 41 |  |  |  |  |  |  | }, | 
| 42 |  |  |  |  |  |  | 'publish' => sub { | 
| 43 |  |  |  |  |  |  | # gets called on a regular interval | 
| 44 |  |  |  |  |  |  | my ($kernel, $heap) = @_[KERNEL, HEAP]; | 
| 45 |  |  |  |  |  |  | my $alarm = $heap->{publish_alarm}; | 
| 46 |  |  |  |  |  |  | $kernel->alarm_remove($alarm) if $alarm; | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | $self->publish(); | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | $heap->{publish_alarm} = | 
| 51 |  |  |  |  |  |  | $kernel->alarm_set('publish', time() + $self->{interval}); | 
| 52 |  |  |  |  |  |  | }, | 
| 53 |  |  |  |  |  |  | 'shutdown' => sub { | 
| 54 |  |  |  |  |  |  | my ($kernel, $heap) = @_[KERNEL, HEAP]; | 
| 55 |  |  |  |  |  |  | my $alarm = $heap->{publish_alarm}; | 
| 56 |  |  |  |  |  |  | $kernel->alarm_remove($alarm) if $alarm; | 
| 57 |  |  |  |  |  |  | $self->publish(); | 
| 58 |  |  |  |  |  |  | # Cut off circular references. | 
| 59 |  |  |  |  |  |  | delete @{$self}{qw(statistics session)}; | 
| 60 |  |  |  |  |  |  | }, | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  | ); | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | sub new | 
| 66 |  |  |  |  |  |  | { | 
| 67 |  |  |  |  |  |  | my $class = shift; | 
| 68 |  |  |  |  |  |  | my $self  = bless { @_ }, $class; # XXX - hack | 
| 69 |  |  |  |  |  |  | $self; | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | sub publish | 
| 73 |  |  |  |  |  |  | { | 
| 74 |  |  |  |  |  |  | my $self = shift; | 
| 75 |  |  |  |  |  |  | my $output = $self->{output}; | 
| 76 |  |  |  |  |  |  | my $ref = ref $output; | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | if (! $ref) { # simple string. a filename | 
| 79 |  |  |  |  |  |  | $self->publish_file( $output ); | 
| 80 |  |  |  |  |  |  | } elsif ($ref eq 'CODE') { | 
| 81 |  |  |  |  |  |  | $self->publish_code( $output ); | 
| 82 |  |  |  |  |  |  | } elsif ($ref eq 'GLOB' || $output->can('print')) { | 
| 83 |  |  |  |  |  |  | $self->publish_handle( $output ); | 
| 84 |  |  |  |  |  |  | } else { | 
| 85 |  |  |  |  |  |  | # don't know what it is. subclasses may detect that we were | 
| 86 |  |  |  |  |  |  | # unable to determine the output type by checking for flase | 
| 87 |  |  |  |  |  |  | # return values from this subroutine | 
| 88 |  |  |  |  |  |  | return (); | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | return 1; | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | sub shutdown | 
| 95 |  |  |  |  |  |  | { | 
| 96 |  |  |  |  |  |  | my $self = shift; | 
| 97 |  |  |  |  |  |  | POE::Kernel->post($self->{session}, 'shutdown'); | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | 1; | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | __END__ | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | =head1 NAME | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | POE::Component::MessageQueue::Statistics::Publish - Base Statistics Publish Class | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | =head1 METHODS | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | =head2 new(%args) | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | Creates a new instance. You must pass in an instance of POE::Component::MessageQueue::Statistics, and an output destination | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | # initialized elsewhere | 
| 115 |  |  |  |  |  |  | my $stats   = POE::Component::MessageQueue::Statistics->new; | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | my $publish = POE::Component::MessageQueue::Statistics::Publish::YAML->new( | 
| 118 |  |  |  |  |  |  | output => \*STDERR, | 
| 119 |  |  |  |  |  |  | statistics => $stats, | 
| 120 |  |  |  |  |  |  | interval => 10, # dump every 10 seconds | 
| 121 |  |  |  |  |  |  | ); | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | =head2 publish() | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | Publishes the current state of the statistics. This is actually a dispatcher | 
| 126 |  |  |  |  |  |  | that dispatches to the appropriate method calls (described below) that are | 
| 127 |  |  |  |  |  |  | specific to a particular output type. | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | Your subclass should implement the appropriate methods (output types) that | 
| 130 |  |  |  |  |  |  | you want to support. | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | =head2 publish_file($filename) | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | Receives a filename to dump the statistics. | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | =head2 publish_handle($handle) | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | Receives a handle to dump the statistics. | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | =head2 publish_code($code) | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | Receives a subroutine reference. Your code should simply pass the result | 
| 143 |  |  |  |  |  |  | output to $code and execute it: | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | sub publish_code | 
| 146 |  |  |  |  |  |  | { | 
| 147 |  |  |  |  |  |  | my ($self, $code) = @_; | 
| 148 |  |  |  |  |  |  | my $output = ....; # generate output here | 
| 149 |  |  |  |  |  |  | $code->( $output ); | 
| 150 |  |  |  |  |  |  | } | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | L<POE::Component::MessageQueue::Statistics>, | 
| 155 |  |  |  |  |  |  | L<POE::Component::MessageQueue::Statistics::Publish::YAML> | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | =head1 AUTHOR | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | Daisuke Maki E<lt>daisuke@endeworks.jpE<gt> | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | =cut |