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
{
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)
{
@ -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)
{

View File

@ -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;

13
udp.py
View File

@ -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()