OverSim
NodeHandle.cc
Go to the documentation of this file.
1 //
2 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
25 #include "NodeHandle.h"
26 #include <cassert>
27 
28 // predefined node handle
30 
31 std::ostream& operator<<(std::ostream& os, const NodeHandle& n)
32 {
33  if (n.ip.isUnspecified()) {
34  os << "<addr unspec> ";
35  } else {
36  os << n.ip << ":" << n.port << " ";
37  }
38 
39  if (n.key.isUnspecified()) {
40  os << "<key unspec>";
41  } else {
42  os << n.key;
43  }
44 
45  return os;
46 };
47 
48 
49 //default-constructor
51 {
52  port = -1;
53  key = OverlayKey(); // unspecified key, note: OverlayKey::UNSPECIFIED_KEY might not be initialized here
54 }
55 
56 //copy constructor
58 {
59  key = handle.key;
60  port = handle.port;
61  ip = handle.ip;
62 }
63 
65 {
66  this->setIp(ta.getIp());
67  this->setPort(ta.getPort());
69 }
70 
71 //complete constructor
73  const IPvXAddress& ip, int port )
74 {
75  this->setIp(ip);
76  this->setPort(port);
77  this->setKey(key);
78 }
79 
81 {
82  this->setIp(ta.getIp());
83  this->setPort(ta.getPort());
84  this->setKey(key);
85 }
86 
87 //public
89 {
90  return (ip.isUnspecified() || key.isUnspecified());
91 }
92 
93 //public
95 {
96  this->key = rhs.key;
97  this->ip = rhs.ip;
98  this->port = rhs.port;
99 
100  return *this;
101 }
102 
103 //public
104 bool NodeHandle::operator==(const NodeHandle& rhs) const
105 {
106  assertUnspecified(rhs);
107  return (this->port == rhs.port && this->ip == rhs.ip &&
108  this->key == rhs.key);
109 }
110 
111 //public
112 bool NodeHandle::operator!=(const NodeHandle& rhs) const
113 {
114  assertUnspecified( rhs );
115  return !operator==(rhs);
116 }
117 
118 //public
119 bool NodeHandle::operator<(const NodeHandle &rhs) const
120 {
121  assertUnspecified(rhs);
122  int cmp = key.compareTo(rhs.key);
123  if (cmp < 0) {
124  return true;
125  } else if (cmp > 0) {
126  return false;
127  } else if (ip < rhs.ip) {
128  return true;
129  } else if (rhs.ip < ip) {
130  return false;
131  } else if (port < rhs.port) {
132  return true;
133  }
134 
135  return false;
136 }
137 
138 //public
139 bool NodeHandle::operator>(const NodeHandle &rhs) const
140 {
141  assertUnspecified(rhs);
142  int cmp = key.compareTo(rhs.key);
143  if (cmp > 0) {
144  return true;
145  } else if (cmp < 0) {
146  return false;
147  } else if (rhs.ip < ip) {
148  return true;
149  } else if (ip < rhs.ip) {
150  return false;
151  } else if (port > rhs.port) {
152  return true;
153  }
154 
155  return false;
156 }
157 
158 //public
159 bool NodeHandle::operator<=(const NodeHandle &rhs) const
160 {
161  return !operator>(rhs);
162 }
163 
164 //public
165 bool NodeHandle::operator>=(const NodeHandle &rhs) const
166 {
167  return !operator<(rhs);
168 }
169 
170 //public
171 void NodeHandle::setKey( const OverlayKey& key )
172 {
173  this->key = key;
174 }
175 
176 //public
178 {
179  return key;
180 }
181 
182 
183 //private
184 inline void NodeHandle::assertUnspecified( const NodeHandle& handle ) const
185 {
186  if ( this->isUnspecified() || handle.isUnspecified() )
187  opp_error("NodeHandle: Trying to compare unspecified NodeHandle!");
188 }
189 
190 
191 void NodeHandle::netPack(cCommBuffer *b)
192 {
193  //cMessage::netPack(b);
194  doPacking(b,this->ip);
195  doPacking(b,this->key);
196  doPacking(b,this->port);
197 }
198 
199 void NodeHandle::netUnpack(cCommBuffer *b)
200 {
201  //cMessage::netUnpack(b);
202  doUnpacking(b,this->ip);
203  doUnpacking(b,this->key);
204  doUnpacking(b,this->port);
205 }
206 
208 {
209  return new NodeHandle(*this);
210 }