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