Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

done for today

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