js8call/MessageClient.hpp

103 lines
2.8 KiB
C++
Raw Normal View History

2018-02-08 21:28:33 -05:00
#ifndef MESSAGE_CLIENT_HPP__
#define MESSAGE_CLIENT_HPP__
#include <QObject>
#include <QTime>
#include <QDataStream>
2018-02-08 21:28:33 -05:00
#include <QDateTime>
#include <QString>
#include <QJsonDocument>
#include <QJsonObject>
2018-02-08 21:28:33 -05:00
#include "Radio.hpp"
#include "pimpl_h.hpp"
class QByteArray;
class QHostAddress;
2018-08-05 11:33:30 -04:00
class QColor;
2018-02-08 21:28:33 -05:00
class Message {
public:
Message();
Message(QString const &type, QString const &value="");
Message(QString const &type, QString const &value, QMap<QString, QVariant> const &params);
void read(const QJsonObject &json);
void write(QJsonObject &json) const;
QByteArray toJson() const;
QString type() const { return type_; }
QString value() const { return value_; }
QMap<QString, QVariant> params() const { return params_; }
private:
QString type_;
QString value_;
QMap<QString, QVariant> params_;
};
2018-02-08 21:28:33 -05:00
//
// MessageClient - Manage messages sent and replies received from a
// matching server (MessageServer) at the other end of
// the wire
//
//
// Each outgoing message type is a Qt slot
//
class MessageClient
: public QObject
{
Q_OBJECT
2018-02-08 21:28:33 -05:00
public:
using Frequency = Radio::Frequency;
using port_type = quint16;
// instantiate and initiate a host lookup on the server
//
// messages will be silently dropped until a server host lookup is complete
MessageClient (QString const& id, QString const& version, QString const& revision,
QString const& server, port_type server_port, QObject * parent = nullptr);
// query server details
QHostAddress server_address () const;
port_type server_port () const;
// initiate a new server host lookup or is the server name is empty
// the sending of messages is disabled
Q_SLOT void set_server (QString const& server = QString {});
// 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 const &message);
2018-02-08 21:28:33 -05:00
// 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
Q_SLOT void send_raw_datagram (QByteArray const&, QHostAddress const& dest_address, port_type dest_port);
2018-03-05 14:49:51 -05:00
// disallowed message destination (does not block datagrams sent
// with send_raw_datagram() above)
Q_SLOT void add_blocked_destination (QHostAddress const&);
Q_SIGNAL void message(Message const &message);
// this signal is emitted when the a reply a message is received
Q_SIGNAL void message_received(QString const &type, QString const &message);
2018-02-08 21:28:33 -05:00
// this signal is emitted when network errors occur or if a host
// lookup fails
Q_SIGNAL void error (QString const&) const;
private:
class impl;
pimpl<impl> m_;
};
#endif