/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Patrick Boenzli (patrick@orxonox.ethz.ch) */ #include "glgui.h" #include "shell_command.h" #include "network_stream.h" #include "debug.h" #include "proxy/network_settings.h" #include "shared_network_data.h" #include "network_monitor.h" #include "network_node.h" #include "peer_info.h" #include "network_stream.h" #include "netdefs.h" #include #include "network_stats_widget.h" SHELL_COMMAND(gui, NetworkMonitor, toggleGUI) ->setAlias("ProxyGui"); SHELL_COMMAND(debug, NetworkMonitor, debug); ObjectListDefinition(NetworkMonitor); /** * starts a network monitor */ NetworkMonitor::NetworkMonitor(NetworkStream* networkStream) : Synchronizeable() { this->registerObject(this, NetworkMonitor::_objectList); this->networkStream = networkStream; this->playerNumber = 0; this->connectionNumber = 0; // create the localnode, init it and add it to the nodes list this->localNode = new NetworkNode( this->networkStream->getPeerInfo()); this->addNode(this->localNode); this->setSynchronized(false); // read in the proxy server list, this is only the case for the master server if( SharedNetworkData::getInstance()->isMasterServer()) { // assuming that the config files are already read we get the proxy servers std::vector* proxyList = NetworkSettings::getInstance()->getProxyList(); std::vector::iterator it = proxyList->begin(); // create a peer info class and a network node class for each new proxy and add them to the passive list for(; it < proxyList->end(); it++) { PeerInfo* peer = new PeerInfo(); peer->ip = (*it); peer->nodeType = NET_PROXY_SERVER_PASSIVE; peer->userId = -1; this->addActiveProxyServer( this->localNode, peer); } } this->box = NULL; } /** * deconstructor */ NetworkMonitor::~NetworkMonitor() { if( this->localNode) delete this->localNode; } /** * add the network node * @param node to add */ void NetworkMonitor::addNetworkNode(NetworkNode* node) { this->nodeList.push_back(node); } /** * add the network node * @param node to add */ void NetworkMonitor::removeNetworkNode(NetworkNode* node) { std::list::iterator it = this->nodeList.begin(); for(; it != this->nodeList.end(); it++) { if( *it == node) { if( node->getNodeType() == NET_CLIENT) this->playerNumber--; this->nodeList.erase(it); return; } } } /** * tihs adds the new network node * @param ip ip of the new node */ void NetworkMonitor::addNode(const IP& ip, int nodeType) { PeerInfo* pInfo = new PeerInfo(); pInfo->nodeType = nodeType; pInfo->ip = ip; this->addNode( pInfo); } /** * adds a network node to the local node * @param pInfo node information */ void NetworkMonitor::addNode(PeerInfo* pInfo) { if( this->localNode == NULL) return; PRINTF(0)("^^^^^^^^^^^^^^^^^^^^^^^^^^ adding node: %i with type: %s\n\n", pInfo->userId, pInfo->getNodeTypeString().c_str()); if( pInfo->isClient()) { this->localNode->addClient(new NetworkNode(pInfo)); } else if( pInfo->isProxyServerActive()) { this->localNode->addActiveProxyServer(new NetworkNode(pInfo)); } else if( pInfo->isProxyServerActivePassive()) { this->localNode->addPassiveProxyServer(new NetworkNode(pInfo)); } else if( pInfo->isMasterServer()) { this->localNode->addMasterServer(new NetworkNode(pInfo)); } else assert(false); } /** * adds a network node to the local node * @param node node to add to * @param pInfo node information */ void NetworkMonitor::addNode(NetworkNode* node, PeerInfo* pInfo) { if( node == NULL) return; if( pInfo->isClient()) node->addClient(new NetworkNode(pInfo)); else if( pInfo->isProxyServerActive()) node->addActiveProxyServer(new NetworkNode(pInfo)); else if( pInfo->isMasterServer()) node->addMasterServer(new NetworkNode(pInfo)); } /** * removes a node from the network monitor * @param pInfo the node to remove */ void NetworkMonitor::removeNode(PeerInfo* pInfo) { this->removeNode(this->localNode, pInfo); } /** * removes the network node * @param node the network node where the PeerInfo node is connected to * @param pInfo the PeerInfo to remove */ void NetworkMonitor::removeNode(NetworkNode* node, PeerInfo* pInfo) { if( node == NULL || pInfo == NULL) return; if( pInfo->isClient()) node->removeClient(pInfo->userId); else if( pInfo->isProxyServerActive()) node->removeActiveProxyServer(pInfo->userId); else if( pInfo->isMasterServer()) node->removeMasterServer(pInfo->userId); } /** * @returns the proxy server of the first choice */ PeerInfo* NetworkMonitor::getFirstChoiceProxy() const { // return the fist proxy in the list return this->localNode->getActiveProxyServer(0); } /** * @returns the proxy server of second choice */ PeerInfo* NetworkMonitor::getSecondChoiceProxy() const { // return the second server in the list return this->localNode->getActiveProxyServer(1); } /** * @param userId of the searched node * @returns the PeerInfo of the userId peer */ PeerInfo* NetworkMonitor::getPeerByUserId( int userId) { NetworkNode* node = this->getNodeByUserId(userId); if( node != NULL) return node->getPeerInfo(); return NULL; } /** * searches for a given NetworkNode * @param userId of the searched node * @returns the PeerInfo of the userId peer */ NetworkNode* NetworkMonitor::getNodeByUserId( int userId) { std::list::iterator it = this->nodeList.begin(); NetworkNode* node = NULL; for(; it != this->nodeList.end(); it++) { node = (*it)->getNodeByUserId(userId); if( node != NULL) return node; } return NULL; } /** * this displays the network monitor gui */ void NetworkMonitor::toggleGUI() { if (this->box == NULL) { this->box = new NetworkStatsWidget(this); this->box->showAll(); } else { delete this->box; this->box = NULL; } } /** * processes the network monitor data */ void NetworkMonitor::process() { this->playerNumber = 0; std::list::iterator it = this->nodeList.begin(); for(; it != this->nodeList.end(); it++) { this->playerNumber += (*it)->getPlayerNumber(); } // check if a proxy server has to activated // this->debug(); } /** * prints out the debug informations */ void NetworkMonitor::debug() const { PRINT(0)("================================= Network Monitor::debug() =====\n"); PRINT(0)(" I am: %s\n", this->localNode->getPeerInfo()->getNodeTypeString().c_str()); PRINT(0)(" Total count of network connections: %i\n", this->connectionNumber); PRINT(0)(" Total count of players: %i\n", this->playerNumber); PRINT(0)(" Max players on this server: %i\n", SharedNetworkData::getInstance()->getMaxPlayer()); std::list::const_iterator it = this->nodeList.begin(); for(; it != this->nodeList.end(); it++) { (*it)->debug(1); } PRINT(0)("================================================================\n"); }