Index: code/branches/masterserver/src/modules/masterserver/MasterServer.cpp
===================================================================
--- code/branches/masterserver/src/modules/masterserver/MasterServer.cpp	(revision 7589)
+++ code/branches/masterserver/src/modules/masterserver/MasterServer.cpp	(revision 7590)
@@ -28,171 +28,190 @@
 
 #include "MasterServer.h"
-
-using namespace std;
-
-/***** EVENTS *****/
-/* connect event */
-int eventConnect( ENetEvent *event, 
-  orxonox::ServerList *serverlist,
-  orxonox::PeerList *peers )
-{ /* check for bad parameters */
-  if( !event )
-  { fprintf( stderr, "No event given.\n" );
-    return -1;
-  }
-
-  /* output debug info */
-  printf( "A new client connected from %x:%u.\n", 
-      event->peer->address.host,
-      event->peer->address.port);
-
-  /* game server or client connection? */
-  /* -> decide in protocol */
+#include "util/ScopedSingletonManager.h"
+#include "core/CoreIncludes.h"
+#include "core/CorePrereqs.h"
+
+namespace orxonox 
+{
+  /* singleton stuff */
+  ManageScopedSingleton( MasterServer, ScopeID::Root, false );
+
+  /***** EVENTS *****/
+  /* connect event */
+  int 
+  eventConnect( ENetEvent *event )
+  { /* check for bad parameters */
+    if( !event )
+    { fprintf( stderr, "No event given.\n" );
+      return -1;
+    }
+
+    /* output debug info */
+    printf( "A new client connected from %x:%u.\n", 
+        (event->peer->address.host),
+        event->peer->address.port );
+
+    /* game server or client connection? */
+    /* -> decide in protocol */
     /* game server */
-      /* add to game server list */
-      /*  */
+    /* add to game server list */
+    /*  */
 
     /* client */
-      /* add to client list */
- 
-  /* NOTE this seems to be some weird snipped from the tutorial as peer.data
-   * is a uint_32 of some kind and hence shouldn't be assigned a c string? :S 
-   */
-  /* Store any relevant client information here. */
-  /* event->peer->data = "Client information"; */
-  /* /NOTE */
-  return 0;
-}
-
-/* disconnect event */
-int eventDisconnect( ENetEvent *event,
-  orxonox::ServerList *serverlist,
-  orxonox::PeerList *peers )
-{ /* check for bad parameters */
-  if( !event )
-  { fprintf( stderr, "No event given.\n" );
-    return -1;
-  }
-
-  /* output that the disconnect happened, to be removed at a later time. */
-  printf( "%s disconnected.\n", event->peer->data );
-
-  /* remove the server from the list it belongs to */
-
-  /* Reset the peer's client information. */
-  event->peer->data = NULL;
-  return 0;
-}
-
-/* data event */
-int eventData( ENetEvent *event )
-{ /* output what's in the packet (to be removed later) */
-  if( !event || !(event->packet) || !(event->peer) || !(event->channelID) )
-  { fprintf( stderr, "No complete event given.\n" );
-    return -1;
-  }
-
-  /* output debug info about the data that has come, to be removed */
-  //printf( "A packet of length %u containing %s was received from %s on channel %u.\n",
-      //event->packet->dataLength,
-      //event->packet->data,
-      //event->peer->data,
-      //event->channelID );
- 
-  /* game server or client connection? */
+    /* add to client list */
+
+    /* NOTE this seems to be some weird snipped from the tutorial as peer.data
+     * is a uint_32 of some kind and hence shouldn't be assigned a c string? :S 
+     */
+    /* Store any relevant client information here. */
+    /* event->peer->data = "Client information"; */
+    /* /NOTE */
+    return 0;
+  }
+
+  /* disconnect event */
+  int 
+  eventDisconnect( ENetEvent *event )
+  { /* check for bad parameters */
+    if( !event )
+    { fprintf( stderr, "No event given.\n" );
+      return -1;
+    }
+
+    /* output that the disconnect happened, to be removed at a later time. */
+    printf( "%s disconnected.\n", (char*)event->peer->data );
+
+    /* remove the server from the list it belongs to */
+
+    /* Reset the peer's client information. */
+    event->peer->data = NULL;
+    return 0;
+  }
+
+  /* data event */
+  int 
+  eventData( ENetEvent *event )
+  { /* output what's in the packet (to be removed later) */
+    if( !event || !(event->packet) || !(event->peer) || !(event->channelID) )
+    { fprintf( stderr, "No complete event given.\n" );
+      return -1;
+    }
+
+    /* output debug info about the data that has come, to be removed */
+    //printf( "A packet of length %u containing %s was received from %s on channel %u.\n",
+    //event->packet->dataLength,
+    //event->packet->data,
+    //event->peer->data,
+    //event->channelID );
+
+    /* game server or client connection? */
     /* game server */
-      /* parse data */
-      /* start actions */
-      /* and send reply */
+    /* parse data */
+    /* start actions */
+    /* and send reply */
     /* client */
-      /* parse data */
-      /* start actions */
-      /* and send reply */
-
-  /* Clean up the packet now that we're done using it. */
-  enet_packet_destroy( event->packet );
-  return 0;
-}
-
-
-
-/**** MAIN ROUTINE *****/
-int main( int argc, char *argv[] )
-{
-  /***** INITIALIZE NETWORKING *****/
-  if( enet_initialize () != 0)
-  { fprintf( stderr, "An error occurred while initializing ENet.\n");
-    return EXIT_FAILURE;
-  }
-
-  /* register deinitialization */
-  atexit( enet_deinitialize );
-
-  /* define our address and a host structure */
-  ENetAddress address;
-  ENetHost *server;
-
-  /* Bind the server to the default localhost and port ORX_MSERVER_PORT */
-  address.host = ENET_HOST_ANY;
-  address.port = ORX_MSERVER_PORT;
-
-  /* create a host with the above settings (the last two 0 mean: accept 
-   * any input/output bandwidth */
-  server = enet_host_create( &address, ORX_MSERVER_MAXCONNS, 
-    ORX_MSERVER_MAXCHANS, 0, 0 );     
-
-  /* see if creation worked */
-  if( !server )
-  { fprintf( stderr, 
+    /* parse data */
+    /* start actions */
+    /* and send reply */
+
+    /* Clean up the packet now that we're done using it. */
+    enet_packet_destroy( event->packet );
+    return 0;
+  }
+
+  /**** MAIN ROUTINE *****/
+  int 
+  MasterServer::run()
+  {
+    COUT(0) << "omg, i got baschtl'd!\n";
+
+    /***** ENTER MAIN LOOP *****/
+    ENetEvent *event = (ENetEvent *)calloc(sizeof(ENetEvent), sizeof(char));
+    if( event == NULL )
+    { fprintf( stderr, "Could not create ENetEvent structure, exiting.\n" );
+      exit( EXIT_FAILURE );
+    }
+
+    /* create an iterator for the loop */
+    while( enet_host_service( this->server, event, 1000 ) > 0 )
+    { /* check what type of event it is and react accordingly */
+      switch (event->type)
+      { /* new connection */
+        case ENET_EVENT_TYPE_CONNECT: 
+          eventConnect( event ); break;
+
+          /* disconnect */
+        case ENET_EVENT_TYPE_DISCONNECT: 
+          eventDisconnect( event ); break;
+
+          /* incoming data */
+        case ENET_EVENT_TYPE_RECEIVE: eventData( event ); break;
+        default: break;
+      }
+    }
+
+    /* free the event */
+    if( event ) free( event );
+
+    /* done */
+    return 0;
+  } 
+
+  /* constructor */
+  MasterServer::MasterServer()
+  {
+    /***** INITIALIZE NETWORKING *****/
+    if( enet_initialize () != 0)
+    { fprintf( stderr, "An error occurred while initializing ENet.\n");
+      exit( EXIT_FAILURE );
+    }
+
+    /* register deinitialization */
+    atexit( enet_deinitialize );
+
+    /* Bind the server to the default localhost and port ORX_MSERVER_PORT */
+    this->address.host = ENET_HOST_ANY;
+    this->address.port = ORX_MSERVER_PORT;
+
+    /* create a host with the above settings (the last two 0 mean: accept 
+     * any input/output bandwidth */
+    this->server = enet_host_create( &this->address, ORX_MSERVER_MAXCONNS, 
+        ORX_MSERVER_MAXCHANS, 0, 0 );     
+
+    /* see if creation worked */
+    if( !this->server )
+    { fprintf( stderr, 
         "An error occurred while trying to create an ENet server host.\n" );
     exit( EXIT_FAILURE );
-  }
-
-  /***** INITIALIZE GAME SERVER LIST *****/
-  orxonox::ServerList *mainlist = new orxonox::ServerList();
-  if( mainlist == NULL )
-  { fprintf( stderr, "Error creating server list.\n" );
-    exit( EXIT_FAILURE );
-  }
-
-  /***** INITIALIZE PEER LIST *****/
-  orxonox::PeerList *peers = new orxonox::PeerList();
-
-  /***** ENTER MAIN LOOP *****/
-  ENetEvent *event = (ENetEvent *)calloc(sizeof(ENetEvent), sizeof(char));
-  if( event == NULL )
-  { fprintf( stderr, "Could not create ENetEvent structure, exiting.\n" );
-    exit( EXIT_FAILURE );
-  }
-
-  /* create an iterator for the loop */
-  while( enet_host_service( server, event, 1000 ) > 0 )
-  { /* check what type of event it is and react accordingly */
-    switch (event->type)
-    { /* new connection */
-      case ENET_EVENT_TYPE_CONNECT: 
-        eventConnect( event, mainlist, peers ); break;
-
-      /* disconnect */
-      case ENET_EVENT_TYPE_DISCONNECT: 
-        eventDisconnect( event, mainlist, peers ); break;
-
-      /* incoming data */
-      case ENET_EVENT_TYPE_RECEIVE: eventData( event ); break;
-      default: break;
-    }
-  }
-
-  /***** CLEANUP PROCESS *****/
-  /* terminate all networking connections */
-  enet_host_destroy( server );
-
-  /* clear the list of connected game servers */
-  /* clear the list of connected game clients */
-
-  /* free all used memory */
-  if( event ) free( event );
-
-  /* all done */
-  return EXIT_SUCCESS;
-} 
+    }
+
+    /***** INITIALIZE GAME SERVER LIST *****/
+    this->mainlist = new ServerList();
+    if( this->mainlist == NULL )
+    { fprintf( stderr, "Error creating server list.\n" );
+      exit( EXIT_FAILURE );
+    }
+
+    /***** INITIALIZE PEER LIST *****/
+    this->peers = new PeerList();
+
+    /* run the main method */
+    run();
+  }
+
+  /* destructor */
+  MasterServer::~MasterServer()
+  {
+    /***** CLEANUP PROCESS *****/
+    /* terminate all networking connections */
+    enet_host_destroy( this->server );
+
+    /* clear the list of connected game servers */
+    /* clear the list of connected game clients */
+
+    /* free all used memory */
+
+  }
+
+/* end of namespace */
+}
Index: code/branches/masterserver/src/modules/masterserver/MasterServer.h
===================================================================
--- code/branches/masterserver/src/modules/masterserver/MasterServer.h	(revision 7589)
+++ code/branches/masterserver/src/modules/masterserver/MasterServer.h	(revision 7590)
@@ -38,4 +38,5 @@
 #include <network/packet/Gamestate.h>
 #include <network/packet/Welcome.h>
+#include <util/Singleton.h>
 
 /* my includes */
@@ -50,3 +51,29 @@
 #define ORX_MSERVER_MAXCHANS 2
 
+namespace orxonox 
+{
+  /* singleton */
+  class MasterServer: public Singleton<MasterServer>
+  {
+    public:
+      MasterServer();
+      ~MasterServer();
+
+      friend class Singleton<MasterServer>;
+      static MasterServer& getInstance(void) 
+      { return Singleton<MasterServer>::getInstance(); }
+
+      /* data fields */
+      static MasterServer* singletonPtr_s;
+    
+    private:
+      ENetAddress address;
+      ENetHost *server;
+      ServerList *mainlist;
+      PeerList *peers;
+      int run();
+
+  };
+}
+
 #endif /* _MasterServer_ */
Index: code/branches/masterserver/src/modules/masterserver/PeerList.cpp
===================================================================
--- code/branches/masterserver/src/modules/masterserver/PeerList.cpp	(revision 7589)
+++ code/branches/masterserver/src/modules/masterserver/PeerList.cpp	(revision 7590)
@@ -28,4 +28,5 @@
 
 #include "PeerList.h"
+#include <network/packet/ServerInformation.h>
 #include <cstdio>
 
@@ -49,16 +50,25 @@
 
   bool sub_compAddr( ENetAddress addr1, ENetAddress addr2 )
-  { return ( (addr1.host == addr2.host) && (addr1.port == addr2.port) ); }
+  { 
+    for( int i = 0; i < 16; ++i )
+      if( addr1.host.addr[i] < addr2.host.addr[i] )
+        return -i;
+      else if( addr1.host.addr[i] > addr2.host.addr[i] )
+        return i;
+
+    return 0;
+  }
+    
 
   bool
   PeerList::remPeerByAddr( ENetAddress addr )
   { /* get an iterator */
-    list<packet::ENetPeer *>::iterator i;
+    std::list<ENetPeer *>::iterator i;
 
     /* loop through list elements */
     for( i = peerlist.begin(); i != peerlist.end(); ++i ) 
-      if( sub_compAddr((*i)->address, addr ) )
+      if( !sub_compAddr((*i)->address, addr ) )
       { /* found this name, remove and quit */
-        this->peerlist.remove( i );
+        this->peerlist.remove( *i );
         return true;
       }
@@ -71,11 +81,11 @@
   PeerList::findPeerByAddr( ENetAddress addr )
   { /* get an iterator */
-    list<packet::ENetPeer *>::iterator i;
+    std::list<ENetPeer *>::iterator i;
 
     /* loop through list elements */
     for( i = peerlist.begin(); i != peerlist.end(); ++i ) 
-      if( sub_compAddr((*i)->address, addr ) )
+      if( !sub_compAddr((*i)->address, addr ) )
         /* found this name, remove and quit */
-        return i;
+        return *i;
 
     /* not found */
