| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package ZeroMQ::PubSub; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 20450 | use Moose; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  | use ZMQ::LibZMQ2; | 
| 5 |  |  |  |  |  |  | use JSON; | 
| 6 |  |  |  |  |  |  | use namespace::autoclean; | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | with 'MooseX::Callbacks'; | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | has 'context' => ( | 
| 11 |  |  |  |  |  |  | is => 'rw', | 
| 12 |  |  |  |  |  |  | isa => 'ZMQ::LibZMQ2::Context', | 
| 13 |  |  |  |  |  |  | lazy_build => 1, | 
| 14 |  |  |  |  |  |  | ); | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | has 'publish_sock' => ( | 
| 17 |  |  |  |  |  |  | is => 'rw', | 
| 18 |  |  |  |  |  |  | isa => 'ZMQ::LibZMQ2::Socket', | 
| 19 |  |  |  |  |  |  | lazy_build => 1, | 
| 20 |  |  |  |  |  |  | predicate => 'publish_socket_exists', | 
| 21 |  |  |  |  |  |  | ); | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | has 'subscribe_sock' => ( | 
| 24 |  |  |  |  |  |  | is => 'rw', | 
| 25 |  |  |  |  |  |  | isa => 'ZMQ::LibZMQ2::Socket', | 
| 26 |  |  |  |  |  |  | lazy_build => 1, | 
| 27 |  |  |  |  |  |  | predicate => 'subscription_socket_exists', | 
| 28 |  |  |  |  |  |  | ); | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | sub _build_context { zmq_init() } | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | sub print_debug { | 
| 33 |  |  |  |  |  |  | my ($self, $msg) = @_; | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | return unless $self->debug; | 
| 36 |  |  |  |  |  |  | print "DEBUG: $msg\n"; | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | sub print_info { | 
| 40 |  |  |  |  |  |  | my ($self, $msg) = @_; | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | print "INFO: $msg\n"; | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | sub DEMOLISH { | 
| 46 |  |  |  |  |  |  | my ($self, $igd) = @_; | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | zmq_close($self->publish_sock) if $self->publish_socket_exists && $self->publish_sock; | 
| 49 |  |  |  |  |  |  | zmq_close($self->subscribe_sock) if $self->subscription_socket_exists && $self->subscribe_sock; | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | =head1 NAME | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | ZeroMQ::PubSub - ZeroMQ-based event messaging system. | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | =cut | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | our $VERSION = '0.10'; | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | See L<ZeroMQ::PubSub::Client>, L<ZeroMQ::PubSub::Server> | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | =head1 ATTRIBUTES | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | =head2 debug | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | =cut | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | has 'debug' => ( is => 'rw', isa => 'Bool' ); | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | =head1 METHODS | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | =head2 subscribe($event, $callback) | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | Calls $callback when a message of type $event is received. Can be used | 
| 78 |  |  |  |  |  |  | on the server or the client. | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | $callback is called with two arguments: $self (client or server instance) and event parameters. | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | =cut | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | sub subscribe { | 
| 85 |  |  |  |  |  |  | my ($self, $evt, $cb) = @_; | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | # create callback wrapper | 
| 88 |  |  |  |  |  |  | my $cb_wrapped = sub { | 
| 89 |  |  |  |  |  |  | $cb->($self, @_); | 
| 90 |  |  |  |  |  |  | }; | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | # set up callback | 
| 93 |  |  |  |  |  |  | $self->register_callback($evt => $cb_wrapped); | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | =head2 dispatch_event($msg) | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | Runs event callbacks for the message based on event type. You probably | 
| 100 |  |  |  |  |  |  | don't need to call this. | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | =cut | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | sub dispatch_event { | 
| 105 |  |  |  |  |  |  | my ($self, $msg) = @_; | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | # message type lives in type | 
| 108 |  |  |  |  |  |  | my $type = $msg->{type}; | 
| 109 |  |  |  |  |  |  | unless ($type) { | 
| 110 |  |  |  |  |  |  | warn "Got ZeroMQ::PubSub message with no type defined\n"; | 
| 111 |  |  |  |  |  |  | return; | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | $self->print_debug("Got $type event"); | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | my $params = $msg->{params} || {}; | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | # calls callbacks | 
| 119 |  |  |  |  |  |  | $self->dispatch($type => $params); | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | L<ZeroMQ::PubSub::Server>, L<ZeroMQ::PubSub::Client> | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | =head1 TODO | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | * Tests | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | * Support non-blocking (w/ L<AnyEvent>) | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | =head1 AUTHOR | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | Mischa Spiegelmock, C<< <revmischa at cpan.org> >> | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | =head1 BUGS | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | Please report any bugs or feature requests to C<bug-zeromq-pubsub at rt.cpan.org>, or through | 
| 139 |  |  |  |  |  |  | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=ZeroMQ-PubSub>.  I will be notified, and then you'll | 
| 140 |  |  |  |  |  |  | automatically be notified of progress on your bug as I make changes. | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | =head1 SUPPORT | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | You can find documentation for this module with the perldoc command. | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | perldoc ZeroMQ::PubSub | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | =head1 ACKNOWLEDGEMENTS | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | L<ZeroMQ> | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | =head1 LICENSE AND COPYRIGHT | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | Copyright 2012 Mischa Spiegelmock. | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | This program is free software; you can redistribute it and/or modify it | 
| 157 |  |  |  |  |  |  | under the terms of either: the GNU General Public License as published | 
| 158 |  |  |  |  |  |  | by the Free Software Foundation; or the Artistic License. | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | See http://dev.perl.org/licenses/ for more information. | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | =cut | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | 1; # End of ZeroMQ::PubSub |