Skip to content

Commit

Permalink
Merge pull request #858 from agoessling/multicast
Browse files Browse the repository at this point in the history
Add multicast support to DataStreamUDP.
  • Loading branch information
facontidavide authored Nov 12, 2023
2 parents a123c36 + a4d8ab9 commit b524543
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 18 deletions.
16 changes: 11 additions & 5 deletions plotjuggler_plugins/DataStreamUDP/udp_client.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#!/usr/bin/python

import argparse
import socket
import math
import json
from time import sleep

parser = argparse.ArgumentParser(description="Send UDP test data.")
parser.add_argument("--address", default="127.0.0.1", help="UDP address")
parser.add_argument("--port", default=9870, type=int, help="UDP port")
args = parser.parse_args()

sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
time = 0.0
Expand All @@ -20,8 +26,8 @@
"sin": math.sin(time)
}
}
sock.sendto( json.dumps(data).encode(), ("127.0.0.1", 9870) )
sock.sendto( json.dumps(data).encode(), (args.address, args.port) )

test_str = "{ \
\"1252\": { \
\"timestamp\": { \
Expand All @@ -35,7 +41,7 @@
\"volt\": 24.852617263793945 \
}\
}\
} }"
sock.sendto( test_str.encode('utf-8'), ("127.0.0.1", 9870) )
} }"

sock.sendto( test_str.encode("utf-8"), (args.address, args.port) )

45 changes: 39 additions & 6 deletions plotjuggler_plugins/DataStreamUDP/udp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ THE SOFTWARE.
#include <QMessageBox>
#include <chrono>
#include <QNetworkDatagram>
#include <QNetworkInterface>

#include "ui_udp_server.h"

Expand Down Expand Up @@ -97,8 +98,10 @@ bool UDP_Server::start(QStringList*)
// load previous values
QSettings settings;
QString protocol = settings.value("UDP_Server::protocol", "JSON").toString();
QString address_str = settings.value("UDP_Server::address", "127.0.0.1").toString();
int port = settings.value("UDP_Server::port", 9870).toInt();

dialog.ui->lineEditAddress->setText(address_str);
dialog.ui->lineEditPort->setText(QString::number(port));

ParserFactoryPlugin::Ptr parser_creator;
Expand Down Expand Up @@ -130,30 +133,60 @@ bool UDP_Server::start(QStringList*)
return false;
}

address_str = dialog.ui->lineEditAddress->text();
port = dialog.ui->lineEditPort->text().toUShort(&ok);
protocol = dialog.ui->comboBoxProtocol->currentText();

_parser = parser_creator->createParser({}, {}, {}, dataMap());

// save back to service
settings.setValue("UDP_Server::protocol", protocol);
settings.setValue("UDP_Server::address", address_str);
settings.setValue("UDP_Server::port", port);

QHostAddress address(address_str);

bool success = true;
success &= !address.isNull();

_udp_socket = new QUdpSocket();
_udp_socket->bind(QHostAddress::Any, port);

if (!address.isMulticast())
{
success &= _udp_socket->bind(address, port);
}
else
{
success &= _udp_socket->bind(
address, port, QAbstractSocket::ShareAddress | QAbstractSocket::ReuseAddressHint);

// Add multicast group membership to all interfaces which support multicast.
for (const auto& interface : QNetworkInterface::allInterfaces())
{
QNetworkInterface::InterfaceFlags iflags = interface.flags();
if (interface.isValid() && !iflags.testFlag(QNetworkInterface::IsLoopBack) &&
iflags.testFlag(QNetworkInterface::CanMulticast) &&
iflags.testFlag(QNetworkInterface::IsRunning))
{
success &= _udp_socket->joinMulticastGroup(address, interface);
}
}
}

_running = true;

connect(_udp_socket, &QUdpSocket::readyRead, this, &UDP_Server::processMessage);

if (_udp_socket)
if (success)
{
qDebug() << "UDP listening on port" << port;
_running = true;
qDebug() << tr("UDP listening on (%1, %2)").arg(address_str).arg(port);
}
else
{
QMessageBox::warning(nullptr, tr("UDP Server"),
tr("Couldn't bind UDP port %1").arg(port), QMessageBox::Ok);
_running = false;
tr("Couldn't bind to UDP (%1, %2)").arg(address_str).arg(port),
QMessageBox::Ok);
shutdown();
}

return _running;
Expand Down
54 changes: 47 additions & 7 deletions plotjuggler_plugins/DataStreamUDP/udp_server.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>293</width>
<height>232</height>
<width>298</width>
<height>312</height>
</rect>
</property>
<property name="windowTitle">
Expand All @@ -19,24 +19,65 @@
<property name="font">
<font>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Port of the UDP server:</string>
<string>UDP Server Settings:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEditPort"/>
<widget class="QWidget" name="widget_2" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label_5">
<property name="font">
<font>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Address:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEditAddress"/>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QWidget" name="widget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_2" stretch="0,1">
<item>
<widget class="QLabel" name="label_4">
<property name="font">
<font>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Port:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEditPort">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="font">
<font>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
Expand Down Expand Up @@ -67,7 +108,6 @@
<widget class="QLabel" name="label_3">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
Expand Down

0 comments on commit b524543

Please sign in to comment.