Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

subprojects/network uses should test net network sockets now

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