Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2378 was 2378, checked in by chpeter, 15 years ago

process ack- solved set back of schedValue, watching targetsize from cut

File size: 14.6 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::processAck(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        clientListPerm_[clientID][(*itvec).objID].objCurGS = gamestateID;
149        clientListPerm_[clientID][(*itvec).objID].objID = (*itvec).objID;
150        clientListPerm_[clientID][(*itvec).objID].objCreatorID = (*itvec).objCreatorID;
151        clientListPerm_[clientID][(*itvec).objID].objSize = (*itvec).objSize;
152      }
153          }
154           // remove temporary list (with acked objects) from the map
155    clientListTemp_[clientID].erase( clientListTemp_[clientID].find(gamestateID) );
156        }
157
158/**
159*Definition of private members
160*/
161       
162       
163        /**
164        *copyList gets list of Gamestate Manager and turns it to *listToProcess
165        */
166//      void TrafficControl::copyList(std::list<obj> *list)
167//      {
168//        std::list<obj>::iterator itvec;
169//        for(itvec = (*list).begin(); itvec != (*list).end(); itvec++)
170//        {
171//          objInfo objectA;
172//          objectA.objCreatorID=(*itvec).objCreatorID;
173//          objectA.objSize = (*itvec).objSize;
174//          listToProcess_.insert(std::pair<currentClientID, map<(*itvec).objID,objectA>>);//unsicher: ob map<...> so richtig ist
175//        }
176//      }
177        /**
178        *updateReferenceList compares the sent list by GSmanager with the current *reference list and updates it.
179        *returns void
180        */
181//      void TrafficControl::updateReferenceList(std::map<unsigned int, objInfo> *list)
182//      {
183//        std::map<unsigned int, Synchronisable*>::iterator itref;
184//        std::map<unsigned int, objInfo>::iterator itproc;
185//        for(itproc=listToProcess_.begin(); itproc != listToProcess_.end(); itproc++)
186//        {
187//          //itproc->first=objectid that is looked for
188//          if(referenceList_->find(itproc->first))
189//          {
190//            continue;
191//          }
192//          else
193//          {
194//            (*referenceList_).insert(pair<unsigned int,          Synchronisable*>((*itproc).first,Synchronisable::getSynchronisable((*itproc).first)));//important: how to get adress of an object!
195//            insertinClientListPerm(currentClientID,itproc->first,itproc->second);
196//          }
197//        }
198//      }
199        /**
200        *updateClientListPerm
201        *returns void
202        */
203        void TrafficControl::insertinClientListPerm(unsigned int clientID, obj objinf)
204        { 
205          std::map<unsigned int,std::map<unsigned int, objInfo> >::iterator itperm;//iterator clientListPerm over clientIDs
206//        itperm = (clientListPerm_).find(clientID);
207//        assert(itperm != clientListPerm_.end() );
208    unsigned int gsid=GAMESTATEID_INITIAL, gsdiff=currentGamestateID, prioperm=Synchronisable::getSynchronisable(objinf.objID)->getPriority(), priomom=0;
209    clientListPerm_[clientID][objinf.objID] = objInfo(objinf.objID, objinf.objCreatorID,gsid,gsdiff, objinf.objSize,prioperm,priomom);
210//        itperm->second.insert(std::pair<unsigned int, objInfo>(objid,objinf));
211//     permObjPrio_.insert(objid, objinf.objValuePerm);
212        }
213       
214  /**
215  * updateClientListTemp
216  * takes the shortened list which will be sent to the gsmanager and puts the *info into clientListTemp
217  */   
218  void TrafficControl::updateClientListTemp(std::list<obj> *list)
219  {
220    clientListTemp_[currentClientID][currentGamestateID] = std::list<obj>(*list);
221//     std::list<obj>::iterator itvec;
222//     std::map<unsigned int,std::map<unsigned int, obj> >::iterator ittemp;
223//     std::map<unsigned int, obj>::iterator ittempgs;
224//     ittemp = clientListTemp_.find(currentClientID);
225//     ittempgs = (*ittemp).find(currentGamestateID);
226//     for(itvec = list.begin(); itvec!=list.end(), itvec++)
227//     {
228//       ittempgs.insert(itvec);
229//     }
230  }
231
232  /**
233  *cut
234  *takes the current list that has to be returned to the gsmanager and shortens it in criteria of bandwidth of clientID(XY)
235  */
236  void TrafficControl::cut(std::list<obj> *list, unsigned int targetsize)
237  {
238    unsigned int size=0;
239    std::list<obj>::iterator itvec;
240    assert(!list->empty());
241    itvec = list->begin();
242    for(itvec = list->begin(); ( itvec != list->end() ) && ( size<targetsize ); itvec++)
243    {
244      assert( (*itvec).objSize < 1000);
245      COUT(0) << "==targetsize==  " << targetsize << endl;
246      if ( ( size + (*itvec).objSize ) < targetsize )
247      {
248        size += (*itvec).objSize;//objSize is given in bytes
249      }
250      else
251      {
252        clientListPerm_[currentClientID][(*itvec).objID].objValueSched -= SCHED_PRIORITY_OFFSET;
253        list->erase(itvec++);
254      }
255    }
256    assert(!list->empty());
257  }
258
259
260        /**
261        *evaluateList evaluates whether new obj are there, whether there are things to be updatet and manipulates all this.
262        */
263        void TrafficControl::evaluateList(unsigned int clientID, std::list<obj> *list)
264        {
265          //copyList(list);
266       
267          //now the sorting
268       
269          //compare listToProcess vs clientListPerm
270    //if listToProcess contains new Objects, add them to clientListPerm
271//        std::map<unsigned int, objInfo>::iterator itproc;
272    std::list<obj>::iterator itvec;
273//        std::map<unsigned int, std::map<unsigned int, objInfo> >::iterator itperm;
274//        std::map<unsigned int, objInfo>::iterator itpermobj;
275          for( itvec=list->begin(); itvec != list->end(); itvec++)
276          {
277//          itperm = clientListPerm_.find(clientID);
278            if ( clientListPerm_[clientID].find( (*itvec).objID) != clientListPerm_[clientID].end() )
279      {
280        // we already have the object in our map
281        //obj bleibt in liste und permanente prio wird berechnet
282        clientListPerm_[clientID][(*itvec).objID].objDiffGS = currentGamestateID - clientListPerm_[clientID][(*itvec).objID].objCurGS;
283//         ((*itpermobj).second).objDiffGS = ((*itpermobj).second).objCurGS - currentGamestateID;
284//         permprio = clientListPerm_[clientID][(*itproc).first].objValuePerm;
285//         itpermprio = (permObjPrio_).find((*itproc).first);
286//         ((*itpermobj).second).objValuePerm = ((*itpermobj).second).objDiffGS * (*itpermprio).second;
287        continue;//check next objId
288      }
289      else
290      {
291        // insert the object into clientListPerm
292        insertinClientListPerm(clientID,*itvec);
293//         itpermobj=(*itperm).find((*itproc).first)
294//         ((*itpermobj).second).objDiffGS = ((*itpermobj).second).objCurGS - currentGamestateID;
295//         itpermprio = (permObjPrio_).find((*itproc).first);
296//         ((*itpermobj).second).objValuePerm = ((*itpermobj).second).objDiffGS * (*itpermprio).second;
297        continue;//check next objId
298      }
299    }
300          //end compare listToProcess vs clientListPerm
301       
302    //listToProc vs clientListTemp
303    //TODO: uncomment it again and change some things
304    /*
305    std::map<unsigned int, std::map<unsigned int, unsigned int> >::iterator ittemp;
306    std::map<unsigned int, unsigned int>::iterator ittempgs;
307    for( itproc=listToProcess_.begin(); itproc != listToProcess_.end();itproc++)
308    {
309      ittemp = clientListTemp_->find(currentClientID);
310      if( ittempgs = (*ittemp).find(currentGamestateID))
311      {
312        if((*itproc).first == (*ittempgs).find((*itproc).first))//ja, dann ist objekt schon in der zu sendenden liste-muss nicht nochmal gesendet werden
313        {
314          (listToProcess_).erase (itproc);
315        }
316        else
317          continue;
318      }
319      else
320        continue;
321    }*/
322    //end listToProc vs clientListTemp
323   
324    //TODO: check whether we need this, cause i don't think so.
325    //listToProcess contains obj to send now, and since we give gsmanager the copiedlist and not listToProcess shorten copiedlist therefor too.
326    /*std::list<obj>::iterator itvec;
327    for(itvec = copiedVector.begin(); itvec != copiedVector.end(); itvec++)
328    {
329      if ( listToProcess_.find(itvec->objID) )
330      {
331        continue;//therefore object wasnt thrown out yet and has to be sent back to gsmanager
332      }
333      else
334      {
335        copiedVector.remove(itvec);
336      }
337    }*/
338    //sort copied list aufgrund der objprioperm in clientlistperm
339    // use boost bind here because we need to pass a memberfunction to stl sort
340    list->sort(boost::bind(&TrafficControl::priodiffer, this, clientID, _1, _2) );
341   
342    //now we check, that the creator of an object always exists on a client
343    printList(list, clientID);
344    std::list<obj>::iterator itcreator;
345    for(itvec = list->begin(); itvec != list->end(); itvec++)
346    { 
347      fixCreatorDependencies(itvec, list, clientID);
348    }
349    //end of sorting
350    //now the cutting, work the same obj out in processobjectlist and copiedlist, compression rate muss noch festgelegt werden.
351    cut(list, targetSize);
352    printList(list, clientID);
353    //diese Funktion updateClientList muss noch gemacht werden
354    updateClientListTemp(list);
355    //end of sorting
356  }
357
358  void TrafficControl::printList(std::list<obj> *list, unsigned int clientID)
359  {
360    std::list<obj>::iterator it;
361    COUT(0) << "=========== Objectlist ===========" << endl;
362    for( it=list->begin(); it!=list->end(); it++)
363      COUT(0) << "ObjectID: " << (*it).objID << " creatorID: " << (*it).objCreatorID << " Priority: " << clientListPerm_[clientID][(*it).objID].objValuePerm + clientListPerm_[clientID][(*it).objID].objValueSched << endl;
364  }
365 
366  void TrafficControl::fixCreatorDependencies(std::list<obj>::iterator it1, std::list<obj> *list, unsigned int clientID)
367  {
368    if ( (*it1).objCreatorID == OBJECTID_UNKNOWN )
369      return;
370    if( clientListPerm_[clientID][(*it1).objCreatorID].objCurGS != GAMESTATEID_INITIAL )
371      return;
372    std::list<obj>::iterator it2, it3=it1;
373    for( it2 = ++it3; it2 != list->end(); it2++ )
374    {
375      if( (*it2).objID == (*it1).objCreatorID )
376      {
377        it3 = list->insert(it1, *it2); //insert creator before it1
378        list->erase(it2);
379//         printList(list, clientID);
380        fixCreatorDependencies( it3, list, clientID );
381        break;
382      }
383    }
384  }
385
386
387/*
388void bvlabla(list *a){
389//sort a
390list *cache;
391cache = new list<unsigned int>(*a);
392return a;
393}
394*/
395
396
397}//namespace network
Note: See TracBrowser for help on using the repository browser.