File Coverage

File:lib/ZMQx/Class.pm
Coverage:94.8%

linestmtbrancondsubtimecode
1package ZMQx::Class;
2
8
8
8
288898
13
157
use strict;
3
8
8
8
16
7
102
use warnings;
4
8
8
8
147
19
156
use 5.010;
5
8
8
8
1019
10
171
use ZMQx::Class::Socket;
6
8
8
8
23
5
343
use Carp qw(croak);
7
8our $VERSION = "0.002";
9# ABSTRACT: OO Interface to ZMQ
10my $__CONTEXT;
11
12
8
8
8
38
38
337
use ZMQ::LibZMQ3 qw(zmq_socket zmq_init);
13
8
8
8
17
7
119
use ZMQ::Constants qw(ZMQ_REQ ZMQ_REP ZMQ_DEALER ZMQ_ROUTER ZMQ_PULL ZMQ_PUSH ZMQ_PUB ZMQ_SUB ZMQ_XPUB ZMQ_XSUB ZMQ_PAIR);
14
15my %types = (
16    'REQ'=>ZMQ_REQ,
17    'REP'=>ZMQ_REP,
18    'DEALER'=>ZMQ_DEALER,
19    'ROUTER'=>ZMQ_ROUTER,
20    'PULL'=>ZMQ_PULL,
21    'PUSH'=>ZMQ_PUSH,
22    'PUB'=>ZMQ_PUB,
23    'SUB'=>ZMQ_SUB,
24    'XPUB'=>ZMQ_XPUB,
25    'XSUB'=>ZMQ_XSUB,
26    'PAIR'=>ZMQ_PAIR,
27);
28
29sub new_context {
30
8
10
    my $class = shift;
31
8
3303
    return zmq_init();
32}
33
34sub context {
35
8
1006159
    my $class = shift;
36
8
56
    return $__CONTEXT //= $class->new_context(@_);
37}
38
39sub socket {
40
52
58063
    my ($class, $context, $type, $connect, $address ) = @_;
41
52
153
    croak "no such socket type: $type" unless defined $types{$type};
42
50
3206
    my $socket = ZMQx::Class::Socket->new(
43        socket => zmq_socket($context,$types{$type}),
44        type => $type,
45    );
46
50
58652
    if ($connect && $address) {
47
26
63
        if ($connect eq 'bind') {
48
12
122
            $socket->bind($address);
49        }
50        elsif ($connect eq 'connect') {
51
12
119
            $socket->connect($address);
52        }
53        else {
54
2
34
            croak "no such connect type: $connect";
55        }
56    }
57
48
494
    return $socket;
58}
59
601;