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