[Orxonox-commit 2908] r7611 - code/branches/masterserver/src/modules/masterserver

smerkli at orxonox.net smerkli at orxonox.net
Wed Nov 3 16:20:02 CET 2010


Author: smerkli
Date: 2010-11-03 16:20:02 +0100 (Wed, 03 Nov 2010)
New Revision: 7611

Added:
   code/branches/masterserver/src/modules/masterserver/MasterServer.cc
   code/branches/masterserver/src/modules/masterserver/MasterServerComm.cc
   code/branches/masterserver/src/modules/masterserver/MasterServerComm.h
   code/branches/masterserver/src/modules/masterserver/PeerList.cc
   code/branches/masterserver/src/modules/masterserver/ServerList.cc
Removed:
   code/branches/masterserver/src/modules/masterserver/MasterServer.cpp
   code/branches/masterserver/src/modules/masterserver/PeerList.cpp
   code/branches/masterserver/src/modules/masterserver/ServerList.cpp
   code/branches/masterserver/src/modules/masterserver/TestClient.cpp
Modified:
   code/branches/masterserver/src/modules/masterserver/MasterServer.h
Log:
Communication channel now working both ways, doxygen documentation added, renamed files to convention, re-wrote the client part object-oriented and non-blocking

Copied: code/branches/masterserver/src/modules/masterserver/MasterServer.cc (from rev 7610, code/branches/masterserver/src/modules/masterserver/MasterServer.cpp)
===================================================================
--- code/branches/masterserver/src/modules/masterserver/MasterServer.cc	                        (rev 0)
+++ code/branches/masterserver/src/modules/masterserver/MasterServer.cc	2010-11-03 15:20:02 UTC (rev 7611)
@@ -0,0 +1,247 @@
+/*
+ *   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 "MasterServer.h"
+#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 
+  MasterServer::eventConnect( ENetEvent *event )
+  { /* check for bad parameters */
+    if( !event )
+    { COUT(2) << "MasterServer::eventConnect: No event given.\n" ;
+      return -1;
+    }
+
+    /* convert address to string. */
+    char *addrconv = (char *) calloc( 50, 1 );
+    enet_address_get_host_ip( &(event->peer->address), addrconv, 49 );
+
+    /* output debug info */
+    COUT(4) << "A new client connected from " 
+      << addrconv 
+      << " on port " 
+      << event->peer->address.port << "\n";
+
+    /* game server or client connection? */
+    /* -> decide in protocol */
+    /* game server */
+    /* add to game server list */
+    /*  */
+
+    /* client */
+    /* add to client list */
+
+    /* store string form of address here */
+    event->peer->data = addrconv; 
+
+    /* all fine. */
+    return 0;
+  }
+
+  /* disconnect event */
+  int 
+  MasterServer::eventDisconnect( ENetEvent *event )
+  { /* check for bad parameters */
+    if( !event )
+    { COUT(2) << "No event given.\n";
+      return -1;
+    }
+
+    /* output that the disconnect happened, to be removed at a later time. */
+    COUT(4) << (char*)event->peer->data << " disconnected.\n";
+
+    /* remove the server from the list it belongs to */
+
+    /* Reset the peer's client information. */
+    if( event->peer->data ) free( event->peer->data );
+    return 0;
+  }
+
+  /* data event */
+  int 
+  MasterServer::eventData( ENetEvent *event )
+  { /* output what's in the packet (to be removed later) */
+    if( !event || !(event->packet) || !(event->peer) )
+    { COUT(2) << "No complete event given.\n";
+      return -1;
+    }
+     
+    /* generate address in readable form */
+    char *addrconv = (char *) calloc( 50, 1 );
+    enet_address_get_host_ip( &(event->peer->address), addrconv, 49 );
+
+    /* DEBUG */
+    /* output debug info about the data that has come, to be removed */
+    COUT(4) << "A packet of length" 
+      << event->packet->dataLength
+      << " containing "
+      << event->packet->data
+      << " was received from "
+      << addrconv 
+      << " on channel "
+      << event->channelID << "\n";
+
+    /* send some packet back for testing */
+    /* TESTING */
+
+    /* Create a reliable reply of size 7 containing "reply\0" */
+    ENetPacket * reply = enet_packet_create ("reply", 
+        strlen ("reply") + 1, 
+        ENET_PACKET_FLAG_RELIABLE);
+
+    /* Send the reply to the peer over channel id 0. */
+    enet_peer_send( event->peer, 0, reply );
+
+    /* One could just use enet_host_service() instead. */
+    enet_host_flush( this->server );
+
+    /* /TESTING */
+
+    /* game server or client connection? */
+    /* game server */
+    /* parse data */
+    /* start actions */
+    /* and send reply */
+    /* client */
+    /* parse data */
+    /* start actions */
+    /* and send reply */
+
+    /* delete addrconv */
+    if( addrconv ) free( addrconv );
+
+    /* Clean up the packet now that we're done using it. */
+    enet_packet_destroy( event->packet );
+    return 0;
+  }
+
+
+  /**** MAIN ROUTINE *****/
+  int 
+  MasterServer::run()
+  {
+    /***** ENTER MAIN LOOP *****/
+    ENetEvent *event = (ENetEvent *)calloc(sizeof(ENetEvent), sizeof(char));
+    if( event == NULL )
+    { 
+      COUT(1) << "Could not create ENetEvent structure, exiting.\n";
+      exit( EXIT_FAILURE );
+    }
+
+    /* tell people we're now initialized and blocking. */
+    COUT(0) << "MasterServer initialized, waiting for connections.\n";
+
+    /* 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)
+    { COUT(1) << "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 )
+    { COUT(1) << 
+        "An error occurred while trying to create an ENet server host.\n";
+      exit( EXIT_FAILURE );
+    }
+
+    /***** INITIALIZE GAME SERVER AND PEER LISTS *****/
+    this->mainlist = new ServerList();
+    this->peers = new PeerList();
+    if( this->mainlist == NULL || this->peers == NULL )
+    { COUT(1) << "Error creating server or peer list.\n";
+      exit( EXIT_FAILURE );
+    }
+
+    /* run the main method */
+    run();
+  }
+
+  /* destructor */
+  MasterServer::~MasterServer()
+  {
+    /***** CLEANUP PROCESS *****/
+    /* terminate all networking connections */
+    enet_host_destroy( this->server );
+
+    /* free all used memory */
+    /* clear the list of connected game servers */
+    /* clear the list of connected game clients */
+
+  }
+
+/* end of namespace */
+}

Deleted: code/branches/masterserver/src/modules/masterserver/MasterServer.cpp
===================================================================
--- code/branches/masterserver/src/modules/masterserver/MasterServer.cpp	2010-11-03 11:51:32 UTC (rev 7610)
+++ code/branches/masterserver/src/modules/masterserver/MasterServer.cpp	2010-11-03 15:20:02 UTC (rev 7611)
@@ -1,217 +0,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 "MasterServer.h"
-#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 */
-    /*  */
-
-    /* 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 )
-  { /* 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 */
-    /* 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 
-  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 *****/
-    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 */
-}

Modified: code/branches/masterserver/src/modules/masterserver/MasterServer.h
===================================================================
--- code/branches/masterserver/src/modules/masterserver/MasterServer.h	2010-11-03 11:51:32 UTC (rev 7610)
+++ code/branches/masterserver/src/modules/masterserver/MasterServer.h	2010-11-03 15:20:02 UTC (rev 7611)
@@ -67,10 +67,18 @@
       static MasterServer* singletonPtr_s;
     
     private:
+      /* methods */
+      int eventConnect( ENetEvent *event );
+      int eventDisconnect( ENetEvent *event );
+      int eventData( ENetEvent *event );
+
+      /* members */
       ENetAddress address;
       ENetHost *server;
       ServerList *mainlist;
       PeerList *peers;
+
+      /* main routine */
       int run();
 
   };

Copied: code/branches/masterserver/src/modules/masterserver/MasterServerComm.cc (from rev 7610, code/branches/masterserver/src/modules/masterserver/TestClient.cpp)
===================================================================
--- code/branches/masterserver/src/modules/masterserver/MasterServerComm.cc	                        (rev 0)
+++ code/branches/masterserver/src/modules/masterserver/MasterServerComm.cc	2010-11-03 15:20:02 UTC (rev 7611)
@@ -0,0 +1,187 @@
+/*
+ *   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);
+  }
+
+  /* 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 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();
+  msc.connect( argv[1], 1234 );
+  
+  /* 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 );
+  }
+
+}

Added: code/branches/masterserver/src/modules/masterserver/MasterServerComm.h
===================================================================
--- code/branches/masterserver/src/modules/masterserver/MasterServerComm.h	                        (rev 0)
+++ code/branches/masterserver/src/modules/masterserver/MasterServerComm.h	2010-11-03 15:20:02 UTC (rev 7611)
@@ -0,0 +1,69 @@
+/*
+ *   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;
+
+
+};

Copied: code/branches/masterserver/src/modules/masterserver/PeerList.cc (from rev 7610, code/branches/masterserver/src/modules/masterserver/PeerList.cpp)
===================================================================
--- code/branches/masterserver/src/modules/masterserver/PeerList.cc	                        (rev 0)
+++ code/branches/masterserver/src/modules/masterserver/PeerList.cc	2010-11-03 15:20:02 UTC (rev 7611)
@@ -0,0 +1,96 @@
+/*
+ *   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 "PeerList.h"
+#include <network/packet/ServerInformation.h>
+#include <cstdio>
+
+namespace orxonox
+{
+  PeerList::PeerList() { }
+  PeerList::~PeerList() { }
+
+  int 
+  PeerList::addPeer( ENetPeer *toadd )
+  { /* error correction */
+    if( toadd == NULL ) 
+    { fprintf( stderr, "PeerList::addPeer: empty peer given.\n" );
+      return -1;
+    }
+
+    /* if all went well, push to back of list */
+    this->peerlist.push_back( toadd );
+    return 0;
+  }
+
+  bool sub_compAddr( ENetAddress addr1, ENetAddress addr2 )
+  { 
+    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 */
+    std::list<ENetPeer *>::iterator i;
+
+    /* loop through list elements */
+    for( i = peerlist.begin(); i != peerlist.end(); ++i ) 
+      if( !sub_compAddr((*i)->address, addr ) )
+      { /* found this name, remove and quit */
+        this->peerlist.remove( *i );
+        return true;
+      }
+
+    /* not found */
+    return false;
+  }
+
+  ENetPeer *
+  PeerList::findPeerByAddr( ENetAddress addr )
+  { /* get an iterator */
+    std::list<ENetPeer *>::iterator i;
+
+    /* loop through list elements */
+    for( i = peerlist.begin(); i != peerlist.end(); ++i ) 
+      if( !sub_compAddr((*i)->address, addr ) )
+        /* found this name, remove and quit */
+        return *i;
+
+    /* not found */
+    return NULL;
+  }
+
+}
+

Deleted: code/branches/masterserver/src/modules/masterserver/PeerList.cpp
===================================================================
--- code/branches/masterserver/src/modules/masterserver/PeerList.cpp	2010-11-03 11:51:32 UTC (rev 7610)
+++ code/branches/masterserver/src/modules/masterserver/PeerList.cpp	2010-11-03 15:20:02 UTC (rev 7611)
@@ -1,96 +0,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 "PeerList.h"
-#include <network/packet/ServerInformation.h>
-#include <cstdio>
-
-namespace orxonox
-{
-  PeerList::PeerList() { }
-  PeerList::~PeerList() { }
-
-  int 
-  PeerList::addPeer( ENetPeer *toadd )
-  { /* error correction */
-    if( toadd == NULL ) 
-    { fprintf( stderr, "PeerList::addPeer: empty peer given.\n" );
-      return -1;
-    }
-
-    /* if all went well, push to back of list */
-    this->peerlist.push_back( toadd );
-    return 0;
-  }
-
-  bool sub_compAddr( ENetAddress addr1, ENetAddress addr2 )
-  { 
-    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 */
-    std::list<ENetPeer *>::iterator i;
-
-    /* loop through list elements */
-    for( i = peerlist.begin(); i != peerlist.end(); ++i ) 
-      if( !sub_compAddr((*i)->address, addr ) )
-      { /* found this name, remove and quit */
-        this->peerlist.remove( *i );
-        return true;
-      }
-
-    /* not found */
-    return false;
-  }
-
-  ENetPeer *
-  PeerList::findPeerByAddr( ENetAddress addr )
-  { /* get an iterator */
-    std::list<ENetPeer *>::iterator i;
-
-    /* loop through list elements */
-    for( i = peerlist.begin(); i != peerlist.end(); ++i ) 
-      if( !sub_compAddr((*i)->address, addr ) )
-        /* found this name, remove and quit */
-        return *i;
-
-    /* not found */
-    return NULL;
-  }
-
-}
-

Copied: code/branches/masterserver/src/modules/masterserver/ServerList.cc (from rev 7610, code/branches/masterserver/src/modules/masterserver/ServerList.cpp)
===================================================================
--- code/branches/masterserver/src/modules/masterserver/ServerList.cc	                        (rev 0)
+++ code/branches/masterserver/src/modules/masterserver/ServerList.cc	2010-11-03 15:20:02 UTC (rev 7611)
@@ -0,0 +1,102 @@
+/*
+ *   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 "ServerList.h"
+
+namespace orxonox
+{ 
+  ServerList::ServerList()
+  { /* create a new list */ }
+
+  ServerList::~ServerList()
+  { /* delete the list */
+    serverlist.clear();
+  }
+
+  int 
+  ServerList::addServer( packet::ServerInformation *toadd )
+  { this->serverlist.push_back( toadd ); 
+    return 0;
+  }
+
+  bool 
+  ServerList::delServerByName( std::string name )
+  { 
+    /* get an iterator */
+    std::list<packet::ServerInformation *>::iterator i;
+
+    /* loop through list elements */
+    for( i = serverlist.begin(); i != serverlist.end(); ++i ) 
+      if( (*i)->getServerName() == name )
+      { /* found this name, remove and quit */
+        this->serverlist.remove( *i );
+        return true;
+      }
+    return false;
+  }
+
+  bool ServerList::delServerByAddress( std::string address )
+  { 
+    /* get an iterator */
+    std::list<packet::ServerInformation *>::iterator i;
+
+    /* loop through list elements */
+    for( i=serverlist.begin(); i != serverlist.end(); ++i ) 
+      if( (*i)->getServerIP() == address )
+      { /* found this name, remove and quit */
+        this->serverlist.remove( *i );
+        return true;
+      }
+    return false;
+  }
+
+
+  /* sort by name */
+  bool sub_compare_names( packet::ServerInformation *no1, 
+    packet::ServerInformation *no2 )
+  { return no1->getServerName() > no2->getServerName(); }
+
+  void ServerList::sortByName()
+  { 
+    this->serverlist.sort( sub_compare_names ); 
+  }
+  
+  /* sort by ping */
+  bool sub_compare_pings( packet::ServerInformation *no1, 
+    packet::ServerInformation *no2 )
+  { 
+    /* TODO */
+    return no1->getServerName() > no2->getServerName();
+  }
+
+  void ServerList::sortByPing()
+  {
+    this->serverlist.sort( sub_compare_pings ); 
+  }
+
+}

Deleted: code/branches/masterserver/src/modules/masterserver/ServerList.cpp
===================================================================
--- code/branches/masterserver/src/modules/masterserver/ServerList.cpp	2010-11-03 11:51:32 UTC (rev 7610)
+++ code/branches/masterserver/src/modules/masterserver/ServerList.cpp	2010-11-03 15:20:02 UTC (rev 7611)
@@ -1,102 +0,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 "ServerList.h"
-
-namespace orxonox
-{ 
-  ServerList::ServerList()
-  { /* create a new list */ }
-
-  ServerList::~ServerList()
-  { /* delete the list */
-    serverlist.clear();
-  }
-
-  int 
-  ServerList::addServer( packet::ServerInformation *toadd )
-  { this->serverlist.push_back( toadd ); 
-    return 0;
-  }
-
-  bool 
-  ServerList::delServerByName( std::string name )
-  { 
-    /* get an iterator */
-    std::list<packet::ServerInformation *>::iterator i;
-
-    /* loop through list elements */
-    for( i = serverlist.begin(); i != serverlist.end(); ++i ) 
-      if( (*i)->getServerName() == name )
-      { /* found this name, remove and quit */
-        this->serverlist.remove( *i );
-        return true;
-      }
-    return false;
-  }
-
-  bool ServerList::delServerByAddress( std::string address )
-  { 
-    /* get an iterator */
-    std::list<packet::ServerInformation *>::iterator i;
-
-    /* loop through list elements */
-    for( i=serverlist.begin(); i != serverlist.end(); ++i ) 
-      if( (*i)->getServerIP() == address )
-      { /* found this name, remove and quit */
-        this->serverlist.remove( *i );
-        return true;
-      }
-    return false;
-  }
-
-
-  /* sort by name */
-  bool sub_compare_names( packet::ServerInformation *no1, 
-    packet::ServerInformation *no2 )
-  { return no1->getServerName() > no2->getServerName(); }
-
-  void ServerList::sortByName()
-  { 
-    this->serverlist.sort( sub_compare_names ); 
-  }
-  
-  /* sort by ping */
-  bool sub_compare_pings( packet::ServerInformation *no1, 
-    packet::ServerInformation *no2 )
-  { 
-    /* TODO */
-    return no1->getServerName() > no2->getServerName();
-  }
-
-  void ServerList::sortByPing()
-  {
-    this->serverlist.sort( sub_compare_pings ); 
-  }
-
-}

Deleted: code/branches/masterserver/src/modules/masterserver/TestClient.cpp
===================================================================
--- code/branches/masterserver/src/modules/masterserver/TestClient.cpp	2010-11-03 11:51:32 UTC (rev 7610)
+++ code/branches/masterserver/src/modules/masterserver/TestClient.cpp	2010-11-03 15:20:02 UTC (rev 7611)
@@ -1,113 +0,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>
-
-int main( int argc, char *argv[] )
-{
-  /* initialize Enet */
-  if (enet_initialize () != 0)
-  { fprintf (stderr, "An error occurred while initializing ENet.\n");
-    return EXIT_FAILURE;
-  }
-  atexit (enet_deinitialize);
-
-  /* client handle */
-  ENetHost *client;
-
-  /* initiate the client */
-  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 (client == NULL)
-  { fprintf (stderr, 
-      "An error occurred while trying to create an ENet client host.\n");
-    exit (EXIT_FAILURE);
-  }
-
-  ENetAddress address;
-  ENetEvent event;
-  ENetPeer *peer;
-
-  /* Connect to some.server.net:1234. */
-  enet_address_set_host (& address, argv[1] );
-  address.port = 1234;
-
-  /* Initiate the connection, allocating the two channels 0 and 1. */
-  peer = enet_host_connect(client, &address, 2, 0);    
-
-  if (peer == NULL )
-  { fprintf( stderr, 
-        "No available peers for initiating an ENet connection.\n");
-    exit (EXIT_FAILURE);
-  }
-
-  /* Wait up to 5 seconds for the connection attempt to succeed. */
-  if (enet_host_service (client, & event, 5000) > 0 &&
-      event.type == ENET_EVENT_TYPE_CONNECT)
-  { fprintf( stdout, "Connection to some.server.net:1234 succeeded." ); 
-    char *theinput = (char *)calloc( 100,1 );
-    while( true )
-    { 
-      fgets( theinput, 90, stdin );
-      if( !strncmp( theinput, "quit", 4 ) )
-        break;
-
-      /* send the data to the friend */
-      /* Create a reliable packet of size 7 containing "packet\0" */
-      ENetPacket * packet = enet_packet_create( theinput, 
-          strlen( theinput ) + 1, 
-          ENET_PACKET_FLAG_RELIABLE);
-
-      /* Send the packet to the peer over channel id 0. */
-      enet_peer_send (peer, 0, packet);
-
-      /* One could just use enet_host_service() instead. */
-      enet_host_flush( client );
-      if( packet ) free( packet );
-    }
-  }
-  else
-  {
-    /* Either the 5 seconds are up or a disconnect event was */
-    /* received. Reset the peer in the event the 5 seconds   */
-    /* had run out without any significant event.            */
-    enet_peer_reset (peer);
-    fprintf( stdout, "Connection to some.server.net:1234 failed." );
-    exit(EXIT_FAILURE);
-  }
-
-  enet_host_destroy(client);
-}




More information about the Orxonox-commit mailing list