Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/proxy/src/subprojects/network/network_unit_test.cc @ 9347

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

orxonox/proxy: merged the proxy.old back again, and it seems to work.

Merged with command
svn merge -r9247:HEAD https://svn.orxonox.net/orxonox/branches/proxy.old .

no conflicts

File size: 12.4 KB
RevLine 
[5579]1
[5580]2#include "stdlibincl.h"
3#include <stdarg.h>
4#include <stdio.h>
5#include "shell_buffer.h"
[5609]6#include "class_list.h"
[5579]7
[5580]8#include "network_manager.h"
[5624]9#include "network_socket.h"
[5996]10#include "server_socket.h"
[7954]11#include "udp_server_socket.h"
12#include "udp_socket.h"
[5649]13#include "network_stream.h"
[5647]14#include "synchronizeable.h"
[6139]15#include "converter.h"
[7954]16#include "state.h"
17#include "shared_network_data.h"
18#include "message_manager.h"
19#include "network_log.h"
[5579]20
[5650]21#include "simple_sync.h"
[5996]22#include "read_sync.h"
[5650]23
[5580]24int verbose = 4;
[5579]25
[5580]26
[5647]27/* outputs the help */
[5580]28int startHelp(int argc, char** argv)
[5609]29{
30  printf("Network is a network unit test\n");
[5812]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");
[5996]36  printf(" --listen [address] [port]  just listens to this connection");
[5609]37  printf("\n");
38}
[5579]39
[5647]40
41/* test SDL network socket */
[5624]42int testSocket(int argc, char** argv)
43{
[7954]44  UdpServerSocket server(9999);
[5732]45
[7954]46  NetworkSocket* client1 = new UdpSocket("localhost", 9999);
[5732]47
[7954]48  NetworkSocket* server1 = NULL;
49  while ( server1 == NULL )
50  {
51    server.update();
52    server1 = server.getNewSocket();
53  }
54 
55  assert( server1->isOk() );
[5996]56
[7954]57  NetworkSocket* client2 = new UdpSocket("localhost", 9999);
[5996]58
[7954]59  NetworkSocket* server2 = NULL;
60  while ( server2 == NULL )
61  {
62    server.update();
63    server2 = server.getNewSocket();
64  }
[5996]65
[5624]66  char buf[1024];
67
[5996]68  printf("read from client1 before sending data\n");
[6139]69  printf("result: %d bytes\n", client1->readPacket((byte*)buf, 1024));
[5624]70
[5996]71  printf("read from client2 before sending data\n");
[6139]72  printf("result: %d bytes\n", client2->readPacket((byte*)buf, 1024));
[5624]73
[5627]74  int n;
[5996]75  char * str1 = "client1 to server";
76  char * str2 = "server1 to client";
77  char * str3 = "client2 to server";
78  char * str4 = "server2 to client";
[6139]79  n = client1->writePacket((byte*)str1, strlen(str1)+1);
[5996]80  printf("%d bytes send from client1\n", n);
[6139]81  n = server1->writePacket((byte*)str2, strlen(str2)+1);
[5996]82  printf("%d bytes send from server1\n", n);
[6139]83  n = client2->writePacket((byte*)str3, strlen(str3)+1);
[5996]84  printf("%d bytes send from client2\n", n);
[6139]85  n = server2->writePacket((byte*)str4, strlen(str4)+1);
[5996]86  printf("%d bytes send from server2\n", n);
[7954]87  SDL_Delay(10);
88 
89  server.update();
[5630]90
[5996]91  printf("read from server1\n");
[6139]92  n = server1->readPacket((byte*)buf, 1024);
[5624]93  printf("read %d bytes\n", n);
94  if (n<0)
95    return -1;
96
[5996]97  printf("data: '%s'\n", buf);
[5624]98
[5996]99  printf("read from server2\n");
[6139]100  n = server2->readPacket((byte*)buf, 1024);
[5624]101  printf("read %d bytes\n", n);
102  if (n<0)
103    return -1;
104
[5996]105  printf("data: '%s'\n", buf);
[5624]106
[5996]107  printf("read from client1\n");
[6139]108  n = client1->readPacket((byte*)buf, 1024);
[5996]109  printf("read %d bytes\n", n);
110  if (n<0)
111    return -1;
[5628]112
[5996]113  printf("data: '%s'\n", buf);
[5628]114
[5996]115  printf("read from client2\n");
[6139]116  n = client2->readPacket((byte*)buf, 1024);
[5996]117  printf("read %d bytes\n", n);
118  if (n<0)
119    return -1;
[5628]120
[5996]121  printf("data: '%s'\n", buf);
[7954]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();
[5628]136
[7954]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
[5996]169  //sending bigger packets than 255 is not supported
[7954]170#if 0
[5996]171  printf("try to send more than 255 bytes\n");
[6139]172  printf("result: %d\n", client1->writePacket((byte*)buf, 1000));
[5628]173
[6139]174  server1->writePacket((byte*)str1, strlen(str1)+1);
[5996]175  SDL_Delay(500);
176  printf("try to read with a too small buffer\n");
[6139]177  printf("result: %d\n", client1->readPacket((byte*)buf, strlen(str1)));
[7954]178#endif
[5628]179
[7954]180  delete client1;
181  delete client2;
182  delete server1;
183  delete server2; 
184 
[5624]185  return 0;
186}
187
[5647]188
189int testFramework(int argc, char** argv)
190{
191  printf("=================\n");
192  printf("TestFramework\n");
193  printf("=================\n");
[5732]194
[7954]195  Synchronizeable* clientSync = new SimpleSync( std::string("Client") );
196  Synchronizeable* serverSync = new SimpleSync( std::string("Server") );
[5732]197
[5804]198  unsigned int port = 9999;
[5732]199
[5647]200  /* create the network manager */
[5996]201  NetworkManager* nm = NetworkManager::getInstance();
[5647]202
203  /* initialize the network manager */
204  nm->initialize();
205
[5649]206  /* create a server stream */
[6424]207  nm->createServer(port);
[5732]208
[5647]209  /* esatblish a connection */
[9347]210  IP ip;
[5804]211  int error = SDLNet_ResolveHost(&ip, "127.0.0.1", port);
212  //SDLNet_ResolveHost(&ip, "localhost", port);
[5996]213  if(error == -1)
214    printf("\n\nerror on address resolution, program inconsistancy\n\n");
[7954]215  nm->establishConnection("localhost", port);
216  nm->connectSynchronizeable( *clientSync );
[5806]217  /* adding some break for connection setup */
218  SDL_Delay(20);
[5649]219
220  /* synchronize the data 1 time (increment for longer tests) */
[5996]221  for( int i = 0; i < 3; i++)
222  {
[7954]223    nm->synchronize( 1000 );
[5805]224    /* simulate the network delay */
225    SDL_Delay(50);
[5649]226  }
[5732]227
[5798]228  printf("Test finished\n");
[5800]229
230
[5647]231  /* delete the network manager again */
232  delete nm;
[5732]233
[5808]234  delete clientSync;
235  delete serverSync;
236
[5647]237  return 0;
238}
239
[7954]240bool testCB( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
241{
242  printf("GOT MESSAGE: %s\n", data);
243  return true;
244}
[5647]245
[5579]246/**
247 *
[5811]248 * @param argc
249 * @param argv
250 * @return
251 */
252int startServer(int argc, char** argv)
253{
254
255
[7954]256  State::setOnline(true);
257  SharedNetworkData::getInstance()->setGameServer( true );
258  printf("Starting Server on port %i\n", 9999);
259
[5996]260  NetworkManager* netMan = NetworkManager::getInstance();
[7954]261 
262  netMan->initialize();
263 
264  netMan->createServer(/**ss, */ 9999);
265 
266  SimpleSync* ss = new SimpleSync("Server");
267  ss->setSynchronized( true );
268 
269  NetworkLog::getInstance()->listen( 8888 );
[5811]270
[7954]271  //MessageManager::getInstance()->initUser( 1 );
272  MessageManager::getInstance()->registerMessageHandler( TESTMESSAGEID, testCB, NULL );
273 
[5812]274  SDL_Delay(20);
[5811]275
[5996]276  for(;;)
277  {
[8623]278    MessageManager::getInstance()->sendMessage( TESTMESSAGEID, (byte*)"server to client", 18, RT_ALL_NOT_ME, 0, MP_HIGHBANDWIDTH );
[7954]279    netMan->synchronize( 100 );
280    SDL_Delay(100);
281    ss->debug();
[5812]282  }
[5996]283
284  delete netMan;
285  delete ss;
286
287
[5811]288  return 0;
289}
290
291
292int startClient(int argc, char** argv)
293{
294
295
[7954]296  State::setOnline(true);
[5996]297  NetworkManager* netMan = NetworkManager::getInstance();
[7954]298 
[5811]299
[7954]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->establishConnection(host, 9999);
310 
311//  SimpleSync* ss = new SimpleSync("SimpleSync");
312//  ss->setSynchronized( true );
313//  netMan->connectSynchronizeable( *ss );
314 
315  NetworkLog::getInstance()->listen( 7777 );
[5811]316
[7954]317  SimpleSync * ss = NULL;
318 
319  //MessageManager::getInstance()->initUser( 0 );
320  MessageManager::getInstance()->registerMessageHandler( TESTMESSAGEID, testCB, NULL );
321 
[5996]322  for(;;)
323  {
[7954]324    netMan->synchronize( 100 );
[8623]325    MessageManager::getInstance()->sendMessage( TESTMESSAGEID, (byte*)"client to server", 18, RT_ALL_NOT_ME, 0, MP_HIGHBANDWIDTH );
[7954]326    ss = dynamic_cast<SimpleSync*>(ClassList::getObject( "Server", CL_SIMPLE_SYNC ) );
327    SDL_Delay(100);
328    if (ss)
329      ss->debug();
[5812]330  }
[5811]331
332
[5996]333  delete netMan;
334  delete ss;
[5811]335
[5996]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
[9347]355  IP ip;
[5996]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();
[7954]361  Synchronizeable* ss = new SimpleSync("SimpleSync\0");
[5996]362
[7954]363  netMan->establishConnection( name, port );
364  netMan->connectSynchronizeable( *ss );
[5996]365
366  for(;;)
367  {
[7954]368    netMan->synchronize( 1000 );
[5996]369    SDL_Delay(10);
370  }
371
372
[5811]373  delete netMan;
374  delete ss;
375
376  return 0;
377}
378
[6341]379void testFloatConverter(float f)
380{
[7954]381#if 0
[6341]382  char* s = Converter::floatToBinString(f);
383  printf("%f = ", f);
384  printf(s); printf("\n");
[6424]385
[6341]386  byte* res = Converter::floatToByteArray(f);
387  printf("Byte Array: ");
388  for (int i = 0; i < 4; i++)
[7954]389//    printf("%i  ", res[i]);
[6341]390  printf("\n");
[6424]391
[6341]392  float b = Converter::byteArrayToFloat(res);
393  printf("ReConvert: %f \n", b);
[7954]394#endif
[6341]395}
[5811]396
[6341]397void testFloatConverter2(float f)
398{
[7954]399#if 0
[6341]400  char* s = Converter::floatToBinString(f);
401  printf("### %f = ", f);
402  printf(s); printf("\n");
[6424]403
[6341]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");
[6424]409
[6341]410  float b = Converter::_byteArrayToFloat(res);
411  printf("ReConvert: %f \n", b);
[7954]412#endif
[6341]413}
[6139]414int converter(int argc, char** argv)
415{
[6341]416  /*
[6139]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");
[6424]423
[6139]424  int z = Converter::byteArrayToInt(res);
[6424]425
[6139]426  printf("ReConvert: %i\n", z);
[5811]427
[6424]428
[6139]429  //float a = 5.4f;
430  //float b = 2.0f;
431  //printf("%f mod %f = %f", a, b, a % b);
[6424]432
[6139]433  printf("\n");
[6424]434
[6341]435  */
436  /*
[6139]437  float y;
438  char* s;
[6424]439
[6139]440  y = 12.0f;
441  s = Converter::floatToBinString(y);
442  printf("%f = ", y);
443  printf(s); printf("\n");
[6424]444
[6139]445  y = 24549026.0f;
446  s = Converter::floatToBinString(y);
447  printf("%f = ", y);
448  printf(s); printf("\n");
[6424]449
[6139]450  y = 12.4e20f;
451  s = Converter::floatToBinString(y);
452  printf("%f = ", y);
453  printf(s); printf("\n");
[6424]454
[6139]455  y = 4.7824f;
456  s = Converter::floatToBinString(y);
457  printf("%f = ", y);
458  printf(s); printf("\n");
[6424]459
[6341]460  y = -4.7824f;
461  s = Converter::floatToBinString(y);
462  printf("%f = ", y);
463  printf(s); printf("\n");
[6424]464
[6341]465  y = -14.35e14f;
466  s = Converter::floatToBinString(y);
467  printf("%f = ", y);
468  printf(s); printf("\n");
469                                                            */
[6424]470
471
[6341]472  /*
473  float a = 12.3f;
[6424]474
[6341]475  char* s = Converter::floatToBinString(a);
476  printf("%f = ", a);
477  printf(s); printf("\n");
[6424]478
[6341]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");
[6424]484
[6341]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");
[6424]491
[6341]492  testFloatConverter(12.3e-7f); printf("\n");
493  testFloatConverter(134.26455646546548741661675165f); printf("\n");
494  testFloatConverter(35.67e14f); printf("\n");
[6424]495
[6139]496  return 0;
497}
498
499
500
[5811]501/**
502 *
[5579]503 *  main function
504 *
505 * here the journey begins
506 */
507int main(int argc, char** argv)
508{
[5811]509  int i;
[5579]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;
[5996]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);
[6139]524    else if (! strcmp( "--converter", argv[i]) || !strcmp("-o", argv[i]))
525      return converter(argc, argv);
[5579]526  }
527
[5647]528  startHelp(argc, argv);
[5732]529
[5580]530  return 0;
[5579]531}
532
533
[7954]534//bool ShellBuffer::addBufferLineStatic(const char* line, ...)
535//{
[5747]536  //va_list arguments;
537  //vprintf(line, arguments);
[7954]538  //  printf("%s", line);
539//}
Note: See TracBrowser for help on using the repository browser.