Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7667 was 7667, checked in by dafrick, 13 years ago

fix this…

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