Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/masterserver/src/libraries/network/WANDiscovery.cc @ 7684

Last change on this file since 7684 was 7672, checked in by smerkli, 14 years ago

just added some notes so I understand my own stuff next week.

File size: 3.9 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau (original)
24 *   Co-authors:
25 *      Sandro 'smerkli' Merkli (adaptions to WAN)
26 *      ...
27 *
28 */
29
30#include "WANDiscovery.h"
31
32#include <enet/enet.h>
33#include <cstring>
34
35#include "util/ScopedSingletonManager.h"
36#include "core/CoreIncludes.h"
37
38
39namespace orxonox
40{
41  ManageScopedSingleton(WANDiscovery, ScopeID::Root, true);
42
43  WANDiscovery::WANDiscovery()
44  {
45    COUT(4) << "Creating WANDiscovery.\n";
46
47    /* initialize it and see if it worked */
48    if( msc.initialize() )
49      COUT(2) << "Error: could not initialize master server communications!\n";
50
51    /* connect and see if it worked */
52    if( msc.connect( MS_ADDRESS, 1234 ) )
53      COUT(2) << "Error: could not connect to master server!\n";
54
55    COUT(4) << "Initialization of WANDiscovery complete.\n";
56  }
57
58  WANDiscovery::~WANDiscovery()
59  { 
60    /* clear server list */
61    this->servers_.clear(); 
62  }
63
64  /* callback for the network reply poller */
65  int rhandler( char *addr, ENetEvent *ev )
66  { 
67    /* error recognition */
68    if( !ev || !ev->packet || !ev->packet->data )
69    { COUT(2) << "Bad arguments received in WANDiscovery's reply handler.\n";
70      return 0;
71    }
72
73    /* handle incoming data */
74    /* if a list entry arrives add to list */
75    if( !strncmp( (char*)ev->packet->data, MSPROTO_SERVERLIST_ITEM,
76      MSPROTO_SERVERLIST_ITEM_LEN ) )
77    { 
78      /* create server structure from that item */
79      packet::ServerInformation toadd;
80
81      /* fill in data */
82      toadd.setServerName( std::string((char*)ev->packet->data + 
83        MSPROTO_SERVERLIST_ITEM_LEN) );
84      toadd.setServerIP( std::string((char*)ev->packet->data + 
85        MSPROTO_SERVERLIST_ITEM_LEN) );
86
87      /* add to list */
88      WANDiscovery::getInstance().servers_.push_back( toadd );
89    }
90    else if( !strncmp( (char*)ev->packet->data, MSPROTO_SERVERLIST_END,
91      MSPROTO_SERVERLIST_END_LEN ) )
92    { 
93      /* this is the only case where 1 should be returned,
94       * as 1 is used to signal that we're done receiving
95       * the list
96       */
97      return 1; 
98    }
99
100    /* done handling, return all ok code 0 */
101    return 0;
102  }
103 
104  void WANDiscovery::discover()
105  {
106    /* clear list */
107    this->servers_.clear();
108
109    /* send request to server */
110    this->msc.sendRequest( MSPROTO_CLIENT " " MSPROTO_REQ_LIST );
111
112    /* poll for replies */
113    /* TODO add some timeout here so we don't wait indefinitely */
114    while( !((this->msc).pollForReply( rhandler )) )
115      /* nothing */;
116
117    /* done receiving. */
118  }
119
120  std::string WANDiscovery::getServerListItemName(unsigned int index)
121  {
122    /* if the index is out of range, return empty */
123    if( index >= this->servers_.size() )
124      return BLANKSTRING;
125    else
126      /* else return the name of the server */
127      return this->servers_[index].getServerName();
128  }
129
130  std::string WANDiscovery::getServerListItemIP(unsigned int index)
131  {
132    /* if the index is out of range, return empty */
133    if( index >= this->servers_.size() )
134      return BLANKSTRING;
135    else
136      /* else return the IP of the server */
137      return this->servers_[index].getServerIP();
138  }
139
140
141} // namespace orxonox
Note: See TracBrowser for help on using the repository browser.