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