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->prev()->head) { |
---|
469 | temp->prev()->setID(0); |
---|
470 | temp->setID(1); |
---|
471 | } |
---|
472 | else |
---|
473 | temp->setID(temp->prev()->getID()+1); |
---|
474 | temp->setPeer(event->peer); |
---|
475 | std::cout << "added client id: " << temp->getID() << std::endl; |
---|
476 | |
---|
477 | temp->setSynched(true); |
---|
478 | return true; |
---|
479 | } |
---|
480 | |
---|
481 | void printClientInformationBox( ClientInformation* box ) { |
---|
482 | std::cout << "ClientList: id: " << box->getID() << "\t"; |
---|
483 | std::cout << "g_id: " << box->getGamestateID() << " \t"; |
---|
484 | std::cout << "synched: " << box->getSynched() << "\t"; |
---|
485 | std::cout << "is head: " << box->head << std::endl; |
---|
486 | } |
---|
487 | |
---|
488 | void printClientInformationList( ClientInformation* list ) { |
---|
489 | printClientInformationBox( list ); |
---|
490 | list = list->next(); |
---|
491 | |
---|
492 | while( list != 0 ) { |
---|
493 | printClientInformationBox( list ); |
---|
494 | list = list->next(); |
---|
495 | } |
---|
496 | return; |
---|
497 | } |
---|
498 | |
---|
499 | void testClientInformation( int numberOfClients ) { |
---|
500 | ClientInformation* head = new ClientInformation( true ); |
---|
501 | ConnectionManager* connectionManager; |
---|
502 | ENetEvent event; |
---|
503 | |
---|
504 | for ( int i=0; i<numberOfClients; i++ ) { |
---|
505 | if ( !addClientTest( &event, head ) ) { |
---|
506 | std::cout << "addClientTest didn't work with: " << i << std::endl; |
---|
507 | } |
---|
508 | } |
---|
509 | std::cout << "(now id should be synched, since that works and this is test of list, this step is left out)" << std::endl; |
---|
510 | |
---|
511 | printClientInformationList( head ); |
---|
512 | |
---|
513 | std::cout << "remove some clients" << std::endl; |
---|
514 | if ( head->removeClient( numberOfClients/3 ) ) std::cout << "client " << numberOfClients/3 << " removed" << std::endl; |
---|
515 | else std::cout << "could not remove client: " << numberOfClients/3 << std::endl; |
---|
516 | if ( head->removeClient( numberOfClients ) ) std::cout << "client " << numberOfClients << " removed" << std::endl; |
---|
517 | else std::cout << "could not remove client: " << numberOfClients << std::endl; |
---|
518 | if ( head->removeClient( 1 ) ) std::cout << "client " << 1 << " removed" << std::endl; |
---|
519 | else std::cout << "could not remove client: " << 1 << std::endl; |
---|
520 | if ( head->removeClient( 1 ) ) std::cout << "client " << 1 << " removed a second time" << std::endl; |
---|
521 | else std::cout << "could not remove client: " << 1 << std::endl; |
---|
522 | if ( head->removeClient( numberOfClients + 100 ) ) std::cout << "client " << numberOfClients + 100 << " removed a second time" << std::endl; |
---|
523 | else std::cout << "could not remove client: " << numberOfClients + 100 << std::endl; |
---|
524 | |
---|
525 | printClientInformationList( head ); |
---|
526 | |
---|
527 | std::cout << "try to find some clients with findClient(..., false)" << std::endl; |
---|
528 | ClientInformation* temp = head->findClient( 2 ); |
---|
529 | printClientInformationBox( temp ); |
---|
530 | temp = head->findClient( numberOfClients/3 ); |
---|
531 | printClientInformationBox( temp ); |
---|
532 | temp = head->findClient( 0 ); |
---|
533 | printClientInformationBox( temp ); |
---|
534 | temp = head->findClient( 8 ); |
---|
535 | printClientInformationBox( temp ); |
---|
536 | |
---|
537 | std::cout << "find the same, output should be identical with above but with findClient(..., TRUE)" << std::endl; |
---|
538 | temp = head->findClient( 2, true ); |
---|
539 | printClientInformationBox( temp ); |
---|
540 | temp = head->findClient( numberOfClients/3, true ); |
---|
541 | printClientInformationBox( temp ); |
---|
542 | temp = head->findClient( 0, true ); |
---|
543 | printClientInformationBox( temp ); |
---|
544 | temp = head->findClient( 8, true ); |
---|
545 | printClientInformationBox( temp ); |
---|
546 | |
---|
547 | std::cout << "test setGamestateID" << std::endl; |
---|
548 | temp->setGamestateID( 8 ); |
---|
549 | printClientInformationBox( temp ); |
---|
550 | |
---|
551 | std::cout << "test insertAfter() and insertBefore()" << std::endl; |
---|
552 | ClientInformation* newInfo = new ClientInformation; |
---|
553 | ClientInformation* toool = new ClientInformation; |
---|
554 | newInfo->setGamestateID( 200 ); |
---|
555 | newInfo->setID( numberOfClients+2); |
---|
556 | newInfo->setSynched( true ); |
---|
557 | newInfo->setPeer( NULL ); |
---|
558 | toool->setGamestateID( 199 ); |
---|
559 | toool->setID( numberOfClients+1); |
---|
560 | toool->setSynched( true ); |
---|
561 | toool->setPeer( NULL ); |
---|
562 | |
---|
563 | //never use the same ClientInformation box in this situation |
---|
564 | //-> results in endless loop |
---|
565 | temp->insertAfter( newInfo ); |
---|
566 | temp->insertBefore( toool ); |
---|
567 | |
---|
568 | printClientInformationList( head ); |
---|
569 | return; |
---|
570 | } |
---|
571 | |
---|
572 | //### following stuff is to test buffer, took from PacketBufferTestExt.cc ### |
---|
573 | |
---|
574 | void write(PacketBuffer *test){ |
---|
575 | ENetEvent event; |
---|
576 | ENetPacket *packet; |
---|
577 | if(test->isEmpty()) |
---|
578 | std::cout << "buffer is empty" << std::endl; |
---|
579 | for(int i=0; i<10; i++){ |
---|
580 | std::string temp = "packet "; |
---|
581 | packet = enet_packet_create("packet", strlen("packet ")+1, |
---|
582 | ENET_PACKET_FLAG_RELIABLE); |
---|
583 | std::cout << i << ": pushing " << packet->data << std::endl; |
---|
584 | event.packet=packet; |
---|
585 | test->push(&event); |
---|
586 | if(i==5) |
---|
587 | usleep(200000); |
---|
588 | } |
---|
589 | test->setClosed(true); |
---|
590 | return; |
---|
591 | } |
---|
592 | |
---|
593 | void read(PacketBuffer *test){ |
---|
594 | //test->print(); |
---|
595 | // exit if the queue is closed and empty |
---|
596 | while(!test->isClosed() || !test->isEmpty()){ |
---|
597 | // only pop if the queue isn't empty |
---|
598 | while(!test->isEmpty()){ |
---|
599 | std::cout << "We popped the value " << test->pop()->data << std::endl; |
---|
600 | } |
---|
601 | } |
---|
602 | return; |
---|
603 | } |
---|
604 | |
---|
605 | void testPacketBuffer() { |
---|
606 | PacketBuffer test = PacketBuffer(); |
---|
607 | boost::thread thrd1(boost::bind(&write, &test)); |
---|
608 | boost::thread thrd2(boost::bind(&read, &test)); |
---|
609 | |
---|
610 | thrd1.join(); |
---|
611 | thrd2.join(); |
---|
612 | return; |
---|
613 | } |
---|
614 | |
---|
615 | //### end packetbuffer test stuff ### |
---|
616 | |
---|
617 | void testSynchronisable() { |
---|
618 | Synchronisable* synchtest; |
---|
619 | unsigned char* data = new unsigned char[100]; |
---|
620 | for( int i=0; i<100; i++ ) { |
---|
621 | data[i] = i%10; |
---|
622 | } |
---|
623 | //synchtest->registerVar( (const void*)data, 100, network::DATA ); |
---|
624 | } |
---|
625 | |
---|
626 | //######################################################################### |
---|
627 | //### this stuff is only a loop to chose one of the prev test functions ### |
---|
628 | //######################################################################### |
---|
629 | void displayModes() { |
---|
630 | std::cout << "mode datalength: length of data array to create" << std::endl; |
---|
631 | std::cout << "mode Data:" << std::endl; |
---|
632 | std::cout << "\t-1 -> array[length] with numbers length%255" << std::endl; |
---|
633 | std::cout << "\t-2 -> array[length] with numbers length%255, every %98 is != 0" << std::endl; |
---|
634 | std::cout << "\t-3 -> array[length] with random numbers (-126:127) no modulo zeros" << std::endl; |
---|
635 | std::cout << "\t-4 -> array[length] with random numbers (0:127) no modulo zeros" << std::endl; |
---|
636 | std::cout << "---------------------------------------------------------------------------------" << std::endl; |
---|
637 | std::cout << "mode Change:" << std::endl; |
---|
638 | std::cout << "\t-1 -> every %10 == 0 index is different from original" << std::endl; |
---|
639 | std::cout << "\t-2 -> every %(rand()%(length/11)) is different from original" << std::endl; |
---|
640 | std::cout << "\t-3 -> every %10 == 0 index is different and randomly longer till 2xlonger" << std::endl; |
---|
641 | std::cout << "\t-4 -> random differences and randomly longer" << std::endl; |
---|
642 | std::cout << "\t-5 -> only half as long and ever %10 == 0 index is different" << std::endl; |
---|
643 | } |
---|
644 | |
---|
645 | int main( int argc, char* argv[] ) { |
---|
646 | int a,b,c,n; |
---|
647 | std::string dec = "nothing"; |
---|
648 | std::cout << "############### START TEST (quit q) ###############" << std::endl; |
---|
649 | std::cout << "possible tests: " << std::endl; |
---|
650 | std::cout << "displayModes:\t\t modes" << std::endl; |
---|
651 | std::cout << "testCompression:\t tc [datalength] [mode Data]" << std::endl; |
---|
652 | std::cout << "testNCompression:\t tnc [#of loops] [datalength] [mode Data]" << std::endl; |
---|
653 | std::cout << "testDifferentiation:\t td [datalength] [mode Data] [mode Change]" << std::endl; |
---|
654 | std::cout << "testCompressWithDiff:\t tcd [datalength] [mode Data] [mode Change]" << std::endl; |
---|
655 | std::cout << "testNCompressWithDiff:\t tncd [#of loops] [datalength] [mode Data] [mode Change]" << std::endl; |
---|
656 | std::cout << "testClientObjectMapping: tcom [#clients]" << std::endl; |
---|
657 | std::cout << "testClientInformation:\t tci [#clients] (call with >10)" << std::endl; |
---|
658 | std::cout << "testPacketBuffer:\t tbuf (comment address assignements in PacketBuffer.cc!)" << std::endl; |
---|
659 | while ( dec.compare("q") != 0 ) { |
---|
660 | std::cin >> dec; |
---|
661 | if ( dec.compare("tc") == 0 ) { |
---|
662 | std::cin >> a; std::cin >> b; |
---|
663 | testCompression( a, b ); |
---|
664 | } |
---|
665 | else if ( dec.compare("td") == 0 ) { |
---|
666 | std::cin>> a; std::cin >> b; std::cin >> c; |
---|
667 | testDifferentiation( a, b, c ); |
---|
668 | } |
---|
669 | else if ( dec.compare("tcd") == 0 ) { |
---|
670 | std::cin>> a; std::cin >> b; std::cin >> c; |
---|
671 | testCompressWithDiff( a, b, c ); |
---|
672 | } |
---|
673 | else if ( dec.compare("modes") == 0 ) |
---|
674 | displayModes(); |
---|
675 | else if ( dec.compare("tcom") == 0 ) { |
---|
676 | std::cin>> a; |
---|
677 | testClientObjectMapping( a ); |
---|
678 | } |
---|
679 | else if ( dec.compare("tci") == 0 ) { |
---|
680 | std::cin >> a; |
---|
681 | testClientInformation( a ); |
---|
682 | } |
---|
683 | else if ( dec.compare("tbuf") == 0 ) { |
---|
684 | testPacketBuffer(); |
---|
685 | } |
---|
686 | else if ( dec.compare("tncd") == 0 ) { |
---|
687 | std::cin >> n; std::cin >> a; std::cin >> b; std::cin >> c; |
---|
688 | for ( int i=1; i<=n; i++ ) { |
---|
689 | std::cout << i << " s" << a << " "; |
---|
690 | //std::cout << "start loop test " << i << std::endl; |
---|
691 | if ( !testNCompressWithDiff( i, a, b, c ) ) { |
---|
692 | std::cout << "#COMPARSION ERROR->VERYVERY BAD" << std::endl; |
---|
693 | break; |
---|
694 | } |
---|
695 | } |
---|
696 | std::cout << "end loop comparsion test" << std::endl; |
---|
697 | } |
---|
698 | else if ( dec.compare("tnc") == 0 ) { |
---|
699 | std::cin >> n; std::cin >> a; std::cin >> b; |
---|
700 | for ( int i=1; i<=n; i++ ) { |
---|
701 | std::cout << i << " s" << a << " "; |
---|
702 | //std::cout << "start loop test " << i << std::endl; |
---|
703 | if ( !testNCompression( i, a, b ) ) { |
---|
704 | std::cout << "#COMPARSION ERROR->VERYVERY BAD" << std::endl; |
---|
705 | break; |
---|
706 | } |
---|
707 | } |
---|
708 | std::cout << "end loop comparsion test" << std::endl; |
---|
709 | } |
---|
710 | else std::cout << "invalid input" << std::endl; |
---|
711 | std::cout << "################## END ONE TURN ##################@" << std::endl; |
---|
712 | } |
---|
713 | return 0; |
---|
714 | } |
---|
715 | /* |
---|
716 | int main() { |
---|
717 | std::cout << "############### START TEST (quit q) ###############" << std::endl; |
---|
718 | testSynchronisable(); |
---|
719 | }*/ |
---|
720 | |
---|
721 | //######################################################################### |
---|
722 | //### the prev loop is only to chose one prev prev test functions ### |
---|
723 | //######################################################################### |
---|