/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx 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, or (at your option) any later version. ### File Specific: main-programmer: Christoph Renner co-programmer: ... */ //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ #include "message_manager.h" using namespace std; MessageManager* MessageManager::singletonRef = NULL; /** * standard constructor */ MessageManager::MessageManager () { this->setClassID( CL_MESSAGE_MANAGER, "MessageManager" ); } /** * standard deconstructor */ MessageManager::~MessageManager () { for ( MessageQueue::iterator it = messageQueue.begin(); it != messageQueue.end(); it++ ) { for ( std::list::iterator it2 = it->second.messages.begin(); it2 != it->second.messages.end(); it2++ ) { if ( it2->data ) { delete it2->data; it2->data = NULL; } } it->second.messages.clear(); it->second.toAck.clear(); } messageQueue.clear(); } /** * get the diff to last acked state of userId * * this class does not use the normal SynchronizeableVars for zynchronisation. instead * it defines its own protocol * * @param userId user to create diff for * @param data buffer to copy diff in * @param maxLength max bytes to copy into data * @param stateId id of current state * @param fromStateId the reference state for the delta state * @param priorityTH tells getStateDiff to not send element with priority \< priorityTH * @return n bytes copied into data */ int MessageManager::getStateDiff( int userId, byte * data, int maxLength, int stateId, int fromStateId, int priorityTH ) { int i = 0; int n; n = Converter::intToByteArray( messageQueue[userId].toAck.size(), data + i, maxLength ); i += n; assert( n == INTSIZE ); for ( std::list::iterator it = messageQueue[userId].toAck.begin(); it != messageQueue[userId].toAck.end(); it++) { n = Converter::intToByteArray( *it, data + i, maxLength ); i += n; assert( n == INTSIZE ); } messageQueue[userId].toAck.clear(); n = Converter::intToByteArray( messageQueue[userId].messages.size(), data + i, maxLength ); i += n; assert( n == INTSIZE ); for ( std::list::iterator it = messageQueue[userId].messages.begin(); it != messageQueue[userId].messages.end(); it++ ) { n = Converter::intToByteArray( it->length, data + i, maxLength ); i += n; assert( n == INTSIZE ); n = Converter::intToByteArray( it->number, data + i, maxLength ); i += n; assert( n == INTSIZE ); n = Converter::intToByteArray( it->messageId, data + i, maxLength ); i += n; assert( n == INTSIZE ); assert( i + it->length <= maxLength ); memcpy( data + i, it->data, it->length ); i += it->length; } return i; } /** * sets a new state out of a diff created on another host * @param userId hostId of user who send me that diff * @param data pointer to diff * @param length length of diff * @param stateId id of current state * @param fromStateId id of the base state id * @return number bytes read * @todo check for permissions */ int MessageManager::setStateDiff( int userId, byte * data, int length, int stateId, int fromStateId ) { int i = 0; int n; int nAcks; n = Converter::byteArrayToInt( data + i, &nAcks ); assert( n == INTSIZE ); i += n; std::list acks; int number; for ( int j = 0; j < nAcks; j++ ) { n = Converter::byteArrayToInt( data + i, &number ); assert( n == INTSIZE ); i += n; acks.push_back( number ); } int nMessages; n = Converter::byteArrayToInt( data + i, &nMessages ); assert( n == INTSIZE ); i += n; for ( int j = 0; j < nMessages; j++ ) { } } /** * clean up memory reserved for user * @param userId userid */ void MessageManager::cleanUpUser( int userId ) { } /** * registers function to handle messages with id messageId. someData is passed to callbackfuntion * @param messageId message id to handle * @param cb function pointer to callback function * @param someData this pointer is passed to callback function without modification * @return true on success */ bool MessageManager::registerMessageHandler( MessageId messageId, MessageCallback cb, void * someData ) { } /** * initializes buffers for user * @param userId userId */ void MessageManager::initUser( int userId ) { }