libzmq master
The Intelligent Transport Layer

sub.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (c) 2009-2011 250bpm s.r.o.
00003     Copyright (c) 2007-2009 iMatix Corporation
00004     Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
00005 
00006     This file is part of 0MQ.
00007 
00008     0MQ is free software; you can redistribute it and/or modify it under
00009     the terms of the GNU Lesser General Public License as published by
00010     the Free Software Foundation; either version 3 of the License, or
00011     (at your option) any later version.
00012 
00013     0MQ is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU Lesser General Public License for more details.
00017 
00018     You should have received a copy of the GNU Lesser General Public License
00019     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020 */
00021 
00022 #include "sub.hpp"
00023 #include "msg.hpp"
00024 
00025 zmq::sub_t::sub_t (class ctx_t *parent_, uint32_t tid_) :
00026     xsub_t (parent_, tid_)
00027 {
00028     options.type = ZMQ_SUB;
00029 
00030     //  Switch filtering messages on (as opposed to XSUB which where the
00031     //  filtering is off).
00032     options.filter = true;
00033 }
00034 
00035 zmq::sub_t::~sub_t ()
00036 {
00037 }
00038 
00039 int zmq::sub_t::xsetsockopt (int option_, const void *optval_,
00040     size_t optvallen_)
00041 {
00042     if (option_ != ZMQ_SUBSCRIBE && option_ != ZMQ_UNSUBSCRIBE) {
00043         errno = EINVAL;
00044         return -1;
00045     }
00046 
00047     //  Create the subscription message.
00048     msg_t msg;
00049     int rc = msg.init_size (optvallen_ + 1);
00050     errno_assert (rc == 0);
00051     unsigned char *data = (unsigned char*) msg.data ();
00052     if (option_ == ZMQ_SUBSCRIBE)
00053         *data = 1;
00054     else if (option_ == ZMQ_UNSUBSCRIBE)
00055         *data = 0;
00056     memcpy (data + 1, optval_, optvallen_);
00057 
00058     //  Pass it further on in the stack.
00059     int err = 0;
00060     rc = xsub_t::xsend (&msg, 0);
00061     if (rc != 0)
00062         err = errno;
00063     int rc2 = msg.close ();
00064     errno_assert (rc2 == 0);
00065     if (rc != 0)
00066         errno = err;
00067     return rc;
00068 }
00069 
00070 int zmq::sub_t::xsend (msg_t *msg_, int flags_)
00071 {
00072     //  Overload the XSUB's send.
00073     errno = ENOTSUP;
00074     return -1;
00075 }
00076 
00077 bool zmq::sub_t::xhas_out ()
00078 {
00079     //  Overload the XSUB's send.
00080     return false;
00081 }
00082 
00083 zmq::sub_session_t::sub_session_t (io_thread_t *io_thread_, bool connect_,
00084       socket_base_t *socket_, const options_t &options_,
00085       const char *protocol_, const char *address_) :
00086     xsub_session_t (io_thread_, connect_, socket_, options_, protocol_,
00087         address_)
00088 {
00089 }
00090 
00091 zmq::sub_session_t::~sub_session_t ()
00092 {
00093 }
00094 
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines