Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

subprojects/network can test handshake now

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