Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed some bugs

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