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

    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/>.
*/

#include "platform.hpp"

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

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

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

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

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

48
int zmq::pgm_sender_t::init (bool udp_encapsulation_, const char *network_)
malosek's avatar
malosek committed
49
{
Martin Sustrik's avatar
Martin Sustrik committed
50 51 52 53 54 55 56
    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);
    zmq_assert (out_buffer);
57 58

    return rc;
malosek's avatar
malosek committed
59 60 61 62 63
}

void zmq::pgm_sender_t::plug (i_inout *inout_)
{
    //  Alocate 2 fds for PGM socket.
malosek's avatar
malosek committed
64 65 66
    int downlink_socket_fd = 0;
    int uplink_socket_fd = 0;
    int rdata_notify_fd = 0;
67
    int pending_notify_fd = 0;
malosek's avatar
malosek committed
68 69 70

    encoder.set_inout (inout_);

Martin Sustrik's avatar
Martin Sustrik committed
71 72
    //  Fill fds from PGM transport and add them to the poller.
    pgm_socket.get_sender_fds (&downlink_socket_fd, &uplink_socket_fd,
73 74
        &rdata_notify_fd, &pending_notify_fd);

malosek's avatar
malosek committed
75 76
    handle = add_fd (downlink_socket_fd);
    uplink_handle = add_fd (uplink_socket_fd);
malosek's avatar
malosek committed
77
    rdata_notify_handle = add_fd (rdata_notify_fd);   
78
    pending_notify_handle = add_fd (pending_notify_fd);
malosek's avatar
malosek committed
79

malosek's avatar
malosek committed
80 81 82
    //  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
83
    set_pollin (rdata_notify_handle);
84
    set_pollin (pending_notify_handle);
malosek's avatar
malosek committed
85 86 87 88 89 90 91 92 93

    //  Set POLLOUT for downlink_socket_handle.
    set_pollout (handle);
}

void zmq::pgm_sender_t::unplug ()
{
    rm_fd (handle);
    rm_fd (uplink_handle);
malosek's avatar
malosek committed
94
    rm_fd (rdata_notify_handle);
95
    rm_fd (pending_notify_handle);
malosek's avatar
malosek committed
96 97 98 99 100 101
    encoder.set_inout (NULL);
}

void zmq::pgm_sender_t::revive ()
{
    set_pollout (handle);
102
    out_event ();
malosek's avatar
malosek committed
103 104 105 106 107
}

zmq::pgm_sender_t::~pgm_sender_t ()
{
    if (out_buffer) {
Martin Sustrik's avatar
Martin Sustrik committed
108
        free (out_buffer);
malosek's avatar
malosek committed
109
        out_buffer = NULL;
malosek's avatar
malosek committed
110 111 112 113 114
    }
}

void zmq::pgm_sender_t::in_event ()
{
Martin Sustrik's avatar
Martin Sustrik committed
115
    //  In event on sender side means NAK or SPMR receiving from some peer.
malosek's avatar
malosek committed
116 117 118 119 120 121 122
    pgm_socket.process_upstream ();
}

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
123
    if (write_size == 0) {
malosek's avatar
malosek committed
124

Martin Sustrik's avatar
Martin Sustrik committed
125 126 127
        //  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
128
        unsigned char *bf = out_buffer + sizeof (uint16_t);
Martin Sustrik's avatar
Martin Sustrik committed
129 130 131
        size_t bfsz = out_buffer_size - sizeof (uint16_t);
        int offset = -1;
        encoder.get_data (&bf, &bfsz, &offset);
malosek's avatar
malosek committed
132 133

        //  If there are no data to write stop polling for output.
Martin Sustrik's avatar
Martin Sustrik committed
134
        if (!bfsz) {
malosek's avatar
malosek committed
135
            reset_pollout (handle);
Martin Sustrik's avatar
Martin Sustrik committed
136
            return;
malosek's avatar
malosek committed
137 138
        }

Martin Sustrik's avatar
Martin Sustrik committed
139 140 141
        //  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
142 143
    }

Martin Sustrik's avatar
Martin Sustrik committed
144 145
    //  Send the data.
    size_t nbytes = pgm_socket.send (out_buffer, write_size);
malosek's avatar
malosek committed
146

Martin Sustrik's avatar
Martin Sustrik committed
147 148 149 150 151
    //  We can write either all data or 0 which means rate limit reached.
    if (nbytes == write_size)
        write_size = 0;
    else
        zmq_assert (nbytes == 0);
malosek's avatar
malosek committed
152
}
Martin Sustrik's avatar
Martin Sustrik committed
153

malosek's avatar
malosek committed
154 155
#endif