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 | COUT(4) << "Creating WANDiscovery.\n"; |
---|
46 | |
---|
47 | /* initialize it and see if it worked */ |
---|
48 | if( msc.initialize() ) |
---|
49 | COUT(2) << "Error: could not initialize master server communications!\n"; |
---|
50 | |
---|
51 | /* connect and see if it worked */ |
---|
52 | if( msc.connect( MS_ADDRESS, 1234 ) ) |
---|
53 | COUT(2) << "Error: could not connect to master server at " |
---|
54 | << MS_ADDRESS << std::endl; |
---|
55 | |
---|
56 | COUT(4) << "Initialization of WANDiscovery complete.\n"; |
---|
57 | } |
---|
58 | |
---|
59 | WANDiscovery::~WANDiscovery() |
---|
60 | { |
---|
61 | /* clear server list */ |
---|
62 | this->servers_.clear(); |
---|
63 | } |
---|
64 | |
---|
65 | /* callback for the network reply poller */ |
---|
66 | int rhandler( char *addr, ENetEvent *ev ) |
---|
67 | { |
---|
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 | |
---|
74 | /* handle incoming data */ |
---|
75 | /* if a list entry arrives add to list */ |
---|
76 | if( !strncmp( (char*)ev->packet->data, MSPROTO_SERVERLIST_ITEM, |
---|
77 | MSPROTO_SERVERLIST_ITEM_LEN ) ) |
---|
78 | { |
---|
79 | /* create server structure from that item */ |
---|
80 | packet::ServerInformation toadd; |
---|
81 | |
---|
82 | /* fill in data */ |
---|
83 | toadd.setServerName( std::string((char*)ev->packet->data + |
---|
84 | MSPROTO_SERVERLIST_ITEM_LEN) ); |
---|
85 | toadd.setServerIP( std::string((char*)ev->packet->data + |
---|
86 | MSPROTO_SERVERLIST_ITEM_LEN) ); |
---|
87 | |
---|
88 | /* add to list */ |
---|
89 | WANDiscovery::getInstance().servers_.push_back( toadd ); |
---|
90 | } |
---|
91 | else if( !strncmp( (char*)ev->packet->data, MSPROTO_SERVERLIST_END, |
---|
92 | MSPROTO_SERVERLIST_END_LEN ) ) |
---|
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 2; |
---|
99 | } |
---|
100 | |
---|
101 | /* done handling, return all ok code 0 */ |
---|
102 | return 1; |
---|
103 | } |
---|
104 | |
---|
105 | void WANDiscovery::discover() |
---|
106 | { |
---|
107 | /* clear list */ |
---|
108 | this->servers_.clear(); |
---|
109 | |
---|
110 | /* send request to server */ |
---|
111 | this->msc.sendRequest( MSPROTO_CLIENT " " MSPROTO_REQ_LIST ); |
---|
112 | |
---|
113 | /* poll for replies */ |
---|
114 | int i = WANDISC_MAXTRIES; |
---|
115 | while( i > 0 ) |
---|
116 | { |
---|
117 | /* poll for reply and act according to what was received */ |
---|
118 | switch( this->msc.pollForReply( rhandler ) ) |
---|
119 | { case 0: /* no event occured, decrease timeout */ |
---|
120 | --i; break; |
---|
121 | case 1: /* got a list element, continue */ |
---|
122 | break; |
---|
123 | case 2: /* done. */ |
---|
124 | i = 0; break; |
---|
125 | } |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | std::string WANDiscovery::getServerListItemName(unsigned int index) |
---|
130 | { |
---|
131 | /* if the index is out of range, return empty */ |
---|
132 | if( index >= this->servers_.size() ) |
---|
133 | return BLANKSTRING; |
---|
134 | else |
---|
135 | /* else return the name of the server */ |
---|
136 | return this->servers_[index].getServerName(); |
---|
137 | } |
---|
138 | |
---|
139 | std::string WANDiscovery::getServerListItemIP(unsigned int index) |
---|
140 | { |
---|
141 | /* if the index is out of range, return empty */ |
---|
142 | if( index >= this->servers_.size() ) |
---|
143 | return BLANKSTRING; |
---|
144 | else |
---|
145 | /* else return the IP of the server */ |
---|
146 | return this->servers_[index].getServerIP(); |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | } // namespace orxonox |
---|