Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/proxy/src/lib/network/monitor/network_node.cc @ 9583

Last change on this file since 9583 was 9583, checked in by patrick, 18 years ago

integration of the networkmonitor into the shared network data, using the new getPeerByUserId

File size: 6.8 KB
Line 
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#include "network_node.h"
17
18#include "debug.h"
19
20
21/**
22 * constructor
23 */
24NetworkNode::NetworkNode(PeerInfo* pInfo)
25{
26  this->playerNumber = 0;
27  this->peerInfo = pInfo;
28}
29
30
31/**
32 * deconstructor
33 */
34NetworkNode::~NetworkNode()
35{}
36
37
38/**
39 * adds a client
40 */
41void NetworkNode::addClient(PeerInfo* node)
42{
43  this->clientList.push_back(node);
44  this->playerNumber++;
45  this->connectionNumber++;
46}
47
48/**
49 * adds a proxy server
50 */
51void NetworkNode::addActiveProxyServer(PeerInfo* node)
52{
53  this->activeProxyServerList.push_back(node);
54  this->connectionNumber++;
55}
56
57/**
58 * adds a proxy server
59 */
60void NetworkNode::addPassiveProxyServer(PeerInfo* node)
61{
62  this->passiveProxyServerList.push_back(node);
63}
64
65/**
66 * adds a master server
67 */
68void NetworkNode::addMasterServer(PeerInfo* node)
69{
70  this->masterServerList.push_back(node);
71  this->playerNumber++;
72}
73
74/**
75 * removes a client
76 */
77void NetworkNode::removeClient(PeerInfo* node)
78{
79  std::list<PeerInfo*>::iterator it = this->clientList.begin();
80  for(; it != this->clientList.end(); it++)
81  {
82    if( *it == node)
83    {
84      this->clientList.erase(it);
85      this->playerNumber--;
86      return;
87    }
88  }
89
90  PRINTF(2)("Could not remove client from the list, very strange...");
91}
92
93/**
94 * removes a proxy server
95 */
96void NetworkNode::removeActiveProxyServer(PeerInfo* node)
97{
98  std::list<PeerInfo*>::iterator it = this->activeProxyServerList.begin();
99  for(; it != this->activeProxyServerList.end(); it++)
100  {
101    if( *it == node)
102    {
103      this->activeProxyServerList.erase(it);
104      this->playerNumber--;
105      return;
106    }
107  }
108
109  PRINTF(2)("Could not remove proxy server from the list, very strange...");
110}
111
112/**
113 * removes a proxy server
114 */
115void NetworkNode::removePassiveProxyServer(PeerInfo* node)
116{
117  std::list<PeerInfo*>::iterator it = this->passiveProxyServerList.begin();
118  for(; it != this->passiveProxyServerList.end(); it++)
119  {
120    if( *it == node)
121    {
122      this->passiveProxyServerList.erase(it);
123      return;
124    }
125  }
126
127  PRINTF(2)("Could not remove proxy server from the list, very strange...");
128}
129
130/**
131 * removes a master server
132 */
133void NetworkNode::removeMasterServer(PeerInfo* node)
134{
135  std::list<PeerInfo*>::iterator it = this->masterServerList.begin();
136  for(; it != this->masterServerList.end(); it++)
137  {
138    if( *it == node)
139    {
140      this->masterServerList.erase(it);
141      this->playerNumber--;
142      return;
143    }
144  }
145
146  PRINTF(2)("Could not remove client from the list, very strange...");
147}
148
149
150
151/**
152 *  gets the peer info by user id
153 * @param userId  the user id of the node to look up
154 * @return the peer info of this node NULL if nothing found
155 */
156PeerInfo* NetworkNode::getPeerByUserId( int userId)
157{
158  // look through the master server lists
159  std::list<PeerInfo*>::const_iterator it = this->masterServerList.begin();
160  for( ;it != this->masterServerList.end(); it++)
161  {
162    if( (*it)->userId == userId)
163      return (*it);
164  }
165
166  // look through the active proxy server list
167  it = this->activeProxyServerList.begin();
168  for( ; it != this->activeProxyServerList.end(); it++)
169  {
170    if( (*it)->userId == userId)
171      return (*it);
172  }
173
174  // look through the passive server list
175  it = this->passiveProxyServerList.begin();
176  for( ; it != this->passiveProxyServerList.end(); it++)
177  {
178    if( (*it)->userId == userId)
179      return (*it);
180  }
181
182  // look through the client list
183  it = this->clientList.begin();
184  for( ; it != this->clientList.end(); it++)
185  {
186    if( (*it)->userId == userId)
187      return (*it);
188  }
189
190  return NULL;
191}
192
193
194/**
195 * @param index index to the client
196 * @return the client in the list or NULL if none
197 */
198PeerInfo* NetworkNode::getClient(int index) const
199{
200  if( (int)this->clientList.size() < index)
201    return NULL;
202
203  std::list<PeerInfo*>::const_iterator it = this->clientList.begin();
204  for(int i = 0; it != this->clientList.end(); it++, i++)
205  {
206  if( i == index)
207    return (*it);
208  }
209
210  return NULL;
211}
212
213
214/**
215 * @param index index to the client
216 * @return the active proxy server in the list or NULL if none
217 */
218PeerInfo* NetworkNode::getActiveProxyServer(int index) const
219{
220  if( (int)this->activeProxyServerList.size() < index)
221    return NULL;
222
223  std::list<PeerInfo*>::const_iterator it = this->activeProxyServerList.begin();
224  for(int i = 0; it != this->activeProxyServerList.end(); it++, i++)
225  {
226    if( i == index)
227      return (*it);
228  }
229
230  return NULL;
231}
232
233
234/**
235 * @param index index to the client
236 * @return the passive proxy server in the list or NULL if none
237 */
238PeerInfo* NetworkNode::getPassiveProxyServer(int index) const
239{
240  if( (int)this->passiveProxyServerList.size() < index)
241    return NULL;
242
243  std::list<PeerInfo*>::const_iterator it = this->passiveProxyServerList.begin();
244  for(int i = 0; it != this->passiveProxyServerList.end(); it++, i++)
245  {
246    if( i == index)
247      return (*it);
248  }
249
250  return NULL;
251}
252
253
254/**
255 * @param index index to the client
256 * @return the master server in the list or NULL if none
257 */
258PeerInfo* NetworkNode::getMasterServer(int index) const
259{
260  if( (int)this->masterServerList.size() < index)
261    return NULL;
262
263  std::list<PeerInfo*>::const_iterator it = this->masterServerList.begin();
264  for(int i = 0; it != this->masterServerList.end(); it++, i++)
265  {
266    if( i == index)
267      return (*it);
268  }
269
270  return NULL;
271}
272
273
274/**
275 * debug function
276 * @param depth: depth in the tree
277 */
278void NetworkNode::debug(int depth) const
279{
280  PRINT(0)(" = %s\n", this->peerInfo->getNodeTypeString().c_str());
281
282  PRINT(0)("    master servers: %i\n", this->masterServerList.size());
283  std::list<PeerInfo*>::const_iterator it = this->masterServerList.begin();
284  for(; it != this->masterServerList.end(); it++)
285  {
286    IP* ip = &(*it)->ip;
287    PRINT(0)("     - ms, id: %i (%s)\n", (*it)->userId, ip->ipString().c_str());
288  }
289
290  PRINT(0)("    proxy servers active: %i\n", this->activeProxyServerList.size());
291  it = this->activeProxyServerList.begin();
292  for(; it != this->activeProxyServerList.end(); it++)
293  {
294    IP* ip = &(*it)->ip;
295    PRINT(0)("     - ps-a, id: %i (%s)\n", (*it)->userId, ip->ipString().c_str());
296  }
297
298  PRINT(0)("    proxy servers passive: %i\n", this->passiveProxyServerList.size());
299  it = this->passiveProxyServerList.begin();
300  for(; it != this->passiveProxyServerList.end(); it++)
301  {
302    IP* ip = &(*it)->ip;
303    PRINT(0)("     - ps-p, id: %i (%s)\n", (*it)->userId, ip->ipString().c_str());
304  }
305
306  PRINT(0)("    clients: %i\n", this->clientList.size());
307  it = this->clientList.begin();
308  for(; it != this->clientList.end(); it++)
309  {
310    IP* ip = &(*it)->ip;
311    PRINT(0)("     - client, id: %i (%s)\n", (*it)->userId, ip->ipString().c_str());
312  }
313}
314
Note: See TracBrowser for help on using the repository browser.