[9276] | 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 | |
---|
[9277] | 18 | #include "debug.h" |
---|
[9276] | 19 | |
---|
[9277] | 20 | /** |
---|
| 21 | * constructor |
---|
| 22 | */ |
---|
[9284] | 23 | NetworkNode::NetworkNode(PeerInfo* pInfo) |
---|
[9277] | 24 | { |
---|
| 25 | this->playerNumber = 0; |
---|
[9284] | 26 | this->peerInfo = pInfo; |
---|
[9277] | 27 | } |
---|
[9276] | 28 | |
---|
| 29 | |
---|
[9277] | 30 | /** |
---|
| 31 | * deconstructor |
---|
| 32 | */ |
---|
[9276] | 33 | NetworkNode::~NetworkNode() |
---|
| 34 | {} |
---|
| 35 | |
---|
[9277] | 36 | |
---|
| 37 | /** |
---|
| 38 | * adds a client |
---|
| 39 | */ |
---|
[9656] | 40 | void NetworkNode::addClient(NetworkNode* node) |
---|
[9277] | 41 | { |
---|
| 42 | this->clientList.push_back(node); |
---|
| 43 | this->playerNumber++; |
---|
[9404] | 44 | this->connectionNumber++; |
---|
[9277] | 45 | } |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * adds a proxy server |
---|
| 49 | */ |
---|
[9656] | 50 | void NetworkNode::addActiveProxyServer(NetworkNode* node) |
---|
[9277] | 51 | { |
---|
[9292] | 52 | this->activeProxyServerList.push_back(node); |
---|
[9404] | 53 | this->connectionNumber++; |
---|
[9277] | 54 | } |
---|
| 55 | |
---|
| 56 | /** |
---|
[9292] | 57 | * adds a proxy server |
---|
| 58 | */ |
---|
[9656] | 59 | void NetworkNode::addPassiveProxyServer(NetworkNode* node) |
---|
[9292] | 60 | { |
---|
| 61 | this->passiveProxyServerList.push_back(node); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | /** |
---|
[9277] | 65 | * adds a master server |
---|
| 66 | */ |
---|
[9656] | 67 | void NetworkNode::addMasterServer(NetworkNode* node) |
---|
[9277] | 68 | { |
---|
| 69 | this->masterServerList.push_back(node); |
---|
| 70 | this->playerNumber++; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | * removes a client |
---|
| 75 | */ |
---|
[9656] | 76 | void NetworkNode::removeClient(NetworkNode* node) |
---|
[9277] | 77 | { |
---|
[9656] | 78 | std::list<NetworkNode*>::iterator it = this->clientList.begin(); |
---|
[9277] | 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 | */ |
---|
[9656] | 95 | void NetworkNode::removeActiveProxyServer(NetworkNode* node) |
---|
[9277] | 96 | { |
---|
[9656] | 97 | std::list<NetworkNode*>::iterator it = this->activeProxyServerList.begin(); |
---|
[9292] | 98 | for(; it != this->activeProxyServerList.end(); it++) |
---|
[9277] | 99 | { |
---|
| 100 | if( *it == node) |
---|
| 101 | { |
---|
[9292] | 102 | this->activeProxyServerList.erase(it); |
---|
[9277] | 103 | this->playerNumber--; |
---|
| 104 | return; |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | PRINTF(2)("Could not remove proxy server from the list, very strange..."); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /** |
---|
[9292] | 112 | * removes a proxy server |
---|
| 113 | */ |
---|
[9656] | 114 | void NetworkNode::removePassiveProxyServer(NetworkNode* node) |
---|
[9292] | 115 | { |
---|
[9656] | 116 | std::list<NetworkNode*>::iterator it = this->passiveProxyServerList.begin(); |
---|
[9292] | 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 | /** |
---|
[9277] | 130 | * removes a master server |
---|
| 131 | */ |
---|
[9656] | 132 | void NetworkNode::removeMasterServer(NetworkNode* node) |
---|
[9277] | 133 | { |
---|
[9656] | 134 | std::list<NetworkNode*>::iterator it = this->masterServerList.begin(); |
---|
[9277] | 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 | } |
---|
[9288] | 147 | |
---|
| 148 | |
---|
[9656] | 149 | |
---|
| 150 | |
---|
[9300] | 151 | /** |
---|
[9656] | 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 | /** |
---|
[9300] | 274 | * @param index index to the client |
---|
| 275 | * @return the client in the list or NULL if none |
---|
| 276 | */ |
---|
[9494] | 277 | PeerInfo* NetworkNode::getClient(int index) const |
---|
[9300] | 278 | { |
---|
[9656] | 279 | if( (int)this->clientList.size() < index) |
---|
[9300] | 280 | return NULL; |
---|
[9288] | 281 | |
---|
[9656] | 282 | std::list<NetworkNode*>::const_iterator it = this->clientList.begin(); |
---|
[9300] | 283 | for(int i = 0; it != this->clientList.end(); it++, i++) |
---|
| 284 | { |
---|
| 285 | if( i == index) |
---|
[9656] | 286 | return (*it)->getPeerInfo(); |
---|
[9300] | 287 | } |
---|
| 288 | |
---|
| 289 | return NULL; |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | |
---|
[9288] | 293 | /** |
---|
[9300] | 294 | * @param index index to the client |
---|
| 295 | * @return the active proxy server in the list or NULL if none |
---|
| 296 | */ |
---|
[9494] | 297 | PeerInfo* NetworkNode::getActiveProxyServer(int index) const |
---|
[9300] | 298 | { |
---|
[9656] | 299 | if( (int)this->activeProxyServerList.size() < index) |
---|
[9300] | 300 | return NULL; |
---|
| 301 | |
---|
[9656] | 302 | std::list<NetworkNode*>::const_iterator it = this->activeProxyServerList.begin(); |
---|
[9300] | 303 | for(int i = 0; it != this->activeProxyServerList.end(); it++, i++) |
---|
| 304 | { |
---|
| 305 | if( i == index) |
---|
[9656] | 306 | return (*it)->getPeerInfo(); |
---|
[9300] | 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 | */ |
---|
[9494] | 317 | PeerInfo* NetworkNode::getPassiveProxyServer(int index) const |
---|
[9300] | 318 | { |
---|
[9656] | 319 | if( (int)this->passiveProxyServerList.size() < index) |
---|
[9300] | 320 | return NULL; |
---|
| 321 | |
---|
[9656] | 322 | std::list<NetworkNode*>::const_iterator it = this->passiveProxyServerList.begin(); |
---|
[9300] | 323 | for(int i = 0; it != this->passiveProxyServerList.end(); it++, i++) |
---|
| 324 | { |
---|
| 325 | if( i == index) |
---|
[9656] | 326 | return (*it)->getPeerInfo(); |
---|
[9300] | 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 | */ |
---|
[9494] | 337 | PeerInfo* NetworkNode::getMasterServer(int index) const |
---|
[9300] | 338 | { |
---|
[9656] | 339 | if( (int)this->masterServerList.size() < index) |
---|
[9300] | 340 | return NULL; |
---|
| 341 | |
---|
[9656] | 342 | std::list<NetworkNode*>::const_iterator it = this->masterServerList.begin(); |
---|
[9300] | 343 | for(int i = 0; it != this->masterServerList.end(); it++, i++) |
---|
| 344 | { |
---|
| 345 | if( i == index) |
---|
[9656] | 346 | return (*it)->getPeerInfo(); |
---|
[9300] | 347 | } |
---|
| 348 | |
---|
| 349 | return NULL; |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | |
---|
[9656] | 353 | |
---|
[9300] | 354 | /** |
---|
[9656] | 355 | * searches for a given NetworkNode |
---|
| 356 | * @param userId of the searched node |
---|
| 357 | * @returns the PeerInfo of the userId peer |
---|
[9288] | 358 | */ |
---|
[9656] | 359 | NetworkNode* NetworkNode::getNodeByUserId( int userId) |
---|
[9288] | 360 | { |
---|
[9656] | 361 | if( this->peerInfo->userId == userId) |
---|
| 362 | return this; |
---|
[9288] | 363 | |
---|
[9656] | 364 | |
---|
| 365 | NetworkNode* node = NULL; |
---|
| 366 | std::list<NetworkNode*>::const_iterator it = this->masterServerList.begin(); |
---|
| 367 | |
---|
[9288] | 368 | for(; it != this->masterServerList.end(); it++) |
---|
| 369 | { |
---|
[9656] | 370 | node = (*it)->getNodeByUserId(userId); |
---|
| 371 | if( node != NULL) |
---|
| 372 | return node; |
---|
[9288] | 373 | } |
---|
| 374 | |
---|
[9292] | 375 | it = this->activeProxyServerList.begin(); |
---|
| 376 | for(; it != this->activeProxyServerList.end(); it++) |
---|
[9288] | 377 | { |
---|
[9656] | 378 | node = (*it)->getNodeByUserId(userId); |
---|
| 379 | if( node != NULL) |
---|
| 380 | return node; |
---|
[9288] | 381 | } |
---|
| 382 | |
---|
[9308] | 383 | it = this->passiveProxyServerList.begin(); |
---|
| 384 | for(; it != this->passiveProxyServerList.end(); it++) |
---|
| 385 | { |
---|
[9656] | 386 | node = (*it)->getNodeByUserId(userId); |
---|
| 387 | if( node != NULL) |
---|
| 388 | return node; |
---|
[9308] | 389 | } |
---|
| 390 | |
---|
[9288] | 391 | it = this->clientList.begin(); |
---|
| 392 | for(; it != this->clientList.end(); it++) |
---|
| 393 | { |
---|
[9656] | 394 | node = (*it)->getNodeByUserId(userId); |
---|
| 395 | if( node != NULL) |
---|
| 396 | return node; |
---|
[9288] | 397 | } |
---|
[9656] | 398 | |
---|
| 399 | return NULL; |
---|
[9288] | 400 | } |
---|
| 401 | |
---|
[9656] | 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 | |
---|