client-cpp  0.7.0
DisconnectMessage.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014 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 
22 #include <boost/format.hpp>
23 
24 namespace kaa
25 {
26 
27 enum class DisconnectReason : std::uint8_t
28 {
29  NONE = 0x00,
30  BAD_REQUEST = 0x01,
31  INTERNAL_ERROR = 0x02,
32 };
33 
35 {
36 public:
37  static std::string reasonToString(DisconnectReason reason)
38  {
39  switch (reason) {
41  return "No error";
43  return "Bad request";
45  return "Internal error has been occurred";
46  default:
47  return (boost::format("Invalid Disconnect reason %1%") % (std::uint8_t) reason).str();
48  }
49  }
50 
51  DisconnectMessage(DisconnectReason reason) : message_(4), reason_(reason)
52  {
53  char header[2];
55  std::copy(reinterpret_cast<const std::uint8_t *>(header),
56  reinterpret_cast<const std::uint8_t *>(header + 2),
57  message_.begin());
58  message_[2] = 0;
59  message_[3] = (std::uint8_t) reason_;
60  }
61 
62  DisconnectMessage(const char *payload, std::uint16_t size)
63  {
64  parseMessage(payload, size);
65  }
66 
68 
69  DisconnectReason getReason() const { return reason_; }
70  std::string getMessage() const { return reasonToString(reason_); }
71 
72  const std::vector<std::uint8_t>& getRawMessage() const { return message_; }
73 
74 private:
75  void parseMessage(const char *payload, std::uint16_t size)
76  {
77  reason_ = (DisconnectReason) *(payload + 1);
78  }
79 
80 private:
81  std::vector<std::uint8_t> message_;
82  DisconnectReason reason_;
83 
84 };
85 
86 }
87 
88 
89 
90 #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