Simplified textual network command structure

This commit is contained in:
Jordan Sherer 2018-08-07 22:41:01 -04:00
parent 10d706e9fa
commit e91e93c349
3 changed files with 35 additions and 9 deletions

View File

@ -136,7 +136,15 @@ void MessageClient::impl::parse_message (QByteArray const& msg)
{ {
try try
{ {
qDebug() << "incoming udp message" << msg; QList<QByteArray> 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) catch (std::exception const& e)
{ {
@ -152,8 +160,7 @@ void MessageClient::impl::heartbeat ()
{ {
if (server_port_ && !server_.isNull ()) if (server_port_ && !server_.isNull ())
{ {
QByteArray message; QByteArray message("PING|");
qDebug() << "outgoing udp heartbeat message" << message;
writeDatagram (message, server_, server_port_); writeDatagram (message, server_, server_port_);
} }
} }
@ -162,8 +169,7 @@ void MessageClient::impl::closedown ()
{ {
if (server_port_ && !server_.isNull ()) if (server_port_ && !server_.isNull ())
{ {
QByteArray message; QByteArray message("EXIT|");
qDebug() << "outgoing udp closedown message" << message;
writeDatagram (message, server_, server_port_); writeDatagram (message, server_, server_port_);
} }
} }
@ -265,6 +271,14 @@ void MessageClient::set_server_port (port_type server_port)
m_->server_port_ = 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 void MessageClient::send_raw_datagram (QByteArray const& message, QHostAddress const& dest_address
, port_type dest_port) , port_type dest_port)
{ {

View File

@ -47,6 +47,9 @@ public:
// change the server port messages are sent to // change the server port messages are sent to
Q_SLOT void set_server_port (port_type server_port = 0u); 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 // this slot may be used to send arbitrary UDP datagrams to and
// destination allowing the underlying socket to be used for general // destination allowing the underlying socket to be used for general
// UDP messaging if desired // UDP messaging if desired
@ -56,6 +59,10 @@ public:
// with send_raw_datagram() above) // with send_raw_datagram() above)
Q_SLOT void add_blocked_destination (QHostAddress const&); 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 // this signal is emitted when network errors occur or if a host
// lookup fails // lookup fails
Q_SIGNAL void error (QString const&) const; Q_SIGNAL void error (QString const&) const;

13
udp.py
View File

@ -11,10 +11,15 @@ def main():
while True: while True:
content, addr = sock.recvfrom(65500) content, addr = sock.recvfrom(65500)
print("from:", ":".join(map(str, addr))) print("from:", ":".join(map(str, addr)))
print("->", repr(content))
print("sending test reply...", end="") typ, msg = content.split('|')
sock.sendto("test", addr) print("->", typ)
print("done") print("->", msg)
if typ == "PING":
print("sending pong reply...", end="")
sock.sendto("PONG|hello world|asdf", addr)
print("done")
finally: finally:
sock.close() sock.close()