| 
									
										
										
										
											2018-02-08 21:28:33 -05:00
										 |  |  | #ifndef MESSAGE_CLIENT_HPP__
 | 
					
						
							|  |  |  | #define MESSAGE_CLIENT_HPP__
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <QObject>
 | 
					
						
							|  |  |  | #include <QTime>
 | 
					
						
							| 
									
										
										
										
											2018-08-10 14:37:52 -04:00
										 |  |  | #include <QDataStream>
 | 
					
						
							| 
									
										
										
										
											2018-02-08 21:28:33 -05:00
										 |  |  | #include <QDateTime>
 | 
					
						
							|  |  |  | #include <QString>
 | 
					
						
							| 
									
										
										
										
											2018-08-08 17:15:49 -04:00
										 |  |  | #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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-08 17:15:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Message { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     Message(); | 
					
						
							|  |  |  |     Message(QString const &type, QString const &value=""); | 
					
						
							|  |  |  |     Message(QString const &type, QString const &value, QMap<QString, QVariant> const ¶ms); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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 | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-08 17:15:49 -04:00
										 |  |  |   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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 22:41:01 -04:00
										 |  |  |   // this slot is used to send an arbitrary message
 | 
					
						
							| 
									
										
										
										
											2018-08-08 17:15:49 -04:00
										 |  |  |   Q_SLOT void send(Message const &message); | 
					
						
							| 
									
										
										
										
											2018-08-07 22:41:01 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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&); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-08 17:15:49 -04:00
										 |  |  |   Q_SIGNAL void message(Message const &message); | 
					
						
							| 
									
										
										
										
											2018-08-07 22:41:01 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // 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
 |