Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2381 was 2381, checked in by scheusso, 15 years ago

corrected some problem with scheduling and cutting (in trafficcontrol)

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