Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

network: modiefied the unit test to enable diffrent modes, extended the NetworkStream constructors interface and NetworkManager interface

File size: 3.9 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 "synchronizeable.h"
11
12int verbose = 4;
13
14
15/* outputs the help */
16int startHelp(int argc, char** argv)
17{
18  printf("Network is a network unit test\n");
19  printf(" --help:           this output\n");
20  printf(" --sockettest      test network_socket\n");
21  printf(" --frameworktest   test the network module\n");
22  printf("\n");
23}
24
25
26/* test SDL network socket */
27int testSocket(int argc, char** argv)
28{
29  NetworkSocket client;
30  NetworkSocket server;
31  IPaddress ip;
32  SDLNet_ResolveHost(&ip, "127.0.0.1", 9999);
33  server.listen(9999);
34  SDL_Delay(20);
35  client.connectToServer(ip, 9999);
36  char buf[1024];
37
38  printf("read from client before sending data\n");
39  printf("result: %d bytes\n", client.readBytes((byte*)buf, 1024));
40
41  printf("read from server before sending data\n");
42  printf("result: %d bytes\n", server.readBytes((byte*)buf, 1024));
43
44  int n;
45  char * str1 = "client to server";
46  char * str2 = "server to client";
47  n = client.writeBytes((byte*)str1, strlen(str1)+1);
48  printf("%d bytes send from client\n", n);
49  n = server.writeBytes((byte*)str2, strlen(str2)+1);
50  printf("%d bytes send from server\n", n);
51  SDL_Delay(1000);
52
53  printf("read from server\n");
54  n = server.readBytes((byte*)buf, 1024);
55  printf("read %d bytes\n", n);
56  if (n<0)
57    return -1;
58
59  printf("data: %s\n", buf);
60  printf("read from client\n");
61  n = client.readBytes((byte*)buf, 1024);
62
63  printf("read %d bytes\n", n);
64  if (n<0)
65    return -1;
66
67  printf("data: %s\n", buf);
68
69  printf("testing a bigger amount of data\n");
70
71#define _N_ELEMENTS 212994
72  char sendbuf[_N_ELEMENTS+1];
73  char recvbuf[_N_ELEMENTS+1];
74  sendbuf[_N_ELEMENTS] = '\0';
75  recvbuf[_N_ELEMENTS] = '\0';
76
77  for (int i = 0; i<_N_ELEMENTS; i++)
78    sendbuf[i] = i%26 + 65;
79
80  printf("write\n");
81  printf("result = %d\n", client.writeBytes((byte*)sendbuf, _N_ELEMENTS));
82
83  SDL_Delay(50);
84
85  printf("read\n");
86  int nbytes = server.readBytes((byte*)recvbuf, _N_ELEMENTS);
87  int offset = nbytes;
88
89  while (nbytes>0)
90  {
91    SDL_Delay(10);
92    //printf("read\n");
93    nbytes = server.readBytes((byte*)recvbuf+offset, _N_ELEMENTS-offset);
94    offset += nbytes;
95    //printf("nbytes=%d, offset=%d\n", nbytes, offset);
96  }
97
98  printf("strcmp = %d (0 is good :D not 0 is evil)\noffset = %d\n", strncmp(sendbuf, recvbuf, _N_ELEMENTS), offset);
99
100  //printf("%s\n%s\n", sendbuf, recvbuf);
101
102  for (int i = 0; i<_N_ELEMENTS; i++)
103  {
104    if (sendbuf[i]!=recvbuf[i])
105    {
106      printf("byte %d is the first difference\n", i+1);
107      break;
108    }
109  }
110
111  return 0;
112}
113
114
115int testFramework(int argc, char** argv)
116{
117  printf("=================\n");
118  printf("TestFramework\n");
119  printf("=================\n");
120 
121  IPaddress ip;
122  //SDLNet_ResolveHost(&ip, "127.0.0.1", 9999);
123  SDLNet_ResolveHost(&ip, "localhost", 9999);
124  Synchronizeable sync;
125 
126  /* create the network manager */
127  NetworkManager* nm = new NetworkManager();
128
129  /* initialize the network manager */
130  nm->initialize();
131
132  /* esatblish a connection */
133  nm->establishConnection(ip, sync);
134 
135  /* delete the network manager again */
136  delete nm;
137 
138  return 0;
139}
140
141
142/**
143 *
144 *  main function
145 *
146 * here the journey begins
147 */
148int main(int argc, char** argv)
149{
150  // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark.
151  int i;
152  for(i = 1; i < argc; ++i)
153  {
154    //else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true;
155    if (! strcmp( "--sockettest", argv[i]) || !strcmp("-s", argv[i])) return testSocket(argc, argv);
156    else if (! strcmp( "--frameworktest", argv[i]) || !strcmp("-s", argv[i])) return testFramework(argc, argv);
157  }
158
159  startHelp(argc, argv);
160 
161  return 0;
162}
163
164
165bool ShellBuffer::addBufferLineStatic(const char* line, ...)
166{
167  va_list arguments;
168  vprintf(line, arguments);
169
170}
Note: See TracBrowser for help on using the repository browser.