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" |
---|
19 | #include "debug.h" |
---|
20 | |
---|
21 | #include "proxy/network_settings.h" |
---|
22 | #include "shared_network_data.h" |
---|
23 | |
---|
24 | #include "network_monitor.h" |
---|
25 | #include "network_node.h" |
---|
26 | #include "peer_info.h" |
---|
27 | #include "network_stream.h" |
---|
28 | #include "netdefs.h" |
---|
29 | |
---|
30 | #include <vector> |
---|
31 | |
---|
32 | |
---|
33 | SHELL_COMMAND(showGUI, NetworkMonitor, showGUI); |
---|
34 | SHELL_COMMAND(hideGUI, NetworkMonitor, hideGUI); |
---|
35 | // SHELL_COMMAND(output, MappedWater, saveParams); |
---|
36 | |
---|
37 | |
---|
38 | |
---|
39 | /** |
---|
40 | * starts a network monitor |
---|
41 | */ |
---|
42 | NetworkMonitor::NetworkMonitor(NetworkStream* networkStream) |
---|
43 | : Synchronizeable() |
---|
44 | { |
---|
45 | this->setClassID(CL_NETWORK_MONITOR, "NetworkMonitor"); |
---|
46 | |
---|
47 | this->networkStream = networkStream; |
---|
48 | this->playerNumber = 0; |
---|
49 | // create the localnode, init it and add it to the nodes list |
---|
50 | this->localNode = new NetworkNode( this->networkStream->getPeerInfo()); |
---|
51 | this->addNode(this->localNode); |
---|
52 | |
---|
53 | this->setSynchronized(false); |
---|
54 | |
---|
55 | // read in the proxy server list, this is only the case for the master server |
---|
56 | if( SharedNetworkData::getInstance()->isMasterServer()) |
---|
57 | { |
---|
58 | // assuming that the config files are already read we get the proxy servers |
---|
59 | std::vector<IPaddress*>* proxyList = NetworkSettings::getInstance()->getProxyList(); |
---|
60 | std::vector<IPaddress*>::iterator it = proxyList->begin(); |
---|
61 | // create a peer info class and a network node class for each new proxy and add them to the passive list |
---|
62 | for(; it < proxyList->end(); it++) |
---|
63 | { |
---|
64 | PeerInfo* peer = new PeerInfo(); |
---|
65 | peer->ip = *(*it); |
---|
66 | peer->nodeType = NET_PROXY_SERVER_ACTIVE; |
---|
67 | peer->userId = -1; |
---|
68 | |
---|
69 | NetworkNode* node = new NetworkNode(peer); |
---|
70 | this->addNode( node); |
---|
71 | this->addActiveProxyServer( this->localNode, peer); |
---|
72 | } |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | /** |
---|
78 | * deconstructor |
---|
79 | */ |
---|
80 | NetworkMonitor::~NetworkMonitor() |
---|
81 | { |
---|
82 | if( this->localNode) |
---|
83 | delete this->localNode; |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | /** |
---|
88 | * add the network node |
---|
89 | * @param node to add |
---|
90 | */ |
---|
91 | void NetworkMonitor::addNetworkNode(NetworkNode* node) |
---|
92 | { |
---|
93 | this->nodeList.push_back(node); |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | /** |
---|
98 | * add the network node |
---|
99 | * @param node to add |
---|
100 | */ |
---|
101 | void NetworkMonitor::removeNetworkNode(NetworkNode* node) |
---|
102 | { |
---|
103 | std::list<NetworkNode*>::iterator it = this->nodeList.begin(); |
---|
104 | for(; it != this->nodeList.end(); it++) |
---|
105 | { |
---|
106 | if( *it == node) |
---|
107 | { |
---|
108 | this->nodeList.erase(it); |
---|
109 | this->playerNumber--; |
---|
110 | return; |
---|
111 | } |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * tihs adds the new network node |
---|
117 | * @param ip ip of the new node |
---|
118 | */ |
---|
119 | void NetworkMonitor::addNode(IP ip, int nodeType) |
---|
120 | { |
---|
121 | PeerInfo* pInfo = new PeerInfo(); |
---|
122 | pInfo->nodeType = nodeType; |
---|
123 | pInfo->ip = ip; |
---|
124 | |
---|
125 | this->addNode( pInfo); |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | /** |
---|
130 | * adds a network node to the local node |
---|
131 | * @param pInfo node information |
---|
132 | */ |
---|
133 | void NetworkMonitor::addNode(PeerInfo* pInfo) |
---|
134 | { |
---|
135 | if( this->localNode == NULL) |
---|
136 | return; |
---|
137 | |
---|
138 | if( pInfo->isClient()) |
---|
139 | this->localNode->addClient(pInfo); |
---|
140 | else if( pInfo->isProxyServer()) |
---|
141 | { |
---|
142 | this->localNode->addActiveProxyServer(pInfo); |
---|
143 | // create a new node, since a proxy can connect clients again |
---|
144 | NetworkNode* node = new NetworkNode(pInfo); |
---|
145 | this->nodeList.push_back(node); |
---|
146 | } |
---|
147 | else if( pInfo->isMasterServer()) |
---|
148 | { |
---|
149 | this->localNode->addMasterServer(pInfo); |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | |
---|
154 | /** |
---|
155 | * adds a network node to the local node |
---|
156 | * @param node node to add to |
---|
157 | * @param pInfo node information |
---|
158 | */ |
---|
159 | void NetworkMonitor::addNode(NetworkNode* node, PeerInfo* pInfo) |
---|
160 | { |
---|
161 | if( node == NULL) |
---|
162 | return; |
---|
163 | |
---|
164 | if( pInfo->isClient()) |
---|
165 | node->addClient(pInfo); |
---|
166 | else if( pInfo->isProxyServer()) |
---|
167 | node->addActiveProxyServer(pInfo); |
---|
168 | else if( pInfo->isMasterServer()) |
---|
169 | node->addMasterServer(pInfo); |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | /** |
---|
174 | * @returns the proxy server of the first choice |
---|
175 | */ |
---|
176 | PeerInfo* NetworkMonitor::getFirstChoiceProxy() |
---|
177 | { |
---|
178 | // return the fist proxy in the list |
---|
179 | return this->localNode->getActiveProxyServer(0); |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | /** |
---|
184 | * @returns the proxy server of second choice |
---|
185 | */ |
---|
186 | PeerInfo* NetworkMonitor::getSecondChoiceProxy() |
---|
187 | { |
---|
188 | // return the second server in the list |
---|
189 | return this->localNode->getActiveProxyServer(1); |
---|
190 | } |
---|
191 | |
---|
192 | |
---|
193 | /** |
---|
194 | * this displays the network monitor gui |
---|
195 | */ |
---|
196 | void NetworkMonitor::showGUI() |
---|
197 | { |
---|
198 | if (this->box == NULL) |
---|
199 | { |
---|
200 | this->box = new OrxGui::GLGuiBox(OrxGui::Vertical); |
---|
201 | { |
---|
202 | OrxGui::GLGuiBox* waterColorBox = new OrxGui::GLGuiBox(OrxGui::Horizontal); |
---|
203 | { |
---|
204 | OrxGui::GLGuiText* waterColorText = new OrxGui::GLGuiText(); |
---|
205 | waterColorText->setText("NetworkMonitor"); |
---|
206 | waterColorBox->pack(waterColorText); |
---|
207 | } |
---|
208 | this->box->pack(waterColorBox); |
---|
209 | } |
---|
210 | |
---|
211 | this->box->showAll(); |
---|
212 | this->box->setAbsCoor2D(300, 40); |
---|
213 | OrxGui::GLGuiHandler::getInstance()->activate(); |
---|
214 | // OrxGui::GLGuiHandler::getInstance()->activateCursor(); |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | |
---|
219 | /** |
---|
220 | * hides the network monitor gui again |
---|
221 | */ |
---|
222 | void NetworkMonitor::hideGUI() |
---|
223 | { |
---|
224 | if( this->box == NULL) |
---|
225 | return; |
---|
226 | |
---|
227 | OrxGui::GLGuiHandler::getInstance()->deactivate(); |
---|
228 | // OrxGui::GLGuiHandler::getInstance()->deactivateCursor(); |
---|
229 | |
---|
230 | delete this->box; |
---|
231 | this->box = NULL; |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | /** |
---|
236 | * processes the network monitor data |
---|
237 | */ |
---|
238 | void NetworkMonitor::process() |
---|
239 | { |
---|
240 | this->playerNumber = 0; |
---|
241 | std::list<NetworkNode*>::iterator it = this->nodeList.begin(); |
---|
242 | for(; it != this->nodeList.end(); it++) |
---|
243 | { |
---|
244 | this->playerNumber += (*it)->getPlayerNumber(); |
---|
245 | } |
---|
246 | |
---|
247 | // check if a proxy server has to activated |
---|
248 | |
---|
249 | // this->debug(); |
---|
250 | } |
---|
251 | |
---|
252 | /** |
---|
253 | * prints out the debug informations |
---|
254 | */ |
---|
255 | void NetworkMonitor::debug() |
---|
256 | { |
---|
257 | PRINT(0)("================================= Network Monitor::debug() =====\n"); |
---|
258 | PRINT(0)(" I am: %s\n", this->localNode->getPeerInfo()->getNodeTypeString().c_str()); |
---|
259 | PRINT(0)(" Total count of network connections: %i\n", this->playerNumber); |
---|
260 | PRINT(0)(" Max players on this server: %i\n", SharedNetworkData::getInstance()->getMaxPlayer()); |
---|
261 | |
---|
262 | std::list<NetworkNode*>::iterator it = this->nodeList.begin(); |
---|
263 | for(; it != this->nodeList.end(); it++) |
---|
264 | { |
---|
265 | (*it)->debug(0); |
---|
266 | } |
---|
267 | |
---|
268 | PRINT(0)("================================================================\n"); |
---|
269 | } |
---|
270 | |
---|