client-cpp  0.7.0
KaaSyncRequest.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 KAASYNCREQUEST_HPP_
18 #define KAASYNCREQUEST_HPP_
19 
22 #include <arpa/inet.h>
23 
24 namespace kaa {
25 
26 enum class KaaSyncMessageType : std::uint8_t {
27  UNUSED = 0x0,
28  SYNC = 0x1,
29  BOOTSTRAP = 0x2
30 };
31 
33 {
34 public:
35  template<class T>
36  KaaSyncRequest(bool zipped, bool encrypted, std::uint16_t messageId, const T& payload, KaaSyncMessageType messageType) : message_(0)
37  {
38  char header[6];
39  std::uint8_t size = KaaTcpCommon::createBasicHeader(
41  payload.size() + KaaTcpCommon::KAA_SYNC_HEADER_LENGTH, header);
42 
43  message_.resize(payload.size() + KaaTcpCommon::KAA_SYNC_HEADER_LENGTH + size);
44 
45  std::copy(reinterpret_cast<const std::uint8_t *>(header),
46  reinterpret_cast<const std::uint8_t *>(header + size),
47  message_.begin());
48 
49  auto messageIt = message_.begin() + size;
50 
51  std::uint16_t nameLengthNetworkOrder = htons(KaaTcpCommon::KAA_TCP_NAME_LENGTH);
52  std::copy(reinterpret_cast<std::uint8_t *>(&nameLengthNetworkOrder), reinterpret_cast<std::uint8_t *>(&nameLengthNetworkOrder) + 2, messageIt);
53  messageIt += 2;
54 
55  std::copy((const std::uint8_t * const ) KaaTcpCommon::KAA_TCP_NAME,
56  (const std::uint8_t * const ) (KaaTcpCommon::KAA_TCP_NAME + KaaTcpCommon::KAA_TCP_NAME_LENGTH), messageIt);
58 
59  *(messageIt++) = KaaTcpCommon::PROTOCOL_VERSION;
60 
61  std::uint16_t messageIdNetworkOrder = htons(messageId);
62  std::copy(reinterpret_cast<std::uint8_t *>(&messageIdNetworkOrder), reinterpret_cast<std::uint8_t *>(&messageIdNetworkOrder) + 2, messageIt);
63  messageIt += 2;
64 
65  *messageIt |= (((std::uint8_t)messageType) << 4);
66 
68  if (zipped) {
70  }
71  if (encrypted) {
73  }
74  ++messageIt;
75 
76  std::copy(payload.begin(), payload.end(), messageIt);
77  }
78 
80 
81  const std::vector<std::uint8_t>& getRawMessage() const { return message_; }
82 
83 private:
84  std::vector<std::uint8_t> message_;
85 
86  static const std::uint8_t KAA_SYNC_MESSAGE_TYPE_SYNC = 0x01;
87  static const std::uint8_t KAA_SYNC_MESSAGE_TYPE_BOOTSTRAP = 0x02;
88 };
89 
90 }
91 
92 
93 #endif /* KAASYNCREQUEST_HPP_ */
static const std::uint8_t KAA_SYNC_ZIPPED_BIT
KaaSyncRequest(bool zipped, bool encrypted, std::uint16_t messageId, const T &payload, KaaSyncMessageType messageType)
const std::vector< std::uint8_t > & getRawMessage() const
static const std::uint8_t KAA_SYNC_REQUEST_BIT
static const std::uint8_t KAA_SYNC_HEADER_LENGTH
static const std::uint8_t KAA_SYNC_ENCRYPTED_BIT
static const std::uint8_t PROTOCOL_VERSION
static const std::uint16_t KAA_TCP_NAME_LENGTH
static const char *const KAA_TCP_NAME
static std::uint8_t createBasicHeader(std::uint8_t messageType, std::uint32_t length, char *message)
KaaSyncMessageType