dealer.cpp 2.84 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2014 Contributors as noted in the AUTHORS file
3 4 5 6

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
7
    the terms of the GNU Lesser General Public License as published by
8 9 10 11 12 13
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    0MQ is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
    GNU Lesser General Public License for more details.
15

16
    You should have received a copy of the GNU Lesser General Public License
17 18 19
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

20
#include "dealer.hpp"
21
#include "err.hpp"
22
#include "msg.hpp"
23

24
zmq::dealer_t::dealer_t (class ctx_t *parent_, uint32_t tid_, int sid_) :
25
    socket_base_t (parent_, tid_, sid_),
26
    probe_router (false)
27
{
28
    options.type = ZMQ_DEALER;
29 30
}

31
zmq::dealer_t::~dealer_t ()
32 33 34
{
}

35
void zmq::dealer_t::xattach_pipe (pipe_t *pipe_, bool subscribe_to_all_)
36
{
37 38
    // subscribe_to_all_ is unused
    (void) subscribe_to_all_;
39

40
    zmq_assert (pipe_);
41

42
    if (probe_router) {
43
        msg_t probe_msg_;
44
        int rc = probe_msg_.init ();
45 46
        errno_assert (rc == 0);

47 48
        rc = pipe_->write (&probe_msg_);
        // zmq_assert (rc) is not applicable here, since it is not a bug.
49 50 51 52 53 54
        pipe_->flush ();

        rc = probe_msg_.close ();
        errno_assert (rc == 0);
    }

55 56
    fq.attach (pipe_);
    lb.attach (pipe_);
57 58
}

59 60 61 62 63 64 65
int zmq::dealer_t::xsetsockopt (int option_, const void *optval_,
    size_t optvallen_)
{
    bool is_int = (optvallen_ == sizeof (int));
    int value = is_int? *((int *) optval_): 0;

    switch (option_) {
66
        case ZMQ_PROBE_ROUTER:
67
            if (is_int && value >= 0) {
68
                probe_router = (value != 0);
69 70 71 72 73 74 75 76 77 78 79 80
                return 0;
            }
            break;

        default:
            break;
    }

    errno = EINVAL;
    return -1;
}

81
int zmq::dealer_t::xsend (msg_t *msg_)
82
{
83
    return sendpipe (msg_, NULL);
84 85
}

86
int zmq::dealer_t::xrecv (msg_t *msg_)
87
{
88
    return recvpipe (msg_, NULL);
89 90
}

91
bool zmq::dealer_t::xhas_in ()
92
{
93
    return fq.has_in ();
94 95
}

96
bool zmq::dealer_t::xhas_out ()
97
{
98
    return lb.has_out ();
99 100
}

101 102 103 104 105 106
zmq::blob_t zmq::dealer_t::get_credential () const
{
    return fq.get_credential ();
}


107
void zmq::dealer_t::xread_activated (pipe_t *pipe_)
108 109 110 111
{
    fq.activated (pipe_);
}

112
void zmq::dealer_t::xwrite_activated (pipe_t *pipe_)
113 114 115 116
{
    lb.activated (pipe_);
}

117
void zmq::dealer_t::xpipe_terminated (pipe_t *pipe_)
118
{
119 120
    fq.pipe_terminated (pipe_);
    lb.pipe_terminated (pipe_);
121
}
122 123 124 125 126 127 128 129 130 131

int zmq::dealer_t::sendpipe (msg_t *msg_, pipe_t **pipe_)
{
    return lb.sendpipe (msg_, pipe_);
}

int zmq::dealer_t::recvpipe (msg_t *msg_, pipe_t **pipe_)
{
    return fq.recvpipe (msg_, pipe_);
}