| 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 | #define DEBUG_MODULE_NETWORK | 
|---|
| 17 |  | 
|---|
| 18 | #include "network_settings.h" | 
|---|
| 19 |  | 
|---|
| 20 | #include "netdefs.h" | 
|---|
| 21 | #include "shared_network_data.h" | 
|---|
| 22 |  | 
|---|
| 23 | #include "loading/load_param.h" | 
|---|
| 24 | #include "loading/resource_manager.h" | 
|---|
| 25 | #include "debug.h" | 
|---|
| 26 |  | 
|---|
| 27 |  | 
|---|
| 28 | ObjectListDefinition(NetworkSettings); | 
|---|
| 29 |  | 
|---|
| 30 | NetworkSettings* NetworkSettings::singletonRef = NULL; | 
|---|
| 31 |  | 
|---|
| 32 | /** | 
|---|
| 33 |  * Standard constructor | 
|---|
| 34 |  */ | 
|---|
| 35 | NetworkSettings::NetworkSettings() | 
|---|
| 36 | { | 
|---|
| 37 |   /* set the class id for the base object */ | 
|---|
| 38 |   this->registerObject(this, NetworkSettings::_objectList); | 
|---|
| 39 |  | 
|---|
| 40 |   // suggest a good standard max players value | 
|---|
| 41 |   this->maxPlayer = 10; | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 |  | 
|---|
| 45 | /** | 
|---|
| 46 |  * Standard destructor | 
|---|
| 47 |  */ | 
|---|
| 48 | NetworkSettings::~NetworkSettings() | 
|---|
| 49 | { | 
|---|
| 50 |   NetworkSettings::singletonRef = NULL; | 
|---|
| 51 |  | 
|---|
| 52 |   // remove all unused proxy data again | 
|---|
| 53 |   for(unsigned int i = 0; i < this->proxies.size(); i++) | 
|---|
| 54 |   { | 
|---|
| 55 |     IP ip = this->proxies.back(); | 
|---|
| 56 |     this->proxies.pop_back(); | 
|---|
| 57 |   } | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 |  | 
|---|
| 61 | /** | 
|---|
| 62 |  *  this loads the proxy settings from a configuration file | 
|---|
| 63 |  */ | 
|---|
| 64 | void NetworkSettings::loadData() | 
|---|
| 65 | { | 
|---|
| 66 |   std::string fileName = Resources::ResourceManager::getInstance()->mainGlobalPath().name(); | 
|---|
| 67 |   fileName += "configs/network_settings.conf"; | 
|---|
| 68 |  | 
|---|
| 69 |   TiXmlDocument doc(fileName); | 
|---|
| 70 |   if( !doc.LoadFile(fileName)) | 
|---|
| 71 |   { | 
|---|
| 72 |     PRINTF(1)("Loading file %s failed for Network Settings.\n", fileName.c_str()); | 
|---|
| 73 |     return; | 
|---|
| 74 |   } | 
|---|
| 75 |   const TiXmlElement* root = doc.RootElement(); | 
|---|
| 76 |  | 
|---|
| 77 |   if( strcmp( root->Value(), "NetworkSettings")) | 
|---|
| 78 |   { | 
|---|
| 79 |     // report an error | 
|---|
| 80 |     PRINTF(1)("Specified XML File is not an orxonox Network Settings file (NetworkSettings element missing)\n"); | 
|---|
| 81 |     return; | 
|---|
| 82 |   } | 
|---|
| 83 |  | 
|---|
| 84 |   this->loadNetworkSettings( root); | 
|---|
| 85 |  | 
|---|
| 86 |   // set the new unique id offset | 
|---|
| 87 |   // setUniqueID( maxCon+2 ) because we need one id for every handshake | 
|---|
| 88 |   // and one for handshake to reject client maxCon+1 | 
|---|
| 89 |   // NEW: at most there will be | 
|---|
| 90 |   SharedNetworkData::getInstance()->setNewUniqueID( this->maxPlayer + NET_ID_PROXY_MAX + 2); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 |  | 
|---|
| 94 |  | 
|---|
| 95 | /** | 
|---|
| 96 |  * load the proxy settings | 
|---|
| 97 |  * @param root: the root element of the xml elemnts | 
|---|
| 98 |  */ | 
|---|
| 99 | void NetworkSettings::loadNetworkSettings(const TiXmlElement* root) | 
|---|
| 100 | { | 
|---|
| 101 |   LoadParam(root, "master-addr", this, NetworkSettings, setMasterAddr); | 
|---|
| 102 |  | 
|---|
| 103 |   LoadParam(root, "max-player", this, NetworkSettings, setMaxPlayer); | 
|---|
| 104 |   LoadParam(root, "max-player-saturation", this, NetworkSettings, setMaxPlayerSaturation); | 
|---|
| 105 |  | 
|---|
| 106 |  | 
|---|
| 107 |   LOAD_PARAM_START_CYCLE(root, element); | 
|---|
| 108 |   { | 
|---|
| 109 |     element->ToText(); | 
|---|
| 110 |     LoadParam_CYCLE(element, "proxy-addr", this, NetworkSettings, setProxyAddr); | 
|---|
| 111 |   } | 
|---|
| 112 |   LOAD_PARAM_END_CYCLE(element); | 
|---|
| 113 |  | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 |  | 
|---|
| 117 | /** | 
|---|
| 118 |  * sets the master server address | 
|---|
| 119 |  * @param masterAddr: the address of the master server | 
|---|
| 120 |  */ | 
|---|
| 121 | void NetworkSettings::setMasterAddr(const std::string& masterAddr) | 
|---|
| 122 | { | 
|---|
| 123 |   IPaddress *ip = new IPaddress; | 
|---|
| 124 |  | 
|---|
| 125 |   SDLNet_ResolveHost( ip, masterAddr.c_str(), 9999 ); | 
|---|
| 126 |  | 
|---|
| 127 |   this->masterServer = *ip; | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 |  | 
|---|
| 131 | /** | 
|---|
| 132 |  * sets the proxy address to | 
|---|
| 133 |  *  @param proxyAddr: the proxy address | 
|---|
| 134 |  */ | 
|---|
| 135 | void NetworkSettings::setProxyAddr(const std::string& proxyAddr) | 
|---|
| 136 | { | 
|---|
| 137 |  | 
|---|
| 138 |   if( !SharedNetworkData::getInstance()->isMasterServer()) | 
|---|
| 139 |     return; | 
|---|
| 140 |  | 
|---|
| 141 |   this->proxies.push_back(IP(proxyAddr, 9999)); | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 |  | 
|---|
| 145 |  | 
|---|
| 146 |  | 
|---|
| 147 |  | 
|---|
| 148 |  | 
|---|
| 149 |  | 
|---|