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