Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/network/message_manager.cc @ 10589

Last change on this file since 10589 was 10114, checked in by patrick, 18 years ago

merged network back to trunk

File size: 16.6 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Christoph Renner
13   co-programmer: Patrick Boenzli (patrick@orxonox.ethz.ch)
14
15     June 2006: finishing work on the network stream for pps presentation (rennerc@ee.ethz.ch)
16     July 2006: some code rearangement and integration of the proxy server mechanism (boenzlip@ee.ethz.ch)
17     July 2006: message forwarding algorithms
18*/
19
20//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
21
22#include "message_manager.h"
23
24#include "network_stream.h"
25#include "shared_network_data.h"
26#include "converter.h"
27#include <cassert>
28
29
30ObjectListDefinition(MessageManager);
31MessageManager* MessageManager::singletonRef = NULL;
32
33
34/**
35 * standard constructor
36*/
37MessageManager::MessageManager ()
38{
39  this->registerObject(this, MessageManager::_objectList);
40  newNumber = 1;
41  setSynchronized( true );
42}
43
44
45/**
46 * standard deconstructor
47*/
48MessageManager::~MessageManager ()
49{
50  for ( MessageQueue::iterator it = outgoingMessageQueue.begin(); it != outgoingMessageQueue.end(); it++ )
51  {
52    for ( std::list<NetworkMessage>::iterator it2 = it->second.messages.begin(); it2 != it->second.messages.end(); it2++ )
53    {
54      if ( it2->data )
55      {
56        delete [] it2->data;
57        it2->data = NULL;
58      }
59    }
60
61    it->second.messages.clear();
62    it->second.toAck.clear();
63  }
64
65  outgoingMessageQueue.clear();
66
67  this->messageHandlerMap.clear();
68
69  MessageManager::singletonRef = NULL;
70}
71
72/**
73 * get the diff to last acked state of userId
74 *
75 * this class does not use the normal SynchronizeableVars for zynchronisation. instead
76 * it defines its own protocol
77 *
78 * @param userId user to create diff for
79 * @param data buffer to copy diff in
80 * @param maxLength max bytes to copy into data
81 * @param stateId id of current state
82 * @param fromStateId the reference state for the delta state
83 * @param priorityTH tells getStateDiff to not send element with priority \< priorityTH
84 * @return n bytes copied into data
85 */
86int MessageManager::getStateDiff( int userId, byte * data, int maxLength, int stateId, int fromStateId, int priorityTH )
87{
88  int i = 0;
89  int n;
90
91  n = Converter::intToByteArray( outgoingMessageQueue[userId].toAck.size(), data + i, maxLength );
92  i += n;
93  assert( n == INTSIZE );
94
95  for ( std::list<int>::iterator it = outgoingMessageQueue[userId].toAck.begin(); it != outgoingMessageQueue[userId].toAck.end(); it++)
96  {
97    n = Converter::intToByteArray( *it, data + i, maxLength );
98    i += n;
99    assert( n == INTSIZE );
100  }
101
102  outgoingMessageQueue[userId].toAck.clear();
103
104  n = Converter::intToByteArray( outgoingMessageQueue[userId].messages.size(), data + i, maxLength );
105  i += n;
106  assert( n == INTSIZE );
107
108  // write the message down, a message has this structure:
109  //   | data_length | serial_number | message_type | source_id | dest_id | ...data... |
110  //      4byte        4byte            4byte         4byte      4byte     data_length
111  for ( std::list<NetworkMessage>::iterator it = outgoingMessageQueue[userId].messages.begin(); it != outgoingMessageQueue[userId].messages.end(); it++ )
112  {
113    // send data length
114    n = Converter::intToByteArray( it->length, data + i, maxLength );
115    i += n;
116    assert( n == INTSIZE );
117
118    // send serial number
119    n = Converter::intToByteArray( it->number, data + i, maxLength );
120    i += n;
121    assert( n == INTSIZE );
122
123    // send message type
124    n = Converter::intToByteArray( it->messageType, data + i, maxLength );
125    i += n;
126    assert( n == INTSIZE );
127
128    // send sender id
129    n = Converter::intToByteArray( it->senderId, data + i, maxLength );
130    i += n;
131    assert( n == INTSIZE );
132
133    // send destination id
134    n = Converter::intToByteArray( it->destinationId, data + i, maxLength );
135    i += n;
136    assert( n == INTSIZE );
137
138    // send receiver type
139    n = Converter::intToByteArray( it->recieverType, data + i, maxLength );
140    i += n;
141    assert( n == INTSIZE );
142
143    // and copy the data
144    assert( i + it->length <= maxLength );
145    memcpy( data + i, it->data, it->length );
146    i += it->length;
147  }
148
149  return i;
150}
151
152/**
153 * sets a new state out of a diff created on another host
154 * @param userId hostId of user who send me that diff
155 * @param data pointer to diff
156 * @param length length of diff
157 * @param stateId id of current state
158 * @param fromStateId id of the base state id
159 * @return number bytes read
160 * @todo check for permissions
161 */
162int MessageManager::setStateDiff( int userId, byte * data, int length, int stateId, int fromStateId )
163{
164  int i = 0;
165  int n;
166
167  int nAcks;
168
169
170  assert( i + INTSIZE <= length );
171  n = Converter::byteArrayToInt( data + i, &nAcks );
172  assert( n == INTSIZE );
173  i += n;
174
175  std::list<int> acks;
176
177  int number;
178
179  for ( int j = 0; j < nAcks; j++ )
180  {
181    assert( i + INTSIZE <= length );
182    n = Converter::byteArrayToInt( data + i, &number );
183    assert( n == INTSIZE );
184    i += n;
185
186    acks.push_back( number );
187  }
188
189  int nMessages;
190
191  assert( i + INTSIZE <= length );
192  n = Converter::byteArrayToInt( data + i, &nMessages );
193  assert( n == INTSIZE );
194  i += n;
195
196  int messageLength, messageType;
197  int senderId, destinationId, recieverType;
198
199  // now go through all newly received messages and assemble them
200  for ( int j = 0; j < nMessages; j++ )
201  {
202    // read the length
203    assert( i + INTSIZE <= length );
204    n = Converter::byteArrayToInt( data + i, &messageLength );
205    assert( n == INTSIZE );
206    i += n;
207
208    // read the serial number
209    assert( i + INTSIZE <= length );
210    n = Converter::byteArrayToInt( data + i, &number );
211    assert( n == INTSIZE );
212    i += n;
213
214    // read the message type
215    assert( i + INTSIZE <= length );
216    n = Converter::byteArrayToInt( data + i, &messageType );
217    assert( n == INTSIZE );
218    i += n;
219
220    // read the sender id
221    assert( i + INTSIZE <= length );
222    n = Converter::byteArrayToInt( data + i, &senderId );
223    assert( n == INTSIZE );
224    i += n;
225
226    //read the destination id
227    assert( i + INTSIZE <= length );
228    n = Converter::byteArrayToInt( data + i, &destinationId);
229    assert( n == INTSIZE );
230    i += n;
231
232    // read the receiver type
233    assert( i + INTSIZE <= length );
234    n = Converter::byteArrayToInt( data + i, &recieverType);
235    assert( n == INTSIZE );
236    i += n;
237
238    if ( number > 0 )
239      outgoingMessageQueue[userId].toAck.push_back( number );
240
241//     PRINTF(0)("got message with type: %i\n", messageType);
242    assert( i + messageLength <= length );
243    // make sure there is a message handler for this message type
244    //if ( !( messageHandlerMap.find( (MessageType)messageType ) != messageHandlerMap.end()) )
245    //{
246//          PRINTF(0)("messagetype: %d\n", messageType);
247//          this->objectList().debugAll( 0 );
248//    }
249   
250    assert( messageHandlerMap.find( (MessageType)messageType ) != messageHandlerMap.end());
251
252
253    if ( std::find( outgoingMessageQueue[userId].recievedMessages.begin(), outgoingMessageQueue[userId].recievedMessages.end(), number ) ==
254         outgoingMessageQueue[userId].recievedMessages.end() )
255    {
256
257      // find out if this message is addressed for this client too
258      if( recieverType == RT_ALL_BUT_ME  && SharedNetworkData::getInstance()->getHostID() != senderId ||
259          recieverType == RT_ALL_ME ||
260          recieverType == RT_NOT_USER && SharedNetworkData::getInstance()->getHostID() != destinationId ||
261          recieverType == RT_USER  && SharedNetworkData::getInstance()->getHostID() == destinationId ||
262          recieverType == RT_SERVER && SharedNetworkData::getInstance()->isMasterServer() ||
263          recieverType == RT_SERVER && SharedNetworkData::getInstance()->isProxyServerActive())
264      {
265
266        PRINTF(0)("<<< MessageManager: got msg with type: %i, from sender %i, to rec: %i\n", messageType, senderId, destinationId);
267      // call the handler function and handle errors
268        if ( !(*(messageHandlerMap[(MessageType)messageType].cb))( (MessageType)messageType, data + i, messageLength,
269                 messageHandlerMap[(MessageType)messageType].someData, senderId, destinationId ) )
270        {
271        // if the message is not handled correctly, bush it back to the incoming packets therefore trying it later
272          NetworkMessage msg;
273
274          msg.data = new byte[messageLength];
275          memcpy( msg.data, data + i, messageLength );
276          msg.length = messageLength;
277          msg.messageType = (MessageType)messageType;
278          msg.number = userId;
279          msg.senderId = senderId;
280          msg.recieverType = (RecieverType)recieverType;
281          msg.destinationId = destinationId;
282
283          incomingMessageQueue.push_back( msg );
284        }
285      }
286
287
288      // check if the message needs to be forwarded
289      if( recieverType == RT_ALL_BUT_ME ||
290          recieverType == RT_ALL_ME ||
291          recieverType == RT_NOT_USER ||
292          recieverType == RT_USER  && SharedNetworkData::getInstance()->getHostID() != destinationId ||
293          recieverType == RT_SERVER && SharedNetworkData::getInstance()->isProxyServerActive() )
294      {
295        // forwarding the messages but only if its a proxy
296        if( SharedNetworkData::getInstance()->isProxyServerActive())
297        {
298          PRINTF(0)("===========>> Forwarding Message msg with type: %i, from sender %i, to rec: %i\n", messageType, senderId, destinationId);
299          NetworkMessage msg;
300
301          msg.data = new byte[messageLength];
302          memcpy( msg.data, data + i, messageLength );
303          msg.length = messageLength;
304          msg.messageType = (MessageType)messageType;
305          msg.number = userId;
306          msg.senderId = senderId;
307          msg.destinationId = destinationId;
308          msg.recieverType = (RecieverType)recieverType;
309
310          this->sendMessage(msg.messageType, msg.data, msg.length, msg.recieverType, msg.senderId = senderId, msg.destinationId, MP_HIGHBANDWIDTH);
311        }
312      }
313
314      // save the serial number for ack signaling
315      outgoingMessageQueue[userId].recievedMessages.push_back( number );
316    }
317
318    i += messageLength;
319  }
320
321
322  //walk throu message queue and remove acked messages
323  for ( std::list<NetworkMessage>::iterator it = outgoingMessageQueue[userId].messages.begin(); it != outgoingMessageQueue[userId].messages.end();  )
324  {
325    if ( std::find( acks.begin(), acks.end(), it->number) != acks.end() )
326    {
327      std::list<NetworkMessage>::iterator delIt = it;
328      it++;
329      outgoingMessageQueue[userId].messages.erase( delIt );
330      continue;
331    }
332    it++;
333  }
334
335  //TODO find bether way. maybe with timestamp
336  if ( outgoingMessageQueue[userId].recievedMessages.size() > 1000 )
337  {
338    for ( int j = 0; j < (int)outgoingMessageQueue[userId].recievedMessages.size() - 1000; j++ )
339      outgoingMessageQueue[userId].recievedMessages.erase( outgoingMessageQueue[userId].recievedMessages.begin() );
340  }
341
342  return i;
343}
344
345
346
347
348/**
349 * processes the message manager data, specialy check for localy generated messages
350 */
351void MessageManager::processData()
352{
353  // now call the message handlers with the new message
354  for ( std::list<NetworkMessage>::iterator it = incomingMessageQueue.begin(); it != incomingMessageQueue.end();  )
355  {
356    PRINTF(0)("<<< MessageManager: got local msg with type: %i, from sender %i, to rec: %i\n", (*it).messageType, (*it).senderId, (*it).destinationId);
357
358    if ( (*(messageHandlerMap[it->messageType].cb))( it->messageType, it->data, it->length, messageHandlerMap[it->messageType].someData,
359                                                     /*it->number, */it->senderId, it->destinationId ) )
360    {
361      std::list<NetworkMessage>::iterator delIt = it;
362      if ( it->data )
363        delete it->data;
364      it++;
365      incomingMessageQueue.erase( delIt );
366      continue;
367    }
368    it++;
369  }
370
371}
372
373
374
375
376/**
377 * clean up memory reserved for user
378 * @param userId userid
379 */
380void MessageManager::cleanUpUser( int userId )
381{
382  if ( outgoingMessageQueue.find( userId ) == outgoingMessageQueue.end() )
383    return;
384
385  for ( std::list<NetworkMessage>::iterator it = outgoingMessageQueue[userId].messages.begin(); it != outgoingMessageQueue[userId].messages.end(); it++ )
386  {
387    if ( it->data )
388      delete it->data;
389    it->data = NULL;
390  }
391
392  outgoingMessageQueue[userId].toAck.clear();
393
394  outgoingMessageQueue.erase( userId );
395}
396
397/**
398 * registers function to handle messages with id messageType. someData is passed to callbackfuntion
399 * @param messageType message id to handle
400 * @param cb function pointer to callback function
401 * @param someData this pointer is passed to callback function without modification
402 * @return true on success
403 */
404bool MessageManager::registerMessageHandler( MessageType messageType, MessageCallback cb, void * someData )
405{
406  MessageHandler messageHandler;
407
408  messageHandler.cb = cb;
409  messageHandler.messageType = messageType;
410  messageHandler.someData = someData;
411
412  messageHandlerMap[messageType] = messageHandler;
413
414  return true;
415}
416
417/**
418 * initializes buffers for user
419 * @param userId userId
420 */
421void MessageManager::initUser( int userId )
422{
423  // just do something so map creates a new entry
424  outgoingMessageQueue[userId].toAck.clear();
425  //assert( outgoingMessageQueue[userId].messages.size() == 0 );
426}
427
428
429
430/**
431 * send a message to one or more clients
432 * recieverType:
433 *               RT_ALL send to all users. reciever is ignored
434 *               RT_USER send only to reciever
435 *               RT_NOT_USER send to all but reciever
436 *
437 * @param messageType message id
438 * @param data pointer to data
439 * @param dataLength length of data
440 * @param recieverType type of the receiver
441 * @param reciever the userId of the receiver if needed (depends on the ReceiverType)
442 */
443void MessageManager::sendMessage( MessageType messageType, byte * data, int dataLength, RecieverType recieverType, int reciever, MessagePriority messagePriority )
444{
445  this->sendMessage(messageType, data, dataLength, recieverType, SharedNetworkData::getInstance()->getHostID(), reciever, messagePriority);
446}
447
448
449/**
450 * send a message to one or more clients as a special client
451 * recieverType:
452 *               RT_ALL send to all users. reciever is ignored
453 *               RT_USER send only to reciever
454 *               RT_NOT_USER send to all but reciever
455 *
456 * @param messageType message id
457 * @param data pointer to data
458 * @param dataLength length of data
459 * @param recieverType type of the receiver
460 * @param sender the userId of the sender if there is need for shadowing it (eg. for msg forwarding)
461 * @param reciever the userId of the receiver if needed (depends on the ReceiverType)
462 */
463    void MessageManager::sendMessage( MessageType messageType, byte * data, int dataLength, RecieverType recieverType, int sender, int reciever, MessagePriority messagePriority )
464{
465  PRINTF(0)(" >>> MessageManager: sending msg with type: %i, recieverType: %i, reciever %i\n", messageType, recieverType, reciever);
466
467  // go through all outgoing message queues and add the message if its appropriate
468  for ( MessageQueue::iterator it = this->outgoingMessageQueue.begin(); it != this->outgoingMessageQueue.end(); it++ )
469  {
470
471    if (
472         recieverType == RT_ALL_ME      ||
473         recieverType == RT_ALL_BUT_ME  ||
474         recieverType == RT_USER        && it->first == reciever ||
475         recieverType == RT_USER        && reciever == NET_ID_MASTER_SERVER && !getNetworkStream()->isUserMasterServer( it->first ) ||  //(*)
476         recieverType == RT_NOT_USER    && it->first != reciever ||
477         recieverType == RT_SERVER      && getNetworkStream()->isUserMasterServer( it->first ) ||
478         recieverType == RT_SERVER      && getNetworkStream()->isUserProxyServerActive( it->first )
479       )// (*) special case: forward
480    {
481      NetworkMessage msg;
482
483      msg.data = new byte[dataLength];
484      memcpy( msg.data, data, dataLength );
485      msg.length = dataLength;
486      msg.messageType = messageType;
487      msg.number = this->newNumber++;
488      msg.senderId = sender;
489      msg.destinationId = reciever;
490      msg.recieverType = recieverType;
491      msg.priority = messagePriority;
492
493      it->second.messages.push_back( msg );
494    }
495
496
497  }
498
499
500  // if the message is also for myself, handle it here
501  if ( recieverType == RT_ALL_ME ||
502       recieverType == RT_USER   && reciever == SharedNetworkData::getInstance()->getHostID()
503     )
504  {
505    NetworkMessage msg;
506
507    msg.data = new byte[dataLength];
508    memcpy( msg.data, data, dataLength );
509    msg.length = dataLength;
510    msg.messageType = messageType;
511    msg.number = SharedNetworkData::getInstance()->getHostID();
512    msg.senderId = sender;
513    msg.destinationId = reciever;
514    msg.recieverType = recieverType;
515    msg.priority = messagePriority;
516
517    this->incomingMessageQueue.push_back( msg );
518  }
519}
520
521
Note: See TracBrowser for help on using the repository browser.