[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 | |
---|
| 39 | namespace orxonox |
---|
[7284] | 40 | { |
---|
[7630] | 41 | ManageScopedSingleton(WANDiscovery, ScopeID::Root, true); |
---|
[7284] | 42 | |
---|
[7630] | 43 | WANDiscovery::WANDiscovery() |
---|
[7161] | 44 | { |
---|
[7631] | 45 | /* create master server communications object */ |
---|
[7659] | 46 | //this->msc = MasterServerComm(); |
---|
[7631] | 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"; |
---|
[7161] | 55 | } |
---|
| 56 | |
---|
[7630] | 57 | WANDiscovery::~WANDiscovery() |
---|
[7631] | 58 | { |
---|
| 59 | /* clear server list */ |
---|
| 60 | this->servers_.clear(); |
---|
[7161] | 61 | } |
---|
[7284] | 62 | |
---|
[7631] | 63 | /* callback for the network reply poller */ |
---|
[7657] | 64 | int rhandler( char *addr, ENetEvent *ev ) |
---|
[7631] | 65 | { |
---|
[7657] | 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 | |
---|
[7650] | 72 | /* handle incoming data */ |
---|
| 73 | /* if a list entry arrives add to list */ |
---|
[7651] | 74 | if( !strncmp( (char*)ev->packet->data, MSPROTO_SERVERLIST_ITEM, |
---|
[7650] | 75 | MSPROTO_SERVERLIST_ITEM_LEN ) ) |
---|
| 76 | { |
---|
| 77 | /* create server structure from that item */ |
---|
[7651] | 78 | packet::ServerInformation toadd; |
---|
[7631] | 79 | |
---|
[7650] | 80 | /* fill in data */ |
---|
[7651] | 81 | toadd.setServerName( std::string((char*)ev->packet->data + |
---|
[7650] | 82 | MSPROTO_SERVERLIST_ITEM_LEN) ); |
---|
[7651] | 83 | toadd.setServerIP( std::string((char*)ev->packet->data + |
---|
[7650] | 84 | MSPROTO_SERVERLIST_ITEM_LEN) ); |
---|
| 85 | |
---|
| 86 | /* add to list */ |
---|
[7657] | 87 | WANDiscovery::getInstance().servers_.push_back( toadd ); |
---|
[7650] | 88 | } |
---|
[7651] | 89 | else if( !strncmp( (char*)ev->packet->data, MSPROTO_SERVERLIST_END, |
---|
[7650] | 90 | MSPROTO_SERVERLIST_END_LEN ) ) |
---|
| 91 | { return 1; } |
---|
| 92 | |
---|
[7631] | 93 | /* done handling, return all ok code 0 */ |
---|
| 94 | return 0; |
---|
| 95 | } |
---|
| 96 | |
---|
[7630] | 97 | void WANDiscovery::discover() |
---|
[7161] | 98 | { |
---|
[7631] | 99 | /* clear list */ |
---|
[7161] | 100 | this->servers_.clear(); |
---|
[7631] | 101 | |
---|
| 102 | /* send request to server */ |
---|
[7657] | 103 | this->msc.sendRequest( MSPROTO_CLIENT " " MSPROTO_REQ_LIST ); |
---|
[7631] | 104 | |
---|
| 105 | /* deal with replies */ |
---|
[7657] | 106 | while( !(this->msc).pollForReply( rhandler ) ) |
---|
[7631] | 107 | /* nothing */; |
---|
| 108 | |
---|
| 109 | /* done receiving. */ |
---|
[7630] | 110 | } |
---|
[7284] | 111 | |
---|
[7630] | 112 | std::string WANDiscovery::getServerListItemName(unsigned int index) |
---|
| 113 | { |
---|
[7631] | 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(); |
---|
[7161] | 120 | } |
---|
[7284] | 121 | |
---|
[7630] | 122 | std::string WANDiscovery::getServerListItemIP(unsigned int index) |
---|
[7161] | 123 | { |
---|
[7631] | 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(); |
---|
[7161] | 130 | } |
---|
[7284] | 131 | |
---|
| 132 | |
---|
[7161] | 133 | } // namespace orxonox |
---|