Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/network/TrafficControl.cc @ 2415

Last change on this file since 2415 was 2415, checked in by scheusso, 15 years ago
  • adjusted some priorities of objects (movableentity, controllableentity, positionableentity, model)
  • dedicated server mode doesn't consume 100%cpu load anymore now
File size: 15.3 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Oliver Scheuss <scheusso [at] ee.ethz.ch>, (C) 2008
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "TrafficControl.h"
30
31#include "synchronisable/Synchronisable.h"
32#include "core/ConfigValueIncludes.h"
33
34#include <cassert>
35#include <boost/bind.hpp>
36
37namespace orxonox {
38 
39  static const unsigned int SCHED_PRIORITY_OFFSET = -1;
40
41  objInfo::objInfo(uint32_t ID, uint32_t creatorID, int32_t curGsID, int32_t diffGsID, uint32_t size, unsigned int prioperm, unsigned int priosched)
42  { 
43    objID = ID; objCreatorID = creatorID; objCurGS = curGsID; objDiffGS = diffGsID; objSize = size; objValuePerm = prioperm; objValueSched = priosched; 
44  }
45 
46  objInfo::objInfo()
47  { 
48    objID = OBJECTID_UNKNOWN; objCreatorID = OBJECTID_UNKNOWN; objCurGS = GAMESTATEID_INITIAL; objDiffGS = objCurGS; objSize = 0; objValuePerm = 0; objValueSched = 0; 
49  }
50 
51 
52 
53  obj::obj()
54  { 
55    objID = OBJECTID_UNKNOWN; objCreatorID = OBJECTID_UNKNOWN; objSize = 0; objDataOffset = 0; 
56  }
57  obj::obj( uint32_t ID, uint32_t creatorID, uint32_t size, uint32_t offset )
58  {
59    objID = ID; objCreatorID = creatorID; objSize = size; objDataOffset = offset;
60  }
61 
62/**
63*Initializing protected members
64*/
65        TrafficControl *TrafficControl::instance_=0;
66       
67        /**
68        * @brief Constructor: assures that only one reference will be created and sets the pointer
69        */
70        TrafficControl::TrafficControl()
71        {
72    RegisterObject(TrafficControl);
73          assert(instance_==0);
74          instance_=this;
75    this->setConfigValues();
76        }
77       
78        /**
79        * @brief Destructor: resets the instance pointer to 0
80        */
81        TrafficControl::~TrafficControl()
82        { 
83          //was macht das genau? da instance ja gleich this ist im moment
84          instance_=0;
85          //muss ich alles deallozieren was ich im constructor aufgebaut habe?
86        }
87
88/**
89*Definition of public members
90*/
91
92  void TrafficControl::setConfigValues()
93  {
94    SetConfigValue ( targetSize, 5000 );
95  }
96
97  /**
98          *eigener sortieralgorithmus
99  */
100//   bool TrafficControl::priodiffer(obj i, obj j)
101//   {
102//     std::map<unsigned int, objInfo>::iterator iti;
103//     std::map<unsigned int, objInfo>::iterator itj;
104//     iti=listToProcess_.find(i.objID);
105//     itj=listToProcess_.find(j.objID);
106//     return iti->second.objValuePerm < itj->second.objValuePerm;
107//   }
108 
109  /**
110  * eigener sortieralgorithmus
111  */
112  bool TrafficControl::priodiffer(uint32_t clientID, obj i, obj j)
113  {
114    assert(clientListPerm_.find(clientID) != clientListPerm_.end());  //make sure the client exists in our list
115    assert(clientListPerm_[clientID].find(i.objID) != clientListPerm_[clientID].end()); // make sure the object i is in the client list
116    assert(clientListPerm_[clientID].find(j.objID) != clientListPerm_[clientID].end()); // make sure the object j is in the client list
117   
118    int prio1 = clientListPerm_[clientID][i.objID].objValuePerm + clientListPerm_[clientID][i.objID].objValueSched;
119    int prio2 = clientListPerm_[clientID][j.objID].objValuePerm + clientListPerm_[clientID][j.objID].objValueSched;
120//     int prio1 = clientListPerm_[clientID][i.objID].objID;
121//     int prio2 = clientListPerm_[clientID][j.objID].objID;
122    // NOTE: smaller priority is better
123    return prio1 < prio2;
124  }
125
126
127        void TrafficControl::processObjectList(unsigned int clientID, unsigned int gamestateID, std::list<obj> *list)
128        {
129//        copiedVector = *list;
130          currentClientID=clientID;
131          currentGamestateID=gamestateID;
132          evaluateList(clientID, list);
133          //list hatte vorher ja vielmehr elemente, nach zuweisung nicht mehr... speicherplatz??
134//        *list=copiedVector;
135    //später wird copiedVector ja überschrieben, ist das ein problem für list-dh. für gamestatemanager?
136          return;
137        }
138 
139  TrafficControl *TrafficControl::getInstance()
140  {
141    assert(instance_);
142    return instance_;
143  }
144       
145        void TrafficControl::ack(unsigned int clientID, unsigned int gamestateID)
146        {
147          std::list<obj>::iterator itvec;  // iterator to iterate through the acked objects
148   
149    //assertions to make sure the maps already exist
150    assert(clientListTemp_.find(clientID) != clientListTemp_.end() );
151    assert(clientListPerm_.find(clientID) != clientListPerm_.end() );
152          assert( clientListTemp_[clientID].find(gamestateID) != clientListTemp_[clientID].end() );
153   
154    for(itvec = clientListTemp_[clientID][gamestateID].begin(); itvec != clientListTemp_[clientID][gamestateID].end(); itvec++)
155          {
156      if(clientListPerm_[clientID].find((*itvec).objID) != clientListPerm_[clientID].end()) // check whether the obj already exists in our lists
157      {
158        clientListPerm_[clientID][(*itvec).objID].objCurGS = gamestateID;
159        clientListPerm_[clientID][(*itvec).objID].objValueSched = 0; //set scheduling value back
160      }
161      else
162      {
163        assert(0);
164        clientListPerm_[clientID][(*itvec).objID].objCurGS = gamestateID;
165        clientListPerm_[clientID][(*itvec).objID].objID = (*itvec).objID;
166        clientListPerm_[clientID][(*itvec).objID].objCreatorID = (*itvec).objCreatorID;
167        clientListPerm_[clientID][(*itvec).objID].objSize = (*itvec).objSize;
168      }
169          }
170           // remove temporary list (with acked objects) from the map
171    clientListTemp_[clientID].erase( clientListTemp_[clientID].find(gamestateID) );
172        }
173
174/**
175*Definition of private members
176*/
177       
178       
179        /**
180        *copyList gets list of Gamestate Manager and turns it to *listToProcess
181        */
182//      void TrafficControl::copyList(std::list<obj> *list)
183//      {
184//        std::list<obj>::iterator itvec;
185//        for(itvec = (*list).begin(); itvec != (*list).end(); itvec++)
186//        {
187//          objInfo objectA;
188//          objectA.objCreatorID=(*itvec).objCreatorID;
189//          objectA.objSize = (*itvec).objSize;
190//          listToProcess_.insert(std::pair<currentClientID, map<(*itvec).objID,objectA>>);//unsicher: ob map<...> so richtig ist
191//        }
192//      }
193        /**
194        *updateReferenceList compares the sent list by GSmanager with the current *reference list and updates it.
195        *returns void
196        */
197//      void TrafficControl::updateReferenceList(std::map<unsigned int, objInfo> *list)
198//      {
199//        std::map<unsigned int, Synchronisable*>::iterator itref;
200//        std::map<unsigned int, objInfo>::iterator itproc;
201//        for(itproc=listToProcess_.begin(); itproc != listToProcess_.end(); itproc++)
202//        {
203//          //itproc->first=objectid that is looked for
204//          if(referenceList_->find(itproc->first))
205//          {
206//            continue;
207//          }
208//          else
209//          {
210//            (*referenceList_).insert(pair<unsigned int,          Synchronisable*>((*itproc).first,Synchronisable::getSynchronisable((*itproc).first)));//important: how to get adress of an object!
211//            insertinClientListPerm(currentClientID,itproc->first,itproc->second);
212//          }
213//        }
214//      }
215        /**
216        *updateClientListPerm
217        *returns void
218        */
219        void TrafficControl::insertinClientListPerm(unsigned int clientID, obj objinf)
220        { 
221          std::map<unsigned int,std::map<unsigned int, objInfo> >::iterator itperm;//iterator clientListPerm over clientIDs
222//        itperm = (clientListPerm_).find(clientID);
223//        assert(itperm != clientListPerm_.end() );
224    unsigned int gsid=GAMESTATEID_INITIAL, gsdiff=currentGamestateID, prioperm=Synchronisable::getSynchronisable(objinf.objID)->getPriority(), priomom=0;
225    clientListPerm_[clientID][objinf.objID] = objInfo(objinf.objID, objinf.objCreatorID,gsid,gsdiff, objinf.objSize,prioperm,priomom);
226//        itperm->second.insert(std::pair<unsigned int, objInfo>(objid,objinf));
227//     permObjPrio_.insert(objid, objinf.objValuePerm);
228        }
229       
230  /**
231  * updateClientListTemp
232  * takes the shortened list which will be sent to the gsmanager and puts the *info into clientListTemp
233  */   
234  void TrafficControl::updateClientListTemp(std::list<obj> *list)
235  {
236    clientListTemp_[currentClientID][currentGamestateID] = std::list<obj>(*list);
237//     std::list<obj>::iterator itvec;
238//     std::map<unsigned int,std::map<unsigned int, obj> >::iterator ittemp;
239//     std::map<unsigned int, obj>::iterator ittempgs;
240//     ittemp = clientListTemp_.find(currentClientID);
241//     ittempgs = (*ittemp).find(currentGamestateID);
242//     for(itvec = list.begin(); itvec!=list.end(), itvec++)
243//     {
244//       ittempgs.insert(itvec);static
245//     }
246  }
247
248  /**
249  *cut
250  *takes the current list that has to be returned to the gsmanager and shortens it in criteria of bandwidth of clientID(XY)
251  */
252  void TrafficControl::cut(std::list<obj> *list, unsigned int targetsize)
253  {
254    unsigned int size=0;
255    std::list<obj>::iterator itvec, ittemp;
256    assert(!list->empty());
257    for(itvec = list->begin(); itvec != list->end();)
258    {
259      assert( (*itvec).objSize < 1000);
260      if ( ( size + (*itvec).objSize ) < targetsize )
261      {
262        size += (*itvec).objSize;//objSize is given in bytes
263        ++itvec;
264      }
265      else
266      {
267        clientListPerm_[currentClientID][(*itvec).objID].objValueSched += SCHED_PRIORITY_OFFSET; // NOTE: SCHED_PRIORITY_OFFSET is negative
268        list->erase(itvec++);
269      }
270//       printList(list, currentClientID);
271    }
272    assert(!list->empty());
273  }
274
275
276        /**
277        *evaluateList evaluates whether new obj are there, whether there are things to be updatet and manipulates all this.
278        */
279        void TrafficControl::evaluateList(unsigned int clientID, std::list<obj> *list)
280        {
281          //copyList(list);
282       
283          //now the sorting
284       
285          //compare listToProcess vs clientListPerm
286    //if listToProcess contains new Objects, add them to clientListPerm
287//        std::map<unsigned int, objInfo>::iterator itproc;
288    std::list<obj>::iterator itvec;
289//        std::map<unsigned int, std::map<unsigned int, objInfo> >::iterator itperm;
290//        std::map<unsigned int, objInfo>::iterator itpermobj;
291          for( itvec=list->begin(); itvec != list->end(); itvec++)
292          {
293//          itperm = clientListPerm_.find(clientID);
294            if ( clientListPerm_[clientID].find( (*itvec).objID) != clientListPerm_[clientID].end() )
295      {
296        // we already have the object in our map
297        //obj bleibt in liste und permanente prio wird berechnet
298        clientListPerm_[clientID][(*itvec).objID].objDiffGS = currentGamestateID - clientListPerm_[clientID][(*itvec).objID].objCurGS;
299//         ((*itpermobj).second).objDiffGS = ((*itpermobj).second).objCurGS - currentGamestateID;
300//         permprio = clientListPerm_[clientID][(*itproc).first].objValuePerm;
301//         itpermprio = (permObjPrio_).find((*itproc).first);
302//         ((*itpermobj).second).objValuePerm = ((*itpermobj).second).objDiffGS * (*itpermprio).second;
303        continue;//check next objId
304      }
305      else
306      {
307        // insert the object into clientListPerm
308        insertinClientListPerm(clientID,*itvec);
309//         itpermobj=(*itperm).find((*itproc).first)
310//         ((*itpermobj).second).objDiffGS = ((*itpermobj).second).objCurGS - currentGamestateID;
311//         itpermprio = (permObjPrio_).find((*itproc).first);
312//         ((*itpermobj).second).objValuePerm = ((*itpermobj).second).objDiffGS * (*itpermprio).second;
313        continue;//check next objId
314      }
315    }
316          //end compare listToProcess vs clientListPerm
317       
318    //listToProc vs clientListTemp
319    //TODO: uncomment it again and change some things
320    /*
321    std::map<unsigned int, std::map<unsigned int, unsigned int> >::iterator ittemp;
322    std::map<unsigned int, unsigned int>::iterator ittempgs;
323    for( itproc=listToProcess_.begin(); itproc != listToProcess_.end();itproc++)
324    {
325      ittemp = clientListTemp_->find(currentClientID);
326      if( ittempgs = (*ittemp).find(currentGamestateID))
327      {
328        if((*itproc).first == (*ittempgs).find((*itproc).first))//ja, dann ist objekt schon in der zu sendenden liste-muss nicht nochmal gesendet werden
329        {
330          (listToProcess_).erase (itproc);
331        }
332        else
333          continue;
334      }
335      else
336        continue;
337    }*/
338    //end listToProc vs clientListTemp
339   
340    //TODO: check whether we need this, cause i don't think so.
341    //listToProcess contains obj to send now, and since we give gsmanager the copiedlist and not listToProcess shorten copiedlist therefor too.
342    /*std::list<obj>::iterator itvec;
343    for(itvec = copiedVector.begin(); itvec != copiedVector.end(); itvec++)
344    {
345      if ( listToProcess_.find(itvec->objID) )
346      {
347        continue;//therefore object wasnt thrown out yet and has to be sent back to gsmanager
348      }
349      else
350      {
351        copiedVector.remove(itvec);
352      }
353    }*/
354    //sort copied list aufgrund der objprioperm in clientlistperm
355    // use boost bind here because we need to pass a memberfunction to stl sort
356    list->sort(boost::bind(&TrafficControl::priodiffer, this, clientID, _1, _2) );
357   
358    //now we check, that the creator of an object always exists on a client
359//     printList(list, clientID);
360    std::list<obj>::iterator itcreator;
361    for(itvec = list->begin(); itvec != list->end(); itvec++)
362    { 
363      fixCreatorDependencies(itvec, list, clientID);
364    }
365    //end of sorting
366    //now the cutting, work the same obj out in processobjectlist and copiedlist, compression rate muss noch festgelegt werden.
367    cut(list, targetSize);
368//     printList(list, clientID);
369    //diese Funktion updateClientList muss noch gemacht werden
370    updateClientListTemp(list);
371    //end of sorting
372  }
373
374  void TrafficControl::printList(std::list<obj> *list, unsigned int clientID)
375  {
376    std::list<obj>::iterator it;
377    COUT(0) << "=========== Objectlist ===========" << endl;
378    for( it=list->begin(); it!=list->end(); it++)
379      COUT(0) << "ObjectID: " << (*it).objID << " creatorID: " << (*it).objCreatorID << " Priority: " << clientListPerm_[clientID][(*it).objID].objValuePerm + clientListPerm_[clientID][(*it).objID].objValueSched << " size: " << (*it).objSize << endl;
380  }
381 
382  void TrafficControl::fixCreatorDependencies(std::list<obj>::iterator it1, std::list<obj> *list, unsigned int clientID)
383  {
384    if ( (*it1).objCreatorID == OBJECTID_UNKNOWN )
385      return;
386    if( clientListPerm_[clientID][(*it1).objCreatorID].objCurGS != GAMESTATEID_INITIAL )
387      return;
388    std::list<obj>::iterator it2, it3=it1;
389    for( it2 = ++it3; it2 != list->end(); it2++ )
390    {
391      if( (*it2).objID == (*it1).objCreatorID )
392      {
393        it3 = list->insert(it1, *it2); //insert creator before it1
394        list->erase(it2);
395//         printList(list, clientID);
396        fixCreatorDependencies( it3, list, clientID );
397        break;
398      }
399    }
400  }
401 
402  void TrafficControl::clientDisconnected(unsigned int clientID)
403  {
404    assert(clientListTemp_.find(clientID) != clientListTemp_.end() );
405    assert(clientListPerm_.find(clientID) != clientListPerm_.end() );
406    clientListTemp_.erase(clientListTemp_.find(clientID));
407    clientListPerm_.erase(clientListPerm_.find(clientID));
408  }
409
410/*
411void bvlabla(list *a){
412//sort a
413list *cache;
414cache = new list<unsigned int>(*a);
415return a;
416}
417*/
418
419
420}//namespace network
Note: See TracBrowser for help on using the repository browser.