Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

synchronisation should work now

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