sMQTTBroker
sMQTTTopic.h
1 #ifndef SMQTTTOPIC_FILE
2 #define SMQTTTOPIC_FILE
3 
4 #define SINGLE_LEVEL_WILDCARD "+"
5 #define TOPIC_LEVEL_SEPARATOR "/"
6 #define MULTI_LEVEL_WILDCARD "#"
7 
8 #include<string>
9 
11 {
12 public:
13  sMQTTTopic(const char *name,char QoS=0);
14  sMQTTTopic(const char *name, unsigned short nameLen, const char *payload, unsigned short payloadLen);
15  sMQTTTopic(std::string &name,std::string &payload, char QoS=0);
16  sMQTTTopic(sMQTTTopic *other);
17  virtual ~sMQTTTopic();
18  void update(sMQTTTopic *other)
19  {
20  if (_payload)
21  delete[] _payload;
22  _name = other->Name();
23 
24  if (other->Payload())
25  {
26  _payload = new char[strlen(other->Payload()) + 1];
27  strcpy(_payload, other->Payload());
28  }
29  else
30  _payload = 0;
31  qos = other->QoS();
32  }
33  const char *Name() {
34  return _name.c_str();
35  }
36  const char *Payload() {
37  return _payload;
38  }
39  const unsigned char QoS() {
40  return qos;
41  }
42  bool match(sMQTTTopic *other);
43  bool match(const std::string &other);
44  void subscribe(sMQTTClient *client);
45  bool unsubscribe(sMQTTClient *client) {
46  sMQTTClientList::iterator cli; for (cli = clients.begin(); cli != clients.end(); cli++)if (*cli == client) { clients.erase(cli); break; }
47  return clients.empty();
48  }
49  const sMQTTClientList getSubscribeList() {
50  return clients;
51  }
52 protected:
53  std::string _name;
54  char *_payload;
55  unsigned char qos;
56  unsigned short _payloadLen;
57  // subscribe to topic
58  sMQTTClientList clients;
59 };
60 
61 typedef std::vector<sMQTTTopic*> sMQTTTopicList;
62 
64 {
65 public:
66  sMQTTTopicRetain(const char *name, const char *payload);
67 };
68 #endif
sMQTTTopicRetain
Definition: sMQTTTopic.h:64
sMQTTTopic
Definition: sMQTTTopic.h:11
sMQTTClient
Definition: sMQTTClient.h:22