summaryrefslogtreecommitdiffstats
path: root/src/InputZeroMQReader.cpp
blob: 40a07d4d6df6b85edc5f2f60382db2937d185295 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
   Her Majesty the Queen in Right of Canada (Communications Research
   Center Canada)

   Copyright (C) 2018
   Matthias P. Braendli, matthias.braendli@mpb.li

    http://opendigitalradio.org
 */
/*
   This file is part of ODR-DabMod.

   ODR-DabMod is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as
   published by the Free Software Foundation, either version 3 of the
   License, or (at your option) any later version.

   ODR-DabMod 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
   GNU General Public License for more details.

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

#ifdef HAVE_CONFIG_H
#   include "config.h"
#endif

#if defined(HAVE_ZEROMQ)

#include <string>
#include <cstring>
#include <cstdio>
#include <stdint.h>
#include "zmq.hpp"
#include "InputReader.h"
#include "PcDebug.h"
#include "Utils.h"

using namespace std;

constexpr int ZMQ_TIMEOUT_MS = 100;

#define NUM_FRAMES_PER_ZMQ_MESSAGE 4
/* A concatenation of four ETI frames,
 * whose maximal size is 6144.
 *
 * Four frames in one zmq message are sent, so that
 * we do not risk breaking ETI vs. transmission frame
 * phase.
 *
 * The header is followed by the four ETI frames.
 */
struct zmq_msg_header_t
{
    uint32_t version;
    uint16_t buflen[NUM_FRAMES_PER_ZMQ_MESSAGE];
};

#define ZMQ_DAB_MESSAGE_T_HEADERSIZE \
    (sizeof(uint32_t) + NUM_FRAMES_PER_ZMQ_MESSAGE*sizeof(uint16_t))

InputZeroMQReader::InputZeroMQReader() :
    InputReader(),
    RemoteControllable("inputzmq")
{
    RC_ADD_PARAMETER(buffer, "Size of input buffer [us] (read-only)");
}

InputZeroMQReader::~InputZeroMQReader()
{
    m_running = false;
    // This avoids the ugly "context was terminated" error because it lets
    // poll do its thing first
    this_thread::sleep_for(chrono::milliseconds(2 * ZMQ_TIMEOUT_MS));
    m_zmqcontext.close();
    if (m_recv_thread.joinable()) {
        m_recv_thread.join();
    }
}

int InputZeroMQReader::Open(const string& uri, size_t max_queued_frames)
{
    // The URL might start with zmq+tcp://
    if (uri.substr(0, 4) == "zmq+") {
        m_uri = uri.substr(4);
    }
    else {
        m_uri = uri;
    }

    m_max_queued_frames = max_queued_frames;

    m_running = true;
    m_recv_thread = std::thread(&InputZeroMQReader::RecvProcess, this);

    return 0;
}

int InputZeroMQReader::GetNextFrame(void* buffer)
{
    if (not m_running) {
        throw runtime_error("ZMQ input is not ready yet");
    }

    message_t incoming;

    /* Do some prebuffering because reads will happen in bursts
     * (4 ETI frames in TM1) and we should make sure that
     * we can serve the data required for a full transmission frame.
     */
    if (m_in_messages.size() < 4) {
        const size_t prebuffering = 10;
        etiLog.log(trace, "ZMQ,wait1");
        m_in_messages.wait_and_pop(incoming, prebuffering);
    }
    else {
        etiLog.log(trace, "ZMQ,wait2");
        m_in_messages.wait_and_pop(incoming);
    }
    etiLog.log(trace, "ZMQ,pop");

    constexpr size_t framesize = 6144;

    if (incoming.timeout) {
        return 0;
    }
    else if (incoming.fault) {
        throw runtime_error("ZMQ input has terminated");
    }
    else if (incoming.overflow) {
        throw zmq_input_overflow();
    }
    else if (incoming.eti_frame.size() == framesize) {
        unique_lock<mutex> lock(m_last_in_messages_size_mutex);
        m_last_in_messages_size--;
        lock.unlock();

        memcpy(buffer, &incoming.eti_frame.front(), framesize);

        return framesize;
    }
    else {
        throw logic_error("ZMQ ETI not 6144");
    }
}

std::string InputZeroMQReader::GetPrintableInfo() const
{
    return "Input ZeroMQ: Receiving from " + m_uri;
}

void InputZeroMQReader::RecvProcess()
{
    set_thread_name("zmqinput");

    size_t queue_size = 0;

    zmq::socket_t subscriber(m_zmqcontext, ZMQ_SUB);
    // zmq sockets are not thread safe. That's why
    // we create it here, and not at object creation.

    bool success = true;

    try {
        subscriber.connect(m_uri.c_str());
    }
    catch (const zmq::error_t& err) {
        etiLog.level(error) << "Failed to connect ZeroMQ socket to '" <<
            m_uri << "': '" << err.what() << "'";
        success = false;
    }

    if (success) try {
        // subscribe to all messages
        subscriber.setsockopt(ZMQ_SUBSCRIBE, NULL, 0);
    }
    catch (const zmq::error_t& err) {
        etiLog.level(error) << "Failed to subscribe ZeroMQ socket to messages: '" <<
            err.what() << "'";
        success = false;
    }

    if (success) try {
        while (m_running) {
            zmq::message_t incoming;
            zmq::pollitem_t items[1];
            items[0].socket = subscriber;
            items[0].events = ZMQ_POLLIN;
            const int num_events = zmq::poll(items, 1, ZMQ_TIMEOUT_MS);
            if (num_events == 0) {
                message_t msg;
                msg.timeout = true;
                m_in_messages.push(move(msg));
                continue;
            }

            subscriber.recv(incoming);

            if (queue_size < m_max_queued_frames) {
                if (incoming.size() < ZMQ_DAB_MESSAGE_T_HEADERSIZE) {
                    throw runtime_error("ZeroMQ packet too small for header");
                }
                else {
                    zmq_msg_header_t dab_msg;
                    memcpy(&dab_msg, incoming.data(), sizeof(zmq_msg_header_t));

                    if (dab_msg.version != 1) {
                        etiLog.level(error) <<
                            "ZeroMQ wrong packet version " <<
                            dab_msg.version;
                    }

                    int offset = sizeof(dab_msg.version) +
                        NUM_FRAMES_PER_ZMQ_MESSAGE * sizeof(*dab_msg.buflen);

                    for (int i = 0; i < NUM_FRAMES_PER_ZMQ_MESSAGE; i++) {
                        if (dab_msg.buflen[i] > 6144) {
                            stringstream ss;
                            ss << "ZeroMQ buffer " << i <<
                                " has invalid buflen " << dab_msg.buflen[i];
                            throw runtime_error(ss.str());
                        }
                        else {
                            vector<uint8_t> buf(6144, 0x55);

                            const int framesize = dab_msg.buflen[i];

                            if ((ssize_t)incoming.size() < offset + framesize) {
                                throw runtime_error("ZeroMQ packet too small");
                            }

                            memcpy(&buf.front(),
                                    ((uint8_t*)incoming.data()) + offset,
                                    framesize);

                            offset += framesize;

                            message_t msg;
                            msg.eti_frame = move(buf);
                            queue_size = m_in_messages.push(move(msg));
                            etiLog.log(trace, "ZMQ,push %zu", queue_size);

                            unique_lock<mutex> lock(m_last_in_messages_size_mutex);
                            m_last_in_messages_size++;
                        }
                    }
                }
            }
            else {
                message_t msg;
                msg.overflow = true;
                queue_size = m_in_messages.push(move(msg));
                etiLog.level(warn) << "ZeroMQ buffer overfull !";
                throw runtime_error("ZMQ input full");
            }

            if (queue_size < 5) {
                etiLog.level(warn) << "ZeroMQ buffer low: " << queue_size << " elements !";
            }
        }
    }
    catch (const zmq::error_t& err) {
        etiLog.level(error) << "ZeroMQ error during receive: '" << err.what() << "'";
    }
    catch (const std::exception& err) {
        etiLog.level(error) << "Exception during receive: '" << err.what() << "'";
    }

    m_running = false;

    etiLog.level(info) << "ZeroMQ input worker terminated";

    subscriber.close();

    message_t msg;
    msg.fault = true;
    queue_size = m_in_messages.push(move(msg));
}

// =======================================
// Remote Control
// =======================================
void InputZeroMQReader::set_parameter(const string& parameter, const string& value)
{
    stringstream ss(value);
    ss.exceptions ( stringstream::failbit | stringstream::badbit );

    if (parameter == "buffer") {
        throw ParameterError("Parameter " + parameter + " is read-only.");
    }
    else {
        stringstream ss_err;
        ss_err << "Parameter '" << parameter
            << "' is not exported by controllable " << get_rc_name();
        throw ParameterError(ss_err.str());
    }
}

const string InputZeroMQReader::get_parameter(const string& parameter) const
{
    stringstream ss;
    ss << std::fixed;
    if (parameter == "buffer") {
        // Do not use size of the queue, as it will contain empty
        // frames to signal timeouts
        unique_lock<mutex> lock(m_last_in_messages_size_mutex);
        const long time_in_buffer_us = 24000 * m_last_in_messages_size;
        ss << time_in_buffer_us;
    }
    else {
        ss << "Parameter '" << parameter <<
            "' is not exported by controllable " << get_rc_name();
        throw ParameterError(ss.str());
    }
    return ss.str();
}

#endif