From e91e93c3495735a8c2c116a4fce8fd63b637c130 Mon Sep 17 00:00:00 2001 From: Jordan Sherer Date: Tue, 7 Aug 2018 22:41:01 -0400 Subject: [PATCH] Simplified textual network command structure --- MessageClient.cpp | 24 +++++++++++++++++++----- MessageClient.hpp | 7 +++++++ udp.py | 13 +++++++++---- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/MessageClient.cpp b/MessageClient.cpp index d7a7470..cf943ad 100644 --- a/MessageClient.cpp +++ b/MessageClient.cpp @@ -136,7 +136,15 @@ void MessageClient::impl::parse_message (QByteArray const& msg) { try { - qDebug() << "incoming udp message" << msg; + QList segments = msg.split('|'); + if(segments.isEmpty()){ + return; + } + + QString type(segments.first()); + QString message(segments.mid(1).join('|')); + qDebug() << "MessageClient: Incoming" << type << message; + Q_EMIT self_->message_received(type, message); } catch (std::exception const& e) { @@ -152,8 +160,7 @@ void MessageClient::impl::heartbeat () { if (server_port_ && !server_.isNull ()) { - QByteArray message; - qDebug() << "outgoing udp heartbeat message" << message; + QByteArray message("PING|"); writeDatagram (message, server_, server_port_); } } @@ -162,8 +169,7 @@ void MessageClient::impl::closedown () { if (server_port_ && !server_.isNull ()) { - QByteArray message; - qDebug() << "outgoing udp closedown message" << message; + QByteArray message("EXIT|"); writeDatagram (message, server_, server_port_); } } @@ -265,6 +271,14 @@ void MessageClient::set_server_port (port_type server_port) m_->server_port_ = server_port; } +void MessageClient::send_message(QString const &type, QString const &message){ + QByteArray b; + b.append(type); + b.append('|'); + b.append(message); + m_->send_message(b); +} + void MessageClient::send_raw_datagram (QByteArray const& message, QHostAddress const& dest_address , port_type dest_port) { diff --git a/MessageClient.hpp b/MessageClient.hpp index a4742bf..4afac71 100644 --- a/MessageClient.hpp +++ b/MessageClient.hpp @@ -47,6 +47,9 @@ public: // change the server port messages are sent to Q_SLOT void set_server_port (port_type server_port = 0u); + // this slot is used to send an arbitrary message + Q_SLOT void send_message(QString const &type, QString const &message); + // this slot may be used to send arbitrary UDP datagrams to and // destination allowing the underlying socket to be used for general // UDP messaging if desired @@ -56,6 +59,10 @@ public: // with send_raw_datagram() above) Q_SLOT void add_blocked_destination (QHostAddress const&); + + // this signal is emitted when the a reply a message is received + Q_SIGNAL void message_received(QString const &type, QString const &message); + // this signal is emitted when network errors occur or if a host // lookup fails Q_SIGNAL void error (QString const&) const; diff --git a/udp.py b/udp.py index 8900d89..15744dc 100644 --- a/udp.py +++ b/udp.py @@ -11,10 +11,15 @@ def main(): while True: content, addr = sock.recvfrom(65500) print("from:", ":".join(map(str, addr))) - print("->", repr(content)) - print("sending test reply...", end="") - sock.sendto("test", addr) - print("done") + + typ, msg = content.split('|') + print("->", typ) + print("->", msg) + + if typ == "PING": + print("sending pong reply...", end="") + sock.sendto("PONG|hello world|asdf", addr) + print("done") finally: sock.close()