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