Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/network/TrafficControl.cc @ 2341

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

made some changes, but now yet complete

File size: 11.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 <cassert>
32#include <boost/bind.hpp>
33
34namespace network {
35
36/**
37*Initializing protected members
38*/
39        TrafficControl *TrafficControl::instance_=0;
40       
41        /**
42        * @brief Constructor: assures that only one reference will be created and sets the pointer
43        */
44        TrafficControl::TrafficControl()
45        {
46          assert(instance_=0);
47          instance_=this;
48    targetSize = 5000;//5000bytes
49        }
50       
51        /**
52        * @brief Destructor: resets the instance pointer to 0
53        */
54        TrafficControl::~TrafficControl()
55        { 
56          //was macht das genau? da instance ja gleich this ist im moment
57          instance_=0;
58          //muss ich alles deallozieren was ich im constructor aufgebaut habe?
59        }
60
61/**
62*Definition of public members
63*/
64
65        /**
66                *eigener sortieralgorithmus
67        */
68        bool TrafficControl::priodiffer(obj i, obj j)
69        {
70          std::map<unsigned int, objInfo>::iterator iti;
71          std::map<unsigned int, objInfo>::iterator itj;
72          iti=listToProcess_.find(i.objID);
73          itj=listToProcess_.find(j.objID);
74          return iti->second.objValuePerm < itj->second.objValuePerm;
75        }
76
77
78
79        std::vector<obj>* TrafficControl::processObjectList(unsigned int clientID, unsigned int gamestateID, std::vector<obj> *list)
80        {
81          copiedVector = *list;
82          currentClientID=clientID;
83          currentGamestateID=gamestateID;
84          evaluateList(list);
85          //list hatte vorher ja vielmehr elemente, nach zuweisung nicht mehr... speicherplatz??
86          *list=copiedVector;
87          //später wird copiedVector ja überschrieben, ist das ein problem für list-dh. für gamestatemanager?
88          return list;
89        }
90       
91        void TrafficControl::processAck(unsigned int clientID, unsigned int gamestateID)
92        {
93          std::map<unsigned int, std::map<unsigned int, std::vector<objInfo> > >::iterator itperm;//iterator clientListPerm
94          std::map<unsigned int, std::vector<objInfo> >::iterator itpermObjectIDList;//iterator over objectid
95    std::vector<objInfo>::iterator itpermObjectID;
96    std::map<unsigned int, std::map<unsigned int, std::vector<obj> > >::iterator ittemp;//iterator clientListTemp, iterates over clientIDs
97          std::map<unsigned int, std::vector<obj> >::iterator ittempgs;//iterator that iterates over gsIDs of a client
98          std::vector<obj>::iterator itvec;
99          //following code helps to put clientListTemp infos into clientListPerm infos
100          ittemp = clientListTemp_.find(clientID);
101          assert(ittemp != clientListTemp_.end() ); //muss da nicht was anderes überprüft werden? -> nein
102          itperm = clientListPerm_.find(clientID);
103          assert(itperm != clientListPerm_.end() );
104          ittempgs = ittemp->second.find(gamestateID);
105          assert( ittempgs != ittemp->second.end() );//gleiche frage wie vorher -> nein
106          for(itvec = ittempgs->second.begin(); itvec != ittempgs->second.end(); itvec++)
107          {
108      bool alreadyExisting = false;
109      itpermObjectIDList = itperm->second.find(gamestateID);
110      assert(itpermObjectIDList != itperm->second.end());
111      for (itpermObjectID = itpermObjectIDList->second.begin(); itpermObjectID != itpermObjectIDList->second.end(); itpermObjectID++ )
112      {
113        if ( (*itpermObjectID).objID == (*itvec).objID)
114        {
115          (*itpermObjectID).objCurGS = gamestateID;
116          alreadyExisting = true;
117          break;
118        }
119        if ( !alreadyExisting )
120        {
121          objInfo objinf;    // TODO: is this initialisation complete ?
122          objinf.objCurGS = gamestateID;
123          objinf.objID = (*itvec).objID;
124          insertinClientListPerm(clientID, (*itvec).objID, objinf);
125        }
126      }
127          }
128           // remove temporary vector (with acked objects) from the map
129    ittemp->second.erase(ittempgs);
130        }
131
132/**
133*Definition of private members
134*/
135       
136       
137        /**
138        *copyList gets vector of Gamestate Manager and turns it to *listToProcess
139        */
140        void TrafficControl::copyList(std::vector<obj> *list)
141        {
142          std::vector<obj>::iterator itvec;
143          for(itvec = (*list).begin(); itvec != (*list).end(); itvec++)
144          {
145            objInfo objectA;
146            objectA.objCreatorID=(*itvec).objCreatorID;
147            objectA.objSize = (*itvec).objSize;
148            listToProcess_.insert(pair<currentClientID, map<(*itvec).objID,objectA>>);//unsicher: ob map<...> so richtig ist
149          }
150        }
151        /**
152        *updateReferenceList compares the sent list by GSmanager with the current *reference list and updates it.
153        *returns void
154        */
155        void TrafficControl::updateReferenceList(std::map<unsigned int, objInfo> *list)
156        {
157          std::map<unsigned int, Synchronisable*>::iterator itref;
158          std::map<unsigned int, objInfo>::iterator itproc;
159          for(itproc=(*listToProcess_).begin();itproc != (*listToProcess_).end(); itproc++)
160          {
161            //itproc->first=objectid that is looked for
162            if(referenceList_->find(itproc->first))
163            {
164              continue;
165            }
166            else
167            {
168              (*referenceList_).insert(pair<unsigned int,          Synchronisable*>((*itproc).first,Synchronisable::getSynchronisable((*itproc).first)));//important: how to get adress of an object!
169              insertinClientListPerm(currentClientID,itproc->first,itproc->second);
170            }
171          }
172        }
173        /**
174        *updateClientListPerm
175        *returns void
176        */
177        void TrafficControl::insertinClientListPerm(unsigned int clientid, unsigned int objid, objInfo objinf)
178        { 
179          std::map<unsigned int,std::map<unsigned int, objInfo>>::iterator itperm;//iterator clientListPerm over clientIDs
180          itperm = (clientListPerm_).find(clientiD);
181          assert(itperm != clientListPerm_.end() );
182          (*itperm).insert(pair<unsigned int, objInfo>(objid,objinf));
183          (permObjPrio_).insert(objid,objinf.objValuePerm);
184        }
185       
186        /**
187        *updateClientListTemp
188        *takes the shortened vector which will be sent to the gsmanager and puts the *info into clientListTemp
189        */     
190        TrafficControl::updateClientListTemp(std::vector<obj> *list)
191        {
192          vector<obj>::iterator itvec;
193          map<unsigned int,std::map<unsigned int, obj>>::iterator ittemp;
194          map<unsigned int, obj>::iterator ittempgs;
195          ittemp = (clientListTemp_).find(currentClientID);
196          ittempgs = (*ittemp).find(currentGamestateID);
197          for(itvec=list.begin();itvec!=list.end(),itvec++)
198          {
199            ittempgs.insert(itvec);
200          }
201        }
202
203        /**
204        *cut
205        *takes the current vector that has to be returned to the gsmanager and shortens it in criteria of bandwidth of clientID(XY)
206        */
207        TrafficControl::cut(std::vector<obj> *list,int targetsize)
208        {
209          unsigned int size=0;
210          vector<obj>::iterator itvec;
211          itvec = list.begin();
212          unsigned int i=0;
213          while(size<targetsize && (itvec!=list.end()))
214          {
215            size = size + (*itvec).objSize;//objSize is given in bytes!??
216            i++;
217            itvec = list.begin()+i;
218          }
219          list.erase(itvec, list.end());
220        }
221
222
223        /**
224        *evaluateList evaluates whether new obj are there, whether there are things to be updatet and manipulates all this.
225        */
226        void TrafficControl::evaluateList(std::vector<obj> *list)
227        {
228          copyList(list);
229       
230          //now the sorting
231       
232          //compare listToProcess vs clientListPerm
233          //if listToProcess contains new Objects, add them to clientListPerm
234          std::map<unsigned int, objInfo>::iterator itproc;
235          std::map<unsigned int, std::map<unsigned int, objInfo> >::iterator itperm;
236          std::map<unsigned int, objInfo>::iterator itpermobj;
237          std::map<unsigned int, unsigned int>::iterator itpermprio;
238          for((*itproc)=(listToProcess_).begin(); itproc != (listToProcess_).end();it++)
239          {
240            itperm=(clientListPerm_).find(currentClientID);
241            if(itpermobj=(*itperm).find((*itproc).first))
242            {
243             if(currentGamestateID > ((*itpermobj).second).objCurGS)
244              {
245              //obj bleibt in liste und permanente prio wird berechnet
246                ((*itpermobj).second).objDiffGS = ((*itpermobj).second).objCurGS - currentGamestateID;
247                itpermprio = (permObjPrio_).find((*itproc).first);
248                ((*itpermobj).second).objValuePerm = ((*itpermobj).second).objDiffGS * (*itpermprio).second;
249                continue;//check next objId
250              }
251              else
252              {
253                (listToProcess_).erase(itproc);
254              }
255             }
256             else
257             {
258               insertinClientListPerm(currentClientID,(*itproc).first,(*itproc).second);
259               itpermobj=(*itperm).find((*itproc).first)
260               ((*itpermobj).second).objDiffGS = ((*itpermobj).second).objCurGS - currentGamestateID;
261               itpermprio = (permObjPrio_).find((*itproc).first);
262               ((*itpermobj).second).objValuePerm = ((*itpermobj).second).objDiffGS * (*itpermprio).second;
263               continue;//check next objId
264             }
265            }
266          //end compare listToProcess vs clientListPerm
267       
268        //listToProc vs clientListTemp
269        map<unsigned int, std::map<unsigned int, unsigned int> >::iterator ittemp;
270        map<unsigned int, unsigned int>::iterator ittempgs;
271        for((itproc=listToProcess_).begin(); itproc != (listToProcess_).end();itproc++)
272        {
273          ittemp = clientListTemp_->find(currentClientID);
274          if(ittempgs = (*ittemp).find(currentGamestateID))
275          {
276            if((*itproc).first == (*ittempgs).find((*itproc).first))//ja, dann ist objekt schon in der zu sendenden liste-muss nicht nochmal gesendet werden
277            {
278              (listToProcess_).erase (itproc);
279            }
280            else continue;
281          }
282          else continue;
283        }
284        //end listToProc vs clientListTemp
285       
286        //listToProcess contains obj to send now, and since we give gsmanager the copiedvector and not listToProcess shorten copiedvector therefor too.
287        vector<obj>::iterator itvec;
288        for(itvec = copiedVector.begin(); itvec < copiedVector.end(); itvec++)
289        {
290          if((listToProcess_).find(itvec->objID))
291          {
292            continue;//therefore object wasnt thrown out yet and has to be sent back to gsmanager
293          }
294          else
295          {
296            copiedVector.remove(itvec);
297          }
298        }
299        //sort copied vector aufgrund der objprioperm in clientlistperm
300        // use boost bind here because we need to pass a memberfunction to stl sort
301        sort(copiedvector.begin(), copiedvector.end(), boost::bind(&TrafficControl::priodiffer,this,_1,_2) );
302        //swappen aufgrund von creator oder ganz rausnehmen!?
303        for(itvec = copiedVector.begin(); itvec < copiedVector.end(); itvec++)
304        { 
305          itproc = (listToProcess_).find((*itvec).objID);
306          if((*itproc).second.objCreatorID)
307          {
308          //vor dem child in copiedvector einfügen, wie?
309            copiedVector.insert(copiedVector.find((*itproc).first),(*itproc).second.objCreatorID);
310          }
311          else continue;
312        }
313          //end of sorting
314          //now the cutting, work the same obj out in processobjectlist and copiedvector, compression rate muss noch festgelegt werden.
315          cut(copiedVector,targetSize);
316          //diese Funktion updateClientList muss noch gemacht werden
317          updateClientListTemp(copiedVector);
318          //end of sorting
319      }
320
321
322
323
324/*
325void bvlabla(vector *a){
326//sort a
327vector *cache;
328cache = new vector<unsigned int>(*a);
329return a;
330}
331*/
332
333
334}//namespace network
Note: See TracBrowser for help on using the repository browser.