[9263] | 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: |
---|
[9656] | 12 | main-programmer: Patrick Boenzli (patrick@orxonox.ethz.ch) |
---|
[9263] | 13 | */ |
---|
| 14 | |
---|
| 15 | #include "glgui.h" |
---|
| 16 | #include "shell_command.h" |
---|
| 17 | |
---|
| 18 | #include "network_stream.h" |
---|
[9266] | 19 | #include "debug.h" |
---|
[9263] | 20 | |
---|
[9396] | 21 | #include "proxy/network_settings.h" |
---|
[9308] | 22 | #include "shared_network_data.h" |
---|
[9300] | 23 | |
---|
[9263] | 24 | #include "network_monitor.h" |
---|
[9276] | 25 | #include "network_node.h" |
---|
[9300] | 26 | #include "peer_info.h" |
---|
[9279] | 27 | #include "network_stream.h" |
---|
[9300] | 28 | #include "netdefs.h" |
---|
[9263] | 29 | |
---|
[9300] | 30 | #include <vector> |
---|
[9276] | 31 | |
---|
[9300] | 32 | |
---|
[9494] | 33 | #include "network_stats_widget.h" |
---|
| 34 | |
---|
| 35 | SHELL_COMMAND(gui, NetworkMonitor, toggleGUI) |
---|
| 36 | ->setAlias("ProxyGui"); |
---|
[9404] | 37 | SHELL_COMMAND(debug, NetworkMonitor, debug); |
---|
[9263] | 38 | |
---|
[9869] | 39 | ObjectListDefinition(NetworkMonitor); |
---|
[9263] | 40 | |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * starts a network monitor |
---|
| 44 | */ |
---|
| 45 | NetworkMonitor::NetworkMonitor(NetworkStream* networkStream) |
---|
[9281] | 46 | : Synchronizeable() |
---|
[9263] | 47 | { |
---|
[9869] | 48 | this->registerObject(this, NetworkMonitor::_objectList); |
---|
[9281] | 49 | |
---|
[9279] | 50 | this->networkStream = networkStream; |
---|
[9272] | 51 | this->playerNumber = 0; |
---|
[9656] | 52 | this->connectionNumber = 0; |
---|
[9287] | 53 | // create the localnode, init it and add it to the nodes list |
---|
[9284] | 54 | this->localNode = new NetworkNode( this->networkStream->getPeerInfo()); |
---|
[9287] | 55 | this->addNode(this->localNode); |
---|
[9283] | 56 | |
---|
[9281] | 57 | this->setSynchronized(false); |
---|
[9300] | 58 | |
---|
[9308] | 59 | // read in the proxy server list, this is only the case for the master server |
---|
| 60 | if( SharedNetworkData::getInstance()->isMasterServer()) |
---|
[9300] | 61 | { |
---|
[9308] | 62 | // assuming that the config files are already read we get the proxy servers |
---|
[9494] | 63 | std::vector<IP>* proxyList = NetworkSettings::getInstance()->getProxyList(); |
---|
| 64 | std::vector<IP>::iterator it = proxyList->begin(); |
---|
[9308] | 65 | // create a peer info class and a network node class for each new proxy and add them to the passive list |
---|
| 66 | for(; it < proxyList->end(); it++) |
---|
| 67 | { |
---|
| 68 | PeerInfo* peer = new PeerInfo(); |
---|
[9494] | 69 | peer->ip = (*it); |
---|
[9656] | 70 | peer->nodeType = NET_PROXY_SERVER_PASSIVE; |
---|
[9308] | 71 | peer->userId = -1; |
---|
[9300] | 72 | |
---|
[9338] | 73 | this->addActiveProxyServer( this->localNode, peer); |
---|
[9308] | 74 | } |
---|
[9300] | 75 | } |
---|
[9494] | 76 | this->box = NULL; |
---|
[9263] | 77 | } |
---|
| 78 | |
---|
| 79 | |
---|
[9264] | 80 | /** |
---|
| 81 | * deconstructor |
---|
| 82 | */ |
---|
| 83 | NetworkMonitor::~NetworkMonitor() |
---|
[9283] | 84 | { |
---|
| 85 | if( this->localNode) |
---|
| 86 | delete this->localNode; |
---|
| 87 | } |
---|
[9263] | 88 | |
---|
| 89 | |
---|
[9266] | 90 | /** |
---|
[9277] | 91 | * add the network node |
---|
| 92 | * @param node to add |
---|
[9266] | 93 | */ |
---|
[9277] | 94 | void NetworkMonitor::addNetworkNode(NetworkNode* node) |
---|
[9266] | 95 | { |
---|
[9277] | 96 | this->nodeList.push_back(node); |
---|
[9266] | 97 | } |
---|
[9263] | 98 | |
---|
[9266] | 99 | |
---|
| 100 | /** |
---|
[9277] | 101 | * add the network node |
---|
| 102 | * @param node to add |
---|
[9266] | 103 | */ |
---|
[9277] | 104 | void NetworkMonitor::removeNetworkNode(NetworkNode* node) |
---|
[9266] | 105 | { |
---|
[9277] | 106 | std::list<NetworkNode*>::iterator it = this->nodeList.begin(); |
---|
| 107 | for(; it != this->nodeList.end(); it++) |
---|
[9266] | 108 | { |
---|
| 109 | if( *it == node) |
---|
| 110 | { |
---|
[9404] | 111 | if( node->getNodeType() == NET_CLIENT) |
---|
| 112 | this->playerNumber--; |
---|
| 113 | |
---|
[9277] | 114 | this->nodeList.erase(it); |
---|
[9266] | 115 | return; |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
[9300] | 120 | /** |
---|
| 121 | * tihs adds the new network node |
---|
| 122 | * @param ip ip of the new node |
---|
| 123 | */ |
---|
[9494] | 124 | void NetworkMonitor::addNode(const IP& ip, int nodeType) |
---|
[9308] | 125 | { |
---|
| 126 | PeerInfo* pInfo = new PeerInfo(); |
---|
| 127 | pInfo->nodeType = nodeType; |
---|
| 128 | pInfo->ip = ip; |
---|
[9266] | 129 | |
---|
[9308] | 130 | this->addNode( pInfo); |
---|
| 131 | } |
---|
[9300] | 132 | |
---|
[9308] | 133 | |
---|
[9283] | 134 | /** |
---|
| 135 | * adds a network node to the local node |
---|
| 136 | * @param pInfo node information |
---|
| 137 | */ |
---|
[9282] | 138 | void NetworkMonitor::addNode(PeerInfo* pInfo) |
---|
| 139 | { |
---|
[9283] | 140 | if( this->localNode == NULL) |
---|
| 141 | return; |
---|
[9282] | 142 | |
---|
[9656] | 143 | PRINTF(0)("^^^^^^^^^^^^^^^^^^^^^^^^^^ adding node: %i with type: %s\n\n", pInfo->userId, pInfo->getNodeTypeString().c_str()); |
---|
| 144 | |
---|
[9282] | 145 | if( pInfo->isClient()) |
---|
[9656] | 146 | { |
---|
| 147 | this->localNode->addClient(new NetworkNode(pInfo)); |
---|
| 148 | } |
---|
[9494] | 149 | else if( pInfo->isProxyServerActive()) |
---|
[9290] | 150 | { |
---|
[9656] | 151 | this->localNode->addActiveProxyServer(new NetworkNode(pInfo)); |
---|
[9290] | 152 | } |
---|
[9656] | 153 | else if( pInfo->isProxyServerActivePassive()) |
---|
| 154 | { |
---|
| 155 | this->localNode->addPassiveProxyServer(new NetworkNode(pInfo)); |
---|
| 156 | } |
---|
[9282] | 157 | else if( pInfo->isMasterServer()) |
---|
[9290] | 158 | { |
---|
[9656] | 159 | this->localNode->addMasterServer(new NetworkNode(pInfo)); |
---|
[9290] | 160 | } |
---|
[9656] | 161 | else |
---|
| 162 | assert(false); |
---|
[9282] | 163 | } |
---|
| 164 | |
---|
[9287] | 165 | |
---|
[9266] | 166 | /** |
---|
[9287] | 167 | * adds a network node to the local node |
---|
| 168 | * @param node node to add to |
---|
| 169 | * @param pInfo node information |
---|
| 170 | */ |
---|
| 171 | void NetworkMonitor::addNode(NetworkNode* node, PeerInfo* pInfo) |
---|
| 172 | { |
---|
| 173 | if( node == NULL) |
---|
| 174 | return; |
---|
| 175 | |
---|
| 176 | if( pInfo->isClient()) |
---|
[9656] | 177 | node->addClient(new NetworkNode(pInfo)); |
---|
[9494] | 178 | else if( pInfo->isProxyServerActive()) |
---|
[9656] | 179 | node->addActiveProxyServer(new NetworkNode(pInfo)); |
---|
[9287] | 180 | else if( pInfo->isMasterServer()) |
---|
[9656] | 181 | node->addMasterServer(new NetworkNode(pInfo)); |
---|
[9287] | 182 | } |
---|
| 183 | |
---|
| 184 | |
---|
[9656] | 185 | |
---|
[9287] | 186 | /** |
---|
[9656] | 187 | * removes a node from the network monitor |
---|
| 188 | * @param pInfo the node to remove |
---|
| 189 | */ |
---|
| 190 | void NetworkMonitor::removeNode(PeerInfo* pInfo) |
---|
| 191 | { |
---|
| 192 | this->removeNode(this->localNode, pInfo); |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | |
---|
| 196 | /** |
---|
| 197 | * removes the network node |
---|
| 198 | * @param node the network node where the PeerInfo node is connected to |
---|
| 199 | * @param pInfo the PeerInfo to remove |
---|
| 200 | */ |
---|
| 201 | void NetworkMonitor::removeNode(NetworkNode* node, PeerInfo* pInfo) |
---|
| 202 | { |
---|
| 203 | if( node == NULL || pInfo == NULL) |
---|
| 204 | return; |
---|
| 205 | |
---|
| 206 | if( pInfo->isClient()) |
---|
| 207 | node->removeClient(pInfo->userId); |
---|
| 208 | else if( pInfo->isProxyServerActive()) |
---|
| 209 | node->removeActiveProxyServer(pInfo->userId); |
---|
| 210 | else if( pInfo->isMasterServer()) |
---|
| 211 | node->removeMasterServer(pInfo->userId); |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | |
---|
| 215 | /** |
---|
[9300] | 216 | * @returns the proxy server of the first choice |
---|
| 217 | */ |
---|
[9494] | 218 | PeerInfo* NetworkMonitor::getFirstChoiceProxy() const |
---|
[9300] | 219 | { |
---|
| 220 | // return the fist proxy in the list |
---|
| 221 | return this->localNode->getActiveProxyServer(0); |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | |
---|
| 225 | /** |
---|
| 226 | * @returns the proxy server of second choice |
---|
| 227 | */ |
---|
[9494] | 228 | PeerInfo* NetworkMonitor::getSecondChoiceProxy() const |
---|
[9300] | 229 | { |
---|
| 230 | // return the second server in the list |
---|
| 231 | return this->localNode->getActiveProxyServer(1); |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | |
---|
| 235 | /** |
---|
[9656] | 236 | * @param userId of the searched node |
---|
| 237 | * @returns the PeerInfo of the userId peer |
---|
| 238 | */ |
---|
| 239 | PeerInfo* NetworkMonitor::getPeerByUserId( int userId) |
---|
| 240 | { |
---|
| 241 | NetworkNode* node = this->getNodeByUserId(userId); |
---|
| 242 | if( node != NULL) |
---|
| 243 | return node->getPeerInfo(); |
---|
| 244 | |
---|
| 245 | return NULL; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | /** |
---|
| 249 | * searches for a given NetworkNode |
---|
| 250 | * @param userId of the searched node |
---|
| 251 | * @returns the PeerInfo of the userId peer |
---|
| 252 | */ |
---|
| 253 | NetworkNode* NetworkMonitor::getNodeByUserId( int userId) |
---|
| 254 | { |
---|
| 255 | std::list<NetworkNode*>::iterator it = this->nodeList.begin(); |
---|
| 256 | NetworkNode* node = NULL; |
---|
| 257 | for(; it != this->nodeList.end(); it++) |
---|
| 258 | { |
---|
| 259 | node = (*it)->getNodeByUserId(userId); |
---|
| 260 | if( node != NULL) |
---|
| 261 | return node; |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | return NULL; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | |
---|
| 268 | /** |
---|
[9265] | 269 | * this displays the network monitor gui |
---|
| 270 | */ |
---|
[9494] | 271 | void NetworkMonitor::toggleGUI() |
---|
[9265] | 272 | { |
---|
[9263] | 273 | if (this->box == NULL) |
---|
| 274 | { |
---|
[9656] | 275 | this->box = new NetworkStatsWidget(this); |
---|
[9265] | 276 | this->box->showAll(); |
---|
| 277 | } |
---|
[9494] | 278 | else |
---|
| 279 | { |
---|
| 280 | delete this->box; |
---|
| 281 | this->box = NULL; |
---|
| 282 | } |
---|
[9265] | 283 | } |
---|
[9263] | 284 | |
---|
[9265] | 285 | /** |
---|
| 286 | * processes the network monitor data |
---|
| 287 | */ |
---|
| 288 | void NetworkMonitor::process() |
---|
| 289 | { |
---|
[9280] | 290 | this->playerNumber = 0; |
---|
[9279] | 291 | std::list<NetworkNode*>::iterator it = this->nodeList.begin(); |
---|
| 292 | for(; it != this->nodeList.end(); it++) |
---|
| 293 | { |
---|
[9280] | 294 | this->playerNumber += (*it)->getPlayerNumber(); |
---|
[9279] | 295 | } |
---|
[9286] | 296 | |
---|
[9300] | 297 | // check if a proxy server has to activated |
---|
| 298 | |
---|
[9400] | 299 | // this->debug(); |
---|
[9265] | 300 | } |
---|
[9263] | 301 | |
---|
[9286] | 302 | /** |
---|
| 303 | * prints out the debug informations |
---|
| 304 | */ |
---|
[9494] | 305 | void NetworkMonitor::debug() const |
---|
[9285] | 306 | { |
---|
[9289] | 307 | PRINT(0)("================================= Network Monitor::debug() =====\n"); |
---|
| 308 | PRINT(0)(" I am: %s\n", this->localNode->getPeerInfo()->getNodeTypeString().c_str()); |
---|
[9494] | 309 | PRINT(0)(" Total count of network connections: %i\n", this->connectionNumber); |
---|
| 310 | PRINT(0)(" Total count of players: %i\n", this->playerNumber); |
---|
[9373] | 311 | PRINT(0)(" Max players on this server: %i\n", SharedNetworkData::getInstance()->getMaxPlayer()); |
---|
[9287] | 312 | |
---|
[9494] | 313 | std::list<NetworkNode*>::const_iterator it = this->nodeList.begin(); |
---|
[9287] | 314 | for(; it != this->nodeList.end(); it++) |
---|
| 315 | { |
---|
[9656] | 316 | (*it)->debug(1); |
---|
[9287] | 317 | } |
---|
| 318 | |
---|
[9289] | 319 | PRINT(0)("================================================================\n"); |
---|
[9285] | 320 | } |
---|
| 321 | |
---|