Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5798 was 5798, checked in by patrick, 18 years ago

network: testing env. got segfault in the code

File size: 4.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 "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, 9999);
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  IPaddress ip;
126  //SDLNet_ResolveHost(&ip, "127.0.0.1", 9999);
127  SDLNet_ResolveHost(&ip, "localhost", 9999);
128  Synchronizeable* clientSync = new SimpleSync();
129  Synchronizeable* serverSync = new SimpleSync();
130
131
132
133  /* create the network manager */
134  NetworkManager* nm = new NetworkManager();
135
136  /* initialize the network manager */
137  nm->initialize();
138
139  /* create a server stream */
140  nm->createServer(*serverSync, 9999);
141
142  /* esatblish a connection */
143  nm->establishConnection(ip, *clientSync);
144
145  /* synchronize the data 1 time (increment for longer tests) */
146  for( int i = 0; i < 3; i++) {
147    nm->synchronize();
148  }
149
150  printf("Test finished\n");
151 
152 
153  /* delete the network manager again */
154  delete nm;
155
156  return 0;
157}
158
159
160/**
161 *
162 *  main function
163 *
164 * here the journey begins
165 */
166int main(int argc, char** argv)
167{
168  // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark.
169  int i;
170  for(i = 1; i < argc; ++i)
171  {
172    //else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true;
173    if (! strcmp( "--sockettest", argv[i]) || !strcmp("-s", argv[i])) return testSocket(argc, argv);
174    else if (! strcmp( "--frameworktest", argv[i]) || !strcmp("-f", argv[i])) return testFramework(argc, argv);
175  }
176
177  startHelp(argc, argv);
178
179  return 0;
180}
181
182
183bool ShellBuffer::addBufferLineStatic(const char* line, ...)
184{
185  //va_list arguments;
186  //vprintf(line, arguments);
187  printf("%s", line);
188
189}
Note: See TracBrowser for help on using the repository browser.