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