client-cpp  0.6.1
AbstractServerInfo.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 ABSTRACTSERVERINFO_HPP_
18 #define ABSTRACTSERVERINFO_HPP_
19 
20 #include <string>
21 #include <sstream>
22 #include <climits>
23 #include <cstdint>
24 
25 #include <boost/lexical_cast.hpp>
26 
27 #include <botan/botan.h>
28 #include <botan/base64.h>
29 
32 #include "kaa/http/HttpUrl.hpp"
33 
34 namespace kaa {
35 
36 template<ChannelType Type>
38 public:
39  AbstractServerInfo(ServerType type, const std::string& host, const std::int32_t& port
40  , const std::string& encodedPublicKey);
41 
42  AbstractServerInfo(ServerType type, const std::string& hostPort, const std::string& encodedPublicKey);
43 
44  AbstractServerInfo(ServerType type, const std::string& host, const std::int32_t& port
45  , const Botan::MemoryVector<std::uint8_t>& publicKey);
46 
47  virtual const std::string& getHost() const {
48  return host_;
49  }
50 
51  virtual std::uint16_t getPort() const {
52  return port_;
53  }
54 
55  virtual const Botan::MemoryVector<std::uint8_t>& getPublicKey() const {
56  return publicKey_;
57  }
58 
59  virtual HttpUrl getUrl() const {
60  std::stringstream ss;
61  ss << host_ << ":" << port_;
62  return HttpUrl(ss.str());
63  }
64 
65  virtual ChannelType getChannelType() const {
66  return channelType_;
67  }
68 
69  virtual ServerType getServerType() const {
70  return serverType_;
71  }
72 
73  virtual ~AbstractServerInfo() {}
74 
75 private:
76  template<typename KeyRepresentation>
77  void verify(const std::string& host, const std::int32_t& port
78  , const KeyRepresentation& encodedPublicKey);
79 
80  void assign(const std::string& host, const std::int32_t& port
81  , const std::string& encodedPublicKey);
82  void assign(const std::string& host, const std::int32_t& port
83  , const Botan::MemoryVector<std::uint8_t>& decodedPublicKey);
84 
85 private:
86  const ChannelType channelType_;
87  const ServerType serverType_;
88 
89  std::string host_;
90  std::uint16_t port_;
91 
92  Botan::MemoryVector<std::uint8_t> publicKey_;
93 };
94 
95 template<ChannelType Type>
96 AbstractServerInfo<Type>::AbstractServerInfo(ServerType type, const std::string& host, const std::int32_t& port
97  , const std::string& encodedPublicKey) : channelType_(Type), serverType_(type)
98 {
99  verify(host, port, encodedPublicKey);
100  assign(host, port, encodedPublicKey);
101 }
102 
103 template<ChannelType Type>
104 AbstractServerInfo<Type>::AbstractServerInfo(ServerType type, const std::string& hostPort, const std::string& encodedPublicKey)
105  : channelType_(Type), serverType_(type)
106 {
107  if (!hostPort.empty()) {
108  std::size_t delimPos = hostPort.find(':');
109  std::string host;
110  std::int32_t port = 0;
111 
112  if (delimPos != std::string::npos) {
113  host = hostPort.substr(0, delimPos);
114  try {
115  port = boost::lexical_cast<std::int32_t>(hostPort.substr(delimPos + 1, std::string::npos));
116  } catch (std::exception& e) {
117  throw KaaException(e.what());
118  }
119  }
120 
121  verify(host, port, encodedPublicKey);
122  assign(host, port, encodedPublicKey);
123  } else {
124  throw KaaException("Empty server host/port info");
125  }
126 }
127 
128 template<ChannelType Type>
129 AbstractServerInfo<Type>::AbstractServerInfo(ServerType type, const std::string& host, const std::int32_t& port
130  , const Botan::MemoryVector<std::uint8_t>& publicKey) : channelType_(Type), serverType_(type)
131 {
132  verify(host, port, publicKey);
133  assign(host, port, publicKey);
134 }
135 
136 template<ChannelType Type>
137 template<typename KeyRepresentation>
138 void AbstractServerInfo<Type>::verify(const std::string& host, const std::int32_t& port, const KeyRepresentation& encodedPublicKey)
139 {
140  if (host.empty()) {
141  throw KaaException("Empty server host");
142  }
143 
144  if (port < 1 || port > USHRT_MAX) {
145  throw KaaException("Server port not in valid range");
146  }
147 
148  if (encodedPublicKey.empty()) {
149  throw KaaException("Empty server public key");
150  }
151 }
152 
153 template<ChannelType Type>
154 void AbstractServerInfo<Type>::assign(const std::string& host, const std::int32_t& port, const std::string& encodedPublicKey)
155 {
156  host_ = host;
157  port_ = port;
158  publicKey_ = Botan::base64_decode(encodedPublicKey);
159 }
160 
161 template<ChannelType Type>
162 void AbstractServerInfo<Type>::assign(const std::string& host, const std::int32_t& port, const Botan::MemoryVector<std::uint8_t>& decodedPublicKey)
163 {
164  host_ = host;
165  port_ = port;
166  publicKey_ = decodedPublicKey;
167 }
168 
169 } /* namespace kaa */
170 
171 #endif /* ABSTRACTSERVERINFO_HPP_ */
virtual const std::string & getHost() const
ServerType
Definition: ServerType.hpp:22
virtual ServerType getServerType() const
AbstractServerInfo(ServerType type, const std::string &host, const std::int32_t &port, const std::string &encodedPublicKey)
virtual const Botan::MemoryVector< std::uint8_t > & getPublicKey() const
virtual ChannelType getChannelType() const
virtual std::uint16_t getPort() const
virtual HttpUrl getUrl() const