Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/subprojects/network/network_unit_test.cc @ 6322

Last change on this file since 6322 was 6139, checked in by patrick, 19 years ago

trunk: merged branche network with trunk using command: svn merge -r5999:HEAD, conflicts resolved in favor of the trunk bla

File size: 8.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 "network_stream.h"
12#include "synchronizeable.h"
13#include "converter.h"
14
15#include "simple_sync.h"
16#include "read_sync.h"
17
18int verbose = 4;
19
20
21/* outputs the help */
22int startHelp(int argc, char** argv)
23{
24  printf("Network is a network unit test\n");
25  printf(" --help                     this output\n");
26  printf(" -st,  --sockettest         test network_socket\n");
27  printf(" -ft,  --frameworktest      test the network module\n");
28  printf(" --server [port number]     creates a test server\n");
29  printf(" --client [address] [port]  connects to a server\n");
30  printf(" --listen [address] [port]  just listens to this connection");
31  printf("\n");
32}
33
34
35/* test SDL network socket */
36int testSocket(int argc, char** argv)
37{
38  IPaddress ip;
39  SDLNet_ResolveHost(&ip, "127.0.0.1", 9999);
40  ServerSocket server;
41  server.listen(9999);
42
43  NetworkSocket* client1 = new NetworkSocket(ip);
44
45  NetworkSocket* server1 = server.getNewSocket();
46
47  NetworkSocket* client2 = new NetworkSocket(ip);
48
49  NetworkSocket* server2 = server.getNewSocket();
50
51  char buf[1024];
52
53  printf("read from client1 before sending data\n");
54  printf("result: %d bytes\n", client1->readPacket((byte*)buf, 1024));
55
56  printf("read from client2 before sending data\n");
57  printf("result: %d bytes\n", client2->readPacket((byte*)buf, 1024));
58
59  int n;
60  char * str1 = "client1 to server";
61  char * str2 = "server1 to client";
62  char * str3 = "client2 to server";
63  char * str4 = "server2 to client";
64  n = client1->writePacket((byte*)str1, strlen(str1)+1);
65  printf("%d bytes send from client1\n", n);
66  n = server1->writePacket((byte*)str2, strlen(str2)+1);
67  printf("%d bytes send from server1\n", n);
68  n = client2->writePacket((byte*)str3, strlen(str3)+1);
69  printf("%d bytes send from client2\n", n);
70  n = server2->writePacket((byte*)str4, strlen(str4)+1);
71  printf("%d bytes send from server2\n", n);
72  SDL_Delay(1000);
73
74  printf("read from server1\n");
75  n = server1->readPacket((byte*)buf, 1024);
76  printf("read %d bytes\n", n);
77  if (n<0)
78    return -1;
79
80  printf("data: '%s'\n", buf);
81
82  printf("read from server2\n");
83  n = server2->readPacket((byte*)buf, 1024);
84  printf("read %d bytes\n", n);
85  if (n<0)
86    return -1;
87
88  printf("data: '%s'\n", buf);
89
90  printf("read from client1\n");
91  n = client1->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 client2\n");
99  n = client2->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  //sending bigger packets than 255 is not supported
107
108  printf("try to send more than 255 bytes\n");
109  printf("result: %d\n", client1->writePacket((byte*)buf, 1000));
110
111  server1->writePacket((byte*)str1, strlen(str1)+1);
112  SDL_Delay(500);
113  printf("try to read with a too small buffer\n");
114  printf("result: %d\n", client1->readPacket((byte*)buf, strlen(str1)));
115
116  return 0;
117}
118
119
120int testFramework(int argc, char** argv)
121{
122  printf("=================\n");
123  printf("TestFramework\n");
124  printf("=================\n");
125
126  Synchronizeable* clientSync = new SimpleSync("Client\0");
127  Synchronizeable* serverSync = new SimpleSync("Server\0");
128
129  unsigned int port = 9999;
130
131  /* create the network manager */
132  NetworkManager* nm = NetworkManager::getInstance();
133
134  /* initialize the network manager */
135  nm->initialize();
136
137  /* create a server stream */
138  nm->createServer(*serverSync, port);
139
140  /* esatblish a connection */
141  IPaddress ip;
142  int error = SDLNet_ResolveHost(&ip, "127.0.0.1", port);
143  //SDLNet_ResolveHost(&ip, "localhost", port);
144  if(error == -1)
145    printf("\n\nerror on address resolution, program inconsistancy\n\n");
146  nm->establishConnection(ip, *clientSync);
147  /* adding some break for connection setup */
148  SDL_Delay(20);
149
150  /* synchronize the data 1 time (increment for longer tests) */
151  for( int i = 0; i < 3; i++)
152  {
153    nm->synchronize();
154    /* simulate the network delay */
155    SDL_Delay(50);
156  }
157
158  printf("Test finished\n");
159
160
161  /* delete the network manager again */
162  delete nm;
163
164  delete clientSync;
165  delete serverSync;
166
167  return 0;
168}
169
170
171
172/**
173 *
174 * @param argc
175 * @param argv
176 * @return
177 */
178int startServer(int argc, char** argv)
179{
180  if( argc <= 2)
181  {
182    printf(" Wrong arguments try following notations:\n");
183    printf("   --server [port number]\n");
184    return 0;
185  }
186
187  int port = atoi(argv[2]);
188  printf("Starting Server on port %i\n", port);
189
190  NetworkManager* netMan = NetworkManager::getInstance();
191  Synchronizeable* ss = new SimpleSync("Server\0");
192
193  netMan->createServer(/**ss, */port);
194  SDL_Delay(20);
195
196  for(;;)
197  {
198    netMan->synchronize();
199    SDL_Delay(1000);
200  }
201
202  delete netMan;
203  delete ss;
204
205
206  return 0;
207}
208
209
210int startClient(int argc, char** argv)
211{
212  if( argc < 3)
213  {
214    printf(" Wrong arguments try following notations:\n");
215    printf("   --client [server ip] [port number]\n");
216    printf("   --client [server name] [port number]\n");
217    return 0;
218  }
219
220  char* name = argv[2];
221  int port = atoi(argv[3]);
222  printf("Connecting to %s, on port %i\n", name, port);
223
224  IPaddress ip;
225  int error = SDLNet_ResolveHost(&ip, name, port);
226  if(error == -1)
227    printf("\n\nerror on address resolution, program inconsistancy\n\n");
228
229  NetworkManager* netMan = NetworkManager::getInstance();
230  Synchronizeable* ss = new SimpleSync("Client\0");
231
232  netMan->establishConnection((const char*)"localhost", port/*,ip, *ss*/);
233
234  for(;;)
235  {
236    netMan->synchronize();
237    SDL_Delay(500);
238  }
239
240
241  delete netMan;
242  delete ss;
243
244  return 0;
245}
246
247
248
249int startListen(int argc, char** argv)
250{
251  if( argc < 3)
252  {
253    printf(" Wrong arguments try following notations:\n");
254    printf("   --listen [server ip] [port number]\n");
255    printf("   --listen [server name] [port number]\n");
256    return 0;
257  }
258
259  char* name = argv[2];
260  int port = atoi(argv[3]);
261  printf("Connecting to %s, on port %i\n", name, port);
262
263  IPaddress ip;
264  int error = SDLNet_ResolveHost(&ip, name, port);
265  if(error == -1)
266    printf("\n\nerror on address resolution, program inconsistancy\n\n");
267
268  NetworkManager* netMan = NetworkManager::getInstance();
269  Synchronizeable* ss = new ReadSync("WriteSync\0");
270
271  netMan->establishConnection(ip, *ss);
272
273  for(;;)
274  {
275    netMan->synchronize();
276    SDL_Delay(10);
277  }
278
279
280  delete netMan;
281  delete ss;
282
283  return 0;
284}
285
286
287int converter(int argc, char** argv)
288{
289  int x = 200564786;
290  printf("To convert: %i\n", x);
291  byte* res = Converter::intToByteArray(x);
292  for (int i = 0; i < 4; i++)
293    printf("%i  ", res[i]);
294  printf("\n");
295 
296  int z = Converter::byteArrayToInt(res);
297 
298  printf("ReConvert: %i\n", z);
299
300 
301  //float a = 5.4f;
302  //float b = 2.0f;
303  //printf("%f mod %f = %f", a, b, a % b);
304 
305  printf("\n");
306 
307  float y;
308  char* s;
309 
310  y = 12.0f;
311  s = Converter::floatToBinString(y);
312  printf("%f = ", y);
313  printf(s); printf("\n");
314 
315  y = 24549026.0f;
316  s = Converter::floatToBinString(y);
317  printf("%f = ", y);
318  printf(s); printf("\n");
319 
320  y = 12.4e20f;
321  s = Converter::floatToBinString(y);
322  printf("%f = ", y);
323  printf(s); printf("\n");
324 
325  y = 4.7824f;
326  s = Converter::floatToBinString(y);
327  printf("%f = ", y);
328  printf(s); printf("\n");
329 
330 
331 
332  return 0;
333}
334
335
336
337/**
338 *
339 *  main function
340 *
341 * here the journey begins
342 */
343int main(int argc, char** argv)
344{
345  int i;
346  // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark.
347  for(i = 1; i < argc; ++i)
348  {
349    //else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true;
350    if (! strcmp( "--sockettest", argv[i]) || !strcmp("-st", argv[i]))
351      return testSocket(argc, argv);
352    else if (! strcmp( "--frameworktest", argv[i]) || !strcmp("-ft", argv[i]))
353      return testFramework(argc, argv);
354    else if (! strcmp( "--server", argv[i]) || !strcmp("-s", argv[i]))
355      return startServer(argc, argv);
356    else if (! strcmp( "--client", argv[i]) || !strcmp("-c", argv[i]))
357      return startClient(argc, argv);
358    else if (! strcmp( "--listen", argv[i]) || !strcmp("-l", argv[i]))
359      return startListen(argc, argv);
360    else if (! strcmp( "--converter", argv[i]) || !strcmp("-o", argv[i]))
361      return converter(argc, argv);
362  }
363
364  startHelp(argc, argv);
365
366  return 0;
367}
368
369
370bool ShellBuffer::addBufferLineStatic(const char* line, ...)
371{
372  //va_list arguments;
373  //vprintf(line, arguments);
374  printf("%s", line);
375}
Note: See TracBrowser for help on using the repository browser.