| 1 | package Net::NATS::Client; | ||
| 2 | |||
| 3 | our $VERSION = '0.2.2'; | ||
| 4 | |||
| 5 | use IO::Select; | ||
| 6 | |||
| 7 | use Class::XSAccessor { | ||
| 8 | constructors => [ '_new' ], | ||
| 9 | accessors => [ | ||
| 10 | 'connection', | ||
| 11 | 'socket_args', | ||
| 12 | 'subscriptions', | ||
| 13 | 'uri', | ||
| 14 | ], | ||
| 15 | lvalue_accessors => [ | ||
| 16 | 'current_sid', | ||
| 17 | 'message_count', | ||
| 18 | ], | ||
| 19 | }; | ||
| 20 | |||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | |||
| 24 | use URI; | ||
| 25 | use JSON; | ||
| 26 | |||
| 27 | use Net::NATS::Connection; | ||
| 28 | use Net::NATS::Message; | ||
| 29 | use Net::NATS::ServerInfo; | ||
| 30 | use Net::NATS::ConnectInfo; | ||
| 31 | use Net::NATS::Subscription; | ||
| 32 | |||
| 33 | sub new { | ||
| 34 | my $class = shift; | ||
| 35 | |||
| 36 | my $self = $class->_new(@_); | ||
| 37 | $self->socket_args({}) unless defined $self->socket_args; | ||
| 38 | $self->subscriptions({}); | ||
| 39 | $self->current_sid = 0; | ||
| 40 | $self->message_count = 0; | ||
| 41 | |||
| 42 | return $self; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub connect { | ||
| 46 | my $self = shift; | ||
| 47 | |||
| 48 | my $uri = URI->new($self->uri) | ||
| 49 | or return; | ||
| 50 | |||
| 51 | $self->socket_args->{PeerAddr} = $uri->host; | ||
| 52 | $self->socket_args->{PeerPort} = $uri->port; | ||
| 53 | |||
| 54 | my $connection = Net::NATS::Connection->new(socket_args => $self->socket_args) | ||
| 55 | or return; | ||
| 56 | $self->connection($connection); | ||
| 57 | |||
| 58 | # Get INFO line | ||
| 59 | my ($op, @args) = $self->read_line; | ||
| 60 | my $info = $self->handle_info(@args); | ||
| 61 | |||
| 62 | my $connect_info = Net::NATS::ConnectInfo->new( | ||
| 63 | lang => 'perl', | ||
| 64 | version => $VERSION, | ||
| 65 | ); | ||
| 66 | |||
| 67 | if ($info->auth_required) { | ||
| 68 | if (!defined $uri->password) { | ||
| 69 | $connect_info->auth_token($uri->user); | ||
| 70 | } else { | ||
| 71 | $connect_info->user($uri->user); | ||
| 72 | $connect_info->pass($uri->password); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | if ($info->ssl_required || $info->tls_required) { | ||
| 77 | $connection->upgrade() | ||
| 78 | or return; | ||
| 79 | |||
| 80 | $self->connection($connection); | ||
| 81 | } | ||
| 82 | |||
| 83 | my $connect = 'CONNECT ' . to_json($connect_info, { convert_blessed => 1}); | ||
| 84 | $self->connection->send($connect); | ||
| 85 | |||
| 86 | return 1; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub subscribe { | ||
| 90 | my $self = shift; | ||
| 91 | |||
| 92 | my ($subject, $group, $callback); | ||
| 93 | |||
| 94 | if (@_ == 2) { | ||
| 95 | ($subject, $callback) = @_; | ||
| 96 | } else { | ||
| 97 | ($subject, $group, $callback) = @_; | ||
| 98 | } | ||
| 99 | |||
| 100 | my $sid = $self->next_sid; | ||
| 101 | |||
| 102 | my $sub = "SUB $subject"; | ||
| 103 | $sub .= " $group" if defined $group; | ||
| 104 | $sub .= " $sid"; | ||
| 105 | |||
| 106 | $self->connection->send($sub); | ||
| 107 | |||
| 108 | my $subscription = Net::NATS::Subscription->new( | ||
| 109 | subject => $subject, | ||
| 110 | group => $group, | ||
| 111 | sid => $sid, | ||
| 112 | callback => $callback, | ||
| 113 | client => $self, | ||
| 114 | ); | ||
| 115 | $self->subscriptions->{$sid} = $subscription; | ||
| 116 | return $subscription; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub unsubscribe { | ||
| 120 | my $self = shift; | ||
| 121 | my ($subscription, $max_msgs) = @_; | ||
| 122 | |||
| 123 | $subscription->max_msgs = $max_msgs; | ||
| 124 | my $sid = $subscription->sid; | ||
| 125 | my $sub = "UNSUB $sid"; | ||
| 126 | $sub .= " $max_msgs" if defined $max_msgs; | ||
| 127 | |||
| 128 | $self->connection->send($sub); | ||
| 129 | |||
| 130 | $self->_remove_subscription($subscription) | ||
| 131 | unless defined $max_msgs; | ||
| 132 | } | ||
| 133 | |||
| 134 | # 0:$self 1:$subject 2:$data 3:$reply_to | ||
| 135 | # Returns 1 on success, undef on failure | ||
| 136 | sub publish { | ||
| 137 | my $reply_to = defined $_[3] ? $_[3].' ' : ''; | ||
| 138 | return $_[0]->connection->send('PUB '.$_[1].' '.$reply_to.length($_[2])."\r\n".$_[2]); | ||
| 139 | } | ||
| 140 | |||
| 141 | sub request { | ||
| 142 | my ($self, $subject, $data, $callback) = @_; | ||
| 143 | |||
| 144 | my $inbox = new_inbox(); | ||
| 145 | my $sub = $self->subscribe($inbox, $callback); | ||
| 146 | $self->unsubscribe($sub, 1); | ||
| 147 | $self->publish($subject, $data, $inbox); | ||
| 148 | } | ||
| 149 | |||
| 150 | sub _remove_subscription { | ||
| 151 | my ($self, $subscription) = @_; | ||
| 152 | |||
| 153 | delete $self->subscriptions->{$subscription->sid}; | ||
| 154 | } | ||
| 155 | |||
| 156 | # blocking read built upon non-blocking read | ||
| 157 | sub read { | ||
| 158 | my ($self, $length) = @_; | ||
| 159 | |||
| 160 | my $data; | ||
| 161 | my $rv = $self->connection->nb_read($data, $length); | ||
| 162 | return unless $rv; # EOF or error | ||
| 163 | |||
| 164 | if ($rv eq '0E0') { | ||
| 165 | while ($rv eq '0E0' && $self->connection->can_read()) { # keep trying until we get the data we need. | ||
| 166 | $rv = $self->connection->nb_read($data,$length); | ||
| 167 | return unless $rv; # EOF or error. should report error somewhere... | ||
| 168 | } | ||
| 169 | return if $rv eq '0E0'; # got timeout from can_read | ||
| 170 | } | ||
| 171 | $data =~ s/\r\n$//; | ||
| 172 | return $data; | ||
| 173 | } | ||
| 174 | |||
| 175 | # non-blocking version of read_line. if no timeout passed, will block | ||
| 176 | sub read_line { | ||
| 177 | my ($self,$timeout) = @_; | ||
| 178 | my $line; | ||
| 179 | |||
| 180 | my $rv = $self->connection->nb_getline($line); | ||
| 181 | return unless $rv; # EOF or error | ||
| 182 | |||
| 183 | if ($rv eq '0E0') { # we do not have a full line | ||
| 184 | while ($rv eq '0E0' && $self->connection->can_read($timeout)) { | ||
| 185 | $rv = $self->connection->nb_getline($line); | ||
| 186 | return unless $rv; # EOF or error. should report error somewhere... | ||
| 187 | } | ||
| 188 | return if $rv eq '0E0'; # got timeout from can_read | ||
| 189 | } | ||
| 190 | $line =~ s/\r\n$//; | ||
| 191 | return split(' ', $line); | ||
| 192 | } | ||
| 193 | |||
| 194 | sub handle_info { | ||
| 195 | my $self = shift; | ||
| 196 | my (@args) = @_; | ||
| 197 | my $hash = decode_json($args[0]); | ||
| 198 | return Net::NATS::ServerInfo->new(%$hash); | ||
| 199 | } | ||
| 200 | |||
| 201 | sub parse_msg { | ||
| 202 | my $self = shift; | ||
| 203 | |||
| 204 | my ($subject, $sid, $length, $reply_to); | ||
| 205 | |||
| 206 | if (@_ == 3) { | ||
| 207 | ($subject, $sid, $length) = @_; | ||
| 208 | } else { | ||
| 209 | ($subject, $sid, $reply_to, $length) = @_; | ||
| 210 | } | ||
| 211 | |||
| 212 | my $data = $self->read($length+2); | ||
| 213 | my $subscription = $self->subscriptions->{$sid}; | ||
| 214 | my $message = Net::NATS::Message->new( | ||
| 215 | subject => $subject, | ||
| 216 | sid => $sid, | ||
| 217 | reply_to => $reply_to, | ||
| 218 | length => $length, | ||
| 219 | data => $data, | ||
| 220 | subscription => $subscription, | ||
| 221 | ); | ||
| 222 | |||
| 223 | $subscription->message_count++; | ||
| 224 | $self->message_count++; | ||
| 225 | |||
| 226 | if ($subscription->defined_max && $subscription->message_count >= $subscription->max_msgs) { | ||
| 227 | $self->_remove_subscription($subscription); | ||
| 228 | } | ||
| 229 | |||
| 230 | &{$subscription->callback}($message); | ||
| 231 | } | ||
| 232 | |||
| 233 | sub wait_for_op { | ||
| 234 | my $self = shift; | ||
| 235 | my $timeout = shift; # in seconds; can be fractional | ||
| 236 | |||
| 237 | my ($op, @args) = $self->read_line($timeout); | ||
| 238 | return unless defined $op; | ||
| 239 | |||
| 240 | if ($op eq 'MSG') { | ||
| 241 | $self->parse_msg(@args); | ||
| 242 | } elsif ($op eq 'PING') { | ||
| 243 | $self->handle_ping; | ||
| 244 | } elsif ($op eq 'PONG') { | ||
| 245 | } elsif ($op eq '+OK') { | ||
| 246 | } elsif ($op eq '-ERR') { | ||
| 247 | return; | ||
| 248 | } | ||
| 249 | return 1; | ||
| 250 | } | ||
| 251 | |||
| 252 | sub handle_ping { | ||
| 253 | my $self = shift; | ||
| 254 | $self->connection->send("PONG"); | ||
| 255 | } | ||
| 256 | |||
| 257 | sub next_sid { | ||
| 258 | my $self = shift; | ||
| 259 | return ++$self->current_sid; | ||
| 260 | } | ||
| 261 | |||
| 262 | sub close { | ||
| 263 | my $self = shift; | ||
| 264 | $self->connection->_socket->close; | ||
| 265 | } | ||
| 266 | |||
| 267 | sub new_inbox { sprintf("_INBOX.%08X%08X%06X", rand(2**32), rand(2**32), rand(2**24)); } | ||
| 268 | |||
| 269 | 1; | ||
| 270 | |||
| 271 | __END__ |