Index: code/branches/masterserverfix/src/libraries/network/MasterServer.cc
===================================================================
--- code/branches/masterserverfix/src/libraries/network/MasterServer.cc	(revision 8934)
+++ code/branches/masterserverfix/src/libraries/network/MasterServer.cc	(revision 8935)
@@ -29,9 +29,72 @@
 #include "MasterServer.h"
 #include "util/ScopedSingletonManager.h"
+#include "core/command/ConsoleCommand.h"
 #include "core/CoreIncludes.h"
 #include "core/CorePrereqs.h"
+#include "util/Output.h"
 
 namespace orxonox 
 {
+  /*** MACROS ***/
+  /* commands for the terminal interface */
+  SetConsoleCommand( "ms-listservers", &MasterServer::listServers );
+  SetConsoleCommand( "ms-delserver", &MasterServer::delServer );
+  //SetConsoleCommand( "ms-serverinfo", &MasterServer::serverInfo );
+
+  /* forward declaration so the linker doesn't complain */
+  MasterServer *MasterServer::instance = NULL;
+
+
+
+
+  /* command: list servers */
+  void 
+  MasterServer::listServers( void )
+  {
+    /* get an iterator */
+    std::list<ServerListElem>::iterator i;
+
+    /* print list header */
+    orxout(user_info) << "List of connected servers" << std::endl;
+
+    /* loop through list elements */
+    for( i = MasterServer::getInstance()->mainlist.serverlist.begin(); 
+      i != MasterServer::getInstance()->mainlist.serverlist.end(); ++i ) 
+    {
+      orxout(user_info) << "  " << (*i).ServerInfo.getServerIP() << std::endl;
+    }
+
+    /* display end of list */
+    orxout(user_info) << MasterServer::getInstance()->mainlist.serverlist.size() <<
+      " servers connected." << std::endl;
+  }
+
+  void 
+  MasterServer::delServer( std::string todeladdr )
+  {
+    /* tell the user we're now removing the entry from the server list */
+    orxout(user_info) << "MS: Deleting server \"" << todeladdr << "\"..." 
+      << std::endl;
+
+    /* see if we actually have that server on our list */
+    ServerListSearchResult shandle = 
+      MasterServer::getInstance()->mainlist.findServerByAddress(todeladdr);
+
+    if( !shandle.success )
+    { orxout(user_info) << "MS: Server not found, not removing." << std::endl;
+      return;
+    }
+
+    /* force-disconnect the server */  
+    enet_peer_disconnect( shandle.result.peer, NULL );
+
+    /* actually remove the entry from the server list by address */
+    MasterServer::getInstance()->mainlist.delServerByAddress( todeladdr);
+
+    /* tell the user about our success */
+    orxout(user_info) << "MS: Server deletion successful." << std::endl;
+  }
+
+
   /* helpers */
   static void 
@@ -53,5 +116,5 @@
   {
     /* get an iterator */
-    std::list<packet::ServerInformation>::iterator i;
+    std::list<ServerListElem>::iterator i;
 
     /* packet holder */
@@ -64,5 +127,5 @@
       /* send this particular server */
       /* build reply string */
-      char *tosend = (char *)calloc( (*i).getServerIP().length() 
+      char *tosend = (char *)calloc( (*i).ServerInfo.getServerIP().length() 
           + MSPROTO_SERVERLIST_ITEM_LEN + 2,1 );
       if( !tosend ) 
@@ -71,5 +134,5 @@
       } 
       sprintf( tosend, "%s %s", MSPROTO_SERVERLIST_ITEM, 
-          (*i).getServerIP().c_str() );
+          (*i).ServerInfo.getServerIP().c_str() );
 
       /* create packet from it */
@@ -98,4 +161,40 @@
     /* One could just use enet_host_service() instead. */
     enet_host_flush( this->server );
+  }
+
+  /* maybe the two methods below can be merged into one and 
+   * made to use ENet's RTT functionality to check for disconnected 
+   * servers.
+   */
+  void 
+  MasterServer::helper_cleanupServers( void )
+  {
+    /* get an iterator */
+    std::list<ServerListElem>::iterator i;
+     
+    if( mainlist.serverlist.size() == 0 )
+      return;
+
+    /* loop through list elements */
+    for( i = mainlist.serverlist.begin(); i 
+        != mainlist.serverlist.end(); ++i ) 
+    { /* see if we have a disconnected peer */
+      if( (*i).peer && 
+         ((*i).peer->state == ENET_PEER_STATE_DISCONNECTED ||
+          (*i).peer->state == ENET_PEER_STATE_ZOMBIE ))
+      { 
+        /* Remove it from the list */
+        orxout(internal_warning) << (char*)(*i).peer->data << " timed out.\n";
+        mainlist.delServerByName( (*i).ServerInfo.getServerName() );
+
+        /* stop iterating, we manipulated the list */
+        /* TODO note: this only removes one dead server per loop
+         * iteration. not beautiful, but one iteration is ~100ms, 
+         * so not really relevant for the moment.
+         */
+        break;
+      }
+    }
+ 
   }
 
@@ -180,5 +279,6 @@
         MSPROTO_REGISTER_SERVER, MSPROTO_REGISTER_SERVER_LEN ) )
       { /* register new server */
-        mainlist.addServer( packet::ServerInformation( event ) );
+        mainlist.addServer( packet::ServerInformation( event ),
+          event->peer );
         
         /* tell people we did so */
@@ -235,6 +335,8 @@
     }
 
-    /* TODO schedule pings for servers somewhere here */
-    
+    /* check for timed out peers and remove those from * the server list */
+    helper_cleanupServers();
+
+
     /* create an iterator for the loop */
     enet_host_service( this->server, event, 100 );
@@ -291,6 +393,6 @@
     }
 
-    /***** INITIALIZE GAME SERVER AND PEER LISTS *****/
-    this->peers = new PeerList();
+    /* set pointer to this instance */
+    MasterServer::setInstance( this );
 
     /* tell people we're now initialized */
Index: code/branches/masterserverfix/src/libraries/network/MasterServer.h
===================================================================
--- code/branches/masterserverfix/src/libraries/network/MasterServer.h	(revision 8934)
+++ code/branches/masterserverfix/src/libraries/network/MasterServer.h	(revision 8935)
@@ -61,4 +61,15 @@
       int run();
 
+      /* static pointer for commands */
+      static MasterServer *instance;
+      static MasterServer *getInstance() 
+        { return instance; }
+      static void setInstance( MasterServer *setto ) 
+        { instance = setto;  }
+      
+      /* functions for commands */
+      static void listServers( void );
+      static void delServer( std::string todeladdr );
+
     private:
       /* methods */
@@ -69,4 +80,5 @@
       /* helpers */
       void helper_sendlist( ENetEvent *event );
+      void helper_cleanupServers( void );
 
       /* members */
@@ -74,5 +86,4 @@
       ENetHost *server;
       ServerList mainlist;
-      PeerList *peers;
 
       unsigned int port;
Index: code/branches/masterserverfix/src/libraries/network/ServerList.cc
===================================================================
--- code/branches/masterserverfix/src/libraries/network/ServerList.cc	(revision 8934)
+++ code/branches/masterserverfix/src/libraries/network/ServerList.cc	(revision 8935)
@@ -40,6 +40,12 @@
 
   int 
-  ServerList::addServer( packet::ServerInformation toadd )
-  { this->serverlist.push_back( toadd ); 
+  ServerList::addServer( packet::ServerInformation toadd,
+    ENetPeer *peer )
+  { 
+    ServerListElem toAdd;
+    toAdd.ServerInfo = toadd;
+    toAdd.peer = peer;
+
+    this->serverlist.push_back( toAdd ); 
     return 0;
   }
@@ -49,9 +55,9 @@
   { 
     /* get an iterator */
-    std::list<packet::ServerInformation>::iterator i;
+    std::list<ServerListElem>::iterator i;
 
     /* loop through list elements */
     for( i = serverlist.begin(); i != serverlist.end(); ++i ) 
-      if( (*i).getServerName() == name )
+      if( (*i).ServerInfo.getServerName() == name )
       { /* found this name, remove and quit */
         this->serverlist.erase( i );
@@ -64,9 +70,9 @@
   { 
     /* get an iterator */
-    std::list<packet::ServerInformation>::iterator i;
+    std::list<ServerListElem>::iterator i;
 
     /* loop through list elements */
-    for( i=serverlist.begin(); i != serverlist.end(); ++i ) 
-      if( (*i).getServerIP() == address )
+    for( i = serverlist.begin(); i != serverlist.end(); ++i ) 
+      if( (*i).ServerInfo.getServerIP() == address )
       { /* found this name, remove and quit */
         this->serverlist.erase( i );
@@ -76,9 +82,49 @@
   }
 
+  /* SEARCHING */
+  ServerListSearchResult
+  ServerList::findServerByAddress( std::string address )
+  {
+    /* get an iterator */
+    std::list<ServerListElem>::iterator i;
 
+    /* loop through list elements */
+    for( i = serverlist.begin(); i != serverlist.end(); ++i ) 
+      if( (*i).ServerInfo.getServerIP() == address )
+      { /* found the target, return it */
+        ServerListSearchResult res = { (*i), true };
+        return res;
+      }
+
+    /* no success */
+    ServerListSearchResult res = { (*i), false };
+    return res;
+  }
+
+  ServerListSearchResult
+  ServerList::findServerByName( std::string name )
+  {
+    /* get an iterator */
+    std::list<ServerListElem>::iterator i;
+
+    /* iterate, return when name found */
+    /* loop through list elements */
+    for( i = serverlist.begin(); i != serverlist.end(); ++i ) 
+      if( (*i).ServerInfo.getServerName() == name )
+      { 
+        ServerListSearchResult res = { (*i), true };
+        return res;
+      }
+
+    /* no luck, return a struct that tells the caller so */
+    ServerListSearchResult res = { (*i), false };
+    return res;
+  }
+
+  /* SORTING */
   /* sort by name */
-  bool sub_compare_names( packet::ServerInformation no1, 
-    packet::ServerInformation no2 )
-  { return no1.getServerName() > no2.getServerName(); }
+  bool sub_compare_names( ServerListElem no1, 
+    ServerListElem no2 )
+  { return no1.ServerInfo.getServerName() > no2.ServerInfo.getServerName(); }
 
   void ServerList::sortByName()
@@ -88,9 +134,8 @@
   
   /* sort by ping */
-  bool sub_compare_pings( packet::ServerInformation no1, 
-    packet::ServerInformation no2 )
+  bool sub_compare_pings( ServerListElem no1, 
+    ServerListElem no2 )
   { 
-    /* TODO */
-    return no1.getServerName() > no2.getServerName();
+    return no1.ServerInfo.getServerName() > no2.ServerInfo.getServerName();
   }
 
Index: code/branches/masterserverfix/src/libraries/network/ServerList.h
===================================================================
--- code/branches/masterserverfix/src/libraries/network/ServerList.h	(revision 8934)
+++ code/branches/masterserverfix/src/libraries/network/ServerList.h	(revision 8935)
@@ -37,4 +37,27 @@
 namespace orxonox 
 { 
+  /* HELPER STRUCTURES */
+  struct ServerListElem 
+  {
+    /* server information (name, IP, etc) */
+    packet::ServerInformation ServerInfo;
+
+    /* peer pointer */
+    ENetPeer* peer;
+  };
+
+  struct ServerListSearchResult
+  {
+    /* list element found */
+    ServerListElem result;
+
+    /* successful search */
+    bool success;
+  };
+
+
+
+
+
   /** This class is keeps a list of game servers
    * and some info about them.
@@ -54,5 +77,6 @@
        * Add server to the game server list
        */
-      int addServer( packet::ServerInformation toadd );
+      int addServer( packet::ServerInformation toadd,
+        ENetPeer *peer );
 
       /** \param name Name of the server to remove
@@ -69,6 +93,29 @@
 
 
-      /* SORTING (to be implemented) */
 
+
+      /* SEARCHING */
+      /* \param address The address of the server that is to be 
+       *  found
+       * \return A struct containing a result of the search and a boolean
+       *  that is only true if the search was successful
+       * 
+       * Find and return the list handle of a given address.
+       */
+      ServerListSearchResult
+      findServerByAddress( std::string address );
+
+
+      /* \param name The name of the server that is to be 
+       *  found
+       * \return The struct containing the list entry of the server
+       * 
+       * Find and return the list handle of a given name.
+       */
+      ServerListSearchResult
+      findServerByName( std::string name );
+
+
+      /* SORTING */
       /** sort by name  */
       void sortByName();
@@ -78,5 +125,5 @@
 
       /** the list of servers for internal storage */
-      std::list<packet::ServerInformation> serverlist;
+      std::list<ServerListElem> serverlist;
     private:
   };
