Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

implemented class to log network things to telnet

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