Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7659 was 7659, checked in by smerkli, 13 years ago

wee

File size: 3.7 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    /* create master server communications object */
46    //this->msc = MasterServerComm();
47
48    /* initialize it and see if it worked */
49    if( msc.initialize() )
50      COUT(1) << "Error: could not initialize master server communications!\n";
51
52    /* connect and see if it worked */
53    if( msc.connect( MS_ADDRESS, 1234 ) )
54      COUT(1) << "Error: could not connect to master server!\n";
55  }
56
57  WANDiscovery::~WANDiscovery()
58  { 
59    /* clear server list */
60    this->servers_.clear(); 
61  }
62
63  /* callback for the network reply poller */
64  int rhandler( char *addr, ENetEvent *ev )
65  { 
66    /* error recognition */
67    if( !ev || !ev->packet || !ev->packet->data )
68    { COUT(2) << "Bad arguments received in WANDiscovery's reply handler.\n";
69      return 0;
70    }
71
72    /* handle incoming data */
73    /* if a list entry arrives add to list */
74    if( !strncmp( (char*)ev->packet->data, MSPROTO_SERVERLIST_ITEM,
75      MSPROTO_SERVERLIST_ITEM_LEN ) )
76    { 
77      /* create server structure from that item */
78      packet::ServerInformation toadd;
79
80      /* fill in data */
81      toadd.setServerName( std::string((char*)ev->packet->data + 
82        MSPROTO_SERVERLIST_ITEM_LEN) );
83      toadd.setServerIP( std::string((char*)ev->packet->data + 
84        MSPROTO_SERVERLIST_ITEM_LEN) );
85
86      /* add to list */
87      WANDiscovery::getInstance().servers_.push_back( toadd );
88    }
89    else if( !strncmp( (char*)ev->packet->data, MSPROTO_SERVERLIST_END,
90      MSPROTO_SERVERLIST_END_LEN ) )
91    { return 1; }
92
93    /* done handling, return all ok code 0 */
94    return 0;
95  }
96 
97  void WANDiscovery::discover()
98  {
99    /* clear list */
100    this->servers_.clear();
101
102    /* send request to server */
103    this->msc.sendRequest( MSPROTO_CLIENT " " MSPROTO_REQ_LIST );
104
105    /* deal with replies */
106    while( !(this->msc).pollForReply( rhandler ) )
107      /* nothing */;
108
109    /* done receiving. */
110  }
111
112  std::string WANDiscovery::getServerListItemName(unsigned int index)
113  {
114    /* if the index is out of range, return empty */
115    if( index >= this->servers_.size() )
116      return BLANKSTRING;
117    else
118      /* else return the name of the server */
119      return this->servers_[index].getServerName();
120  }
121
122  std::string WANDiscovery::getServerListItemIP(unsigned int index)
123  {
124    /* if the index is out of range, return empty */
125    if( index >= this->servers_.size() )
126      return BLANKSTRING;
127    else
128      /* else return the IP of the server */
129      return this->servers_[index].getServerIP();
130  }
131
132
133} // namespace orxonox
Note: See TracBrowser for help on using the repository browser.