Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5732 was 5732, checked in by rennerc, 19 years ago

network_socket: added constructor which connects directly

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