| 1 | package Exobrain::Router; | ||
| 2 | use v5.10.0; | ||
| 3 | use Moose; | ||
| 4 | use warnings; | ||
| 5 | use Method::Signatures; | ||
| 6 | use Carp qw(croak); | ||
| 7 | use ZMQ; | ||
| 8 | use ZMQ::Constants qw(ZMQ_SUB ZMQ_SUBSCRIBE ZMQ_PUB ZMQ_FORWARDER); | ||
| 9 | |||
| 10 | # ABSTRACT: Central hub for the exobrain bus | ||
| 11 | our $VERSION = '1.08'; # VERSION | ||
| 12 | |||
| 13 | |||
| 14 | # TODO: Don't use hard-coded sockets. Seriously. Ugh. | ||
| 15 | |||
| 16 | has subscriber => (is => 'ro', default => 'tcp://127.0.0.1:3546'); # Subscribers connect here | ||
| 17 | has publisher => (is => 'ro', default => 'tcp://127.0.0.1:3547'); # Publishers connect here | ||
| 18 | has server => (is => 'ro', isa => 'Bool', default => 0); | ||
| 19 | |||
| 20 | # Clients should never call start. | ||
| 21 | |||
| 22 | method start() { | ||
| 23 | |||
| 24 | # Most things SHOULDN'T be trying to start this. So we'll | ||
| 25 | # check if we were built with the server flag to prevent | ||
| 26 | # accidental starting. | ||
| 27 | |||
| 28 | if (not $self->server) { | ||
| 29 | croak "Attempt to start non-server."; | ||
| 30 | } | ||
| 31 | |||
| 32 | my $zmq = ZMQ::Context->new; | ||
| 33 | my $sub = $zmq->socket(ZMQ_SUB); | ||
| 34 | $sub->bind($self->publisher); | ||
| 35 | $sub->setsockopt(ZMQ_SUBSCRIBE, ''); # Sub everything | ||
| 36 | |||
| 37 | my $pub = $zmq->socket(ZMQ_PUB); | ||
| 38 | $pub->bind($self->subscriber); | ||
| 39 | |||
| 40 | # Breaking encapsulation! Eeew! | ||
| 41 | ZMQ::call('zmq_device', ZMQ_FORWARDER, $pub->{_socket}, $sub->{_socket}); | ||
| 42 | } | ||
| 43 | |||
| 44 | |||
| 45 | 1; | ||
| 46 | |||
| 47 | __END__ |