Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

further debugging.

File size: 4.0 KB
RevLine 
[7161]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:
[7631]23 *      Fabian 'x3n' Landau (original)
[7161]24 *   Co-authors:
[7631]25 *      Sandro 'smerkli' Merkli (adaptions to WAN)
[7161]26 *      ...
27 *
28 */
29
[7630]30#include "WANDiscovery.h"
[7161]31
32#include <enet/enet.h>
33#include <cstring>
34
[7284]35#include "util/ScopedSingletonManager.h"
[7161]36#include "core/CoreIncludes.h"
37
38
39namespace orxonox
[7284]40{
[7630]41  ManageScopedSingleton(WANDiscovery, ScopeID::Root, true);
[7284]42
[7630]43  WANDiscovery::WANDiscovery()
[7161]44  {
[7672]45    COUT(4) << "Creating WANDiscovery.\n";
[7631]46
47    /* initialize it and see if it worked */
48    if( msc.initialize() )
[7672]49      COUT(2) << "Error: could not initialize master server communications!\n";
[7631]50
51    /* connect and see if it worked */
52    if( msc.connect( MS_ADDRESS, 1234 ) )
[7688]53      COUT(2) << "Error: could not connect to master server at " 
54        << MS_ADDRESS << std::endl;
[7667]55
[7672]56    COUT(4) << "Initialization of WANDiscovery complete.\n";
[7161]57  }
58
[7630]59  WANDiscovery::~WANDiscovery()
[7631]60  { 
61    /* clear server list */
62    this->servers_.clear(); 
[7161]63  }
[7284]64
[7631]65  /* callback for the network reply poller */
[7657]66  int rhandler( char *addr, ENetEvent *ev )
[7631]67  { 
[7657]68    /* error recognition */
69    if( !ev || !ev->packet || !ev->packet->data )
70    { COUT(2) << "Bad arguments received in WANDiscovery's reply handler.\n";
71      return 0;
72    }
73
[7650]74    /* handle incoming data */
75    /* if a list entry arrives add to list */
[7651]76    if( !strncmp( (char*)ev->packet->data, MSPROTO_SERVERLIST_ITEM,
[7650]77      MSPROTO_SERVERLIST_ITEM_LEN ) )
78    { 
79      /* create server structure from that item */
[7651]80      packet::ServerInformation toadd;
[7631]81
[7650]82      /* fill in data */
[7651]83      toadd.setServerName( std::string((char*)ev->packet->data + 
[7650]84        MSPROTO_SERVERLIST_ITEM_LEN) );
[7651]85      toadd.setServerIP( std::string((char*)ev->packet->data + 
[7650]86        MSPROTO_SERVERLIST_ITEM_LEN) );
87
88      /* add to list */
[7657]89      WANDiscovery::getInstance().servers_.push_back( toadd );
[7650]90    }
[7651]91    else if( !strncmp( (char*)ev->packet->data, MSPROTO_SERVERLIST_END,
[7650]92      MSPROTO_SERVERLIST_END_LEN ) )
[7672]93    { 
94      /* this is the only case where 1 should be returned,
95       * as 1 is used to signal that we're done receiving
96       * the list
97       */
98      return 1; 
99    }
[7650]100
[7631]101    /* done handling, return all ok code 0 */
102    return 0;
103  }
104 
[7630]105  void WANDiscovery::discover()
[7161]106  {
[7631]107    /* clear list */
[7161]108    this->servers_.clear();
[7631]109
110    /* send request to server */
[7657]111    this->msc.sendRequest( MSPROTO_CLIENT " " MSPROTO_REQ_LIST );
[7631]112
[7672]113    /* poll for replies */
114    /* TODO add some timeout here so we don't wait indefinitely */
[7667]115    while( !((this->msc).pollForReply( rhandler )) )
[7631]116      /* nothing */;
117
118    /* done receiving. */
[7630]119  }
[7284]120
[7630]121  std::string WANDiscovery::getServerListItemName(unsigned int index)
122  {
[7631]123    /* if the index is out of range, return empty */
124    if( index >= this->servers_.size() )
125      return BLANKSTRING;
126    else
127      /* else return the name of the server */
128      return this->servers_[index].getServerName();
[7161]129  }
[7284]130
[7630]131  std::string WANDiscovery::getServerListItemIP(unsigned int index)
[7161]132  {
[7631]133    /* if the index is out of range, return empty */
134    if( index >= this->servers_.size() )
135      return BLANKSTRING;
136    else
137      /* else return the IP of the server */
138      return this->servers_[index].getServerIP();
[7161]139  }
[7284]140
141
[7161]142} // namespace orxonox
Note: See TracBrowser for help on using the repository browser.