Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Minimum target achieved for today, servers can log on to master server, clients can get server list. To be debugged.

File size: 3.5 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  /* NOTE implement protocol-specific part here. */
65  int rhandler( char *addr, ENetEvent *ev )
66  { 
67    /* handle incoming data */
68    /* if a list entry arrives add to list */
69    if( !strncmp( ev->packet->data, MSPROTO_SERVERLIST_ITEM,
70      MSPROTO_SERVERLIST_ITEM_LEN ) )
71    { 
72      /* create server structure from that item */
73      ServerInformation toadd;
74
75      /* fill in data */
76      toadd->setName( std::string(ev->packet->data + 
77        MSPROTO_SERVERLIST_ITEM_LEN) );
78      toadd->setIP( std::string(ev->packet->data + 
79        MSPROTO_SERVERLIST_ITEM_LEN) );
80
81      /* add to list */
82      this->servers_.add( toadd );
83    }
84    else if( !strncmp( ev->packet->data, MSPROTO_SERVERLIST_END,
85      MSPROTO_SERVERLIST_END_LEN ) )
86    { return 1; }
87
88    /* done handling, return all ok code 0 */
89    return 0;
90  }
91 
92  void WANDiscovery::discover()
93  {
94    /* clear list */
95    this->servers_.clear();
96
97    /* send request to server */
98    msc.sendRequest( MSPROTO_CLIENT " " MSPROTO_REQ_LIST );
99
100    /* deal with replies */
101    while( !msc.pollForReply( rhandler ) ) 
102      /* nothing */;
103
104    /* done receiving. */
105  }
106
107  std::string WANDiscovery::getServerListItemName(unsigned int index)
108  {
109    /* if the index is out of range, return empty */
110    if( index >= this->servers_.size() )
111      return BLANKSTRING;
112    else
113      /* else return the name of the server */
114      return this->servers_[index].getServerName();
115  }
116
117  std::string WANDiscovery::getServerListItemIP(unsigned int index)
118  {
119    /* if the index is out of range, return empty */
120    if( index >= this->servers_.size() )
121      return BLANKSTRING;
122    else
123      /* else return the IP of the server */
124      return this->servers_[index].getServerIP();
125  }
126
127
128} // namespace orxonox
Note: See TracBrowser for help on using the repository browser.