~ubuntu-branches/ubuntu/saucy/zeromq3/saucy

« back to all changes in this revision

Viewing changes to src/sub.cpp

  • Committer: Package Import Robot
  • Author(s): Alessandro Ghedini
  • Date: 2012-06-04 21:21:09 UTC
  • Revision ID: package-import@ubuntu.com-20120604212109-b7b3m0rn21o8oo2q
Tags: upstream-3.1.0~beta+dfsg
ImportĀ upstreamĀ versionĀ 3.1.0~beta+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (c) 2009-2011 250bpm s.r.o.
 
3
    Copyright (c) 2007-2009 iMatix Corporation
 
4
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
 
5
 
 
6
    This file is part of 0MQ.
 
7
 
 
8
    0MQ is free software; you can redistribute it and/or modify it under
 
9
    the terms of the GNU Lesser General Public License as published by
 
10
    the Free Software Foundation; either version 3 of the License, or
 
11
    (at your option) any later version.
 
12
 
 
13
    0MQ is distributed in the hope that it will be useful,
 
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
    GNU Lesser General Public License for more details.
 
17
 
 
18
    You should have received a copy of the GNU Lesser General Public License
 
19
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
*/
 
21
 
 
22
#include "sub.hpp"
 
23
#include "msg.hpp"
 
24
 
 
25
zmq::sub_t::sub_t (class ctx_t *parent_, uint32_t tid_) :
 
26
    xsub_t (parent_, tid_)
 
27
{
 
28
    options.type = ZMQ_SUB;
 
29
 
 
30
    //  Switch filtering messages on (as opposed to XSUB which where the
 
31
    //  filtering is off).
 
32
    options.filter = true;
 
33
}
 
34
 
 
35
zmq::sub_t::~sub_t ()
 
36
{
 
37
}
 
38
 
 
39
int zmq::sub_t::xsetsockopt (int option_, const void *optval_,
 
40
    size_t optvallen_)
 
41
{
 
42
    if (option_ != ZMQ_SUBSCRIBE && option_ != ZMQ_UNSUBSCRIBE) {
 
43
        errno = EINVAL;
 
44
        return -1;
 
45
    }
 
46
 
 
47
    //  Create the subscription message.
 
48
    msg_t msg;
 
49
    int rc = msg.init_size (optvallen_ + 1);
 
50
    errno_assert (rc == 0);
 
51
    unsigned char *data = (unsigned char*) msg.data ();
 
52
    if (option_ == ZMQ_SUBSCRIBE)
 
53
        *data = 1;
 
54
    else if (option_ == ZMQ_UNSUBSCRIBE)
 
55
        *data = 0;
 
56
    memcpy (data + 1, optval_, optvallen_);
 
57
 
 
58
    //  Pass it further on in the stack.
 
59
    int err = 0;
 
60
    rc = xsub_t::xsend (&msg, 0);
 
61
    if (rc != 0)
 
62
        err = errno;
 
63
    int rc2 = msg.close ();
 
64
    errno_assert (rc2 == 0);
 
65
    if (rc != 0)
 
66
        errno = err;
 
67
    return rc;
 
68
}
 
69
 
 
70
int zmq::sub_t::xsend (msg_t *msg_, int flags_)
 
71
{
 
72
    //  Overload the XSUB's send.
 
73
    errno = ENOTSUP;
 
74
    return -1;
 
75
}
 
76
 
 
77
bool zmq::sub_t::xhas_out ()
 
78
{
 
79
    //  Overload the XSUB's send.
 
80
    return false;
 
81
}
 
82
 
 
83
zmq::sub_session_t::sub_session_t (io_thread_t *io_thread_, bool connect_,
 
84
      socket_base_t *socket_, const options_t &options_,
 
85
      const char *protocol_, const char *address_) :
 
86
    xsub_session_t (io_thread_, connect_, socket_, options_, protocol_,
 
87
        address_)
 
88
{
 
89
}
 
90
 
 
91
zmq::sub_session_t::~sub_session_t ()
 
92
{
 
93
}
 
94