| 1 | package POE::Component::Server::AsyncEndpoint::ChannelAdapter::Stomp; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | our @EXPORT = ( ); | ||
| 6 | use Switch; | ||
| 7 | |||
| 8 | use Carp qw(croak); | ||
| 9 | use POE; | ||
| 10 | use base qw(Exporter POE::Component::Client::Stomp); | ||
| 11 | use vars qw($VERSION); | ||
| 12 | $VERSION = '0.10'; | ||
| 13 | |||
| 14 | |||
| 15 | sub spawn{ | ||
| 16 | |||
| 17 | my $package = shift; | ||
| 18 | croak "$package requires an even number of parameters" if @_ & 1; | ||
| 19 | my %args = @_; | ||
| 20 | |||
| 21 | my $alias = $args{Alias}; | ||
| 22 | my $mq_address = $args{mq_address}; | ||
| 23 | my $mq_port = $args{mq_port}; | ||
| 24 | my $queue = $args{queue}; | ||
| 25 | my $direction = $args{direction}; | ||
| 26 | my $user = $args{user}; | ||
| 27 | my $pass = $args{pass}; | ||
| 28 | my $rx_callback = $args{rx_callback}; | ||
| 29 | my $rcpt_callback = $args{rcpt_callback}; | ||
| 30 | my $err_callback = $args{err_callback}; | ||
| 31 | |||
| 32 | |||
| 33 | croak "Cannot init Stomp Client without valid Alias!" | ||
| 34 | unless $alias; | ||
| 35 | |||
| 36 | croak "Cannot init Stomp Client without valid IP and Port!" | ||
| 37 | unless ($mq_address && $mq_port); | ||
| 38 | |||
| 39 | croak "Direction must be OB or IB!" | ||
| 40 | unless ($direction =~ /^OB|IB$/); | ||
| 41 | |||
| 42 | croak "Cannot init IB/OB Stomp Client without a Queue!" | ||
| 43 | unless ($queue); | ||
| 44 | |||
| 45 | if ($direction eq 'IB') { | ||
| 46 | |||
| 47 | croak "If Stomp Client is IB you must define Message Handler Callback!" | ||
| 48 | unless ($rx_callback->{to_session} && $rx_callback->{to_handler}); | ||
| 49 | |||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | if ($direction eq 'OB') { | ||
| 54 | croak "If Stomp Client is OB you must define Receipt Handler Callback!" | ||
| 55 | unless ($rcpt_callback->{to_session} && $rcpt_callback->{to_handler}); | ||
| 56 | } | ||
| 57 | |||
| 58 | my $self = $package->SUPER::spawn( | ||
| 59 | Alias => $alias, | ||
| 60 | RemoteAddress => $mq_address, | ||
| 61 | RemotePort => $mq_port, | ||
| 62 | Queue => $queue, | ||
| 63 | User => $user, | ||
| 64 | Pass => $pass, | ||
| 65 | Direction => $direction, | ||
| 66 | RxCallback => $rx_callback, | ||
| 67 | RcptCallback => $rcpt_callback, | ||
| 68 | ErrCallback => $err_callback, | ||
| 69 | ); | ||
| 70 | |||
| 71 | # initialized flag (will be set to 1 uppon connected | ||
| 72 | $self->{stc_stat} = 0; | ||
| 73 | |||
| 74 | return $self; | ||
| 75 | |||
| 76 | } | ||
| 77 | |||
| 78 | # called when connected to the port | ||
| 79 | sub handle_connection { | ||
| 80 | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||
| 81 | |||
| 82 | my $nframe = $self->stomp->connect({ | ||
| 83 | login => $self->config('User'), | ||
| 84 | passcode => $self->config('Pass'), | ||
| 85 | }); | ||
| 86 | |||
| 87 | $kernel->post($self->config('Alias'), 'send_data', $nframe); | ||
| 88 | |||
| 89 | } | ||
| 90 | |||
| 91 | # called when CONNECT frame is recieved from stomp server | ||
| 92 | sub handle_connected { | ||
| 93 | my ($kernel, $self, $frame) = @_[KERNEL,OBJECT,ARG0]; | ||
| 94 | |||
| 95 | # marks stomp client as initialized | ||
| 96 | $self->{stc_stat} = 1; | ||
| 97 | |||
| 98 | # add the ACK_message object state | ||
| 99 | $kernel->state('ACK_message', $self); | ||
| 100 | |||
| 101 | # only IB stomp clients need to subscribe | ||
| 102 | if ($self->config('Direction') eq 'IB') { | ||
| 103 | |||
| 104 | my $nframe = $self->stomp->subscribe({ | ||
| 105 | destination => $self->config('Queue'), | ||
| 106 | ack => 'client' | ||
| 107 | }); | ||
| 108 | $kernel->post($self->config('Alias'), 'send_data', $nframe); | ||
| 109 | } | ||
| 110 | |||
| 111 | } | ||
| 112 | |||
| 113 | # Main Message Handler | ||
| 114 | sub handle_message { | ||
| 115 | my ($kernel, $self, $frame) = @_[KERNEL, OBJECT, ARG0]; | ||
| 116 | |||
| 117 | if ($self->config('Direction') eq 'IB') { | ||
| 118 | my $to_session = $self->config('RxCallback')->{to_session}; | ||
| 119 | my $to_handler = $self->config('RxCallback')->{to_handler}; | ||
| 120 | $kernel->post($to_session, $to_handler, $frame); | ||
| 121 | } | ||
| 122 | |||
| 123 | } | ||
| 124 | |||
| 125 | sub handle_error { | ||
| 126 | my ($kernel, $self, $frame) = @_[KERNEL,OBJECT,ARG0]; | ||
| 127 | |||
| 128 | #TODO: Error Handling | ||
| 129 | |||
| 130 | print STDERR "TODO: HANDLE ERROR"; | ||
| 131 | |||
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | sub handle_receipt { | ||
| 136 | my ($kernel, $self, $frame) = @_[KERNEL,OBJECT,ARG0]; | ||
| 137 | my $receipt = $frame->headers->{receipt}; | ||
| 138 | |||
| 139 | if ($self->config('Direction') eq 'OB') { | ||
| 140 | my $to_session = $self->config('RcptCallback')->{to_session}; | ||
| 141 | my $to_handler = $self->config('RcptCallback')->{to_handler}; | ||
| 142 | $kernel->post($to_session, $to_handler, $frame); | ||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 146 | } | ||
| 147 | |||
| 148 | sub ACK_message { | ||
| 149 | |||
| 150 | my ($kernel, $self, $message_id) = @_[KERNEL,OBJECT,ARG0]; | ||
| 151 | |||
| 152 | my $nframe = $self->stomp->ack({'message-id' => $message_id}); | ||
| 153 | $kernel->call($self->config('Alias'), 'send_data', $nframe); | ||
| 154 | |||
| 155 | } | ||
| 156 | |||
| 157 | |||
| 158 | sub log { | ||
| 159 | |||
| 160 | # Left to be overriden by implementation | ||
| 161 | |||
| 162 | } | ||
| 163 | |||
| 164 | |||
| 165 | # PoCo::Client::Stomp is not clear on what this is. From the doc: | ||
| 166 | #This event and corresponding method is used to "gather data". How | ||
| 167 | #that is done is up to your program. But usually a "send_data" event | ||
| 168 | #is generated. | ||
| 169 | sub gather_data { | ||
| 170 | warn "gather_data() - please report if you see this message"; | ||
| 171 | } | ||
| 172 | |||
| 173 | |||
| 174 | |||
| 175 | |||
| 176 | 1; | ||
| 177 | |||
| 178 | __END__ |