aboutsummaryrefslogtreecommitdiffstats
path: root/src/InputZeroMQReader.cpp
blob: 8bea76133bd7da4ad360f8ed302907c0eafdceab (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
/*
   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
   Her Majesty the Queen in Right of Canada (Communications Research
   Center Canada)

   Copyrigth (C) 2013
   Matthias P. Braendli, matthias.braendli@mpb.li
 */
/*
   This file is part of CRC-DADMOD.

   CRC-DADMOD 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.

   CRC-DADMOD 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 CRC-DADMOD.  If not, see <http://www.gnu.org/licenses/>.
 */

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

#if defined(HAVE_INPUT_ZEROMQ)

#include <string>
#include <cstring>
#include <cstdio>
#include <stdint.h>
#include <zmq.hpp>
#include <boost/thread/thread.hpp>
#include "porting.h"
#include "InputReader.h"
#include "PcDebug.h"

#define MAX_QUEUE_SIZE 50

int InputZeroMQReader::Open(std::string uri)
{
    uri_ = uri;
    workerdata_.uri = uri;
    // launch receiver thread
    worker_.Start(&workerdata_);

    return 0;
}

int InputZeroMQReader::GetNextFrame(void* buffer)
{
    zmq::message_t* incoming;
    in_messages_.wait_and_pop(incoming);

    size_t framesize = incoming->size();

    // guarantee that we never will write more than 6144 bytes
    if (framesize > 6144) {
        fprintf(stderr, "ZeroMQ message too large: %zu!\n", framesize);
        logger_.level(error) << "ZeroMQ message too large" << framesize;
        return -1;
    }

    memcpy(buffer, incoming->data(), framesize);

    delete incoming;

    // pad to 6144 bytes
    memset(&((uint8_t*)buffer)[framesize], 0x55, 6144 - framesize);


    return 6144;
}

void InputZeroMQReader::PrintInfo()
{
    fprintf(stderr, "Input ZeroMQ:\n");
    fprintf(stderr, "  Receiving from %s\n\n", uri_.c_str());
}

// ------------- Worker functions

void InputZeroMQWorker::RecvProcess(struct InputZeroMQThreadData* workerdata)
{
    size_t queue_size = 0;

    try {
        subscriber.connect(workerdata->uri.c_str());

        subscriber.setsockopt(ZMQ_SUBSCRIBE, NULL, 0); // subscribe to all messages

        while (running)
        {
            zmq::message_t incoming;
            subscriber.recv(&incoming);

            if (queue_size < MAX_QUEUE_SIZE) {
                zmq::message_t* holder = new zmq::message_t();
                holder->move(&incoming); // move the message into the holder
                queue_size = workerdata->in_messages->push(holder);
            }
            else
            {
                workerdata->in_messages->notify();
                fprintf(stderr, "ZeroMQ message overfull: %zu elements !\n", queue_size);
            }

            if (queue_size < 5) {
                fprintf(stderr, "ZeroMQ message underfull: %zu elements !\n", queue_size);
            }
        }
    }
    catch (zmq::error_t& err) {
        printf("ZeroMQ error in RecvProcess: '%s'\n", err.what());
    }
}

void InputZeroMQWorker::Start(struct InputZeroMQThreadData* workerdata)
{
    running = true;
    recv_thread = boost::thread(&InputZeroMQWorker::RecvProcess, this, workerdata);
}

void InputZeroMQWorker::Stop()
{
    subscriber.close();
    running = false;
}

#endif