m2etis  0.4
OmNetKeyProvider.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_SIM_OMNETKEYPROVIDER_H__
23 #define __M2ETIS_SIM_OMNETKEYPROVIDER_H__
24 
25 #include "m2etis/message/key/Key.h"
26 
27 #include "boost/serialization/array.hpp"
28 
29 namespace m2etis {
30 namespace sim {
31 
33  private:
34  unsigned char ip[4];
35  unsigned short port;
36 
37  public:
41  virtual ~OmNetKeyProvider() {}
42 
43  protected:
47  OmNetKeyProvider() : port() {
48  for (int i = 0; i < 4; ++i) {
49  ip[i] = 0;
50  }
51  }
52 
56  std::string keytoStr() const {
57  std::ostringstream ret;
58 
59  ret << int(ip[0]) << "." << int(ip[1]) << "." << int(ip[2]) << "." << int(ip[3])<< ":" << port;
60 
61  return ret.str();
62  }
63 
70  ip[0] = key.ip[0];
71  ip[1] = key.ip[1];
72  ip[2] = key.ip[2];
73  ip[3] = key.ip[3];
74 
75  port = key.port;
76  }
77 
83  void setKey(const std::string & key) {
84  size_t ipend = key.find(":", 0);
85 
86  std::string locip(key.substr(0, ipend));
87  size_t pos = 0;
88  for (unsigned int i = 0; i < 4; ++i) {
89  size_t bytend = key.find(".", pos);
90  std::stringstream ss(key.substr(pos, bytend));
91  int t;
92  ss >> t;
93  ip[i] = t;
94  pos = bytend + 1;
95  }
96 
97  port = std::atoi(key.substr(ipend + 1, key.find(":", ipend + 1) - ipend).c_str());
98  }
99 
103  bool equals(const message::Key<OmNetKeyProvider> & rval) const {
104  for (unsigned int i = 0; i < 4; ++i) {
105  if (ip[i] != rval.ip[i]) {
106  return false;
107  }
108  }
109  if (port != rval.port) {
110  return false;
111  }
112 
113  return true;
114  }
115 
119  bool smaller(const message::Key<OmNetKeyProvider> & rval) const {
120  for (unsigned int i = 0; i < 4; ++i) {
121  if (ip[i] != rval.ip[i]) {
122  if (ip[i] < rval.ip[i]) {
123  return true;
124  } else {
125  return false;
126  }
127  }
128  }
129  if (port != rval.port) {
130  if (port < rval.port) {
131  return true;
132  } else {
133  return false;
134  }
135  }
136 
137  return false;
138  }
139 
140  private:
142  template<class Archive>
143  void serialize(Archive & ar, unsigned int version) {
144  ar & ip;
145  ar & port;
146  }
147  };
148 
149 } /* namespace sim */
150 } /* namespace m2etis */
151 
152 #endif /* __M2ETIS_SIM_OMNETKEYPROVIDER_H__ */
153 
void setKey(const message::Key< OmNetKeyProvider > &key)
duplicates a key
virtual ~OmNetKeyProvider()
destructor
bool equals(const message::Key< OmNetKeyProvider > &rval) const
equals operator of the key class
void setKey(const std::string &key)
creates key out of a string
friend class boost::serialization::access
std::string keytoStr() const
transforms key to a string representation
OmNetKeyProvider()
default constructor
bool smaller(const message::Key< OmNetKeyProvider > &rval) const
lesser than operator for the key