Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

synchronisation should work now

File size: 11.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#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( std::string("Client") );
194  Synchronizeable* serverSync = new SimpleSync( std::string("Server") );
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");
261  ss->setSynchronized( true );
262
263 
264  SDL_Delay(20);
265
266  for(;;)
267  {
268    netMan->synchronize();
269    SDL_Delay(1000);
270    ss->debug();
271  }
272
273  delete netMan;
274  delete ss;
275
276
277  return 0;
278}
279
280
281int startClient(int argc, char** argv)
282{
283
284
285  State::setOnline(true);
286  NetworkManager* netMan = NetworkManager::getInstance();
287 
288
289  netMan->initialize();
290 
291 
292 
293  netMan->establishConnection("localhost", 9999);
294 
295//  SimpleSync* ss = new SimpleSync("SimpleSync");
296//  ss->setSynchronized( true );
297//  netMan->connectSynchronizeable( *ss );
298
299  SimpleSync * ss = NULL;
300 
301  for(;;)
302  {
303    netMan->synchronize();
304    ss = dynamic_cast<SimpleSync*>(ClassList::getObject( "Server", CL_SIMPLE_SYNC ) );
305    SDL_Delay(1000);
306    if (ss)
307      ss->debug();
308  }
309
310
311  delete netMan;
312  delete ss;
313
314  return 0;
315}
316
317
318
319int startListen(int argc, char** argv)
320{
321  if( argc < 3)
322  {
323    printf(" Wrong arguments try following notations:\n");
324    printf("   --listen [server ip] [port number]\n");
325    printf("   --listen [server name] [port number]\n");
326    return 0;
327  }
328
329  char* name = argv[2];
330  int port = atoi(argv[3]);
331  printf("Connecting to %s, on port %i\n", name, port);
332
333  IPaddress ip;
334  int error = SDLNet_ResolveHost(&ip, name, port);
335  if(error == -1)
336    printf("\n\nerror on address resolution, program inconsistancy\n\n");
337
338  NetworkManager* netMan = NetworkManager::getInstance();
339  Synchronizeable* ss = new SimpleSync("SimpleSync\0");
340
341  netMan->establishConnection( name, port );
342  netMan->connectSynchronizeable( *ss );
343
344  for(;;)
345  {
346    netMan->synchronize();
347    SDL_Delay(10);
348  }
349
350
351  delete netMan;
352  delete ss;
353
354  return 0;
355}
356
357void testFloatConverter(float f)
358{
359#if 0
360  char* s = Converter::floatToBinString(f);
361  printf("%f = ", f);
362  printf(s); printf("\n");
363
364  byte* res = Converter::floatToByteArray(f);
365  printf("Byte Array: ");
366  for (int i = 0; i < 4; i++)
367//    printf("%i  ", res[i]);
368  printf("\n");
369
370  float b = Converter::byteArrayToFloat(res);
371  printf("ReConvert: %f \n", b);
372#endif
373}
374
375void testFloatConverter2(float f)
376{
377#if 0
378  char* s = Converter::floatToBinString(f);
379  printf("### %f = ", f);
380  printf(s); printf("\n");
381
382  byte* res = Converter::_floatToByteArray(f);
383  printf("Byte Array: ");
384  for (int i = 0; i < 4; i++)
385    printf("%i  ", res[i]);
386  printf("\n");
387
388  float b = Converter::_byteArrayToFloat(res);
389  printf("ReConvert: %f \n", b);
390#endif
391}
392int converter(int argc, char** argv)
393{
394  /*
395  int x = 200564786;
396  printf("To convert: %i\n", x);
397  byte* res = Converter::intToByteArray(x);
398  for (int i = 0; i < 4; i++)
399    printf("%i  ", res[i]);
400  printf("\n");
401
402  int z = Converter::byteArrayToInt(res);
403
404  printf("ReConvert: %i\n", z);
405
406
407  //float a = 5.4f;
408  //float b = 2.0f;
409  //printf("%f mod %f = %f", a, b, a % b);
410
411  printf("\n");
412
413  */
414  /*
415  float y;
416  char* s;
417
418  y = 12.0f;
419  s = Converter::floatToBinString(y);
420  printf("%f = ", y);
421  printf(s); printf("\n");
422
423  y = 24549026.0f;
424  s = Converter::floatToBinString(y);
425  printf("%f = ", y);
426  printf(s); printf("\n");
427
428  y = 12.4e20f;
429  s = Converter::floatToBinString(y);
430  printf("%f = ", y);
431  printf(s); printf("\n");
432
433  y = 4.7824f;
434  s = Converter::floatToBinString(y);
435  printf("%f = ", y);
436  printf(s); printf("\n");
437
438  y = -4.7824f;
439  s = Converter::floatToBinString(y);
440  printf("%f = ", y);
441  printf(s); printf("\n");
442
443  y = -14.35e14f;
444  s = Converter::floatToBinString(y);
445  printf("%f = ", y);
446  printf(s); printf("\n");
447                                                            */
448
449
450  /*
451  float a = 12.3f;
452
453  char* s = Converter::floatToBinString(a);
454  printf("%f = ", a);
455  printf(s); printf("\n");
456
457  byte* res = Converter::floatToByteArray(a);
458  printf("Byte Array: \n");
459  for (int i = 0; i < 4; i++)
460    printf("%i  ", res[i]);
461  printf("\n");
462
463  float b = Converter::byteArrayToFloat(res);
464  printf("ReConvert: %f \n", b);
465  */
466//  testFloatConverter(12.3e-53f); printf("\n");
467//  testFloatConverter(134.26455646546548741661675165f); printf("\n");
468 // testFloatConverter(35.67e14f); printf("\n");
469
470  testFloatConverter(12.3e-7f); printf("\n");
471  testFloatConverter(134.26455646546548741661675165f); printf("\n");
472  testFloatConverter(35.67e14f); printf("\n");
473
474  return 0;
475}
476
477
478
479/**
480 *
481 *  main function
482 *
483 * here the journey begins
484 */
485int main(int argc, char** argv)
486{
487  int i;
488  // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark.
489  for(i = 1; i < argc; ++i)
490  {
491    //else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true;
492    if (! strcmp( "--sockettest", argv[i]) || !strcmp("-st", argv[i]))
493      return testSocket(argc, argv);
494    else if (! strcmp( "--frameworktest", argv[i]) || !strcmp("-ft", argv[i]))
495      return testFramework(argc, argv);
496    else if (! strcmp( "--server", argv[i]) || !strcmp("-s", argv[i]))
497      return startServer(argc, argv);
498    else if (! strcmp( "--client", argv[i]) || !strcmp("-c", argv[i]))
499      return startClient(argc, argv);
500    else if (! strcmp( "--listen", argv[i]) || !strcmp("-l", argv[i]))
501      return startListen(argc, argv);
502    else if (! strcmp( "--converter", argv[i]) || !strcmp("-o", argv[i]))
503      return converter(argc, argv);
504  }
505
506  startHelp(argc, argv);
507
508  return 0;
509}
510
511
512//bool ShellBuffer::addBufferLineStatic(const char* line, ...)
513//{
514  //va_list arguments;
515  //vprintf(line, arguments);
516  //  printf("%s", line);
517//}
Note: See TracBrowser for help on using the repository browser.