Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

some more entities should sync now

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