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