Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

reimplemented NetworkStream

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