Index: code/branches/network/src/network/ClientConnection.cc
===================================================================
--- code/branches/network/src/network/ClientConnection.cc	(revision 217)
+++ code/branches/network/src/network/ClientConnection.cc	(revision 217)
@@ -0,0 +1,161 @@
+//
+// C++ Interface: ClientConnection
+//
+// Description: The Class ClientConnection manages the servers conenctions to the clients. 
+// each connection is provided by a new process. communication between master process and 
+// connection processes is provided by ...
+//
+//
+// Author:  Oliver Scheuss
+//
+
+#include "network/ClientConnection.h"
+
+namespace network{
+  
+  boost::thread_group network_threads;
+  
+  ClientConnection::ClientConnection(int port, std::string address){
+    quit=false;
+    server=NULL;
+    enet_address_set_host(&serverAddress, address.c_str());
+    serverAddress.port = NETWORK_PORT;
+    established=false;
+  }
+  
+  ClientConnection::ClientConnection(int port, const char *address){
+    quit=false;
+    server=NULL;
+    enet_address_set_host(&serverAddress, address);
+    serverAddress.port = NETWORK_PORT;
+    established=false;
+  }
+  
+  bool ClientConnection::waitEstablished(int milisec){
+    for(int i=0; i<=milisec && !established; i++)
+      usleep(1000);
+    return established;
+  }
+  
+  
+  ENetPacket *ClientConnection::getPacket(ENetAddress &address){
+    if(!buffer.isEmpty())
+      return buffer.pop(address);
+    else
+        return NULL;
+  }
+  
+  bool ClientConnection::queueEmpty(){
+    return buffer.isEmpty();
+  }
+  
+  void ClientConnection::createConnection(){
+    network_threads.create_thread(boost::bind(boost::mem_fn(&ClientConnection::receiverThread), this));
+//     boost::thread thr(boost::bind(boost::mem_fn(&ClientConnection::receiverThread), this));
+    return;
+  }
+  
+  bool ClientConnection::closeConnection(){
+    quit=true;
+    network_threads.join_all();
+    established=false;
+    return true;
+  }
+  
+  
+  bool ClientConnection::addPacket(ENetPacket *packet){
+    if(server==NULL)
+      return false;
+    if(enet_peer_send(server, 1, packet)!=0)
+      return false;
+    else
+      return true;
+  }
+  
+  bool ClientConnection::sendPackets(ENetEvent *event){
+    if(server==NULL)
+      return false;
+    if(enet_host_service(client, event, NETWORK_SEND_WAIT)>=0)
+      return true;
+    else 
+      return false;
+  }
+  
+  void ClientConnection::receiverThread(){
+    // what about some error-handling here ?
+    enet_initialize();
+    atexit(enet_deinitialize);
+    ENetEvent event;
+    client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0);
+    if(client==NULL)
+      // add some error handling here ==========================
+      quit=true;
+    //connect to the server
+    if(!establishConnection())
+      quit=true;
+    //main loop
+    while(!quit){
+      if(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT)<0){
+        // we should never reach this point
+        quit=true;
+        // add some error handling here ========================
+      }
+      switch(event.type){
+        // log handling ================
+      case ENET_EVENT_TYPE_RECEIVE:
+        processData(&event);
+        break;
+      case ENET_EVENT_TYPE_DISCONNECT:
+        // add some error/log handling here
+        // extend =====================
+        break;
+      }
+    }
+    // now disconnect
+    
+    if(!disconnectConnection()) 
+    // if disconnecting failed destroy conn.
+      enet_peer_reset(server);
+    return;
+  }
+  
+  bool ClientConnection::disconnectConnection(){
+    ENetEvent event;
+    enet_peer_disconnect(server, 0);
+    while(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT) > 0){
+      switch (event.type)
+      {
+        case ENET_EVENT_TYPE_RECEIVE:
+          enet_packet_destroy(event.packet);
+          break;
+        case ENET_EVENT_TYPE_DISCONNECT:
+          return true;
+      }
+    }
+    enet_peer_reset(server);
+  }
+  
+  bool ClientConnection::establishConnection(){
+    ENetEvent event;
+    // connect to peer
+    server = enet_host_connect(client, &serverAddress, NETWORK_CLIENT_CHANNELS);
+    if(server==NULL)
+      // error handling
+      return false;
+    // handshake
+    if(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT)>0 && event.type == ENET_EVENT_TYPE_CONNECT){
+      established=true;
+      return true;
+    }
+    else
+      return false;
+  }
+  
+  bool ClientConnection::processData(ENetEvent *event){
+    // just add packet to the buffer
+    // this can be extended with some preprocessing
+    return buffer.push(event);
+  }
+  
+  
+}
Index: code/branches/network/src/network/ClientConnection.h
===================================================================
--- code/branches/network/src/network/ClientConnection.h	(revision 217)
+++ code/branches/network/src/network/ClientConnection.h	(revision 217)
@@ -0,0 +1,77 @@
+//
+// C++ Interface: ClientConnection
+//
+// Description: 
+//
+//
+// Author:  Oliver Scheuss, (C) 2007
+//
+// Copyright: See COPYING file that comes with this distribution
+//
+//
+#ifndef NETWORK_CLIENTCONNECTION_H
+#define NETWORK_CLIENTCONNECTION_H
+
+#include <iostream>
+#include <string>
+// enet library for networking support
+#include <enet/enet.h>
+// boost.thread library for multithreading support
+#include <boost/thread/thread.hpp>
+#include <boost/bind.hpp>
+// headerfile
+#include "network/ClientConnection.h"
+#include "network/PacketBuffer.h"
+
+namespace network{
+  //
+#define NETWORK_PORT 55556
+#define NETWORK_CLIENT_MAX_CONNECTIONS 5
+#define NETWORK_WAIT_TIMEOUT 5000
+#define NETWORK_SEND_WAIT 5
+#define NETWORK_CLIENT_CHANNELS 2
+  
+  
+  class ClientConnection{
+    public:
+    ClientConnection(int port, std::string address);
+    ClientConnection(int port, const char* address);
+    ENetPacket *getPacket(ENetAddress &address); // thread1
+    // check wheter the packet queue is empty
+    bool queueEmpty();
+    // create a new listener thread
+    void createConnection();
+    bool closeConnection();
+    // add a packet to queue for the server
+    bool addPacket(ENetPacket *packet);
+    // send out all queued packets
+    bool sendPackets(ENetEvent *event);
+    bool waitEstablished(int milisec);
+    private:
+    bool processData(ENetEvent *event);
+    // implementation of the listener
+    void receiverThread(); //thread2
+    //packetbuffer
+    bool establishConnection();
+    bool disconnectConnection();
+    PacketBuffer buffer;
+    // enet stuff
+    ENetHost *client;
+    ENetAddress serverAddress;
+    // quit-variable (communication with threads)
+    bool quit;
+    bool established;
+    // clientlist
+    ENetPeer *server;
+  };
+  
+  
+  
+  
+  
+  
+  
+  
+}
+
+#endif
Index: code/branches/network/src/network/Makefile
===================================================================
--- code/branches/network/src/network/Makefile	(revision 213)
+++ code/branches/network/src/network/Makefile	(revision 217)
@@ -14,4 +14,6 @@
 ConnectionManager.o: ConnectionManager.cc ConnectionManager.h
 	${CC} -c ConnectionManager.cc -o ConnectionManager.o -g ${INC}
+ClientConnection.o: ClientConnection.cc ClientConnection.h
+	${CC} -c ClientConnection.cc -o ClientConnection.o -g ${INC}
 
 PacketBuffer.o: PacketBuffer.cc
@@ -26,4 +28,6 @@
 	rm -rf *.o
 
-client: PacketGenerator.o dummyclient.cc
+client_old: PacketGenerator.o dummyclient.cc
 	${CC} dummyclient.cc PacketGenerator.o -o client -lenet -g ${INC}
+client: PacketGenerator.o ClientConnection.o PacketBuffer.o dummyclient2.cc 
+	${CC} dummyclient2.cc PacketGenerator.o PacketBuffer.o ClientConnection.o -o client -lenet -lboost_thread -g ${INC}
Index: code/branches/network/src/network/dummyclient.cc
===================================================================
--- code/branches/network/src/network/dummyclient.cc	(revision 213)
+++ code/branches/network/src/network/dummyclient.cc	(revision 217)
@@ -52,4 +52,5 @@
     cout << "Connection to " << str << " failed." << endl;
     //puts("Connection to localhost:5555 failed.");
+    exit(EXIT_FAILURE);
   }
 
@@ -59,5 +60,5 @@
         // extend the packet and append the string foo to it
         // send packet to peer on channel id 0
-    enet_peer_send(peer, 1, pck.chatMessage("test"));
+    enet_peer_send(peer, 1, pck.chatMessage("test2"));
         // keep the timeout very small for low delay
     if(enet_host_service(client, &event, 1)==0){
@@ -66,4 +67,5 @@
       cout << "failed sending" << endl;
     }
+    usleep(1000000);
   }
 
Index: code/branches/network/src/network/dummyclient2.cc
===================================================================
--- code/branches/network/src/network/dummyclient2.cc	(revision 217)
+++ code/branches/network/src/network/dummyclient2.cc	(revision 217)
@@ -0,0 +1,61 @@
+//
+// Dummy client to test ConnectionManager, PacketBuffer, ClientConnection and other classes
+//
+// Author: Oliver Scheuss
+
+#include <iostream>
+#include <string>
+#include <enet/enet.h>
+#include "network/PacketManager.h"
+#include "network/ClientConnection.h"
+
+using namespace network;
+
+int main(){
+  network::PacketGenerator pck;
+
+  std::cout << "Enter address of the server xxx.xxx.xxx.xxx (enter for localhost)" << std::endl;
+  std::string str;
+  std::getline(std::cin, str);
+  std::cout << "You entered: " << str << std::endl;
+  if(str.compare("")==0)
+    str="127.0.0.1";
+
+  ClientConnection client = ClientConnection(55556, str);
+
+  client.createConnection();
+  
+  if(client.waitEstablished(10000))
+    std::cout << "Connection established" << std::endl;
+  else
+    std::cout << "Connection failed" << std::endl;
+  
+  ENetPacket *packet;
+  ENetEvent event;
+  
+  
+  
+  for(int i=0; i<10; i++){
+        // weihnachtsmann bringt packete
+        // extend the packet and append the string foo to it
+        // send packet to peer on channel id 0
+    client.addPacket(pck.chatMessage("test"));
+        // keep the timeout very small for low delay
+    if(client.sendPackets(&event)){
+      std::cout << "successfully sent: " << event.type << std::endl;
+    }else{
+      std::cout << "failed sending" << std::endl;
+    }
+    //usleep(100000);
+  }
+
+        // now disconnect
+  if(client.closeConnection())
+    std::cout << "Connection successfully closed" << std::endl;
+  else
+    std::cout << "Connection closing failed" << std::endl;
+
+        // 3 seconds timeout
+  return 0;
+}
+
