Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

network: finished the simple_sync program

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