void
newsoul::DistributedSocket::onMessageReceived(const MessageData * data)
{
    if (m_DataTimeout.isValid())
        newsoul()->reactor()->removeTimeout(m_DataTimeout);

  switch(data->type)
  {
    #define MAP_MESSAGE(ID, TYPE, EVENT) \
      case ID: \
      { \
        NNLOG("newsoul.messages.distributed", "Received distributed message " #TYPE "."); \
        TYPE msg; \
        msg.setDistributedSocket(this); \
        msg.parse_network_packet(data->data, data->length); \
        EVENT(&msg); \
        break; \
      }
    #include "distributedeventtable.h"
    #undef MAP_MESSAGE

    default:
        NNLOG("newsoul.distrib.warn", "Received unknown distributed message, type: %u, length: %u", data->type, data->length);
        NetworkMessage msg;
        msg.parse_network_packet(data->data, data->length);
  }
}
void
newsoul::HandshakeSocket::onMessageReceived(const MessageData * data)
{
  switch(data->type)
  {
    case 0:
    {
      // Apparently, we requested somebody to connect to us.
      NNLOG("newsoul.messages.handshake", "Received peer handshake message HPierceFirewall");
      HPierceFirewall msg;
      msg.parse_network_packet(data->data, data->length);
      m_Token = msg.token;
      receiveBuffer().seek(data->length + 5);
      // Tell the peer manager, it should know more.
      m_Newsoul->peers()->firewallPiercedEvent(this);
      // This particular socket is no longer needed. Remove it from the reactor.
      reactor()->remove(this);
      return;
    }
    case 1:
    {
      // Somebody wants to establish a connection.
      NNLOG("newsoul.messages.handshake", "Received peer handshake message HInitiate");
      HInitiate msg;
      msg.parse_network_packet(data->data, data->length);
      NNLOG("newsoul.hand.debug", "HInitiate payload: %s %s %u", msg.user.c_str(), msg.type.c_str(), msg.token);
      // Set some variables.
      m_Token = msg.token;
      m_User = msg.user;
      // Seek past the message.
      if (receiveBuffer().count() >= data->length + 5)
        receiveBuffer().seek(data->length + 5);
      else
        receiveBuffer().clear();
      if(msg.type == "P")
      {
        // Create a new PeerSocket which will copy our descriptor and state.
        newsoul::PeerSocket * that = new newsoul::PeerSocket(this);
        newsoul()->peers()->addPeerSocket(that);
        // Add the newly constructed socket to the reactor.
        reactor()->add(that);
      }
      else if(msg.type == "F")
      {
        // Create a new TicketSocket which will copy our descriptor and state.
        newsoul::TicketSocket * that = new newsoul::TicketSocket(this);
        // Add the newly constructed socket to the reactor.
        reactor()->add(that);
        // There may be some data waiting in the buffer (sent at connection). We have to ask the ticketsocket to check it.
        that->findTicket();
      }
      else if(msg.type == "D")
      {
        // Create a new DistributedSocket which will copy our descriptor and state.
        newsoul::DistributedSocket * that = new newsoul::DistributedSocket(this);
        // A potential parent doesn't care about our position
        if (!newsoul()->searches()->isPotentialParent(m_User))
            that->sendPosition();
        // Add the newly constructed socket to the reactor.
        reactor()->add(that);
      }
      else
      {
        NNLOG("newsoul.hand.warn", "Invalid incoming connection type '%s'.", msg.type.c_str());
      }
      // Clear our receive buffer so we stop processing data.
      receiveBuffer().clear();
      // Remove this socket from the reactor as it's no longer needed.
      reactor()->remove(this);
      return;
    }
    default:
        NNLOG("newsoul.hand.warn", "Received unknown peer handshake message, type: %u, length: %u", data->type, data->length);
        NetworkMessage msg;
        msg.parse_network_packet(data->data, data->length);
  }
}