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