Index: trunk/src/lib/network/handshake.cc
===================================================================
--- trunk/src/lib/network/handshake.cc	(revision 6815)
+++ trunk/src/lib/network/handshake.cc	(revision 7954)
@@ -24,5 +24,5 @@
 #include <cassert>
 
-Handshake::Handshake( bool server, int clientId, int networkGameManagerId )
+Handshake::Handshake( bool server, int clientId, int networkGameManagerId, int messageManagerId )
   : Synchronizeable()
 {
@@ -31,175 +31,84 @@
 
   this->setIsServer(server);
-  this->clientId = clientId;
-  this->networkGameManagerId = networkGameManagerId;
-  this->state = 0;
-  this->isOk = false;
-  this->setOwner(0);
+ 
+  orxId_handler = registerVarId( new SynchronizeableInt( &localState.orxId, &remoteState.orxId, "orxonoxId", PERMISSION_ALL ) );
+  version_handler = registerVarId( new SynchronizeableInt( &localState.version, &remoteState.version, "version", PERMISSION_ALL ) );
+  netManId_handler = registerVarId( new SynchronizeableInt( &localState.networkManagerId, &remoteState.networkManagerId, "networkManagerId", PERMISSION_ALL ) );
+  msgManId_handler = registerVarId( new SynchronizeableInt( &localState.messageManagerId, &remoteState.messageManagerId, "messageManagerId", PERMISSION_ALL ) );
+  hostId_handler = registerVarId( new SynchronizeableInt( &localState.hostId, &remoteState.hostId, "hostId", PERMISSION_ALL ) );
+  completed_handler = registerVarId( new SynchronizeableInt( &localState.completed, &remoteState.completed, "completed", PERMISSION_ALL ) );
+  error_handler = registerVarId( new SynchronizeableInt( &localState.error, &remoteState.error, "error", PERMISSION_ALL ) );
+  errorString_handler = registerVarId( new SynchronizeableString( &localState.errorString, &remoteState.errorString, "errorString", PERMISSION_ALL ) );
+  
+  candel_id = registerVarId( new SynchronizeableInt( &localState.canDel, &remoteState.canDel, "canDel", PERMISSION_ALL ) );
+  
+  localState.completed = 0;
+  localState.error = 0;
+  localState.errorString = "";
+  localState.hostId = clientId;
+  localState.networkManagerId = networkGameManagerId;
+  this->localState.messageManagerId = messageManagerId;
+  localState.orxId = _ORXONOX_ID;
+  localState.version = _ORXONOX_VERSION;
+  localState.canDel = 0;
+  
+  remoteState.completed = 0;
+  remoteState.error = 0;
+  remoteState.errorString = "";
+  remoteState.hostId = -1;
+  remoteState.networkManagerId = -1;
+  remoteState.messageManagerId = -1;
+  remoteState.orxId = 0;
+  remoteState.version = 0;
+  remoteState.canDel = 0;
 
   this->setSynchronized(true);
-  PRINTF(5)("Handshake created clientId = %d\n", clientId);
+  PRINTF(0)("Handshake created clientId = %d\n", clientId);
 }
 
-int Handshake::writeBytes( const byte * data, int length, int sender)
+/**
+ * handler for changes in synced vars
+ * @param id id's which have changed
+ */
+void Handshake::varChangeHandler( std::list< int > & id )
 {
-  PRINTF(5)("Handshake::writeBytes states = %d %d %d %d (%d)\n", hasState( HS_RECVD_INIT ), hasState( HS_RECVD_VER ), hasState( HS_RECVD_HID ), hasState( HS_COMPLETED ), state);
+  for ( std::list<int>::iterator it = id.begin(); it != id.end(); it++ )
+  {
+    if ( *it == orxId_handler )
+    {
+      if ( remoteState.orxId != _ORXONOX_ID )
+      {
+        localState.error = 1;
+        localState.completed = 1;
+        localState.errorString = "Seems not to be orxonox!";
+        continue;
+      }
 
-  SYNCHELP_READ_BEGIN();
+    }
+     
+    if ( *it == version_handler )
+    {
+      if ( remoteState.version != _ORXONOX_VERSION )
+      {
+        localState.error = 2;
+        localState.completed = 1;
+        localState.errorString = "Versions of server and client do not match!";
+        continue;
+      }
 
-  if ( hasState( HS_COMPLETED ) )
-       return 0;
-
-  if ( !hasState( HS_RECVD_INIT ) )
-  {
-    if ( length != _INITIAL_DATA_LENGTH )
+    }
+    
+    if ( *it == candel_id )
     {
-      PRINTF(0)("initial packet has wrong size %d instead of %d\n", length, _INITIAL_DATA_LENGTH);
-      setState( HS_COMPLETED );
-      return 0;
+      PRINTF(0)("handshake finished candel changed\n");
     }
-
-    if ( strncmp((char*)data, _INITIAL_DATA, length) )
-    {
-      PRINTF(0)("initial packed does not match\n");
-      setState( HS_COMPLETED );
-      return length;
-    }
-
-    setState( HS_RECVD_INIT );
-    PRINTF(0)("got valid initial packet from client %d\n", clientId);
-    return length;
+    
   }
-
-  if ( hasState( HS_RECVD_INIT ) && !hasState( HS_RECVD_VER ) )
-  {
-    if ( length != _ORXONOX_VERSION_LENGTH )
-    {
-      PRINTF(0)("version number packet has wrong size %d instead of %d\n", length, _ORXONOX_VERSION_LENGTH);
-      setState( HS_COMPLETED );
-      return 0;
-    }
-
-    if ( strncmp((char*)data, _ORXONOX_VERSION, length) )
-    {
-      PRINTF(0)("versions do not match\n");
-      setState( HS_COMPLETED );
-      return length;
-    }
-
-    setState( HS_RECVD_VER );
-
-    PRINTF(0)("client %d's version does match\n", clientId);
-    return length;
-  }
-
-  if ( !isServer() && hasState( HS_RECVD_VER ) && !hasState( HS_RECVD_HID ) )
-  {
-    /*if ( length != INTSIZE+INTSIZE )
-    {
-      PRINTF(0)("hostID packet has wrong size %d instead of %d\n", length, INTSIZE+INTSIZE);
-      setState( HS_COMPLETED );
-      return 0;
-  }*/
-
-    setState( HS_COMPLETED );
-    setState( HS_RECVD_HID );
-    this->isOk = true;
-    SYNCHELP_READ_INT( this->newHostId, NWT_HS_HOST_ID );
-    SYNCHELP_READ_INT( this->newNetworkGameManagerId, NWT_HS_NGM_ID );
-
-    if ( newHostId == 0 )
-    {
-      setState( HS_WAS_REJECT );
-      isOk = false;
-      PRINTF(0)("Server did not accept handshake!\n");
-    }
-    else
-    {
-      PRINTF(0)("got my hostID: %d and networkGameManagerId: %d\n", newHostId, newNetworkGameManagerId);
-    }
-    return SYNCHELP_READ_N;
-  }
-
+  
+  if (
+      remoteState.orxId == _ORXONOX_ID &&
+      remoteState.version == _ORXONOX_VERSION
+     )
+    localState.completed = 1;
 }
 
-int Handshake::readBytes( byte * data, int maxLength, int * reciever )
-{
-  PRINTF(5)("Handshake::readBytes states = %d %d %d %d (%d)\n", hasState( HS_SENT_INIT ), hasState( HS_SENT_VER ), hasState( HS_SENT_HID ), hasState( HS_COMPLETED ), state);
-
-  SYNCHELP_WRITE_BEGIN();
-
-  if ( hasState( HS_COMPLETED ) )
-    return 0;
-
-  if ( !hasState( HS_SENT_INIT ) )
-  {
-    if ( maxLength < _INITIAL_DATA_LENGTH )
-    {
-      PRINTF(0)("buffer too small for _INITIAL_DATA");
-      setState( HS_COMPLETED );
-      return 0;
-    }
-
-    setState( HS_SENT_INIT );
-    memcpy(data, _INITIAL_DATA, _INITIAL_DATA_LENGTH);
-    if ( this->isServer() )
-      *reciever = clientId;
-    return _INITIAL_DATA_LENGTH;
-  }
-
-  if ( hasState( HS_RECVD_INIT ) && hasState( HS_SENT_INIT ) && !hasState( HS_SENT_VER ) )
-  {
-    if ( maxLength < _ORXONOX_VERSION_LENGTH )
-    {
-      PRINTF(0)("buffer too small for version number");
-      setState( HS_COMPLETED );
-      return 0;
-    }
-
-    setState( HS_SENT_VER );
-    memcpy(data, _ORXONOX_VERSION, _ORXONOX_VERSION_LENGTH);
-    if ( this->isServer() )
-      *reciever = clientId;
-    return _ORXONOX_VERSION_LENGTH;
-  }
-
-  if ( isServer() && hasState( HS_RECVD_VER) && hasState( HS_SENT_VER ) && !hasState( HS_SENT_HID ) )
-  {
-    if ( maxLength < 2 )
-    {
-      PRINTF(0)("buffer too small for ID");
-      setState( HS_COMPLETED );
-      return 0;
-    }
-
-    setState( HS_SENT_HID );
-    setState( HS_COMPLETED );
-
-    if ( hasState( HS_DO_REJECT ) )
-    {
-      isOk = false;
-      //memcpy(data, (byte*)0, 4);
-      SYNCHELP_WRITE_INT( 0, NWT_HS_HOST_ID);
-      SYNCHELP_WRITE_INT( 0, NWT_HS_NGM_ID);
-    }
-    else
-    {
-      isOk = true;
-      //memcpy(data, &clientId, 4);
-      SYNCHELP_WRITE_INT( clientId, NWT_HS_HOST_ID );
-      SYNCHELP_WRITE_INT( networkGameManagerId, NWT_HS_NGM_ID );
-    }
-    *reciever = clientId;
-    return SYNCHELP_WRITE_N;
-  }
-
-  return 0;
-}
-
-void Handshake::writeDebug( ) const
-{
-}
-
-void Handshake::readDebug( ) const
-{
-}
