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