simplemqttservermessage.h 3.03 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//================================================================================
// Name        : simplemqttservermessage.h
// Author      : Axel Auweter
// Copyright   : Leibniz Supercomputing Centre
// Description : Headers of a class for receiving a simple MQTT message
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2011-2016 Leibniz Supercomputing Centre
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//================================================================================
26
27
28
29

#ifndef SIMPLEMQTTMESSAGE_H_
#define SIMPLEMQTTMESSAGE_H_

30
#define MQTT_RESERVED           0x0
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#define MQTT_CONNECT            0x1
#define MQTT_CONNACK            0x2
#define MQTT_PUBLISH            0x3
#define MQTT_PUBACK             0x4
#define MQTT_PUBREC             0x5
#define MQTT_PUBREL             0x6
#define MQTT_PUBCOMP            0x7
#define MQTT_SUBSCRIBE          0x8
#define MQTT_SUBACK             0x9
#define MQTT_UNSUBSCRIBE        0xa
#define MQTT_UNSUBACK           0xb
#define MQTT_PINGREQ            0xc
#define MQTT_PINGRESP           0xd
#define MQTT_DISCONNECT         0xe

46
47
48
#define DCDB_MAP "/DCDB_MAP/"
#define DCDB_MAP_LEN 10

49
50
51
52
53
54
55
56
57
58
#pragma pack(push,1)

typedef union {
  uint8_t raw[5];
  struct {
    uint8_t retain      :1;
    uint8_t qos         :2;
    uint8_t dup         :1;
    uint8_t type        :4;
    uint8_t remaining_length[4];
59
  };
60
61
62
63
} MQTTFixedHeader;

#pragma pack(pop)

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
enum MQTTMessageState {
  Empty,
  DecodingFixedHeader,
  FixedHeaderOk,
  Complete,
  Error
};

class SimpleMQTTMessage
{
protected:
  MQTTMessageState state;
  MQTTFixedHeader fixedHeader;
  size_t fixedHeaderLength;
  size_t bytesProcessed;
  size_t remainingLength;
80
81
  uint16_t msgId;
  std::string topic;
82
  void *remainingRaw;
83
  size_t payloadLength;
84
  void *payloadPtr;
85
86
87
88
89
90
91

  ssize_t decodeFixedHeader(void* buf, size_t len);
  ssize_t receiveMessage(void* buf, size_t len);

public:
  ssize_t appendRawData(void* buf, size_t len);
  bool complete();
92
  bool isPublish();
93
  uint8_t getType();
94
95
96
  const std::string& getTopic();
  uint16_t getMsgId();
  uint8_t getQoS();
97
  size_t getPayloadLength();
98
  void* getPayload();  
99
100
101
102
103
104

  void dump();

  SimpleMQTTMessage();
  virtual ~SimpleMQTTMessage();
};
105
106

#endif /* SIMPLEMQTTMESSAGE_H_ */