Pool Video Switch v2
Software video switch for distributed remote display in a lecture environment
networkmessage.h
Go to the documentation of this file.
1 /*
2  * NetworkMessage.h
3  *
4  * Created on: 18.01.2013
5  * Author: sr
6  */
7 
8 #ifndef NETWORKMESSAGE_H_
9 #define NETWORKMESSAGE_H_
10 
11 #define NM_READ_OK (1)
12 #define NM_READ_INCOMPLETE (2)
13 #define NM_READ_FAILED (0)
14 
15 #include <QHostAddress>
16 #include <QByteArray>
17 #include <QString>
18 
19 class QAbstractSocket;
20 class QUdpSocket;
21 
22 // define QByteArrays for message ids. this prevents implicit instantiation of the same QByteArray over and over again
23 #define MSGTYPE(name) static const QByteArray _ ## name ( #name )
24 MSGTYPE(ID);
25 MSGTYPE(IMG);
26 MSGTYPE(LOGIN);
27 MSGTYPE(THUMB);
28 MSGTYPE(X);
29 MSGTYPE(Y);
30 MSGTYPE(VNCSERVER);
31 MSGTYPE(VNCCLIENT);
32 MSGTYPE(LOCK);
33 MSGTYPE(HASH);
34 MSGTYPE(SALT1);
35 MSGTYPE(SALT2);
36 MSGTYPE(IPLIST);
37 MSGTYPE(PORT);
38 MSGTYPE(CERT);
39 MSGTYPE(CHALLENGE);
40 MSGTYPE(ENABLE);
41 MSGTYPE(ERROR);
42 MSGTYPE(TUTOR);
43 MSGTYPE(EXAMMODE);
44 MSGTYPE(ATTENTION);
45 
46 
47 static const QByteArray __TRUE("1");
48 static const QByteArray __FALSE("0");
49 static const inline QByteArray& _BOOL(bool val) { if (val) return __TRUE; return __FALSE; }
50 
52 {
53 private:
54  char *_buffer;
56  QHash<QByteArray, QByteArray> _fields;
57  int _mode; // 0 = none, 1 = reading, 2 = writing, 3 = read complete, 4 = write complete
58 
59  void allocBuffer();
60  bool parseHeader(char *header);
61  bool parseMessage(char *buffer);
62  void serializeMessage();
63 
64 public:
66  virtual ~NetworkMessage();
67  int readMessage(QAbstractSocket *socket);
68  int readMessage(char* data, quint32 len);
69  bool writeMessage(QAbstractSocket *socket);
70  bool writeMessage(QUdpSocket* socket, const QHostAddress& address, quint16 port);
71  void reset() { _fields.clear(); _bufferSize = 0; _mode = 0; }
72  bool readComplete() const { return _mode == 3; }
73  bool writeComplete() const { return _mode == 4; }
74  bool hasField(QByteArray& key) { return _fields.contains(key); }
75  QString getFieldString(const QByteArray& key) const { return QString::fromUtf8(_fields.value(key)); }
76  QByteArray getFieldBytes(const QByteArray& key) const { return _fields.value(key); }
77  void setField(const QByteArray& key, const QByteArray& value) { if (_mode == 1 || _mode == 2) qFatal("setField called in bad state."); _fields.insert(key, value); _mode = 0; }
78  void setField(const QByteArray& key, const QString& value) { setField(key, value.toUtf8()); }
79  // Convenience
80  void buildErrorMessage(const QString& error);
81 };
82 
83 #endif /* NETWORKMESSAGE_H_ */
static const QByteArray __TRUE("1")
static const QByteArray __FALSE("0")
void setField(const QByteArray &key, const QString &value)
#define MSGTYPE(name)
bool writeMessage(QAbstractSocket *socket)
QString getFieldString(const QByteArray &key) const
bool readComplete() const
bool parseHeader(char *header)
virtual ~NetworkMessage()
quint32 _bufferPos
int readMessage(QAbstractSocket *socket)
quint32 _lastBufferSize
bool hasField(QByteArray &key)
bool writeComplete() const
QHash< QByteArray, QByteArray > _fields
quint32 _bufferSize
static const QByteArray & _BOOL(bool val)
QByteArray getFieldBytes(const QByteArray &key) const
void setField(const QByteArray &key, const QByteArray &value)
bool parseMessage(char *buffer)
void buildErrorMessage(const QString &error)