Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7748 was 7748, checked in by rennerc, 18 years ago

implemented class to log network things to telnet

File size: 12.2 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"
[7550]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"
[7613]16#include "state.h"
17#include "shared_network_data.h"
[7681]18#include "message_manager.h"
[7748]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{
[7550]44  UdpServerSocket server(9999);
[5732]45
[7550]46  NetworkSocket* client1 = new UdpSocket("localhost", 9999);
[5732]47
[7553]48  NetworkSocket* server1 = NULL;
49  while ( server1 == NULL )
[7565]50  {
51    server.update();
[7553]52    server1 = server.getNewSocket();
[7565]53  }
[7553]54 
55  assert( server1->isOk() );
[5996]56
[7550]57  NetworkSocket* client2 = new UdpSocket("localhost", 9999);
[5996]58
[7553]59  NetworkSocket* server2 = NULL;
60  while ( server2 == NULL )
[7565]61  {
62    server.update();
[7553]63    server2 = server.getNewSocket();
[7565]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);
[7565]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);
[7565]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
[7565]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
[7556]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)));
[7556]178#endif
[5628]179
[7556]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
[7617]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 */
[5804]210  IPaddress ip;
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");
[7550]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  {
[7731]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
[7731]240bool testCB( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
[7681]241{
242  printf("GOT MESSAGE: %s\n", data);
[7731]243  return true;
[7681]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
[7613]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();
[7613]261 
262  netMan->initialize();
263 
264  netMan->createServer(/**ss, */ 9999);
265 
[7617]266  SimpleSync* ss = new SimpleSync("Server");
[7631]267  ss->setSynchronized( true );
[7748]268 
269  NetworkLog::getInstance()->listen( 8888 );
[5811]270
[7681]271  //MessageManager::getInstance()->initUser( 1 );
272  MessageManager::getInstance()->registerMessageHandler( TESTMESSAGEID, testCB, NULL );
[7613]273 
[5812]274  SDL_Delay(20);
[5811]275
[5996]276  for(;;)
277  {
[7681]278    MessageManager::getInstance()->sendMessage( TESTMESSAGEID, (byte*)"server to client", 18, RT_ALL, 0, MP_HIGHBANDWIDTH );
[7731]279    netMan->synchronize( 1000 );
[6139]280    SDL_Delay(1000);
[7613]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
[7613]296  State::setOnline(true);
[5996]297  NetworkManager* netMan = NetworkManager::getInstance();
[7613]298 
[5811]299
[7613]300  netMan->initialize();
301 
302 
303 
304  netMan->establishConnection("localhost", 9999);
305 
[7631]306//  SimpleSync* ss = new SimpleSync("SimpleSync");
307//  ss->setSynchronized( true );
308//  netMan->connectSynchronizeable( *ss );
[5811]309
[7631]310  SimpleSync * ss = NULL;
311 
[7681]312  //MessageManager::getInstance()->initUser( 0 );
313  MessageManager::getInstance()->registerMessageHandler( TESTMESSAGEID, testCB, NULL );
314 
[5996]315  for(;;)
316  {
[7731]317    netMan->synchronize( 1000 );
[7681]318    MessageManager::getInstance()->sendMessage( TESTMESSAGEID, (byte*)"client to server", 18, RT_ALL, 0, MP_HIGHBANDWIDTH );
[7631]319    ss = dynamic_cast<SimpleSync*>(ClassList::getObject( "Server", CL_SIMPLE_SYNC ) );
[7613]320    SDL_Delay(1000);
[7631]321    if (ss)
322      ss->debug();
[5812]323  }
[5811]324
325
[5996]326  delete netMan;
327  delete ss;
[5811]328
[5996]329  return 0;
330}
331
332
333
334int startListen(int argc, char** argv)
335{
336  if( argc < 3)
337  {
338    printf(" Wrong arguments try following notations:\n");
339    printf("   --listen [server ip] [port number]\n");
340    printf("   --listen [server name] [port number]\n");
341    return 0;
342  }
343
344  char* name = argv[2];
345  int port = atoi(argv[3]);
346  printf("Connecting to %s, on port %i\n", name, port);
347
348  IPaddress ip;
349  int error = SDLNet_ResolveHost(&ip, name, port);
350  if(error == -1)
351    printf("\n\nerror on address resolution, program inconsistancy\n\n");
352
353  NetworkManager* netMan = NetworkManager::getInstance();
[7613]354  Synchronizeable* ss = new SimpleSync("SimpleSync\0");
[5996]355
[7550]356  netMan->establishConnection( name, port );
357  netMan->connectSynchronizeable( *ss );
[5996]358
359  for(;;)
360  {
[7731]361    netMan->synchronize( 1000 );
[5996]362    SDL_Delay(10);
363  }
364
365
[5811]366  delete netMan;
367  delete ss;
368
369  return 0;
370}
371
[6341]372void testFloatConverter(float f)
373{
[7550]374#if 0
[6341]375  char* s = Converter::floatToBinString(f);
376  printf("%f = ", f);
377  printf(s); printf("\n");
[6424]378
[6341]379  byte* res = Converter::floatToByteArray(f);
380  printf("Byte Array: ");
381  for (int i = 0; i < 4; i++)
[7550]382//    printf("%i  ", res[i]);
[6341]383  printf("\n");
[6424]384
[6341]385  float b = Converter::byteArrayToFloat(res);
386  printf("ReConvert: %f \n", b);
[7550]387#endif
[6341]388}
[5811]389
[6341]390void testFloatConverter2(float f)
391{
[7550]392#if 0
[6341]393  char* s = Converter::floatToBinString(f);
394  printf("### %f = ", f);
395  printf(s); printf("\n");
[6424]396
[6341]397  byte* res = Converter::_floatToByteArray(f);
398  printf("Byte Array: ");
399  for (int i = 0; i < 4; i++)
400    printf("%i  ", res[i]);
401  printf("\n");
[6424]402
[6341]403  float b = Converter::_byteArrayToFloat(res);
404  printf("ReConvert: %f \n", b);
[7550]405#endif
[6341]406}
[6139]407int converter(int argc, char** argv)
408{
[6341]409  /*
[6139]410  int x = 200564786;
411  printf("To convert: %i\n", x);
412  byte* res = Converter::intToByteArray(x);
413  for (int i = 0; i < 4; i++)
414    printf("%i  ", res[i]);
415  printf("\n");
[6424]416
[6139]417  int z = Converter::byteArrayToInt(res);
[6424]418
[6139]419  printf("ReConvert: %i\n", z);
[5811]420
[6424]421
[6139]422  //float a = 5.4f;
423  //float b = 2.0f;
424  //printf("%f mod %f = %f", a, b, a % b);
[6424]425
[6139]426  printf("\n");
[6424]427
[6341]428  */
429  /*
[6139]430  float y;
431  char* s;
[6424]432
[6139]433  y = 12.0f;
434  s = Converter::floatToBinString(y);
435  printf("%f = ", y);
436  printf(s); printf("\n");
[6424]437
[6139]438  y = 24549026.0f;
439  s = Converter::floatToBinString(y);
440  printf("%f = ", y);
441  printf(s); printf("\n");
[6424]442
[6139]443  y = 12.4e20f;
444  s = Converter::floatToBinString(y);
445  printf("%f = ", y);
446  printf(s); printf("\n");
[6424]447
[6139]448  y = 4.7824f;
449  s = Converter::floatToBinString(y);
450  printf("%f = ", y);
451  printf(s); printf("\n");
[6424]452
[6341]453  y = -4.7824f;
454  s = Converter::floatToBinString(y);
455  printf("%f = ", y);
456  printf(s); printf("\n");
[6424]457
[6341]458  y = -14.35e14f;
459  s = Converter::floatToBinString(y);
460  printf("%f = ", y);
461  printf(s); printf("\n");
462                                                            */
[6424]463
464
[6341]465  /*
466  float a = 12.3f;
[6424]467
[6341]468  char* s = Converter::floatToBinString(a);
469  printf("%f = ", a);
470  printf(s); printf("\n");
[6424]471
[6341]472  byte* res = Converter::floatToByteArray(a);
473  printf("Byte Array: \n");
474  for (int i = 0; i < 4; i++)
475    printf("%i  ", res[i]);
476  printf("\n");
[6424]477
[6341]478  float b = Converter::byteArrayToFloat(res);
479  printf("ReConvert: %f \n", b);
480  */
481//  testFloatConverter(12.3e-53f); printf("\n");
482//  testFloatConverter(134.26455646546548741661675165f); printf("\n");
483 // testFloatConverter(35.67e14f); printf("\n");
[6424]484
[6341]485  testFloatConverter(12.3e-7f); printf("\n");
486  testFloatConverter(134.26455646546548741661675165f); printf("\n");
487  testFloatConverter(35.67e14f); printf("\n");
[6424]488
[6139]489  return 0;
490}
491
492
493
[5811]494/**
495 *
[5579]496 *  main function
497 *
498 * here the journey begins
499 */
500int main(int argc, char** argv)
501{
[5811]502  int i;
[5579]503  // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark.
504  for(i = 1; i < argc; ++i)
505  {
506    //else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true;
[5996]507    if (! strcmp( "--sockettest", argv[i]) || !strcmp("-st", argv[i]))
508      return testSocket(argc, argv);
509    else if (! strcmp( "--frameworktest", argv[i]) || !strcmp("-ft", argv[i]))
510      return testFramework(argc, argv);
511    else if (! strcmp( "--server", argv[i]) || !strcmp("-s", argv[i]))
512      return startServer(argc, argv);
513    else if (! strcmp( "--client", argv[i]) || !strcmp("-c", argv[i]))
514      return startClient(argc, argv);
515    else if (! strcmp( "--listen", argv[i]) || !strcmp("-l", argv[i]))
516      return startListen(argc, argv);
[6139]517    else if (! strcmp( "--converter", argv[i]) || !strcmp("-o", argv[i]))
518      return converter(argc, argv);
[5579]519  }
520
[5647]521  startHelp(argc, argv);
[5732]522
[5580]523  return 0;
[5579]524}
525
526
[7552]527//bool ShellBuffer::addBufferLineStatic(const char* line, ...)
528//{
[5747]529  //va_list arguments;
530  //vprintf(line, arguments);
[7552]531  //  printf("%s", line);
532//}
Note: See TracBrowser for help on using the repository browser.