1 | #include "enet/enet.h" |
---|
2 | #include "Orxonox.h" |
---|
3 | #include "NetworkPrereqs.h" |
---|
4 | #include "PacketTypes.h" |
---|
5 | #include "GameStateManager.h" |
---|
6 | #include "Synchronisable.h" |
---|
7 | #include "GameStateClient.h" |
---|
8 | #include "iostream" |
---|
9 | #include "core/CoreIncludes.h" |
---|
10 | #include "time.h" |
---|
11 | #include "ConnectionManager.h" |
---|
12 | #include "ClientInformation.h" |
---|
13 | #include "Synchronisable.h" |
---|
14 | #include <boost/thread/thread.hpp> |
---|
15 | #include <boost/bind.hpp> |
---|
16 | #include "util/Sleep.h" |
---|
17 | |
---|
18 | using namespace network; |
---|
19 | |
---|
20 | void printData( unsigned char* data, int length ) { |
---|
21 | for ( int i=0; i<length; i++ ) |
---|
22 | std::cout << (int)data[i] << " "; |
---|
23 | std::cout << std::endl; |
---|
24 | } |
---|
25 | |
---|
26 | void printGameStateCompressed( GameStateCompressed* gc ) { |
---|
27 | //std::cout << "=================================================" << std::endl; |
---|
28 | std::cout << "GameStateCompressed id:\t\t" << gc->id << std::endl; |
---|
29 | std::cout << "GameStateCompressed normsize:\t" << gc->normsize << std::endl; |
---|
30 | std::cout << "GameStateCompressed compsize:\t" << gc->compsize << std::endl; |
---|
31 | std::cout << "GameStateCompressed diffed:\t" << gc->diffed << std::endl; |
---|
32 | //std::cout << "GameState data:\t" << gc->data << std::endl; |
---|
33 | std::cout << "GameStateCompressed compressing rate:\t" << 100.0-((100.0/(gc->normsize))*(gc->compsize)) << "%" << std::endl; |
---|
34 | std::cout << "=================================================" << std::endl; |
---|
35 | return; |
---|
36 | } |
---|
37 | |
---|
38 | bool compareData( GameState* g1, GameState* g2 ) { |
---|
39 | if ( g1->id != g2->id ) { |
---|
40 | std::cout << "\t--> GameStates are not comparable -> not same id" << std::endl; |
---|
41 | return true; |
---|
42 | } |
---|
43 | else if ( g1->size != g2->size ) { |
---|
44 | std::cout << "\t--> GameStates are not the same size!!" << std::endl; |
---|
45 | std::cout << g1->size << " != " << g2->size << std::endl; |
---|
46 | } |
---|
47 | int length = g1->size; |
---|
48 | for ( int i=0; i<length; i++ ) { |
---|
49 | if ( g1->data[i] != g2->data[i] ) { |
---|
50 | std::cout << "\t--> data of both GameStates are not identical" << std::endl; |
---|
51 | return false; |
---|
52 | } |
---|
53 | } |
---|
54 | //std::cout << "\t--> GameData are identical (compareData)" << std::endl; |
---|
55 | return true; |
---|
56 | } |
---|
57 | |
---|
58 | bool compareData2( GameState* g1, GameState* g2 ) { |
---|
59 | int length = g1->size; |
---|
60 | for ( int i=0; i<length; i++ ) { |
---|
61 | if ( g1->data[i] != g2->data[i] ) { |
---|
62 | return false; |
---|
63 | } |
---|
64 | } |
---|
65 | //std::cout << "\t--> GameData are identical (compareData)" << std::endl; |
---|
66 | return true; |
---|
67 | } |
---|
68 | |
---|
69 | bool compareGameStates( GameState* g1, GameState* g2 ) { |
---|
70 | if ( g1->id != g2->id ) { |
---|
71 | std::cout << "\t==> GameState id's not identical (GameStateCompare)" << std::endl; |
---|
72 | } |
---|
73 | if( g1->size != g2->size ) { |
---|
74 | std::cout << "\t==> GameState sizes are not identical (GameStateCompare)" << std::endl; |
---|
75 | return false; |
---|
76 | } |
---|
77 | else if ( g1->diffed != g2->diffed ) { |
---|
78 | std::cout << "\t==> GameState diffed params not identical (GameStateCompare)" << std::endl; |
---|
79 | return false; |
---|
80 | } |
---|
81 | else if ( !compareData2( g1, g2 ) ) { |
---|
82 | std::cout << "\t==> GameState data are not identical (GameStateCompare)" << std::endl; |
---|
83 | return false; |
---|
84 | } |
---|
85 | //std::cout << "\t==> GameStates are identical (GameStateCompare)" << std::endl; |
---|
86 | return true; |
---|
87 | } |
---|
88 | |
---|
89 | void printGameState( GameState* gstate ) { |
---|
90 | //std::cout << "=================================================" << std::endl; |
---|
91 | std::cout << "GameState id:\t\t" << gstate->id << std::endl; |
---|
92 | std::cout << "GameState size:\t\t" << gstate->size << std::endl; |
---|
93 | std::cout << "GameState diffed:\t" << gstate->diffed << std::endl; |
---|
94 | //std::cout << "GameState data:\t" << gstate->data << std::endl; |
---|
95 | std::cout << "=================================================" << std::endl; |
---|
96 | return; |
---|
97 | } |
---|
98 | |
---|
99 | unsigned char* createData( int length, int mode ) { |
---|
100 | char* data = new char[ length ]; |
---|
101 | if ( mode == 1 ) { |
---|
102 | for ( int i=0; i<length; i++ ) |
---|
103 | data[i] = (char)(i%255); |
---|
104 | } |
---|
105 | else if ( mode == 2 ) { |
---|
106 | for ( int i=0; i<length; i++ ) { |
---|
107 | if ( i%98 == 0 ) data[i] = (char)(i%255); |
---|
108 | else data[i] = (char)0; |
---|
109 | } |
---|
110 | } |
---|
111 | else if ( mode == 3 ) { |
---|
112 | for ( int i=0; i<length; i++ ){ |
---|
113 | data[i] = (char)(rand()%255); |
---|
114 | } |
---|
115 | } |
---|
116 | else if ( mode == 4 ) { |
---|
117 | for ( int i=0; i<length; i++ ){ |
---|
118 | data[i] = (char)(rand()%127); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | //printData( data, length ); |
---|
123 | |
---|
124 | unsigned char* dat = (unsigned char*)data; |
---|
125 | return dat; |
---|
126 | } |
---|
127 | |
---|
128 | GameState* changeGameStateABit( GameState* a, int mode ) { |
---|
129 | int length = a->size; |
---|
130 | GameState* b = new GameState; |
---|
131 | b->id = a->id; |
---|
132 | b->diffed = a->diffed; |
---|
133 | |
---|
134 | if ( mode == 1 ) { |
---|
135 | b->data = new unsigned char[length]; |
---|
136 | b->size = a->size; |
---|
137 | for ( int i=0; i<length; i++ ) { |
---|
138 | if ( i%10 == 0 ) b->data[i] = rand()%255; |
---|
139 | else b->data[i] = a->data[i]; |
---|
140 | } |
---|
141 | } |
---|
142 | else if ( mode == 2 ) { |
---|
143 | b->data = new unsigned char[length]; |
---|
144 | b->size = length; |
---|
145 | for ( int i=0; i<length; i++ ) { |
---|
146 | if ( i%(rand()%((length)/11)+rand()) == 0 ) b->data[i] = (char)rand()%255; |
---|
147 | else b->data[i] = a->data[i]; |
---|
148 | } |
---|
149 | } |
---|
150 | else if ( mode == 3 ) { |
---|
151 | int s = length + (rand()%(length)); |
---|
152 | b->data = new unsigned char[s]; |
---|
153 | b->size = s; |
---|
154 | for ( int i=0; i<length; i++ ) { |
---|
155 | if ( i%10 == 0 ) b->data[i] = (char)rand()%255; |
---|
156 | else b->data[i] = a->data[i]; |
---|
157 | } |
---|
158 | for( int i=length; i<s; i++ ) { |
---|
159 | b->data[i] = (char)rand()%255; |
---|
160 | } |
---|
161 | } |
---|
162 | else if ( mode == 4 ) { |
---|
163 | int s = length + (rand()%(length)); |
---|
164 | b->data = new unsigned char[s]; |
---|
165 | b->size = s; |
---|
166 | for ( int i=0; i<length; i++ ) { |
---|
167 | if ( i%(rand()%(length)+rand()) == 0 ) b->data[i] = (char)rand()%255; |
---|
168 | else b->data[i] = a->data[i]; |
---|
169 | } |
---|
170 | for( int i=length; i<s; i++ ) { |
---|
171 | b->data[i] = rand()%255; |
---|
172 | } |
---|
173 | } |
---|
174 | else if ( mode == 5 ) { |
---|
175 | int s = (length)/2; |
---|
176 | b->data = new unsigned char[s]; |
---|
177 | b->size = s; |
---|
178 | for ( int i=0; i<s; i++ ) { |
---|
179 | if ( i%10 == 0 ) b->data[i] = (char)rand()%255; |
---|
180 | else b->data[i] = a->data[i]; |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | return b; |
---|
185 | } |
---|
186 | |
---|
187 | void testCompression( int size, int mode ) { |
---|
188 | std::cout << "testing compression with: size = " << size << " ,mode = " << mode << std::endl; |
---|
189 | GameStateClient* g_client; |
---|
190 | GameStateManager* g_manager;; |
---|
191 | |
---|
192 | GameState* g_new = new GameState; |
---|
193 | GameState* g_old = new GameState; |
---|
194 | GameStateCompressed* gc = new GameStateCompressed; |
---|
195 | |
---|
196 | g_old->data = createData( size, mode ); |
---|
197 | g_old->size = size; |
---|
198 | g_old->id = 0; |
---|
199 | g_old->diffed = false; |
---|
200 | printGameState( g_old ); |
---|
201 | |
---|
202 | gc = g_manager->testCompress( g_old ); |
---|
203 | printGameStateCompressed( gc ); |
---|
204 | |
---|
205 | g_new = g_client->testDecompress( gc ); |
---|
206 | printGameState( g_new ); |
---|
207 | |
---|
208 | compareGameStates( g_new, g_old ); |
---|
209 | |
---|
210 | return; |
---|
211 | } |
---|
212 | |
---|
213 | void testDifferentiation( int size, int modeCreateData, int modeChangeData ) { |
---|
214 | std::cout << "testing diff with: size = " << size << " modeCreateData = " |
---|
215 | << modeCreateData << " modeChangeData = " << modeChangeData << std::endl; |
---|
216 | GameStateClient* g_client; |
---|
217 | GameStateManager* g_manager; |
---|
218 | |
---|
219 | GameState* g_undiff1 = new GameState; |
---|
220 | GameState* g_undiff2 = new GameState; |
---|
221 | GameState* g_diffed; |
---|
222 | GameState* g_result; |
---|
223 | |
---|
224 | g_undiff1->data = createData( size, modeCreateData ); |
---|
225 | g_undiff1->size = size; |
---|
226 | g_undiff1->id = 1; |
---|
227 | g_undiff1->diffed = false; |
---|
228 | printGameState( g_undiff1 ); |
---|
229 | |
---|
230 | g_undiff2 = changeGameStateABit( g_undiff1, modeChangeData ); |
---|
231 | printGameState( g_undiff2 ); |
---|
232 | |
---|
233 | if( !compareData( g_undiff1, g_undiff2 ) ) std::cout << " BUT THAT'S HOW IT HAS TO BE" << std::endl; |
---|
234 | |
---|
235 | //printData( g_undiff1->data, g_undiff1->size ); |
---|
236 | //printData( g_undiff2->data, g_undiff2->size ); |
---|
237 | |
---|
238 | g_diffed = g_manager->testDiff( g_undiff1, g_undiff2 ); |
---|
239 | printGameState( g_diffed ); |
---|
240 | //printData( g_diffed->data, g_diffed->size ); |
---|
241 | |
---|
242 | g_result = g_client->testUndiff( g_undiff1, g_diffed ); |
---|
243 | compareGameStates( g_result, g_undiff2 ); |
---|
244 | |
---|
245 | return; |
---|
246 | } |
---|
247 | |
---|
248 | void testCompressWithDiff( int size, int modeCreateData, int modeChangeData ) { |
---|
249 | std::cout << "testing CompressWithDiff with: size = " << size << " modeCreateData = " |
---|
250 | << modeCreateData << " modeChangeData = " << modeChangeData << std::endl; |
---|
251 | GameStateClient* g_client; |
---|
252 | GameStateManager* g_manager;; |
---|
253 | |
---|
254 | GameStateCompressed* gc = new GameStateCompressed; |
---|
255 | GameStateCompressed* g_compressedNoDiff; |
---|
256 | GameState* g_undiff1 = new GameState; |
---|
257 | GameState* g_undiff2 = new GameState; |
---|
258 | GameState* g_diffed; |
---|
259 | GameState* g_resultDiffed; |
---|
260 | GameState* g_resultUndiffed; |
---|
261 | |
---|
262 | g_undiff1->data = createData( size, modeCreateData ); |
---|
263 | g_undiff1->size = size; |
---|
264 | g_undiff1->id = 1; |
---|
265 | g_undiff1->diffed = false; |
---|
266 | |
---|
267 | std::cout << "---First generated Gamestate" << std::endl; |
---|
268 | printGameState( g_undiff1 ); |
---|
269 | |
---|
270 | g_undiff2 = changeGameStateABit( g_undiff1, modeChangeData ); |
---|
271 | std::cout << "---First gererated Gamestate with some changes ev. longer" << std::endl; |
---|
272 | printGameState( g_undiff2 ); |
---|
273 | |
---|
274 | if( !compareData( g_undiff1, g_undiff2 ) ) std::cout << " DATA not identical.. ok" << std::endl; |
---|
275 | |
---|
276 | g_diffed = g_manager->testDiff( g_undiff1, g_undiff2 ); |
---|
277 | std::cout << "---Diffed Gamestate not compressed" << std::endl; |
---|
278 | printGameState( g_diffed ); |
---|
279 | |
---|
280 | gc = g_manager->testCompress( g_diffed ); |
---|
281 | std::cout << "---Diffed Gamestate compressed" << std::endl; |
---|
282 | printGameStateCompressed( gc ); |
---|
283 | |
---|
284 | g_compressedNoDiff = g_manager->testCompress( g_undiff2 ); |
---|
285 | std::cout << "---Same not Diffed Gamestate compressen" << std::endl; |
---|
286 | printGameStateCompressed( g_compressedNoDiff ); |
---|
287 | |
---|
288 | g_resultDiffed = g_client->testDecompress( gc ); |
---|
289 | std::cout << "---Diffed Gamestate of above uncompressed" << std::endl; |
---|
290 | printGameState( g_resultDiffed ); |
---|
291 | |
---|
292 | std::cout << "---Diffed Gamestates before compressed and after uncompress comparsion" << std::endl; |
---|
293 | if ( compareGameStates( g_resultDiffed, g_diffed ) ) std::cout << "GAMESTATES IDENTICAL" << std::endl; |
---|
294 | |
---|
295 | g_resultUndiffed = g_client->testUndiff( g_undiff1, g_resultDiffed ); |
---|
296 | std::cout << "---New Gamestate of pseudo Server compared with new gamestate that Client gets" << std::endl; |
---|
297 | if ( compareGameStates( g_resultUndiffed, g_undiff2 ) ) std::cout << "GAMESTATES IDENTICAL" << std::endl; |
---|
298 | |
---|
299 | return; |
---|
300 | } |
---|
301 | |
---|
302 | bool testNCompressWithDiff( int n, int size, int modeCreateData, int modeChangeData ) { |
---|
303 | |
---|
304 | GameStateClient* g_client; |
---|
305 | GameStateManager* g_manager;; |
---|
306 | |
---|
307 | GameStateCompressed* gc = new GameStateCompressed; |
---|
308 | GameState* g_undiff1 = new GameState; |
---|
309 | GameState* g_undiff2 = new GameState; |
---|
310 | GameState* g_diffed; |
---|
311 | GameState* g_resultDiffed; |
---|
312 | GameState* g_resultUndiffed; |
---|
313 | |
---|
314 | g_undiff1->data = createData( size, modeCreateData ); |
---|
315 | g_undiff1->size = size; |
---|
316 | g_undiff1->id = 1; |
---|
317 | g_undiff1->diffed = false; |
---|
318 | //l = -1; |
---|
319 | g_undiff2 = changeGameStateABit( g_undiff1, modeChangeData ); |
---|
320 | |
---|
321 | while( compareData2( g_undiff1, g_undiff2 ) ) { |
---|
322 | delete g_undiff2->data; |
---|
323 | g_undiff2 = changeGameStateABit( g_undiff1, modeChangeData ); |
---|
324 | } |
---|
325 | //l = -2; |
---|
326 | g_diffed = g_manager->testDiff( g_undiff1, g_undiff2 ); |
---|
327 | gc = g_manager->testCompress( g_diffed ); |
---|
328 | g_resultDiffed = g_client->testDecompress( gc ); |
---|
329 | |
---|
330 | if ( !compareGameStates( g_resultDiffed, g_diffed ) ) return false; |
---|
331 | //l = -3; |
---|
332 | g_resultUndiffed = g_client->testUndiff( g_undiff1, g_resultDiffed ); |
---|
333 | if ( !compareGameStates( g_resultUndiffed, g_undiff2 ) ) return false; |
---|
334 | //l = 1; |
---|
335 | /*if ( gc != NULL && gc->data != NULL ) |
---|
336 | delete gc->data; |
---|
337 | //l = 2; |
---|
338 | //if ( gc != NULL ) |
---|
339 | //delete gc; |
---|
340 | //l = 3; |
---|
341 | if ( g_undiff1 != NULL && g_undiff1->data != NULL ) |
---|
342 | delete g_undiff1->data; |
---|
343 | //l = 4; |
---|
344 | //if ( g_undiff1 != NULL ) |
---|
345 | //delete g_undiff1; |
---|
346 | //l = 5; |
---|
347 | if ( g_undiff2 != NULL && g_undiff2->data ) |
---|
348 | delete g_undiff2->data; |
---|
349 | //l = 6; |
---|
350 | //if ( g_undiff2 != NULL ) |
---|
351 | //delete g_undiff2; |
---|
352 | //l = 7; |
---|
353 | if ( g_diffed != NULL && g_diffed->data ) |
---|
354 | //delete g_diffed->data; |
---|
355 | //l = 8; |
---|
356 | //if ( g_diffed ) |
---|
357 | //delete g_diffed; |
---|
358 | //l = 9; |
---|
359 | if ( g_resultDiffed != NULL && g_resultDiffed->data ) |
---|
360 | delete g_resultDiffed->data; |
---|
361 | //l = 10; |
---|
362 | //if ( g_resultDiffed ) |
---|
363 | //delete g_resultDiffed; |
---|
364 | //l = 11;*/ |
---|
365 | |
---|
366 | return true; |
---|
367 | } |
---|
368 | |
---|
369 | bool testNCompression( int n, int size, int mode ) { |
---|
370 | GameStateClient* g_client; |
---|
371 | GameStateManager* g_manager;; |
---|
372 | |
---|
373 | GameState* g_new = new GameState; |
---|
374 | GameState* g_old = new GameState; |
---|
375 | GameStateCompressed* gc = new GameStateCompressed; |
---|
376 | |
---|
377 | g_old->data = createData( size, mode ); |
---|
378 | g_old->size = size; |
---|
379 | g_old->id = 0; |
---|
380 | g_old->diffed = false; |
---|
381 | |
---|
382 | gc = g_manager->testCompress( g_old ); |
---|
383 | |
---|
384 | g_new = g_client->testDecompress( gc ); |
---|
385 | |
---|
386 | if ( !compareGameStates( g_new, g_old ) ) return false; |
---|
387 | |
---|
388 | |
---|
389 | if ( g_new != NULL && g_new->data != NULL ) |
---|
390 | delete g_new->data; |
---|
391 | |
---|
392 | if ( g_old != NULL && g_old->data != NULL ) |
---|
393 | delete g_old->data; |
---|
394 | |
---|
395 | if ( gc != NULL && gc->data ) |
---|
396 | delete gc->data; |
---|
397 | |
---|
398 | return true; |
---|
399 | } |
---|
400 | |
---|
401 | void printClientObjectMapping( ConnectionManager* cmanager, int clients ) { |
---|
402 | std::map<int, int>::iterator iter; |
---|
403 | std::map<int, int> clientsmap = cmanager->testGetClientsShip(); |
---|
404 | for( iter = clientsmap.begin(); iter != clientsmap.end(); iter++ ) { |
---|
405 | std::cout << "clientID: " << iter->first << "\t-> objectID: " << iter->second << std::endl; |
---|
406 | } |
---|
407 | return; |
---|
408 | } |
---|
409 | |
---|
410 | bool is( int a[], int b, int size ) { |
---|
411 | for ( int i=0; i<size; i++ ) { |
---|
412 | if ( a[i] == b ) return true; |
---|
413 | } |
---|
414 | return false; |
---|
415 | } |
---|
416 | |
---|
417 | void testClientObjectMapping( int clients ) { |
---|
418 | ConnectionManager* cmanager = new ConnectionManager(); |
---|
419 | int shift = 2; |
---|
420 | std::cout << "create a map length [clients]" << std::endl; |
---|
421 | for ( int i=0; i<clients; i++ ) { |
---|
422 | cmanager->testAddClientsShipID( i, i+shift ); |
---|
423 | } |
---|
424 | printClientObjectMapping( cmanager, clients ); |
---|
425 | |
---|
426 | std::cout << "get random client's ship id" << std::endl; |
---|
427 | int id; |
---|
428 | for ( int i=0; i<(clients/3); i++ ) { |
---|
429 | id = rand()%clients; |
---|
430 | std::cout << "client: " << id << "\t-> ship: " << cmanager->testGetClientsShipID( id ) << std::endl; |
---|
431 | } |
---|
432 | |
---|
433 | std::cout <<"get random ship's client id" << std::endl; |
---|
434 | for ( int i=0; i<(clients/3); i++ ) { |
---|
435 | id = (rand()%clients)+shift; |
---|
436 | std::cout << "ship: " << id << "\t-> client: " << cmanager->testGetObjectsClientID( id ) << std::endl; |
---|
437 | } |
---|
438 | |
---|
439 | std::cout << "delete random client from map" << std::endl; |
---|
440 | int deleted[clients/3]; |
---|
441 | for ( int i=0; i<(clients/3); i++ ) { |
---|
442 | id = rand()%clients; |
---|
443 | if ( !is( deleted, id, clients/3 ) ) { |
---|
444 | std::cout << "delete client " << id << std::endl; |
---|
445 | cmanager->testDeleteClientsIDReg( id ); |
---|
446 | } |
---|
447 | deleted[i] = id; |
---|
448 | } |
---|
449 | std::cout << "resulting list:" << std::endl; |
---|
450 | printClientObjectMapping( cmanager, clients-(clients/3)); |
---|
451 | |
---|
452 | std::cout << "delete random object from map" << std::endl; |
---|
453 | int jap = 0; |
---|
454 | while( jap < 3 ) { |
---|
455 | id = (rand()%clients) + shift; |
---|
456 | if ( !is( deleted, id, clients/3 ) ) { |
---|
457 | std::cout << "delete object: " << id << std::endl; |
---|
458 | cmanager->testDeleteObjectIDReg( id ); |
---|
459 | jap++; |
---|
460 | } |
---|
461 | } |
---|
462 | std::cout << "resulting list:" << std::endl; |
---|
463 | printClientObjectMapping( cmanager, clients-(clients/3)-3); |
---|
464 | } |
---|
465 | |
---|
466 | bool addClientTest( ENetEvent* event, ClientInformation*& head ) { |
---|
467 | ClientInformation *temp = head->insertBack(new ClientInformation); |
---|
468 | if(!temp) |
---|
469 | return false; |
---|
470 | if(temp->prev()->head) { |
---|
471 | temp->prev()->setID(0); |
---|
472 | temp->setID(1); |
---|
473 | } |
---|
474 | else |
---|
475 | temp->setID(temp->prev()->getID()+1); |
---|
476 | temp->setPeer(event->peer); |
---|
477 | std::cout << "added client id: " << temp->getID() << std::endl; |
---|
478 | |
---|
479 | return temp->setSynched(true); |
---|
480 | } |
---|
481 | |
---|
482 | void printClientInformationBox( ClientInformation* box ) { |
---|
483 | if(!box) |
---|
484 | return; |
---|
485 | std::cout << "ClientList: id: " << box->getID() << "\t"; |
---|
486 | std::cout << "g_id: " << box->getGamestateID() << " \t"; |
---|
487 | std::cout << "synched: " << box->getSynched() << "\t"; |
---|
488 | std::cout << "is head: " << box->head << std::endl; |
---|
489 | } |
---|
490 | |
---|
491 | void printClientInformationList( ClientInformation* list ) { |
---|
492 | printClientInformationBox( list ); |
---|
493 | list = list->next(); |
---|
494 | |
---|
495 | while( list != 0 ) { |
---|
496 | printClientInformationBox( list ); |
---|
497 | list = list->next(); |
---|
498 | } |
---|
499 | return; |
---|
500 | } |
---|
501 | |
---|
502 | void testClientInformation( int numberOfClients ) { |
---|
503 | ClientInformation* head = new ClientInformation( true ); |
---|
504 | ConnectionManager* connectionManager; |
---|
505 | ENetEvent event; |
---|
506 | |
---|
507 | for ( int i=0; i<numberOfClients; i++ ) { |
---|
508 | if ( !addClientTest( &event, head ) ) { |
---|
509 | std::cout << "addClientTest didn't work with: " << i << std::endl; |
---|
510 | } |
---|
511 | } |
---|
512 | std::cout << "(now id should be synched, since that works and this is test of list, this step is left out)" << std::endl; |
---|
513 | |
---|
514 | printClientInformationList( head ); |
---|
515 | |
---|
516 | std::cout << "remove some clients" << std::endl; |
---|
517 | if ( head->removeClient( numberOfClients/3 ) ) std::cout << "client " << numberOfClients/3 << " removed" << std::endl; |
---|
518 | else std::cout << "could not remove client: " << numberOfClients/3 << std::endl; |
---|
519 | if ( head->removeClient( numberOfClients ) ) std::cout << "client " << numberOfClients << " removed" << std::endl; |
---|
520 | else std::cout << "could not remove client: " << numberOfClients << std::endl; |
---|
521 | if ( head->removeClient( 1 ) ) std::cout << "client " << 1 << " removed" << std::endl; |
---|
522 | else std::cout << "could not remove client: " << 1 << std::endl; |
---|
523 | if ( head->removeClient( 1 ) ) std::cout << "client " << 1 << " removed a second time" << std::endl; |
---|
524 | else std::cout << "could not remove client: " << 1 << std::endl; |
---|
525 | if ( head->removeClient( numberOfClients + 100 ) ) std::cout << "client " << numberOfClients + 100 << " removed a second time" << std::endl; |
---|
526 | else std::cout << "could not remove client: " << numberOfClients + 100 << std::endl; |
---|
527 | |
---|
528 | printClientInformationList( head ); |
---|
529 | |
---|
530 | std::cout << "try to find some clients with findClient(..., false)" << std::endl; |
---|
531 | ClientInformation* temp = head->findClient( 2 ); |
---|
532 | printClientInformationBox( temp ); |
---|
533 | temp = head->findClient( numberOfClients/3 ); |
---|
534 | printClientInformationBox( temp ); |
---|
535 | temp = head->findClient( 0 ); |
---|
536 | printClientInformationBox( temp ); |
---|
537 | temp = head->findClient( 8 ); |
---|
538 | printClientInformationBox( temp ); |
---|
539 | |
---|
540 | std::cout << "find the same, output should be identical with above but with findClient(..., TRUE)" << std::endl; |
---|
541 | temp = head->findClient( 2, true ); |
---|
542 | printClientInformationBox( temp ); |
---|
543 | temp = head->findClient( numberOfClients/3, true ); |
---|
544 | printClientInformationBox( temp ); |
---|
545 | temp = head->findClient( 0, true ); |
---|
546 | printClientInformationBox( temp ); |
---|
547 | temp = head->findClient( 8, true ); |
---|
548 | printClientInformationBox( temp ); |
---|
549 | |
---|
550 | std::cout << "test setGamestateID" << std::endl; |
---|
551 | temp->setGameStateID( 8 ); |
---|
552 | printClientInformationBox( temp ); |
---|
553 | |
---|
554 | std::cout << "test insertAfter() and insertBefore()" << std::endl; |
---|
555 | ClientInformation* newInfo = new ClientInformation; |
---|
556 | ClientInformation* toool = new ClientInformation; |
---|
557 | newInfo->setGamestateID( 200 ); |
---|
558 | newInfo->setID( numberOfClients+2); |
---|
559 | newInfo->setSynched( true ); |
---|
560 | newInfo->setPeer( NULL ); |
---|
561 | toool->setGameStateID( 199 ); |
---|
562 | toool->setID( numberOfClients+1); |
---|
563 | toool->setSynched( true ); |
---|
564 | toool->setPeer( NULL ); |
---|
565 | |
---|
566 | //never use the same ClientInformation box in this situation |
---|
567 | //-> results in endless loop |
---|
568 | temp->insertAfter( newInfo ); |
---|
569 | temp->insertBefore( toool ); |
---|
570 | |
---|
571 | printClientInformationList( head ); |
---|
572 | return; |
---|
573 | } |
---|
574 | |
---|
575 | //### following stuff is to test buffer, took from PacketBufferTestExt.cc ### |
---|
576 | |
---|
577 | void write(PacketBuffer *test){ |
---|
578 | ENetEvent event; |
---|
579 | ENetPacket *packet; |
---|
580 | if(test->isEmpty()) |
---|
581 | std::cout << "buffer is empty" << std::endl; |
---|
582 | for(int i=0; i<10; i++){ |
---|
583 | std::string temp = "packet "; |
---|
584 | packet = enet_packet_create("packet", strlen("packet ")+1, |
---|
585 | ENET_PACKET_FLAG_RELIABLE); |
---|
586 | std::cout << i << ": pushing " << packet->data << std::endl; |
---|
587 | event.packet=packet; |
---|
588 | test->push(&event); |
---|
589 | if(i==5) |
---|
590 | usleep(200000); |
---|
591 | } |
---|
592 | test->setClosed(true); |
---|
593 | return; |
---|
594 | } |
---|
595 | |
---|
596 | void read(PacketBuffer *test){ |
---|
597 | //test->print(); |
---|
598 | // exit if the queue is closed and empty |
---|
599 | while(!test->isClosed() || !test->isEmpty()){ |
---|
600 | // only pop if the queue isn't empty |
---|
601 | while(!test->isEmpty()){ |
---|
602 | std::cout << "We popped the value " << test->pop()->data << std::endl; |
---|
603 | } |
---|
604 | } |
---|
605 | return; |
---|
606 | } |
---|
607 | |
---|
608 | void testPacketBuffer() { |
---|
609 | PacketBuffer test = PacketBuffer(); |
---|
610 | boost::thread thrd1(boost::bind(&write, &test)); |
---|
611 | boost::thread thrd2(boost::bind(&read, &test)); |
---|
612 | |
---|
613 | thrd1.join(); |
---|
614 | thrd2.join(); |
---|
615 | return; |
---|
616 | } |
---|
617 | |
---|
618 | //### end packetbuffer test stuff ### |
---|
619 | |
---|
620 | void testSynchronisable() { |
---|
621 | Synchronisable* synchtest; |
---|
622 | unsigned char* data = new unsigned char[100]; |
---|
623 | for( int i=0; i<100; i++ ) { |
---|
624 | data[i] = i%10; |
---|
625 | } |
---|
626 | //synchtest->registerVar( (const void*)data, 100, network::DATA ); |
---|
627 | } |
---|
628 | |
---|
629 | //######################################################################### |
---|
630 | //### this stuff is only a loop to chose one of the prev test functions ### |
---|
631 | //######################################################################### |
---|
632 | void displayModes() { |
---|
633 | std::cout << "mode datalength: length of data array to create" << std::endl; |
---|
634 | std::cout << "mode Data:" << std::endl; |
---|
635 | std::cout << "\t-1 -> array[length] with numbers length%255" << std::endl; |
---|
636 | std::cout << "\t-2 -> array[length] with numbers length%255, every %98 is != 0" << std::endl; |
---|
637 | std::cout << "\t-3 -> array[length] with random numbers (-126:127) no modulo zeros" << std::endl; |
---|
638 | std::cout << "\t-4 -> array[length] with random numbers (0:127) no modulo zeros" << std::endl; |
---|
639 | std::cout << "---------------------------------------------------------------------------------" << std::endl; |
---|
640 | std::cout << "mode Change:" << std::endl; |
---|
641 | std::cout << "\t-1 -> every %10 == 0 index is different from original" << std::endl; |
---|
642 | std::cout << "\t-2 -> every %(rand()%(length/11)) is different from original" << std::endl; |
---|
643 | std::cout << "\t-3 -> every %10 == 0 index is different and randomly longer till 2xlonger" << std::endl; |
---|
644 | std::cout << "\t-4 -> random differences and randomly longer" << std::endl; |
---|
645 | std::cout << "\t-5 -> only half as long and ever %10 == 0 index is different" << std::endl; |
---|
646 | } |
---|
647 | |
---|
648 | int main( int argc, char* argv[] ) { |
---|
649 | int a,b,c,n; |
---|
650 | std::string dec = "nothing"; |
---|
651 | std::cout << "############### START TEST (quit q) ###############" << std::endl; |
---|
652 | std::cout << "possible tests: " << std::endl; |
---|
653 | std::cout << "displayModes:\t\t modes" << std::endl; |
---|
654 | std::cout << "testCompression:\t tc [datalength] [mode Data]" << std::endl; |
---|
655 | std::cout << "testNCompression:\t tnc [#of loops] [datalength] [mode Data]" << std::endl; |
---|
656 | std::cout << "testDifferentiation:\t td [datalength] [mode Data] [mode Change]" << std::endl; |
---|
657 | std::cout << "testCompressWithDiff:\t tcd [datalength] [mode Data] [mode Change]" << std::endl; |
---|
658 | std::cout << "testNCompressWithDiff:\t tncd [#of loops] [datalength] [mode Data] [mode Change]" << std::endl; |
---|
659 | std::cout << "testClientObjectMapping: tcom [#clients]" << std::endl; |
---|
660 | std::cout << "testClientInformation:\t tci [#clients] (call with >10)" << std::endl; |
---|
661 | std::cout << "testPacketBuffer:\t tbuf (comment address assignements in PacketBuffer.cc!)" << std::endl; |
---|
662 | while ( dec.compare("q") != 0 ) { |
---|
663 | std::cin >> dec; |
---|
664 | if ( dec.compare("tc") == 0 ) { |
---|
665 | std::cin >> a; std::cin >> b; |
---|
666 | testCompression( a, b ); |
---|
667 | } |
---|
668 | else if ( dec.compare("td") == 0 ) { |
---|
669 | std::cin>> a; std::cin >> b; std::cin >> c; |
---|
670 | testDifferentiation( a, b, c ); |
---|
671 | } |
---|
672 | else if ( dec.compare("tcd") == 0 ) { |
---|
673 | std::cin>> a; std::cin >> b; std::cin >> c; |
---|
674 | testCompressWithDiff( a, b, c ); |
---|
675 | } |
---|
676 | else if ( dec.compare("modes") == 0 ) |
---|
677 | displayModes(); |
---|
678 | else if ( dec.compare("tcom") == 0 ) { |
---|
679 | std::cin>> a; |
---|
680 | testClientObjectMapping( a ); |
---|
681 | } |
---|
682 | else if ( dec.compare("tci") == 0 ) { |
---|
683 | std::cin >> a; |
---|
684 | testClientInformation( a ); |
---|
685 | } |
---|
686 | else if ( dec.compare("tbuf") == 0 ) { |
---|
687 | testPacketBuffer(); |
---|
688 | } |
---|
689 | else if ( dec.compare("tncd") == 0 ) { |
---|
690 | std::cin >> n; std::cin >> a; std::cin >> b; std::cin >> c; |
---|
691 | for ( int i=1; i<=n; i++ ) { |
---|
692 | std::cout << i << " s" << a << " "; |
---|
693 | //std::cout << "start loop test " << i << std::endl; |
---|
694 | if ( !testNCompressWithDiff( i, a, b, c ) ) { |
---|
695 | std::cout << "#COMPARSION ERROR->VERYVERY BAD" << std::endl; |
---|
696 | break; |
---|
697 | } |
---|
698 | } |
---|
699 | std::cout << "end loop comparsion test" << std::endl; |
---|
700 | } |
---|
701 | else if ( dec.compare("tnc") == 0 ) { |
---|
702 | std::cin >> n; std::cin >> a; std::cin >> b; |
---|
703 | for ( int i=1; i<=n; i++ ) { |
---|
704 | std::cout << i << " s" << a << " "; |
---|
705 | //std::cout << "start loop test " << i << std::endl; |
---|
706 | if ( !testNCompression( i, a, b ) ) { |
---|
707 | std::cout << "#COMPARSION ERROR->VERYVERY BAD" << std::endl; |
---|
708 | break; |
---|
709 | } |
---|
710 | } |
---|
711 | std::cout << "end loop comparsion test" << std::endl; |
---|
712 | } |
---|
713 | else std::cout << "invalid input" << std::endl; |
---|
714 | std::cout << "################## END ONE TURN ##################@" << std::endl; |
---|
715 | } |
---|
716 | return 0; |
---|
717 | } |
---|
718 | /* |
---|
719 | int main() { |
---|
720 | std::cout << "############### START TEST (quit q) ###############" << std::endl; |
---|
721 | testSynchronisable(); |
---|
722 | }*/ |
---|
723 | |
---|
724 | //######################################################################### |
---|
725 | //### the prev loop is only to chose one prev prev test functions ### |
---|
726 | //######################################################################### |
---|