pgm_sender.cpp 5.98 KB
Newer Older
malosek's avatar
malosek committed
1
/*
2 3
    Copyright (c) 2007-2011 iMatix Corporation
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
malosek's avatar
malosek committed
4 5 6 7

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
8
    the terms of the GNU Lesser General Public License as published by
malosek's avatar
malosek committed
9 10 11 12 13 14
    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
15
    GNU Lesser General Public License for more details.
malosek's avatar
malosek committed
16

17
    You should have received a copy of the GNU Lesser General Public License
malosek's avatar
malosek committed
18 19 20 21 22
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "platform.hpp"

malosek's avatar
malosek committed
23
#if defined ZMQ_HAVE_OPENPGM
malosek's avatar
malosek committed
24

25 26 27 28
#ifdef ZMQ_HAVE_WINDOWS
#include "windows.hpp"
#endif

Martin Sustrik's avatar
Martin Sustrik committed
29
#include <stdlib.h>
malosek's avatar
malosek committed
30 31 32

#include "io_thread.hpp"
#include "pgm_sender.hpp"
33
#include "session_base.hpp"
malosek's avatar
malosek committed
34 35
#include "err.hpp"
#include "wire.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
36
#include "stdint.hpp"
malosek's avatar
malosek committed
37 38

zmq::pgm_sender_t::pgm_sender_t (io_thread_t *parent_, 
39
      const options_t &options_) :
malosek's avatar
malosek committed
40
    io_object_t (parent_),
41 42
    has_tx_timer (false),
    has_rx_timer (false),
43
    encoder (0),
malosek's avatar
malosek committed
44 45 46 47
    pgm_socket (false, options_),
    options (options_),
    out_buffer (NULL),
    out_buffer_size (0),
Martin Sustrik's avatar
Martin Sustrik committed
48
    write_size (0)
malosek's avatar
malosek committed
49 50 51
{
}

52
int zmq::pgm_sender_t::init (bool udp_encapsulation_, const char *network_)
malosek's avatar
malosek committed
53
{
Martin Sustrik's avatar
Martin Sustrik committed
54 55 56 57 58 59
    int rc = pgm_socket.init (udp_encapsulation_, network_);
    if (rc != 0)
        return rc;

    out_buffer_size = pgm_socket.get_max_tsdu_size ();
    out_buffer = (unsigned char*) malloc (out_buffer_size);
60
    alloc_assert (out_buffer);
61 62

    return rc;
malosek's avatar
malosek committed
63 64
}

65
void zmq::pgm_sender_t::plug (io_thread_t *io_thread_, session_base_t *session_)
malosek's avatar
malosek committed
66 67
{
    //  Alocate 2 fds for PGM socket.
68 69 70 71
    fd_t downlink_socket_fd = retired_fd;
    fd_t uplink_socket_fd = retired_fd;
    fd_t rdata_notify_fd = retired_fd;
    fd_t pending_notify_fd = retired_fd;
malosek's avatar
malosek committed
72

73
    encoder.set_session (session_);
malosek's avatar
malosek committed
74

Martin Sustrik's avatar
Martin Sustrik committed
75 76
    //  Fill fds from PGM transport and add them to the poller.
    pgm_socket.get_sender_fds (&downlink_socket_fd, &uplink_socket_fd,
77 78
        &rdata_notify_fd, &pending_notify_fd);

malosek's avatar
malosek committed
79 80
    handle = add_fd (downlink_socket_fd);
    uplink_handle = add_fd (uplink_socket_fd);
malosek's avatar
malosek committed
81
    rdata_notify_handle = add_fd (rdata_notify_fd);   
82
    pending_notify_handle = add_fd (pending_notify_fd);
malosek's avatar
malosek committed
83

malosek's avatar
malosek committed
84 85 86
    //  Set POLLIN. We wont never want to stop polling for uplink = we never
    //  want to stop porocess NAKs.
    set_pollin (uplink_handle);
malosek's avatar
malosek committed
87
    set_pollin (rdata_notify_handle);
88
    set_pollin (pending_notify_handle);
malosek's avatar
malosek committed
89 90 91

    //  Set POLLOUT for downlink_socket_handle.
    set_pollout (handle);
92 93 94 95 96

    //  PGM is not able to pass subscriptions upstream, thus we have no idea
    //  what messages are peers interested in. Because of that we have to
    //  subscribe for all the messages.
    msg_t msg;
97 98
    msg.init_size (1);
    *(unsigned char*) msg.data () = 1;
99
    bool ok = session_->write (&msg);
100
    zmq_assert (ok);
101
    session_->flush ();
malosek's avatar
malosek committed
102 103 104 105
}

void zmq::pgm_sender_t::unplug ()
{
106 107 108 109 110 111 112 113 114 115
    if (has_rx_timer) {
        cancel_timer (rx_timer_id);
        has_rx_timer = false;
    }

    if (has_tx_timer) {
        cancel_timer (tx_timer_id);
        has_tx_timer = false;
    }

malosek's avatar
malosek committed
116 117
    rm_fd (handle);
    rm_fd (uplink_handle);
malosek's avatar
malosek committed
118
    rm_fd (rdata_notify_handle);
119
    rm_fd (pending_notify_handle);
120
    encoder.set_session (NULL);
malosek's avatar
malosek committed
121 122
}

123 124 125 126 127 128 129
void zmq::pgm_sender_t::terminate ()
{
    unplug ();
    delete this;
}

void zmq::pgm_sender_t::activate_out ()
malosek's avatar
malosek committed
130 131
{
    set_pollout (handle);
132
    out_event ();
malosek's avatar
malosek committed
133 134
}

135
void zmq::pgm_sender_t::activate_in ()
Martin Hurton's avatar
Martin Hurton committed
136 137 138 139
{
    zmq_assert (false);
}

malosek's avatar
malosek committed
140 141 142
zmq::pgm_sender_t::~pgm_sender_t ()
{
    if (out_buffer) {
Martin Sustrik's avatar
Martin Sustrik committed
143
        free (out_buffer);
malosek's avatar
malosek committed
144
        out_buffer = NULL;
malosek's avatar
malosek committed
145 146 147 148 149
    }
}

void zmq::pgm_sender_t::in_event ()
{
150 151 152 153 154
    if (has_rx_timer) {
        cancel_timer (rx_timer_id);
        has_rx_timer = false;
    }

155
    //  In-event on sender side means NAK or SPMR receiving from some peer.
malosek's avatar
malosek committed
156
    pgm_socket.process_upstream ();
157
    if (errno == ENOMEM || errno == EBUSY) {
158 159 160 161
        const long timeout = pgm_socket.get_rx_timeout ();
        add_timer (timeout, rx_timer_id);
        has_rx_timer = true;
    }
malosek's avatar
malosek committed
162 163 164 165 166 167
}

void zmq::pgm_sender_t::out_event ()
{
    //  POLLOUT event from send socket. If write buffer is empty, 
    //  try to read new data from the encoder.
Martin Sustrik's avatar
Martin Sustrik committed
168
    if (write_size == 0) {
malosek's avatar
malosek committed
169

Martin Sustrik's avatar
Martin Sustrik committed
170 171 172
        //  First two bytes (sizeof uint16_t) are used to store message 
        //  offset in following steps. Note that by passing our buffer to
        //  the get data function we prevent it from returning its own buffer.
Martin Sustrik's avatar
Martin Sustrik committed
173
        unsigned char *bf = out_buffer + sizeof (uint16_t);
Martin Sustrik's avatar
Martin Sustrik committed
174 175 176
        size_t bfsz = out_buffer_size - sizeof (uint16_t);
        int offset = -1;
        encoder.get_data (&bf, &bfsz, &offset);
malosek's avatar
malosek committed
177 178

        //  If there are no data to write stop polling for output.
Martin Sustrik's avatar
Martin Sustrik committed
179
        if (!bfsz) {
malosek's avatar
malosek committed
180
            reset_pollout (handle);
Martin Sustrik's avatar
Martin Sustrik committed
181
            return;
malosek's avatar
malosek committed
182 183
        }

Martin Sustrik's avatar
Martin Sustrik committed
184 185 186
        //  Put offset information in the buffer.
        write_size = bfsz + sizeof (uint16_t);
        put_uint16 (out_buffer, offset == -1 ? 0xffff : (uint16_t) offset);
malosek's avatar
malosek committed
187 188
    }

189 190 191
    if (has_tx_timer) {
        cancel_timer (tx_timer_id);
        has_tx_timer = false;
192 193
    }

Martin Sustrik's avatar
Martin Sustrik committed
194 195
    //  Send the data.
    size_t nbytes = pgm_socket.send (out_buffer, write_size);
malosek's avatar
malosek committed
196

Martin Sustrik's avatar
Martin Sustrik committed
197
    //  We can write either all data or 0 which means rate limit reached.
198
    if (nbytes == write_size) {
Martin Sustrik's avatar
Martin Sustrik committed
199
        write_size = 0;
200
    } else {
Martin Sustrik's avatar
Martin Sustrik committed
201
        zmq_assert (nbytes == 0);
202 203 204 205 206 207 208 209 210 211 212 213

        if (errno == ENOMEM) {
            const long timeout = pgm_socket.get_tx_timeout ();
            add_timer (timeout, tx_timer_id);
            has_tx_timer = true;
        } else
            zmq_assert (errno == EBUSY);
    }
}

void zmq::pgm_sender_t::timer_event (int token)
{
214 215 216
    //  Timer cancels on return by poller_base.
    if (token == rx_timer_id) {
        has_rx_timer = false;
217
        in_event ();
218 219 220 221 222
    } else if (token == tx_timer_id) {
        has_tx_timer = false;
        out_event ();
    } else
        zmq_assert (false);
malosek's avatar
malosek committed
223
}
Martin Sustrik's avatar
Martin Sustrik committed
224

malosek's avatar
malosek committed
225 226
#endif