Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/subprojects/network/network_unit_test.cc @ 10767

Last change on this file since 10767 was 9656, checked in by bensch, 18 years ago

orxonox/trunk: merged the proxy bache back with no conflicts

File size: 12.3 KB
Line 
1
2#include "stdlibincl.h"
3#include <stdarg.h>
4#include <stdio.h>
5#include "shell_buffer.h"
6#include "class_list.h"
7
8#include "network_manager.h"
9#include "network_socket.h"
10#include "server_socket.h"
11#include "udp_server_socket.h"
12#include "udp_socket.h"
13#include "network_stream.h"
14#include "synchronizeable.h"
15#include "converter.h"
16#include "state.h"
17#include "shared_network_data.h"
18#include "message_manager.h"
19#include "network_log.h"
20
21#include "simple_sync.h"
22#include "read_sync.h"
23
24int verbose = 4;
25
26
27/* outputs the help */
28int startHelp(int argc, char** argv)
29{
30  printf("Network is a network unit test\n");
31  printf(" --help                     this output\n");
32  printf(" -st,  --sockettest         test network_socket\n");
33  printf(" -ft,  --frameworktest      test the network module\n");
34  printf(" --server [port number]     creates a test server\n");
35  printf(" --client [address] [port]  connects to a server\n");
36  printf(" --listen [address] [port]  just listens to this connection");
37  printf("\n");
38}
39
40
41/* test SDL network socket */
42int testSocket(int argc, char** argv)
43{
44  UdpServerSocket server(9999);
45
46  NetworkSocket* client1 = new UdpSocket("localhost", 9999);
47
48  NetworkSocket* server1 = NULL;
49  while ( server1 == NULL )
50  {
51    server.update();
52    server1 = server.getNewSocket();
53  }
54
55  assert( server1->isOk() );
56
57  NetworkSocket* client2 = new UdpSocket("localhost", 9999);
58
59  NetworkSocket* server2 = NULL;
60  while ( server2 == NULL )
61  {
62    server.update();
63    server2 = server.getNewSocket();
64  }
65
66  char buf[1024];
67
68  printf("read from client1 before sending data\n");
69  printf("result: %d bytes\n", client1->readPacket((byte*)buf, 1024));
70
71  printf("read from client2 before sending data\n");
72  printf("result: %d bytes\n", client2->readPacket((byte*)buf, 1024));
73
74  int n;
75  char * str1 = "client1 to server";
76  char * str2 = "server1 to client";
77  char * str3 = "client2 to server";
78  char * str4 = "server2 to client";
79  n = client1->writePacket((byte*)str1, strlen(str1)+1);
80  printf("%d bytes send from client1\n", n);
81  n = server1->writePacket((byte*)str2, strlen(str2)+1);
82  printf("%d bytes send from server1\n", n);
83  n = client2->writePacket((byte*)str3, strlen(str3)+1);
84  printf("%d bytes send from client2\n", n);
85  n = server2->writePacket((byte*)str4, strlen(str4)+1);
86  printf("%d bytes send from server2\n", n);
87  SDL_Delay(10);
88
89  server.update();
90
91  printf("read from server1\n");
92  n = server1->readPacket((byte*)buf, 1024);
93  printf("read %d bytes\n", n);
94  if (n<0)
95    return -1;
96
97  printf("data: '%s'\n", buf);
98
99  printf("read from server2\n");
100  n = server2->readPacket((byte*)buf, 1024);
101  printf("read %d bytes\n", n);
102  if (n<0)
103    return -1;
104
105  printf("data: '%s'\n", buf);
106
107  printf("read from client1\n");
108  n = client1->readPacket((byte*)buf, 1024);
109  printf("read %d bytes\n", n);
110  if (n<0)
111    return -1;
112
113  printf("data: '%s'\n", buf);
114
115  printf("read from client2\n");
116  n = client2->readPacket((byte*)buf, 1024);
117  printf("read %d bytes\n", n);
118  if (n<0)
119    return -1;
120
121  printf("data: '%s'\n", buf);
122
123
124  //22222222222222222222222222222222222222222
125  n = client1->writePacket((byte*)str1, strlen(str1)+1);
126  printf("%d bytes send from client1\n", n);
127  n = server1->writePacket((byte*)str2, strlen(str2)+1);
128  printf("%d bytes send from server1\n", n);
129  n = client2->writePacket((byte*)str3, strlen(str3)+1);
130  printf("%d bytes send from client2\n", n);
131  n = server2->writePacket((byte*)str4, strlen(str4)+1);
132  printf("%d bytes send from server2\n", n);
133  SDL_Delay(10);
134
135  server.update();
136
137  printf("read from server1\n");
138  n = server1->readPacket((byte*)buf, 1024);
139  printf("read %d bytes\n", n);
140  if (n<0)
141    return -1;
142
143  printf("data: '%s'\n", buf);
144
145  printf("read from server2\n");
146  n = server2->readPacket((byte*)buf, 1024);
147  printf("read %d bytes\n", n);
148  if (n<0)
149    return -1;
150
151  printf("data: '%s'\n", buf);
152
153  printf("read from client1\n");
154  n = client1->readPacket((byte*)buf, 1024);
155  printf("read %d bytes\n", n);
156  if (n<0)
157    return -1;
158
159  printf("data: '%s'\n", buf);
160
161  printf("read from client2\n");
162  n = client2->readPacket((byte*)buf, 1024);
163  printf("read %d bytes\n", n);
164  if (n<0)
165    return -1;
166
167  printf("data: '%s'\n", buf);
168
169  //sending bigger packets than 255 is not supported
170#if 0
171  printf("try to send more than 255 bytes\n");
172  printf("result: %d\n", client1->writePacket((byte*)buf, 1000));
173
174  server1->writePacket((byte*)str1, strlen(str1)+1);
175  SDL_Delay(500);
176  printf("try to read with a too small buffer\n");
177  printf("result: %d\n", client1->readPacket((byte*)buf, strlen(str1)));
178#endif
179
180  delete client1;
181  delete client2;
182  delete server1;
183  delete server2;
184
185  return 0;
186}
187
188
189int testFramework(int argc, char** argv)
190{
191  printf("=================\n");
192  printf("TestFramework\n");
193  printf("=================\n");
194
195  Synchronizeable* clientSync = new SimpleSync( std::string("Client") );
196  Synchronizeable* serverSync = new SimpleSync( std::string("Server") );
197
198  unsigned int port = 9999;
199
200  /* create the network manager */
201  NetworkManager* nm = NetworkManager::getInstance();
202
203  /* initialize the network manager */
204  nm->initialize();
205
206  /* create a server stream */
207  nm->createMasterServer(port);
208
209  /* esatblish a connection */
210  IP ip;
211  int error = SDLNet_ResolveHost(&ip, "127.0.0.1", port);
212  //SDLNet_ResolveHost(&ip, "localhost", port);
213  if(error == -1)
214    printf("\n\nerror on address resolution, program inconsistancy\n\n");
215  nm->createClient("localhost", port);
216  nm->connectSynchronizeable( *clientSync );
217  /* adding some break for connection setup */
218  SDL_Delay(20);
219
220  /* synchronize the data 1 time (increment for longer tests) */
221  for( int i = 0; i < 3; i++)
222  {
223    nm->synchronize( 1000 );
224    /* simulate the network delay */
225    SDL_Delay(50);
226  }
227
228  printf("Test finished\n");
229
230
231  /* delete the network manager again */
232  delete nm;
233
234  delete clientSync;
235  delete serverSync;
236
237  return 0;
238}
239
240bool testCB( MessageType messageType, byte * data, int dataLength, void * someData, int userId )
241{
242  printf("GOT MESSAGE: %s\n", data);
243  return true;
244}
245
246/**
247 *
248 * @param argc
249 * @param argv
250 * @return
251 */
252int startServer(int argc, char** argv)
253{
254
255
256  State::setOnline(true);
257  SharedNetworkData::getInstance()->setGameServer( true );
258  printf("Starting Server on port %i\n", 9999);
259
260  NetworkManager* netMan = NetworkManager::getInstance();
261
262  netMan->initialize();
263
264  netMan->createMasterServer(/**ss, */ 9999);
265
266  SimpleSync* ss = new SimpleSync("Server");
267  ss->setSynchronized( true );
268
269  NetworkLog::getInstance()->listen( 8888 );
270
271  //MessageManager::getInstance()->initUser( 1 );
272  MessageManager::getInstance()->registerMessageHandler( TESTMESSAGEID, testCB, NULL );
273
274  SDL_Delay(20);
275
276  for(;;)
277  {
278    MessageManager::getInstance()->sendMessage( TESTMESSAGEID, (byte*)"server to client", 18, RT_ALL_BUT_ME, NET_UNASSIGNED, MP_HIGHBANDWIDTH );
279    netMan->synchronize( 100 );
280    SDL_Delay(100);
281    ss->debug();
282  }
283
284  delete netMan;
285  delete ss;
286
287
288  return 0;
289}
290
291
292int startClient(int argc, char** argv)
293{
294
295
296  State::setOnline(true);
297  NetworkManager* netMan = NetworkManager::getInstance();
298
299
300  netMan->initialize();
301
302  std::string host;
303
304  if ( argc > 2 )
305    host = argv[2];
306  else
307    host = "localhost";
308
309  netMan->createClient(host, 9999);
310
311//  SimpleSync* ss = new SimpleSync("SimpleSync");
312//  ss->setSynchronized( true );
313//  netMan->connectSynchronizeable( *ss );
314
315  NetworkLog::getInstance()->listen( 7777 );
316
317  SimpleSync * ss = NULL;
318
319  //MessageManager::getInstance()->initUser( 0 );
320  MessageManager::getInstance()->registerMessageHandler( TESTMESSAGEID, testCB, NULL );
321
322  for(;;)
323  {
324    netMan->synchronize( 100 );
325    MessageManager::getInstance()->sendMessage( TESTMESSAGEID, (byte*)"client to server", 18, RT_ALL_BUT_ME, NET_UNASSIGNED, MP_HIGHBANDWIDTH );
326    ss = dynamic_cast<SimpleSync*>(ClassList::getObject( "Server", CL_SIMPLE_SYNC ) );
327    SDL_Delay(100);
328    if (ss)
329      ss->debug();
330  }
331
332
333  delete netMan;
334  delete ss;
335
336  return 0;
337}
338
339
340
341int startListen(int argc, char** argv)
342{
343  if( argc < 3)
344  {
345    printf(" Wrong arguments try following notations:\n");
346    printf("   --listen [server ip] [port number]\n");
347    printf("   --listen [server name] [port number]\n");
348    return 0;
349  }
350
351  char* name = argv[2];
352  int port = atoi(argv[3]);
353  printf("Connecting to %s, on port %i\n", name, port);
354
355  IP ip;
356  int error = SDLNet_ResolveHost(&ip, name, port);
357  if(error == -1)
358    printf("\n\nerror on address resolution, program inconsistancy\n\n");
359
360  NetworkManager* netMan = NetworkManager::getInstance();
361  Synchronizeable* ss = new SimpleSync("SimpleSync\0");
362
363  netMan->createClient( name, port );
364  netMan->connectSynchronizeable( *ss );
365
366  for(;;)
367  {
368    netMan->synchronize( 1000 );
369    SDL_Delay(10);
370  }
371
372
373  delete netMan;
374  delete ss;
375
376  return 0;
377}
378
379void testFloatConverter(float f)
380{
381#if 0
382  char* s = Converter::floatToBinString(f);
383  printf("%f = ", f);
384  printf(s); printf("\n");
385
386  byte* res = Converter::floatToByteArray(f);
387  printf("Byte Array: ");
388  for (int i = 0; i < 4; i++)
389//    printf("%i  ", res[i]);
390  printf("\n");
391
392  float b = Converter::byteArrayToFloat(res);
393  printf("ReConvert: %f \n", b);
394#endif
395}
396
397void testFloatConverter2(float f)
398{
399#if 0
400  char* s = Converter::floatToBinString(f);
401  printf("### %f = ", f);
402  printf(s); printf("\n");
403
404  byte* res = Converter::_floatToByteArray(f);
405  printf("Byte Array: ");
406  for (int i = 0; i < 4; i++)
407    printf("%i  ", res[i]);
408  printf("\n");
409
410  float b = Converter::_byteArrayToFloat(res);
411  printf("ReConvert: %f \n", b);
412#endif
413}
414int converter(int argc, char** argv)
415{
416  /*
417  int x = 200564786;
418  printf("To convert: %i\n", x);
419  byte* res = Converter::intToByteArray(x);
420  for (int i = 0; i < 4; i++)
421    printf("%i  ", res[i]);
422  printf("\n");
423
424  int z = Converter::byteArrayToInt(res);
425
426  printf("ReConvert: %i\n", z);
427
428
429  //float a = 5.4f;
430  //float b = 2.0f;
431  //printf("%f mod %f = %f", a, b, a % b);
432
433  printf("\n");
434
435  */
436  /*
437  float y;
438  char* s;
439
440  y = 12.0f;
441  s = Converter::floatToBinString(y);
442  printf("%f = ", y);
443  printf(s); printf("\n");
444
445  y = 24549026.0f;
446  s = Converter::floatToBinString(y);
447  printf("%f = ", y);
448  printf(s); printf("\n");
449
450  y = 12.4e20f;
451  s = Converter::floatToBinString(y);
452  printf("%f = ", y);
453  printf(s); printf("\n");
454
455  y = 4.7824f;
456  s = Converter::floatToBinString(y);
457  printf("%f = ", y);
458  printf(s); printf("\n");
459
460  y = -4.7824f;
461  s = Converter::floatToBinString(y);
462  printf("%f = ", y);
463  printf(s); printf("\n");
464
465  y = -14.35e14f;
466  s = Converter::floatToBinString(y);
467  printf("%f = ", y);
468  printf(s); printf("\n");
469                                                            */
470
471
472  /*
473  float a = 12.3f;
474
475  char* s = Converter::floatToBinString(a);
476  printf("%f = ", a);
477  printf(s); printf("\n");
478
479  byte* res = Converter::floatToByteArray(a);
480  printf("Byte Array: \n");
481  for (int i = 0; i < 4; i++)
482    printf("%i  ", res[i]);
483  printf("\n");
484
485  float b = Converter::byteArrayToFloat(res);
486  printf("ReConvert: %f \n", b);
487  */
488//  testFloatConverter(12.3e-53f); printf("\n");
489//  testFloatConverter(134.26455646546548741661675165f); printf("\n");
490 // testFloatConverter(35.67e14f); printf("\n");
491
492  testFloatConverter(12.3e-7f); printf("\n");
493  testFloatConverter(134.26455646546548741661675165f); printf("\n");
494  testFloatConverter(35.67e14f); printf("\n");
495
496  return 0;
497}
498
499
500
501/**
502 *
503 *  main function
504 *
505 * here the journey begins
506 */
507int main(int argc, char** argv)
508{
509  int i;
510  // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark.
511  for(i = 1; i < argc; ++i)
512  {
513    //else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true;
514    if (! strcmp( "--sockettest", argv[i]) || !strcmp("-st", argv[i]))
515      return testSocket(argc, argv);
516    else if (! strcmp( "--frameworktest", argv[i]) || !strcmp("-ft", argv[i]))
517      return testFramework(argc, argv);
518    else if (! strcmp( "--server", argv[i]) || !strcmp("-s", argv[i]))
519      return startServer(argc, argv);
520    else if (! strcmp( "--client", argv[i]) || !strcmp("-c", argv[i]))
521      return startClient(argc, argv);
522    else if (! strcmp( "--listen", argv[i]) || !strcmp("-l", argv[i]))
523      return startListen(argc, argv);
524    else if (! strcmp( "--converter", argv[i]) || !strcmp("-o", argv[i]))
525      return converter(argc, argv);
526  }
527
528  startHelp(argc, argv);
529
530  return 0;
531}
532
533
534//bool ShellBuffer::addBufferLineStatic(const char* line, ...)
535//{
536  //va_list arguments;
537  //vprintf(line, arguments);
538  //  printf("%s", line);
539//}
Note: See TracBrowser for help on using the repository browser.