[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: |
---|
| 12 | main-programmer: Patrick Boenzli |
---|
| 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 | |
---|
[9438] | 33 | #include "network_stats_widget.h" |
---|
| 34 | |
---|
[9439] | 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; |
---|
[9287] | 51 | // create the localnode, init it and add it to the nodes list |
---|
[9284] | 52 | this->localNode = new NetworkNode( this->networkStream->getPeerInfo()); |
---|
[9287] | 53 | this->addNode(this->localNode); |
---|
[9283] | 54 | |
---|
[9281] | 55 | this->setSynchronized(false); |
---|
[9300] | 56 | |
---|
[9308] | 57 | // read in the proxy server list, this is only the case for the master server |
---|
| 58 | if( SharedNetworkData::getInstance()->isMasterServer()) |
---|
[9300] | 59 | { |
---|
[9308] | 60 | // assuming that the config files are already read we get the proxy servers |
---|
[9426] | 61 | std::vector<IP>* proxyList = NetworkSettings::getInstance()->getProxyList(); |
---|
| 62 | std::vector<IP>::iterator it = proxyList->begin(); |
---|
[9308] | 63 | // create a peer info class and a network node class for each new proxy and add them to the passive list |
---|
| 64 | for(; it < proxyList->end(); it++) |
---|
| 65 | { |
---|
| 66 | PeerInfo* peer = new PeerInfo(); |
---|
[9426] | 67 | peer->ip = (*it); |
---|
[9308] | 68 | peer->nodeType = NET_PROXY_SERVER_ACTIVE; |
---|
| 69 | peer->userId = -1; |
---|
[9300] | 70 | |
---|
[9308] | 71 | NetworkNode* node = new NetworkNode(peer); |
---|
| 72 | this->addNode( node); |
---|
[9338] | 73 | this->addActiveProxyServer( this->localNode, peer); |
---|
[9308] | 74 | } |
---|
[9300] | 75 | } |
---|
[9438] | 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 | */ |
---|
[9431] | 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 | |
---|
| 143 | if( pInfo->isClient()) |
---|
| 144 | this->localNode->addClient(pInfo); |
---|
| 145 | else if( pInfo->isProxyServer()) |
---|
[9290] | 146 | { |
---|
[9292] | 147 | this->localNode->addActiveProxyServer(pInfo); |
---|
[9290] | 148 | // create a new node, since a proxy can connect clients again |
---|
| 149 | NetworkNode* node = new NetworkNode(pInfo); |
---|
| 150 | this->nodeList.push_back(node); |
---|
| 151 | } |
---|
[9282] | 152 | else if( pInfo->isMasterServer()) |
---|
[9290] | 153 | { |
---|
[9282] | 154 | this->localNode->addMasterServer(pInfo); |
---|
[9290] | 155 | } |
---|
[9282] | 156 | } |
---|
| 157 | |
---|
[9287] | 158 | |
---|
[9266] | 159 | /** |
---|
[9287] | 160 | * adds a network node to the local node |
---|
| 161 | * @param node node to add to |
---|
| 162 | * @param pInfo node information |
---|
| 163 | */ |
---|
| 164 | void NetworkMonitor::addNode(NetworkNode* node, PeerInfo* pInfo) |
---|
| 165 | { |
---|
| 166 | if( node == NULL) |
---|
| 167 | return; |
---|
| 168 | |
---|
| 169 | if( pInfo->isClient()) |
---|
| 170 | node->addClient(pInfo); |
---|
| 171 | else if( pInfo->isProxyServer()) |
---|
[9292] | 172 | node->addActiveProxyServer(pInfo); |
---|
[9287] | 173 | else if( pInfo->isMasterServer()) |
---|
| 174 | node->addMasterServer(pInfo); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | |
---|
| 178 | /** |
---|
[9300] | 179 | * @returns the proxy server of the first choice |
---|
| 180 | */ |
---|
[9445] | 181 | PeerInfo* NetworkMonitor::getFirstChoiceProxy() const |
---|
[9300] | 182 | { |
---|
| 183 | // return the fist proxy in the list |
---|
| 184 | return this->localNode->getActiveProxyServer(0); |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | |
---|
| 188 | /** |
---|
| 189 | * @returns the proxy server of second choice |
---|
| 190 | */ |
---|
[9445] | 191 | PeerInfo* NetworkMonitor::getSecondChoiceProxy() const |
---|
[9300] | 192 | { |
---|
| 193 | // return the second server in the list |
---|
| 194 | return this->localNode->getActiveProxyServer(1); |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | |
---|
| 198 | /** |
---|
[9265] | 199 | * this displays the network monitor gui |
---|
| 200 | */ |
---|
[9438] | 201 | void NetworkMonitor::toggleGUI() |
---|
[9265] | 202 | { |
---|
[9263] | 203 | if (this->box == NULL) |
---|
| 204 | { |
---|
[9265] | 205 | this->box = new OrxGui::GLGuiBox(OrxGui::Vertical); |
---|
[9263] | 206 | { |
---|
[9439] | 207 | NetworkStatsWidget* netStats = new NetworkStatsWidget(this); |
---|
[9438] | 208 | this->box->pack(netStats); |
---|
| 209 | |
---|
[9263] | 210 | } |
---|
| 211 | |
---|
[9265] | 212 | this->box->showAll(); |
---|
| 213 | this->box->setAbsCoor2D(300, 40); |
---|
| 214 | } |
---|
[9438] | 215 | else |
---|
| 216 | { |
---|
| 217 | delete this->box; |
---|
| 218 | this->box = NULL; |
---|
| 219 | } |
---|
[9265] | 220 | } |
---|
[9263] | 221 | |
---|
[9265] | 222 | /** |
---|
| 223 | * processes the network monitor data |
---|
| 224 | */ |
---|
| 225 | void NetworkMonitor::process() |
---|
| 226 | { |
---|
[9280] | 227 | this->playerNumber = 0; |
---|
[9279] | 228 | std::list<NetworkNode*>::iterator it = this->nodeList.begin(); |
---|
| 229 | for(; it != this->nodeList.end(); it++) |
---|
| 230 | { |
---|
[9280] | 231 | this->playerNumber += (*it)->getPlayerNumber(); |
---|
[9279] | 232 | } |
---|
[9286] | 233 | |
---|
[9300] | 234 | // check if a proxy server has to activated |
---|
| 235 | |
---|
[9400] | 236 | // this->debug(); |
---|
[9265] | 237 | } |
---|
[9263] | 238 | |
---|
[9286] | 239 | /** |
---|
| 240 | * prints out the debug informations |
---|
| 241 | */ |
---|
[9451] | 242 | void NetworkMonitor::debug() const |
---|
[9285] | 243 | { |
---|
[9289] | 244 | PRINT(0)("================================= Network Monitor::debug() =====\n"); |
---|
| 245 | PRINT(0)(" I am: %s\n", this->localNode->getPeerInfo()->getNodeTypeString().c_str()); |
---|
[9410] | 246 | PRINT(0)(" Total count of network connections: %i\n", this->connectionNumber); |
---|
| 247 | PRINT(0)(" Total count of players: %i\n", this->playerNumber); |
---|
[9373] | 248 | PRINT(0)(" Max players on this server: %i\n", SharedNetworkData::getInstance()->getMaxPlayer()); |
---|
[9287] | 249 | |
---|
[9451] | 250 | std::list<NetworkNode*>::const_iterator it = this->nodeList.begin(); |
---|
[9287] | 251 | for(; it != this->nodeList.end(); it++) |
---|
| 252 | { |
---|
[9288] | 253 | (*it)->debug(0); |
---|
[9287] | 254 | } |
---|
| 255 | |
---|
[9289] | 256 | PRINT(0)("================================================================\n"); |
---|
[9285] | 257 | } |
---|
| 258 | |
---|