Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

network: again some changes in the functino arguments order, more test abilities in the network subproject

File size: 6.0 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 "network_stream.h"
11#include "synchronizeable.h"
12
13#include "simple_sync.h"
14
15int verbose = 4;
16
17
18/* outputs the help */
19int startHelp(int argc, char** argv)
20{
21  printf("Network is a network unit test\n");
22  printf(" --help:           this output\n");
23  printf(" --sockettest      test network_socket\n");
24  printf(" --frameworktest   test the network module\n");
25  printf("\n");
26}
27
28
29/* test SDL network socket */
30int testSocket(int argc, char** argv)
31{
32  IPaddress ip;
33  SDLNet_ResolveHost(&ip, "127.0.0.1", 9999);
34  NetworkSocket server;
35  server.listen(9999);
36  SDL_Delay(20);
37
38  NetworkSocket client(ip);
39
40  char buf[1024];
41
42  printf("read from client before sending data\n");
43  printf("result: %d bytes\n", client.readBytes((byte*)buf, 1024));
44
45  printf("read from server before sending data\n");
46  printf("result: %d bytes\n", server.readBytes((byte*)buf, 1024));
47
48  int n;
49  char * str1 = "client to server";
50  char * str2 = "server to client";
51  n = client.writeBytes((byte*)str1, strlen(str1)+1);
52  printf("%d bytes send from client\n", n);
53  n = server.writeBytes((byte*)str2, strlen(str2)+1);
54  printf("%d bytes send from server\n", n);
55  SDL_Delay(1000);
56
57  printf("read from server\n");
58  n = server.readBytes((byte*)buf, 1024);
59  printf("read %d bytes\n", n);
60  if (n<0)
61    return -1;
62
63  printf("data: %s\n", buf);
64  printf("read from client\n");
65  n = client.readBytes((byte*)buf, 1024);
66
67  printf("read %d bytes\n", n);
68  if (n<0)
69    return -1;
70
71  printf("data: %s\n", buf);
72
73  printf("testing a bigger amount of data\n");
74
75#define _N_ELEMENTS 212994
76  char sendbuf[_N_ELEMENTS+1];
77  char recvbuf[_N_ELEMENTS+1];
78  sendbuf[_N_ELEMENTS] = '\0';
79  recvbuf[_N_ELEMENTS] = '\0';
80
81  for (int i = 0; i<_N_ELEMENTS; i++)
82    sendbuf[i] = i%26 + 65;
83
84  printf("write\n");
85  printf("result = %d\n", client.writeBytes((byte*)sendbuf, _N_ELEMENTS));
86
87  SDL_Delay(50);
88
89  printf("read\n");
90  int nbytes = server.readBytes((byte*)recvbuf, _N_ELEMENTS);
91  int offset = nbytes;
92
93  while (nbytes>0)
94  {
95    SDL_Delay(10);
96    //printf("read\n");
97    nbytes = server.readBytes((byte*)recvbuf+offset, _N_ELEMENTS-offset);
98    offset += nbytes;
99    //printf("nbytes=%d, offset=%d\n", nbytes, offset);
100  }
101
102  printf("strcmp = %d (0 is good :D not 0 is evil)\noffset = %d\n", strncmp(sendbuf, recvbuf, _N_ELEMENTS), offset);
103
104  //printf("%s\n%s\n", sendbuf, recvbuf);
105
106  for (int i = 0; i<_N_ELEMENTS; i++)
107  {
108    if (sendbuf[i]!=recvbuf[i])
109    {
110      printf("byte %d is the first difference\n", i+1);
111      break;
112    }
113  }
114
115  return 0;
116}
117
118
119int testFramework(int argc, char** argv)
120{
121  printf("=================\n");
122  printf("TestFramework\n");
123  printf("=================\n");
124
125  Synchronizeable* clientSync = new SimpleSync("Client\0");
126  Synchronizeable* serverSync = new SimpleSync("Server\0");
127
128  unsigned int port = 9999;
129
130  /* create the network manager */
131  NetworkManager* nm = new NetworkManager();
132
133  /* initialize the network manager */
134  nm->initialize();
135
136  /* create a server stream */
137  nm->createServer(*serverSync, port);
138
139  /* esatblish a connection */
140  IPaddress ip;
141  int error = SDLNet_ResolveHost(&ip, "127.0.0.1", port);
142  //SDLNet_ResolveHost(&ip, "localhost", port);
143  if(error == -1) printf("\n\nerror on address resolution, program inconsistancy\n\n");
144  nm->establishConnection(ip, *clientSync);
145  /* adding some break for connection setup */
146  SDL_Delay(20);
147
148  /* synchronize the data 1 time (increment for longer tests) */
149  for( int i = 0; i < 3; i++) {
150    nm->synchronize();
151    /* simulate the network delay */
152    SDL_Delay(50);
153  }
154
155  printf("Test finished\n");
156
157
158  /* delete the network manager again */
159  delete nm;
160
161  delete clientSync;
162  delete serverSync;
163
164  return 0;
165}
166
167
168
169/**
170 *
171 * @param argc
172 * @param argv
173 * @return
174 */
175int startServer(int argc, char** argv)
176{
177  if( argc <= 2) {
178    printf(" Wrong arguments try following notations:\n");
179    printf("   --server [port number]\n");
180    return 0;
181  }
182
183  int port = atoi(argv[2]);
184  printf("Starting Server on port %i\n", port);
185
186  NetworkManager* netMan = new NetworkManager();
187  Synchronizeable* ss = new SimpleSync("Server\0");
188
189  //NetworkStream* server = new NetworkStream(port, ss, NET_SERVER);
190  netMan->createServer(*ss, port);
191
192  return 0;
193}
194
195
196int startClient(int argc, char** argv)
197{
198  if( argc < 3) {
199    printf(" Wrong arguments try following notations:\n");
200    printf("   --client [server ip] [port number]\n");
201    printf("   --server [server name] [port number]\n");
202    return 0;
203  }
204
205  char* name = argv[2];
206  int port = atoi(argv[3]);
207  printf("Connecting to %s, on port %i\n", name, port);
208
209  IPaddress ip;
210  int error = SDLNet_ResolveHost(&ip, name, port);
211  //SDLNet_ResolveHost(&ip, "localhost", port);
212  if(error == -1) printf("\n\nerror on address resolution, program inconsistancy\n\n");
213
214  NetworkManager* netMan = new NetworkManager();
215  Synchronizeable* ss = new SimpleSync("Client\0");
216
217  netMan->establishConnection(ip, *ss);
218
219
220  //NetworkStream* client = new NetworkStream(ip, ss, NET_CLIENT);
221
222
223  delete netMan;
224  delete ss;
225
226  return 0;
227}
228
229
230
231/**
232 *
233 *  main function
234 *
235 * here the journey begins
236 */
237int main(int argc, char** argv)
238{
239  int i;
240  // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark.
241  for(i = 1; i < argc; ++i)
242  {
243    //else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true;
244    if (! strcmp( "--sockettest", argv[i]) || !strcmp("-st", argv[i])) return testSocket(argc, argv);
245    else if (! strcmp( "--frameworktest", argv[i]) || !strcmp("-ft", argv[i])) return testFramework(argc, argv);
246    else if (! strcmp( "--server", argv[i]) || !strcmp("-s", argv[i])) return startServer(argc, argv);
247    else if (! strcmp( "--client", argv[i]) || !strcmp("-c", argv[i])) return startClient(argc, argv);
248  }
249
250  startHelp(argc, argv);
251
252  return 0;
253}
254
255
256bool ShellBuffer::addBufferLineStatic(const char* line, ...)
257{
258  //va_list arguments;
259  //vprintf(line, arguments);
260  printf("%s", line);
261
262}
Note: See TracBrowser for help on using the repository browser.