Index: code/branches/masterserver/src/libraries/network/CMakeLists.txt
===================================================================
--- code/branches/masterserver/src/libraries/network/CMakeLists.txt	(revision 7630)
+++ code/branches/masterserver/src/libraries/network/CMakeLists.txt	(revision 7631)
@@ -32,4 +32,6 @@
   LANDiscoverable.cc
   LANDiscovery.cc
+  WANDiscovery.cc
+  MasterServerComm.cc
   NetworkFunction.cc
   Host.cc
@@ -54,4 +56,6 @@
   LANDiscoverable.h
   LANDiscovery.h
+  WANDiscovery.h
+  MasterServerComm.h
   NetworkFunction.h
   NetworkPrecompiledHeaders.h
Index: code/branches/masterserver/src/libraries/network/MasterServerComm.cc
===================================================================
--- code/branches/masterserver/src/libraries/network/MasterServerComm.cc	(revision 7631)
+++ code/branches/masterserver/src/libraries/network/MasterServerComm.cc	(revision 7631)
@@ -0,0 +1,208 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Sandro 'smerkli' Merkli
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#include "MasterServerComm.h"
+
+namespace orxonox
+{
+ 
+  MasterServerComm::MasterServerComm()
+  { /* nothing anymore, everything's been outsourced to 
+     * the initialize method to facilitate debugging
+     */
+  } 
+
+
+  int MasterServerComm::initialize()
+  {
+    /* initialize Enet */
+    if (enet_initialize () != 0)
+    { COUT(1) << "An error occurred while initializing ENet.\n";
+      return 1;
+    }
+    
+    /* install atexit handler for enet */
+    atexit( enet_deinitialize );
+
+
+    /* initiate the client */
+    this->client = enet_host_create( NULL /* create a client host */,
+        1,
+        2, /* allow up 2 channels to be used, 0 and 1 */
+        0, 
+        0 ); 
+
+    /* see if it worked */
+    if (this->client == NULL)
+    { COUT(1) << "An error occurred while trying to create an ENet client host.\n";
+      return 1;
+    }
+  }
+
+  MasterServerComm::~MasterServerComm()
+  {
+    /* destroy the enet facilities */
+    enet_host_destroy(this->client);
+  }
+
+  int MasterServerComm::connect( char *address, unsigned int port )
+  {
+    /* Connect to address:port. */
+    enet_address_set_host( &this->address, address );
+    this->address.port = port;
+
+    /* Initiate the connection, allocating the two channels 0 and 1. */
+    this->peer = enet_host_connect(this->client, &this->address, 2, 0);    
+
+    if (this->peer == NULL )
+    { fprintf( stderr, 
+        "No available peers for initiating an ENet connection.\n");
+    //exit (EXIT_FAILURE);
+    return -1;
+    }
+
+    /* Wait up to 2 seconds for the connection attempt to succeed. */
+    if (enet_host_service (this->client, &this->event, 2000) > 0 &&
+        this->event.type == ENET_EVENT_TYPE_CONNECT )
+      fprintf( stdout, "Connection to server succeeded." );
+    else
+    {
+      enet_peer_reset (this->peer);
+      fprintf( stdout, "Connection to %s failed.", address );
+      //exit(EXIT_FAILURE);
+      return -1;
+    }
+
+    return 0;
+  }
+
+  int MasterServerComm::pollForReply( int (*callback)( char*, ENetEvent* ) )
+  { 
+    /* see whether anything happened */
+    if( enet_host_service( this->client, &this->event, 100 ) >= 0 )
+    { 
+      /* address buffer */
+      char *addrconv = NULL;
+
+      /* check what type of event it is and react accordingly */
+      switch (this->event.type)
+      { /* new connection, not supposed to happen. */
+        case ENET_EVENT_TYPE_CONNECT: break;
+
+        /* disconnect */
+        case ENET_EVENT_TYPE_DISCONNECT: /* ?? */ break;
+
+        /* incoming data */
+        case ENET_EVENT_TYPE_RECEIVE: 
+          addrconv = (char *) calloc( 50, 1 );
+          enet_address_get_host_ip( &(this->event.peer->address), addrconv, 49 );
+
+          /* DEBUG */
+          printf( "A packet of length %u containing %s was "
+            "received from %s on channel %u.\n",
+            this->event.packet->dataLength,
+            this->event.packet->data,
+            addrconv,
+            this->event.channelID );
+          /* END DEBUG */
+
+          /* call the supplied callback, if any. */
+          if( (*callback) != NULL )
+            (*callback)( addrconv, &(this->event) );
+
+          enet_packet_destroy( event.packet );
+          if( addrconv ) free( addrconv );
+          break;
+        default: break;
+      }
+
+      /* event handled, return 0 */
+      return 0;
+    }
+
+    /* show that no event occured */
+    return 1;
+  }
+
+  int MasterServerComm::sendRequest( char *data )
+  {
+    /* send the data to the friend */
+    /* Create a reliable packet of size 7 containing "packet\0" */
+    ENetPacket * packet = enet_packet_create( data, 
+        strlen( data ) + 1, 
+        ENET_PACKET_FLAG_RELIABLE);
+
+    /* Send the packet to the peer over channel id 0. */
+    enet_peer_send (this->peer, 0, packet);
+
+    /* One could just use enet_host_service() instead. */
+    enet_host_flush( this->client );
+    if( packet ) free( packet );
+  }
+
+}
+
+
+/* DON'T DELETE THIS I MIGHT NEED IT AGAIN -smerkli */
+/* not needed anymore, only here for testing purposes */
+/*
+//[> sample callback to output debugging info. <]
+//int callb( char *addr, ENetEvent *ev )
+//{ 
+  //printf( "A packet of length %u containing %s was "
+      //"received from %s on channel %u.\n",
+      //ev->packet->dataLength,
+      //ev->packet->data,
+      //addr,
+      //ev->channelID );
+  //return 0;
+//}
+
+//[> small testing implementation <]
+//int
+//main( int argc, char *argv[] )
+//{
+  //[> setup object and connect <]
+  //MasterServerComm msc = MasterServerComm();
+  //if( msc.connect( argv[1], 1234 ) )
+    //exit(EXIT_FAILURE);
+  
+  //[> send some data and poll for replies <]
+  //char *theinput = (char *)calloc( 100,1 );
+  //while( true )
+  //{ 
+    //fgets( theinput, 90, stdin );
+    //if( !strncmp( theinput, "quit", 4 ) )
+      //break;
+
+    //msc.sendRequest( theinput );
+    //msc.pollForReply( &callb );
+  //}
+
+//}
+*/
Index: code/branches/masterserver/src/libraries/network/MasterServerComm.h
===================================================================
--- code/branches/masterserver/src/libraries/network/MasterServerComm.h	(revision 7631)
+++ code/branches/masterserver/src/libraries/network/MasterServerComm.h	(revision 7631)
@@ -0,0 +1,87 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Sandro 'smerkli' Merkli
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#include <cstdlib>
+#include <cstdio>
+#include <cstring>
+#include <enet/enet.h>
+
+namespace orxonox
+{
+  class MasterServerComm
+  {
+    public: 
+      /** constructor */
+      MasterServerComm();
+
+      /** destructor */
+      ~MasterServerComm();
+
+      /** \return 0 for success, other for error
+       * 
+       * Initialize everything for the master server communication 
+       */
+      int initialize();
+
+
+      /** \param address Address to connect to (Host name or IP in text form)
+       * \param port Port to connect on 
+       * \return 0 for success, other for error 
+       * 
+       * Connect to the master server with the given address on the given port. 
+       */
+      int connect( char *address, unsigned int port );
+
+      /** \param data The data to be sent. 
+       * \return 0 for success, other for error. 
+       * 
+       * Send a request to the master server containing data specified in data
+       */
+      int sendRequest( char *data );
+
+      /** \param callback The callback function to call with data receivced.
+       * \return 0 for success, other for error
+       * 
+       * Poll the master server for new data and act accordingly */
+      int pollForReply( int (*callback)( char*, ENetEvent* ) );
+
+    private:
+      /** client handle */
+      ENetHost *client;
+
+      /** event data holder */
+      ENetEvent event;
+
+      /** address holder */
+      ENetAddress address;
+
+      /** peer data holder */
+      ENetPeer *peer;
+  };
+
+}
Index: code/branches/masterserver/src/libraries/network/WANDiscovery.cc
===================================================================
--- code/branches/masterserver/src/libraries/network/WANDiscovery.cc	(revision 7630)
+++ code/branches/masterserver/src/libraries/network/WANDiscovery.cc	(revision 7631)
@@ -21,6 +21,7 @@
  *
  *   Author:
- *      Fabian 'x3n' Landau
+ *      Fabian 'x3n' Landau (original)
  *   Co-authors:
+ *      Sandro 'smerkli' Merkli (adaptions to WAN)
  *      ...
  *
@@ -42,23 +43,68 @@
   WANDiscovery::WANDiscovery()
   {
+    /* create master server communications object */
+    this->msc = MasterServerComm();
+
+    /* initialize it and see if it worked */
+    if( msc.initialize() )
+      COUT(1) << "Error: could not initialize master server communications!\n";
+
+    /* connect and see if it worked */
+    if( msc.connect( MS_ADDRESS, 1234 ) )
+      COUT(1) << "Error: could not connect to master server!\n";
   }
 
   WANDiscovery::~WANDiscovery()
-  {
+  { 
+    /* clear server list */
+    this->servers_.clear();  
   }
 
+  /* callback for the network reply poller */
+  /* NOTE implement protocol-specific part here. */
+  int replyhandler( char *addr, ENetEvent *ev )
+  { 
+    /* handle incoming data
+     * if a list entry arrives add to list
+     * if a done entry arrives set done to true
+     */
+
+    /* done handling, return all ok code 0 */
+    return 0;
+  }
+ 
   void WANDiscovery::discover()
   {
+    /* clear list */
     this->servers_.clear();
+
+    /* send request to server */
+    msc.sendRequest( MSPROTO_CLIENT MSPROTO_REQ_LIST );
+
+    /* deal with replies */
+    while( msc.pollForReply( replyhandler ) ) 
+      /* nothing */;
+
+    /* done receiving. */
   }
 
   std::string WANDiscovery::getServerListItemName(unsigned int index)
   {
-
+    /* if the index is out of range, return empty */
+    if( index >= this->servers_.size() )
+      return BLANKSTRING;
+    else
+      /* else return the name of the server */
+      return this->servers_[index].getServerName();
   }
 
   std::string WANDiscovery::getServerListItemIP(unsigned int index)
   {
-
+    /* if the index is out of range, return empty */
+    if( index >= this->servers_.size() )
+      return BLANKSTRING;
+    else
+      /* else return the IP of the server */
+      return this->servers_[index].getServerIP();
   }
 
Index: code/branches/masterserver/src/libraries/network/WANDiscovery.h
===================================================================
--- code/branches/masterserver/src/libraries/network/WANDiscovery.h	(revision 7630)
+++ code/branches/masterserver/src/libraries/network/WANDiscovery.h	(revision 7631)
@@ -33,6 +33,14 @@
 #include "packet/ServerInformation.h"
 #include "util/Singleton.h"
+#include "MasterServerComm.h"
 
 #include <vector>
+
+/* master server address (to be moved elsewhere later) */
+#define MS_ADDRESS "localhost"
+
+/* protocol (to be moved elsewhere later) */
+#define MSPROTO_CLIENT "CL "
+#define MSPROTO_REQ_LIST "REQ:LIST"
 
 // tolua_begin
@@ -46,15 +54,43 @@
     friend class Singleton<WANDiscovery>;
     public:
+      /** constructor */
       WANDiscovery();
+
+      /** destructor */
       ~WANDiscovery();
+
+      /** ask server for server list  */
       void discover(); // tolua_export
+
+      /** \param index Index to get the name of 
+       * \return The name of the server
+       * 
+       * Get the name of the server at index index. 
+       */
       std::string getServerListItemName( unsigned int index ); // tolua_export
+
+      /** \param index Index to get the IP of 
+       * \return The IP of the server
+       * 
+       * Get the IP of the server at index index. 
+       */
       std::string getServerListItemIP( unsigned int index ); // tolua_export
-      static WANDiscovery& getInstance(){ return Singleton<WANDiscovery>::getInstance(); } // tolua_export
+
+      /** \return an instance of WANDiscovery
+       * 
+       * Create and return an instance of WANDiscovery.
+       */
+      static WANDiscovery& getInstance() { return Singleton<WANDiscovery>::getInstance(); } // tolua_export
       
     private:
+      /** Singleton pointer */
       static WANDiscovery* singletonPtr_s;
-      ENetHost* host_;
+
+      /** Master server communications object */
+      MasterServerComm msc;
+
+      /** game server list */
       std::vector<packet::ServerInformation> servers_;
+
   }; // tolua_export
 
Index: code/branches/masterserver/src/modules/masterserver/MasterServerComm.cc
===================================================================
--- code/branches/masterserver/src/modules/masterserver/MasterServerComm.cc	(revision 7630)
+++ 	(revision )
@@ -1,190 +1,0 @@
-/*
- *   ORXONOX - the hottest 3D action shooter ever to exist
- *                    > www.orxonox.net <
- *
- *
- *   License notice:
- *
- *   This program is free software; you can redistribute it and/or
- *   modify it under the terms of the GNU General Public License
- *   as published by the Free Software Foundation; either version 2
- *   of the License, or (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program; if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- *
- *   Author:
- *      Sandro 'smerkli' Merkli
- *   Co-authors:
- *      ...
- *
- */
-
-#include <cstdlib>
-#include <cstdio>
-#include <cstring>
-#include <enet/enet.h>
-#include "MasterServerComm.h"
-
-MasterServerComm::MasterServerComm()
-{
-  /* initialize Enet */
-  if (enet_initialize () != 0)
-  { fprintf (stderr, "An error occurred while initializing ENet.\n");
-    exit(EXIT_FAILURE);
-  }
-  atexit (enet_deinitialize);
-
-
-  /* initiate the client */
-  this->client = enet_host_create( NULL /* create a client host */,
-      1,
-      2, /* allow up 2 channels to be used, 0 and 1 */
-      0, 
-      0 ); 
-
-  /* see if it worked */
-  if (this->client == NULL)
-  { fprintf (stderr, 
-      "An error occurred while trying to create an ENet client host.\n");
-    exit (EXIT_FAILURE);
-  }
-}
-
-MasterServerComm::~MasterServerComm()
-{
-  enet_host_destroy(this->client);
-}
-
-int 
-MasterServerComm::connect( char *address, unsigned int port )
-{
-  /* Connect to address:port. */
-  enet_address_set_host( &this->address, address );
-  this->address.port = port;
-
-  /* Initiate the connection, allocating the two channels 0 and 1. */
-  this->peer = enet_host_connect(this->client, &this->address, 2, 0);    
-
-  if (this->peer == NULL )
-  { fprintf( stderr, 
-        "No available peers for initiating an ENet connection.\n");
-    //exit (EXIT_FAILURE);
-    return -1;
-  }
-
-  /* Wait up to 2 seconds for the connection attempt to succeed. */
-  if (enet_host_service (this->client, &this->event, 2000) > 0 &&
-      this->event.type == ENET_EVENT_TYPE_CONNECT )
-    fprintf( stdout, "Connection to server succeeded." );
-  else
-  {
-    enet_peer_reset (this->peer);
-    fprintf( stdout, "Connection to %s failed.", address );
-    //exit(EXIT_FAILURE);
-    return -1;
-  }
-
-  return 0;
-}
-
-int 
-MasterServerComm::pollForReply( int (*callback)( char*, ENetEvent* ) )
-{ 
-  if( enet_host_service( this->client, &this->event, 100 ) >= 0 )
-  { 
-    char *addrconv = NULL;
-    /* check what type of event it is and react accordingly */
-    switch (this->event.type)
-    { /* new connection, not supposed to happen. */
-      case ENET_EVENT_TYPE_CONNECT: break;
-
-      /* disconnect */
-      case ENET_EVENT_TYPE_DISCONNECT: 
-        /* ?? */ break;
-
-        /* incoming data */
-      case ENET_EVENT_TYPE_RECEIVE: 
-        addrconv = (char *) calloc( 50, 1 );
-        enet_address_get_host_ip( &(this->event.peer->address), addrconv, 49 );
-
-        /* DEBUG */
-        printf( "A packet of length %u containing %s was "
-          "received from %s on channel %u.\n",
-          this->event.packet->dataLength,
-          this->event.packet->data,
-          addrconv,
-          this->event.channelID );
-        /* END DEBUG */
-
-        /* call the supplied callback, if any. */
-        if( (*callback) != NULL )
-          (*callback)( addrconv, &(this->event) );
-
-        enet_packet_destroy( event.packet );
-        if( addrconv ) free( addrconv );
-        break;
-      default: break;
-    }
-  }
-  
-  return 0;
-}
-
-int 
-MasterServerComm::sendRequest( char *data )
-{
-  /* send the data to the friend */
-  /* Create a reliable packet of size 7 containing "packet\0" */
-  ENetPacket * packet = enet_packet_create( data, 
-      strlen( data ) + 1, 
-      ENET_PACKET_FLAG_RELIABLE);
-
-  /* Send the packet to the peer over channel id 0. */
-  enet_peer_send (this->peer, 0, packet);
-
-  /* One could just use enet_host_service() instead. */
-  enet_host_flush( this->client );
-  if( packet ) free( packet );
-}
-
-/* sample callback to output debugging info. */
-int callb( char *addr, ENetEvent *ev )
-{ 
-  printf( "A packet of length %u containing %s was "
-      "received from %s on channel %u.\n",
-      ev->packet->dataLength,
-      ev->packet->data,
-      addr,
-      ev->channelID );
-  return 0;
-}
-
-/* small testing implementation */
-int
-main( int argc, char *argv[] )
-{
-  /* setup object and connect */
-  MasterServerComm msc = MasterServerComm();
-  if( msc.connect( argv[1], 1234 ) )
-    exit(EXIT_FAILURE);
-  
-  /* send some data and poll for replies */
-  char *theinput = (char *)calloc( 100,1 );
-  while( true )
-  { 
-    fgets( theinput, 90, stdin );
-    if( !strncmp( theinput, "quit", 4 ) )
-      break;
-
-    msc.sendRequest( theinput );
-    msc.pollForReply( &callb );
-  }
-
-}
Index: code/branches/masterserver/src/modules/masterserver/MasterServerComm.h
===================================================================
--- code/branches/masterserver/src/modules/masterserver/MasterServerComm.h	(revision 7630)
+++ 	(revision )
@@ -1,69 +1,0 @@
-/*
- *   ORXONOX - the hottest 3D action shooter ever to exist
- *                    > www.orxonox.net <
- *
- *
- *   License notice:
- *
- *   This program is free software; you can redistribute it and/or
- *   modify it under the terms of the GNU General Public License
- *   as published by the Free Software Foundation; either version 2
- *   of the License, or (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program; if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- *
- *   Author:
- *      Sandro 'smerkli' Merkli
- *   Co-authors:
- *      ...
- *
- */
-
-class MasterServerComm
-{
-  public: 
-    /** constructor */
-    MasterServerComm();
-
-    /** destructor */
-    ~MasterServerComm();
-
-    
-    /** \param address Address to connect to (Host name or IP in text form)
-     * \param port Port to connect on 
-     * \return 0 for success, other for error 
-     * 
-     * Connect to the master server with the given address on the given port. 
-     */
-    int connect( char *address, unsigned int port );
-
-    /** \param data The data to be sent. 
-     * \return 0 for success, other for error. 
-     * 
-     * Send a request to the master server containing data specified in data
-     */
-    int sendRequest( char *data );
-
-    /** \param callback The callback function to call with data receivced.
-     * \return 0 for success, other for error
-     * 
-     * Poll the master server for new data and act accordingly */
-    int pollForReply( int (*callback)( char*, ENetEvent* ) );
-
-  private:
-    /* client handle */
-    ENetHost *client;
-    ENetEvent event;
-
-    ENetAddress address;
-    ENetPeer *peer;
-
-
-};
Index: code/branches/masterserver/src/modules/masterserver/ServerList.h
===================================================================
--- code/branches/masterserver/src/modules/masterserver/ServerList.h	(revision 7630)
+++ code/branches/masterserver/src/modules/masterserver/ServerList.h	(revision 7631)
@@ -69,5 +69,5 @@
 
 
-      /* SORTING */
+      /* SORTING (to be implemented) */
 
       /** sort by name  */
