16 #include <QTextStream> 18 #include <QMessageBox> 24 #include <QIntValidator> 25 #include <QMessageBox> 27 #include <QNetworkDatagram> 29 #include "ui_udp_server.h" 36 ui(new
Ui::UDPServerDialog)
39 ui->lineEditPort->setValidator(
new QIntValidator() );
40 setWindowTitle(
"UDP Server");
42 connect(
ui->buttonBox, &QDialogButtonBox::accepted,
43 this, &QDialog::accept );
44 connect(
ui->buttonBox, &QDialogButtonBox::rejected,
45 this, &QDialog::reject );
49 while(
ui->layoutOptions->count() > 0)
51 auto item =
ui->layoutOptions->takeAt(0);
52 item->widget()->setParent(
nullptr);
56 Ui::UDPServerDialog*
ui;
78 QMessageBox::warning(
nullptr,tr(
"UDP Server"), tr(
"No available MessageParsers"), QMessageBox::Ok);
89 dialog.
ui->comboBoxProtocol->addItem( it.first );
91 if(
auto widget = it.second->optionsWidget() )
93 widget->setVisible(
false);
94 dialog.
ui->layoutOptions->addWidget( widget );
100 QString protocol = settings.value(
"UDP_Server::protocol",
"JSON").toString();
101 int port = settings.value(
"UDP_Server::port", 9870).toInt();
103 dialog.
ui->lineEditPort->setText( QString::number(port) );
105 std::shared_ptr<MessageParserCreator> parser_creator;
107 connect(dialog.
ui->comboBoxProtocol, qOverload<const QString &>( &QComboBox::currentIndexChanged),
this,
108 [&](QString protocol)
110 if( parser_creator ){
111 QWidget* prev_widget = parser_creator->optionsWidget();
112 prev_widget->setVisible(false);
116 if(
auto widget = parser_creator->optionsWidget() ){
117 widget->setVisible(
true);
121 dialog.ui->comboBoxProtocol->setCurrentText(protocol);
123 int res = dialog.exec();
124 if( res == QDialog::Rejected )
130 port = dialog.ui->lineEditPort->text().toUShort(&ok);
131 protocol = dialog.ui->comboBoxProtocol->currentText();
136 settings.setValue(
"UDP_Server::protocol", protocol);
137 settings.setValue(
"UDP_Server::port", port);
147 qDebug() <<
"UDP listening on port" << port;
152 QMessageBox::warning(
nullptr,tr(
"UDP Server"),
153 tr(
"Couldn't bind UDP port %1").
arg(port),
174 QNetworkDatagram datagram =
_udp_socket->receiveDatagram();
177 using namespace std::chrono;
178 auto ts = high_resolution_clock::now().time_since_epoch();
179 double timestamp = 1e-6* double( duration_cast<microseconds>(ts).
count() );
181 MessageRef msg ( reinterpret_cast<uint8_t*>(datagram.data().data()), datagram.data().size() );
184 std::lock_guard<std::mutex> lock(
mutex());
186 _parser->parseMessage(msg, timestamp);
187 }
catch (std::exception& err)
189 QMessageBox::warning(
nullptr,
191 tr(
"Problem parsing the message. UDP Server will be stopped.\n%1").
arg(err.what()),
virtual ~UDP_Server() override
virtual void shutdown() override
MessageParserFactory * availableParsers()
PlotDataMapRef & dataMap()
detail::named_arg< Char, T > arg(const Char *name, const T &arg)
virtual bool start(QStringList *) override
PJ::MessageParserPtr _parser