m2etis  0.4
IPv4KeyProvider.h
Go to the documentation of this file.
1 /*
2  Copyright (2016) Michael Baer, Daniel Bonrath, All rights reserved.
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 
22 #ifndef __M2ETIS_MESSAGE_IPV4KEYPROVIDER_H__
23 #define __M2ETIS_MESSAGE_IPV4KEYPROVIDER_H__
24 
25 #include <cstring>
26 
27 #include "m2etis/message/key/Key.h"
28 
29 namespace m2etis {
30 namespace message {
31 
33  protected:
34  uint8_t ip[4];
35  uint16_t port;
36 
37  public:
38  std::string ipStr() const {
39  std::ostringstream ret;
40  ret << int(ip[0]) << "." << int(ip[1]) << "." << int(ip[2]) << "." << int(ip[3]);
41  return ret.str();
42  }
43 
44  inline std::string portStr() {
45  return std::to_string(port);
46  }
47 
48  inline uint16_t getPort() const {
49  return port;
50  }
51 
52  virtual ~IPv4KeyProvider() {}
53 
54  protected:
55  IPv4KeyProvider() : port(0) {
56  for (int i = 0; i < 4; ++i) {
57  ip[i] = 0;
58  }
59  }
60 
61  std::string keytoStr() const {
62  std::ostringstream ret;
63  ret << int(ip[0]) << "." << int(ip[1]) << "." << int(ip[2]) << "." << int(ip[3])<< ":" << port;
64  return ret.str();
65  }
66 
67  void setKey(const Key<IPv4KeyProvider> & key) {
68  memcpy(&ip, &(key.ip), sizeof(ip));
69  port = key.port;
70  }
71 
72  void setKey(const std::string & key) {
73  size_t ipend = key.find(":", 0);
74  std::string locip(key.substr(0, ipend));
75  size_t pos = 0;
76  for (size_t i = 0; i < 4; ++i) {
77  size_t bytend = key.find(".", pos);
78  std::stringstream ss(key.substr(pos, bytend));
79  int t;
80  ss >> t;
81  ip[i] = uint8_t(t);
82  pos = bytend + 1;
83  }
84  std::stringstream sport(key.substr(ipend + 1, key.length() - ipend - 1));
85  sport >> port;
86  }
87 
88  bool equals(const Key<IPv4KeyProvider> & rval) const {
89  for (size_t i = 0; i < 4; ++i) {
90  if (ip[i] != rval.ip[i]) {
91  return false;
92  }
93  }
94  if (port != rval.port) {
95  return false;
96  }
97 
98  return true;
99  }
100 
101  bool smaller(const Key<IPv4KeyProvider> & rval) const {
102  for (size_t i = 0; i < 4; ++i) {
103  if (ip[i] != rval.ip[i]) {
104  if (ip[i] < rval.ip[i]) {
105  return true;
106  } else {
107  return false;
108  }
109  }
110  }
111  if (port != rval.port) {
112  if (port < rval.port) {
113  return true;
114  } else {
115  return false;
116  }
117  }
118 
119  return false;
120  }
121 
122  private:
124 
125  template<class Archive>
126  void serialize(Archive & ar, unsigned int /*version*/) {
127  ar & ip;
128  ar & port;
129  }
130  };
131 
132 } /* namespace message */
133 } /* namespace m2etis */
134 
135 #endif /* __M2ETIS_MESSAGE_IPV4KEYPROVIDER_H__ */
136 
bool equals(const Key< IPv4KeyProvider > &rval) const
void setKey(const Key< IPv4KeyProvider > &key)
void setKey(const std::string &key)
friend class boost::serialization::access
bool smaller(const Key< IPv4KeyProvider > &rval) const