Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed bug

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