Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed some bugs in udpSockets. subprojects/network should work now

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