| 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 | |
|---|
| 39 | namespace 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 replyhandler( char *addr, ENetEvent *ev ) |
|---|
| 66 | { |
|---|
| 67 | /* handle incoming data |
|---|
| 68 | * if a list entry arrives add to list |
|---|
| 69 | * if a done entry arrives set done to true |
|---|
| 70 | */ |
|---|
| 71 | |
|---|
| 72 | /* done handling, return all ok code 0 */ |
|---|
| 73 | return 0; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | void WANDiscovery::discover() |
|---|
| 77 | { |
|---|
| 78 | /* clear list */ |
|---|
| 79 | this->servers_.clear(); |
|---|
| 80 | |
|---|
| 81 | /* send request to server */ |
|---|
| 82 | msc.sendRequest( MSPROTO_CLIENT MSPROTO_REQ_LIST ); |
|---|
| 83 | |
|---|
| 84 | /* deal with replies */ |
|---|
| 85 | while( msc.pollForReply( replyhandler ) ) |
|---|
| 86 | /* nothing */; |
|---|
| 87 | |
|---|
| 88 | /* done receiving. */ |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | std::string WANDiscovery::getServerListItemName(unsigned int index) |
|---|
| 92 | { |
|---|
| 93 | /* if the index is out of range, return empty */ |
|---|
| 94 | if( index >= this->servers_.size() ) |
|---|
| 95 | return BLANKSTRING; |
|---|
| 96 | else |
|---|
| 97 | /* else return the name of the server */ |
|---|
| 98 | return this->servers_[index].getServerName(); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | std::string WANDiscovery::getServerListItemIP(unsigned int index) |
|---|
| 102 | { |
|---|
| 103 | /* if the index is out of range, return empty */ |
|---|
| 104 | if( index >= this->servers_.size() ) |
|---|
| 105 | return BLANKSTRING; |
|---|
| 106 | else |
|---|
| 107 | /* else return the IP of the server */ |
|---|
| 108 | return this->servers_[index].getServerIP(); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | } // namespace orxonox |
|---|