Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ipv6/src/libraries/network/LANDiscovery.cc @ 7322

Last change on this file since 7322 was 7322, checked in by adrfried, 14 years ago

IPv6 Multicast LANDiscovery

File size: 3.5 KB
Line 
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
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "LANDiscovery.h"
30
31#include <enet/enet.h>
32#include <cstring>
33
34#include "util/ScopedSingletonManager.h"
35#include "core/CoreIncludes.h"
36
37
38namespace orxonox
39{
40  ManageScopedSingleton(LANDiscovery, ScopeID::Root, true);
41
42  LANDiscovery::LANDiscovery()
43  {
44    this->host_ = enet_host_create(NULL, 10, 0, 0, 0 );
45  }
46
47  LANDiscovery::~LANDiscovery()
48  {
49    enet_host_destroy(this->host_);
50  }
51
52  void LANDiscovery::discover()
53  {
54    this->servers_.clear();
55    ENetPeer* peer;
56    ENetAddress address;
57    memset(&address, 0, sizeof(ENetAddress));
58    address.port = LAN_DISCOVERY_PORT;
59
60    /* TODO: check for availability of each protocol */
61    /* IPv4 */
62    address.host = ENET_HOST_BROADCAST;
63    peer = enet_host_connect(this->host_, &address, 0, 0);
64
65    /* IPv6 */
66    enet_address_set_host(&address, "ff02::1"); // TODO: use a multicast group
67    peer = enet_host_connect(this->host_, &address, 0, 0);
68
69    ENetEvent event;
70    while( enet_host_service(this->host_, &event, 1000 ) )
71    {
72      switch( event.type )
73      {
74        case ENET_EVENT_TYPE_CONNECT:
75        {
76          //COUT(0) << "connect from server: " << event.peer->address.host << endl; FIXME
77          ENetPacket* packet = enet_packet_create(LAN_DISCOVERY_MESSAGE, strlen(LAN_DISCOVERY_MESSAGE)+1, ENET_PACKET_FLAG_RELIABLE);
78          enet_peer_send(event.peer, 0, packet);
79          break;
80        }
81        case ENET_EVENT_TYPE_RECEIVE:
82          {
83            packet::ServerInformation info(&event);
84            COUT(0) << "received server information; name: " << info.getServerName() << ", IP: " << info.getServerIP() << ", RTT: " << info.getServerRTT() << endl;
85            std::vector<packet::ServerInformation>::iterator it;
86            for( it=this->servers_.begin(); it!=this->servers_.end(); ++it )
87            {
88              if( (*it).getServerIP() == info.getServerIP() )
89                break;
90            }
91            if( it==this->servers_.end() )
92              this->servers_.push_back(info);
93          }
94//           enet_address_get_host_ip(&event.peer->address, buffer, buflen );
95//           serverIPs.push_back(std::string(buffer));
96          break;
97        default:
98          break;
99      }
100    }
101  }
102
103  std::string LANDiscovery::getServerListItemName(unsigned int index)
104  {
105    if( index >= this->servers_.size() )
106      return BLANKSTRING;
107    else
108      return this->servers_[index].getServerName();
109  }
110
111  std::string LANDiscovery::getServerListItemIP(unsigned int index)
112  {
113    if( index >= this->servers_.size() )
114      return BLANKSTRING;
115    else
116      return this->servers_[index].getServerIP();
117  }
118
119
120} // namespace orxonox
Note: See TracBrowser for help on using the repository browser.