Index: code/branches/masterserver/src/modules/masterserver/MasterServerComm.cc
===================================================================
--- code/branches/masterserver/src/modules/masterserver/TestClient.cpp	(revision 7589)
+++ code/branches/masterserver/src/modules/masterserver/MasterServerComm.cc	(revision 7611)
@@ -31,19 +31,18 @@
 #include <cstring>
 #include <enet/enet.h>
+#include "MasterServerComm.h"
 
-int main( int argc, char *argv[] )
+MasterServerComm::MasterServerComm()
 {
   /* initialize Enet */
   if (enet_initialize () != 0)
   { fprintf (stderr, "An error occurred while initializing ENet.\n");
-    return EXIT_FAILURE;
+    exit(EXIT_FAILURE);
   }
   atexit (enet_deinitialize);
 
-  /* client handle */
-  ENetHost *client;
 
   /* initiate the client */
-  client = enet_host_create( NULL /* create a client host */,
+  this->client = enet_host_create( NULL /* create a client host */,
       1,
       2, /* allow up 2 channels to be used, 0 and 1 */
@@ -52,22 +51,27 @@
 
   /* see if it worked */
-  if (client == NULL)
+  if (this->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;
+MasterServerComm::~MasterServerComm()
+{
+  enet_host_destroy(this->client);
+}
 
-  /* Connect to some.server.net:1234. */
-  enet_address_set_host (& address, argv[1] );
-  address.port = 1234;
+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. */
-  peer = enet_host_connect(client, &address, 2, 0);    
+  this->peer = enet_host_connect(this->client, &this->address, 2, 0);    
 
-  if (peer == NULL )
+  if (this->peer == NULL )
   { fprintf( stderr, 
         "No available peers for initiating an ENet connection.\n");
@@ -75,39 +79,109 @@
   }
 
-  /* 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 );
-    }
-  }
+  /* 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
   {
-    /* 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." );
+    enet_peer_reset (this->peer);
+    fprintf( stdout, "Connection to %s failed.", address );
     exit(EXIT_FAILURE);
   }
 
-  enet_host_destroy(client);
+  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 );
+  }
+
+}
