client-cpp  0.10.0
DisconnectMessage.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014-2016 CyberVision, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef DISCONNECTMESSAGE_HPP_
18 #define DISCONNECTMESSAGE_HPP_
19 
23 #include <boost/format.hpp>
24 
25 namespace kaa
26 {
27 
28 enum class DisconnectReason : std::uint8_t
29 {
30  NONE = 0x00,
31  BAD_REQUEST = 0x01,
32  INTERNAL_ERROR = 0x02,
33  CREDENTIALS_REVOKED = 0x03,
34 };
35 
37 {
38 public:
39  static std::string reasonToString(DisconnectReason reason)
40  {
41  switch (reason) {
43  return "No error";
45  return "Bad request";
47  return "Internal error has been occurred";
49  return "Credentials have been revoked";
50  default:
51  return (boost::format("Invalid Disconnect reason %1%") % (std::uint8_t) reason).str();
52  }
53  }
54 
55  DisconnectMessage(DisconnectReason reason) : message_(4), reason_(reason)
56  {
57  char header[2];
59  std::copy(reinterpret_cast<const std::uint8_t *>(header),
60  reinterpret_cast<const std::uint8_t *>(header + 2),
61  message_.begin());
62  message_[2] = 0;
63  message_[3] = (std::uint8_t) reason_;
64  }
65 
66  DisconnectMessage(const char *payload, std::uint16_t size)
67  {
68  parseMessage(payload, size);
69  }
70 
72 
73  DisconnectReason getReason() const { return reason_; }
74  std::string getMessage() const { return reasonToString(reason_); }
75 
76  const std::vector<std::uint8_t>& getRawMessage() const { return message_; }
77 
78 private:
79  void parseMessage(const char *payload, std::uint16_t size)
80  {
81  if (!payload || !size) {
82  throw KaaException("Bad Disconnect payload data");
83  }
84 
85  int code = *(payload + 1);
86  if (code < (int)DisconnectReason::NONE || code > (int)DisconnectReason::CREDENTIALS_REVOKED) {
87  throw KaaException(boost::format("Bad Disconnect return code: %1%") % code);
88  }
89  reason_ = (DisconnectReason) code;
90  }
91 
92 private:
93  std::vector<std::uint8_t> message_;
94  DisconnectReason reason_;
95 
96 };
97 
98 }
99 
100 
101 
102 #endif /* DISCONNECTREQUEST_HPP_ */
static std::string reasonToString(DisconnectReason reason)
DisconnectMessage(const char *payload, std::uint16_t size)
DisconnectMessage(DisconnectReason reason)
DisconnectReason getReason() const
std::string getMessage() const
static std::uint8_t createBasicHeader(std::uint8_t messageType, std::uint32_t length, char *message)
const std::vector< std::uint8_t > & getRawMessage() const