pipe.cpp 4.14 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2010 iMatix Corporation
Martin Sustrik's avatar
Martin Sustrik committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
    the terms of the Lesser GNU General Public License as published by
    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
    Lesser GNU General Public License for more details.

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

20
#include "../bindings/c/zmq.h"
Martin Sustrik's avatar
Martin Sustrik committed
21

Martin Sustrik's avatar
Martin Sustrik committed
22 23
#include "pipe.hpp"

unknown's avatar
unknown committed
24
zmq::reader_t::reader_t (object_t *parent_,
Martin Sustrik's avatar
Martin Sustrik committed
25 26
      uint64_t hwm_, uint64_t lwm_) :
    object_t (parent_),
unknown's avatar
unknown committed
27 28
    pipe (NULL),
    peer (NULL),
Martin Sustrik's avatar
Martin Sustrik committed
29 30 31 32 33 34 35 36
    hwm (hwm_),
    lwm (lwm_),
    endpoint (NULL)
{
}

zmq::reader_t::~reader_t ()
{
37 38
    if (pipe)
        unregister_pipe (pipe);
Martin Sustrik's avatar
Martin Sustrik committed
39 40
}

unknown's avatar
unknown committed
41 42 43 44
void zmq::reader_t::set_pipe (pipe_t *pipe_)
{
    zmq_assert (!pipe);
    pipe = pipe_;
45 46
    peer = &pipe->writer;
    register_pipe (pipe);
unknown's avatar
unknown committed
47 48
}

49 50 51 52 53 54 55 56 57 58 59
bool zmq::reader_t::check_read ()
{
    //  Check if there's an item in the pipe.
    if (pipe->check_read ())
        return true;

    //  If not, deactivate the pipe.
    endpoint->kill (this);
    return false;
}

Martin Sustrik's avatar
Martin Sustrik committed
60 61
bool zmq::reader_t::read (zmq_msg_t *msg_)
{
62 63
    if (!pipe->read (msg_)) {
        endpoint->kill (this);
Martin Sustrik's avatar
Martin Sustrik committed
64
        return false;
65
    }
Martin Sustrik's avatar
Martin Sustrik committed
66 67 68 69 70 71 72 73 74

    //  If delimiter was read, start termination process of the pipe.
    unsigned char *offset = 0;
    if (msg_->content == (void*) (offset + ZMQ_DELIMITER)) {
        if (endpoint)
            endpoint->detach_inpipe (this);
        term ();
        return false;
    }
Martin Sustrik's avatar
Martin Sustrik committed
75 76

    //  TODO: Adjust the size of the pipe.
Martin Sustrik's avatar
Martin Sustrik committed
77 78

    return true;
Martin Sustrik's avatar
Martin Sustrik committed
79 80 81 82 83 84 85
}

void zmq::reader_t::set_endpoint (i_endpoint *endpoint_)
{
    endpoint = endpoint_;
}

Martin Sustrik's avatar
Martin Sustrik committed
86 87 88 89 90 91
void zmq::reader_t::term ()
{
    endpoint = NULL;
    send_pipe_term (peer);
}

Martin Sustrik's avatar
Martin Sustrik committed
92 93
void zmq::reader_t::process_revive ()
{
94 95 96 97 98
    //  Beacuse of command throttling mechanism, incoming termination request
    //  may not have been processed before subsequent send.
    //  In that case endpoint is NULL.
    if (endpoint)
        endpoint->revive (this);
Martin Sustrik's avatar
Martin Sustrik committed
99 100
}

Martin Sustrik's avatar
Martin Sustrik committed
101 102 103 104 105 106
void zmq::reader_t::process_pipe_term_ack ()
{
    peer = NULL;
    delete pipe;
}

unknown's avatar
unknown committed
107
zmq::writer_t::writer_t (object_t *parent_,
Martin Sustrik's avatar
Martin Sustrik committed
108 109
      uint64_t hwm_, uint64_t lwm_) :
    object_t (parent_),
unknown's avatar
unknown committed
110 111
    pipe (NULL),
    peer (NULL),
Martin Sustrik's avatar
Martin Sustrik committed
112
    hwm (hwm_),
Martin Sustrik's avatar
Martin Sustrik committed
113 114 115 116 117 118 119 120 121 122
    lwm (lwm_),
    endpoint (NULL)
{
}

void zmq::writer_t::set_endpoint (i_endpoint *endpoint_)
{
    endpoint = endpoint_;
}

Martin Sustrik's avatar
Martin Sustrik committed
123 124 125 126
zmq::writer_t::~writer_t ()
{
}

unknown's avatar
unknown committed
127 128 129 130
void zmq::writer_t::set_pipe (pipe_t *pipe_)
{
    zmq_assert (!pipe);
    pipe = pipe_;
131
    peer = &pipe->reader;
unknown's avatar
unknown committed
132 133
}

Martin Sustrik's avatar
Martin Sustrik committed
134 135 136 137 138 139 140
bool zmq::writer_t::check_write (uint64_t size_)
{
    //  TODO: Check whether hwm is exceeded.

    return true;
}

141
bool zmq::writer_t::write (zmq_msg_t *msg_)
Martin Sustrik's avatar
Martin Sustrik committed
142 143 144 145 146 147 148 149 150 151 152 153 154
{
    pipe->write (*msg_);
    return true;

    //  TODO: Adjust size of the pipe.
}

void zmq::writer_t::flush ()
{
    if (!pipe->flush ())
        send_revive (peer);
}

Martin Sustrik's avatar
Martin Sustrik committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
void zmq::writer_t::term ()
{
    endpoint = NULL;

    //  Push delimiter into the pipe.
    //  Trick the compiler to belive that the tag is a valid pointer.
    zmq_msg_t msg;
    const unsigned char *offset = 0;
    msg.content = (void*) (offset + ZMQ_DELIMITER);
    msg.shared = false;
    pipe->write (msg);
    pipe->flush ();
}

void zmq::writer_t::process_pipe_term ()
{
    if (endpoint)
        endpoint->detach_outpipe (this);

    reader_t *p = peer;
    peer = NULL;
    send_pipe_term_ack (p);
}

Martin Sustrik's avatar
Martin Sustrik committed
179 180
zmq::pipe_t::pipe_t (object_t *reader_parent_, object_t *writer_parent_,
      uint64_t hwm_, uint64_t lwm_) :
unknown's avatar
unknown committed
181 182
    reader (reader_parent_, hwm_, lwm_),
    writer (writer_parent_, hwm_, lwm_)
Martin Sustrik's avatar
Martin Sustrik committed
183
{
unknown's avatar
unknown committed
184 185
    reader.set_pipe (this);
    writer.set_pipe (this);
Martin Sustrik's avatar
Martin Sustrik committed
186 187 188 189
}

zmq::pipe_t::~pipe_t ()
{
Martin Sustrik's avatar
Martin Sustrik committed
190 191 192 193 194 195
    //  Deallocate all the unread messages in the pipe. We have to do it by
    //  hand because zmq_msg_t is a POD, not a class, so there's no associated
    //  destructor.
    zmq_msg_t msg;
    while (read (&msg))
       zmq_msg_close (&msg);
Martin Sustrik's avatar
Martin Sustrik committed
196
}