1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Patrick Boenzli |
---|
13 | */ |
---|
14 | |
---|
15 | |
---|
16 | #include "network_node.h" |
---|
17 | |
---|
18 | #include "debug.h" |
---|
19 | |
---|
20 | |
---|
21 | /** |
---|
22 | * constructor |
---|
23 | */ |
---|
24 | NetworkNode::NetworkNode(PeerInfo* pInfo) |
---|
25 | { |
---|
26 | this->playerNumber = 0; |
---|
27 | this->peerInfo = pInfo; |
---|
28 | } |
---|
29 | |
---|
30 | |
---|
31 | /** |
---|
32 | * deconstructor |
---|
33 | */ |
---|
34 | NetworkNode::~NetworkNode() |
---|
35 | {} |
---|
36 | |
---|
37 | |
---|
38 | /** |
---|
39 | * adds a client |
---|
40 | */ |
---|
41 | void NetworkNode::addClient(NetworkNode* node) |
---|
42 | { |
---|
43 | this->clientList.push_back(node); |
---|
44 | this->playerNumber++; |
---|
45 | this->connectionNumber++; |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * adds a proxy server |
---|
50 | */ |
---|
51 | void NetworkNode::addActiveProxyServer(NetworkNode* node) |
---|
52 | { |
---|
53 | this->activeProxyServerList.push_back(node); |
---|
54 | this->connectionNumber++; |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * adds a proxy server |
---|
59 | */ |
---|
60 | void NetworkNode::addPassiveProxyServer(NetworkNode* node) |
---|
61 | { |
---|
62 | this->passiveProxyServerList.push_back(node); |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * adds a master server |
---|
67 | */ |
---|
68 | void NetworkNode::addMasterServer(NetworkNode* node) |
---|
69 | { |
---|
70 | this->masterServerList.push_back(node); |
---|
71 | this->playerNumber++; |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * removes a client |
---|
76 | */ |
---|
77 | void NetworkNode::removeClient(NetworkNode* node) |
---|
78 | { |
---|
79 | std::list<NetworkNode*>::iterator it = this->clientList.begin(); |
---|
80 | for(; it != this->clientList.end(); it++) |
---|
81 | { |
---|
82 | if( *it == node) |
---|
83 | { |
---|
84 | this->clientList.erase(it); |
---|
85 | this->playerNumber--; |
---|
86 | return; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | PRINTF(2)("Could not remove client from the list, very strange..."); |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * removes a proxy server |
---|
95 | */ |
---|
96 | void NetworkNode::removeActiveProxyServer(NetworkNode* node) |
---|
97 | { |
---|
98 | std::list<NetworkNode*>::iterator it = this->activeProxyServerList.begin(); |
---|
99 | for(; it != this->activeProxyServerList.end(); it++) |
---|
100 | { |
---|
101 | if( *it == node) |
---|
102 | { |
---|
103 | this->activeProxyServerList.erase(it); |
---|
104 | this->playerNumber--; |
---|
105 | return; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | PRINTF(2)("Could not remove proxy server from the list, very strange..."); |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * removes a proxy server |
---|
114 | */ |
---|
115 | void NetworkNode::removePassiveProxyServer(NetworkNode* node) |
---|
116 | { |
---|
117 | std::list<NetworkNode*>::iterator it = this->passiveProxyServerList.begin(); |
---|
118 | for(; it != this->passiveProxyServerList.end(); it++) |
---|
119 | { |
---|
120 | if( *it == node) |
---|
121 | { |
---|
122 | this->passiveProxyServerList.erase(it); |
---|
123 | return; |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | PRINTF(2)("Could not remove proxy server from the list, very strange..."); |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | * removes a master server |
---|
132 | */ |
---|
133 | void NetworkNode::removeMasterServer(NetworkNode* node) |
---|
134 | { |
---|
135 | std::list<NetworkNode*>::iterator it = this->masterServerList.begin(); |
---|
136 | for(; it != this->masterServerList.end(); it++) |
---|
137 | { |
---|
138 | if( *it == node) |
---|
139 | { |
---|
140 | this->masterServerList.erase(it); |
---|
141 | this->playerNumber--; |
---|
142 | return; |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | PRINTF(2)("Could not remove client from the list, very strange..."); |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | |
---|
151 | |
---|
152 | /** |
---|
153 | * removes a client |
---|
154 | */ |
---|
155 | void NetworkNode::removeClient(int userId) |
---|
156 | { |
---|
157 | std::list<NetworkNode*>::iterator it = this->clientList.begin(); |
---|
158 | for(; it != this->clientList.end(); it++) |
---|
159 | { |
---|
160 | if( (*it)->getPeerInfo()->userId == userId) |
---|
161 | { |
---|
162 | this->clientList.erase(it); |
---|
163 | this->playerNumber--; |
---|
164 | return; |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | PRINTF(2)("Could not remove client from the list, very strange..."); |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | * removes a proxy server |
---|
173 | */ |
---|
174 | void NetworkNode::removeActiveProxyServer(int userId) |
---|
175 | { |
---|
176 | std::list<NetworkNode*>::iterator it = this->activeProxyServerList.begin(); |
---|
177 | for(; it != this->activeProxyServerList.end(); it++) |
---|
178 | { |
---|
179 | if( (*it)->getPeerInfo()->userId == userId) |
---|
180 | { |
---|
181 | this->activeProxyServerList.erase(it); |
---|
182 | this->playerNumber--; |
---|
183 | return; |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | PRINTF(2)("Could not remove proxy server from the list, very strange..."); |
---|
188 | } |
---|
189 | |
---|
190 | /** |
---|
191 | * removes a proxy server |
---|
192 | */ |
---|
193 | void NetworkNode::removePassiveProxyServer(int userId) |
---|
194 | { |
---|
195 | std::list<NetworkNode*>::iterator it = this->passiveProxyServerList.begin(); |
---|
196 | for(; it != this->passiveProxyServerList.end(); it++) |
---|
197 | { |
---|
198 | if( (*it)->getPeerInfo()->userId == userId) |
---|
199 | { |
---|
200 | this->passiveProxyServerList.erase(it); |
---|
201 | return; |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | PRINTF(2)("Could not remove proxy server from the list, very strange..."); |
---|
206 | } |
---|
207 | |
---|
208 | /** |
---|
209 | * removes a master server |
---|
210 | */ |
---|
211 | void NetworkNode::removeMasterServer(int userId) |
---|
212 | { |
---|
213 | std::list<NetworkNode*>::iterator it = this->masterServerList.begin(); |
---|
214 | for(; it != this->masterServerList.end(); it++) |
---|
215 | { |
---|
216 | if( (*it)->getPeerInfo()->userId == userId) |
---|
217 | { |
---|
218 | this->masterServerList.erase(it); |
---|
219 | this->playerNumber--; |
---|
220 | return; |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | PRINTF(2)("Could not remove client from the list, very strange..."); |
---|
225 | } |
---|
226 | |
---|
227 | |
---|
228 | |
---|
229 | |
---|
230 | |
---|
231 | /** |
---|
232 | * gets the peer info by user id |
---|
233 | * @param userId the user id of the node to look up |
---|
234 | * @return the peer info of this node NULL if nothing found |
---|
235 | */ |
---|
236 | PeerInfo* NetworkNode::getPeerByUserId( int userId) |
---|
237 | { |
---|
238 | // look through the master server lists |
---|
239 | std::list<NetworkNode*>::const_iterator it = this->masterServerList.begin(); |
---|
240 | for( ;it != this->masterServerList.end(); it++) |
---|
241 | { |
---|
242 | if( (*it)->getPeerInfo()->userId == userId) |
---|
243 | return (*it)->getPeerInfo(); |
---|
244 | } |
---|
245 | |
---|
246 | // look through the active proxy server list |
---|
247 | it = this->activeProxyServerList.begin(); |
---|
248 | for( ; it != this->activeProxyServerList.end(); it++) |
---|
249 | { |
---|
250 | if( (*it)->getPeerInfo()->userId == userId) |
---|
251 | return (*it)->getPeerInfo(); |
---|
252 | } |
---|
253 | |
---|
254 | // look through the passive server list |
---|
255 | it = this->passiveProxyServerList.begin(); |
---|
256 | for( ; it != this->passiveProxyServerList.end(); it++) |
---|
257 | { |
---|
258 | if( (*it)->getPeerInfo()->userId == userId) |
---|
259 | return (*it)->getPeerInfo(); |
---|
260 | } |
---|
261 | |
---|
262 | // look through the client list |
---|
263 | it = this->clientList.begin(); |
---|
264 | for( ; it != this->clientList.end(); it++) |
---|
265 | { |
---|
266 | if( (*it)->getPeerInfo()->userId == userId) |
---|
267 | return (*it)->getPeerInfo(); |
---|
268 | } |
---|
269 | |
---|
270 | return NULL; |
---|
271 | } |
---|
272 | |
---|
273 | |
---|
274 | /** |
---|
275 | * @param index index to the client |
---|
276 | * @return the client in the list or NULL if none |
---|
277 | */ |
---|
278 | PeerInfo* NetworkNode::getClient(int index) const |
---|
279 | { |
---|
280 | if( (int)this->clientList.size() < index) |
---|
281 | return NULL; |
---|
282 | |
---|
283 | std::list<NetworkNode*>::const_iterator it = this->clientList.begin(); |
---|
284 | for(int i = 0; it != this->clientList.end(); it++, i++) |
---|
285 | { |
---|
286 | if( i == index) |
---|
287 | return (*it)->getPeerInfo(); |
---|
288 | } |
---|
289 | |
---|
290 | return NULL; |
---|
291 | } |
---|
292 | |
---|
293 | |
---|
294 | /** |
---|
295 | * @param index index to the client |
---|
296 | * @return the active proxy server in the list or NULL if none |
---|
297 | */ |
---|
298 | PeerInfo* NetworkNode::getActiveProxyServer(int index) const |
---|
299 | { |
---|
300 | if( (int)this->activeProxyServerList.size() < index) |
---|
301 | return NULL; |
---|
302 | |
---|
303 | std::list<NetworkNode*>::const_iterator it = this->activeProxyServerList.begin(); |
---|
304 | for(int i = 0; it != this->activeProxyServerList.end(); it++, i++) |
---|
305 | { |
---|
306 | if( i == index) |
---|
307 | return (*it)->getPeerInfo(); |
---|
308 | } |
---|
309 | |
---|
310 | return NULL; |
---|
311 | } |
---|
312 | |
---|
313 | |
---|
314 | /** |
---|
315 | * @param index index to the client |
---|
316 | * @return the passive proxy server in the list or NULL if none |
---|
317 | */ |
---|
318 | PeerInfo* NetworkNode::getPassiveProxyServer(int index) const |
---|
319 | { |
---|
320 | if( (int)this->passiveProxyServerList.size() < index) |
---|
321 | return NULL; |
---|
322 | |
---|
323 | std::list<NetworkNode*>::const_iterator it = this->passiveProxyServerList.begin(); |
---|
324 | for(int i = 0; it != this->passiveProxyServerList.end(); it++, i++) |
---|
325 | { |
---|
326 | if( i == index) |
---|
327 | return (*it)->getPeerInfo(); |
---|
328 | } |
---|
329 | |
---|
330 | return NULL; |
---|
331 | } |
---|
332 | |
---|
333 | |
---|
334 | /** |
---|
335 | * @param index index to the client |
---|
336 | * @return the master server in the list or NULL if none |
---|
337 | */ |
---|
338 | PeerInfo* NetworkNode::getMasterServer(int index) const |
---|
339 | { |
---|
340 | if( (int)this->masterServerList.size() < index) |
---|
341 | return NULL; |
---|
342 | |
---|
343 | std::list<NetworkNode*>::const_iterator it = this->masterServerList.begin(); |
---|
344 | for(int i = 0; it != this->masterServerList.end(); it++, i++) |
---|
345 | { |
---|
346 | if( i == index) |
---|
347 | return (*it)->getPeerInfo(); |
---|
348 | } |
---|
349 | |
---|
350 | return NULL; |
---|
351 | } |
---|
352 | |
---|
353 | |
---|
354 | /** |
---|
355 | * debug function |
---|
356 | * @param depth: depth in the tree |
---|
357 | */ |
---|
358 | void NetworkNode::debug(int depth) const |
---|
359 | { |
---|
360 | char indent[depth +1]; |
---|
361 | for( int i = 0; i < depth; i++) { indent[i] = ' '; } |
---|
362 | indent[depth] = '\0'; |
---|
363 | |
---|
364 | PRINT(0)("%s + %s, with ip: %s\n", indent, this->peerInfo->getNodeTypeString().c_str(), this->peerInfo->ip.ipString().c_str()); |
---|
365 | std::list<NetworkNode*>::const_iterator it; |
---|
366 | if( !this->masterServerList.empty()) |
---|
367 | { |
---|
368 | // PRINT(0)("%s + master servers: %i\n", indent, this->masterServerList.size()); |
---|
369 | it = this->masterServerList.begin(); |
---|
370 | |
---|
371 | for(; it != this->masterServerList.end(); it++) |
---|
372 | { |
---|
373 | // IP* ip = &(*it)->getPeerInfo()->ip; |
---|
374 | // PRINT(0)("%s - ms, id: %i (%s)\n", indent, (*it)->getPeerInfo()->userId, ip->ipString().c_str()); |
---|
375 | (*it)->debug(depth+1); |
---|
376 | } |
---|
377 | } |
---|
378 | |
---|
379 | if( !this->activeProxyServerList.empty()) |
---|
380 | { |
---|
381 | // PRINT(0)("%s + proxy servers active: %i\n", indent, this->activeProxyServerList.size()); |
---|
382 | it = this->activeProxyServerList.begin(); |
---|
383 | |
---|
384 | for(; it != this->activeProxyServerList.end(); it++) |
---|
385 | { |
---|
386 | // IP* ip = &(*it)->getPeerInfo()->ip; |
---|
387 | // PRINT(0)("%s - ps-a, id: %i (%s)\n", indent, (*it)->getPeerInfo()->userId, ip->ipString().c_str()); |
---|
388 | (*it)->debug(depth+1); |
---|
389 | } |
---|
390 | } |
---|
391 | |
---|
392 | |
---|
393 | if( !this->passiveProxyServerList.empty()) |
---|
394 | { |
---|
395 | // PRINT(0)("%s proxy servers passive: %i\n", indent, this->passiveProxyServerList.size()); |
---|
396 | it = this->passiveProxyServerList.begin(); |
---|
397 | |
---|
398 | for(; it != this->passiveProxyServerList.end(); it++) |
---|
399 | { |
---|
400 | // IP* ip = &(*it)->getPeerInfo()->ip; |
---|
401 | // PRINT(0)("%s - ps-p, id: %i (%s)\n", indent, (*it)->getPeerInfo()->userId, ip->ipString().c_str()); |
---|
402 | (*it)->debug(depth+1); |
---|
403 | } |
---|
404 | } |
---|
405 | |
---|
406 | if( !this->clientList.empty()) |
---|
407 | { |
---|
408 | // PRINT(0)("%s clients: %i\n", indent, this->clientList.size()); |
---|
409 | it = this->clientList.begin(); |
---|
410 | |
---|
411 | for(; it != this->clientList.end(); it++) |
---|
412 | { |
---|
413 | // IP* ip = &(*it)->getPeerInfo()->ip; |
---|
414 | // PRINT(0)("%s - client, id: %i (%s)\n", indent, (*it)->getPeerInfo()->userId, ip->ipString().c_str()); |
---|
415 | (*it)->debug(depth+1); |
---|
416 | } |
---|
417 | } |
---|
418 | } |
---|
419 | |
---|