Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5996 in orxonox.OLD for trunk/src/subprojects/network


Ignore:
Timestamp:
Dec 9, 2005, 12:31:01 PM (19 years ago)
Author:
patrick
Message:

orxonox/trunk: merged network branche into trunk with command svn merge -r 5824:HEAD

Location:
trunk/src/subprojects/network
Files:
4 edited
4 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/subprojects/network/Makefile.am

    r5822 r5996  
    1515network_SOURCES=  network_unit_test.cc \
    1616                  simple_sync.cc \
     17                  read_sync.cc \
     18                  write_sync.cc \
    1719                  \
    1820                  \
     
    2123                  $(MAINSRCDIR)/util/loading/load_param.cc \
    2224                  $(MAINSRCDIR)/lib/util/substring.cc \
    23                   $(MAINSRCDIR)/lib/util/helper_functions.cc
     25                  $(MAINSRCDIR)/util/loading/load_param_description.cc \
     26                  $(MAINSRCDIR)/lib/util/helper_functions.cc \
     27                  $(MAINSRCDIR)/lib/util/executor/executor.cc \
     28                  $(MAINSRCDIR)/lib/util/multi_type.cc
    2429
    2530
    2631
    2732noinst_HEADERS  =  network_unit_test.h \
    28                    simple_sync.h
     33                   simple_sync.h \
     34                   read_sync.h \
     35                   write_sync.h
    2936
  • trunk/src/subprojects/network/network_unit_test.cc

    r5822 r5996  
    88#include "network_manager.h"
    99#include "network_socket.h"
     10#include "server_socket.h"
    1011#include "network_stream.h"
    1112#include "synchronizeable.h"
    1213
    1314#include "simple_sync.h"
     15#include "read_sync.h"
    1416
    1517int verbose = 4;
     
    2527  printf(" --server [port number]     creates a test server\n");
    2628  printf(" --client [address] [port]  connects to a server\n");
     29  printf(" --listen [address] [port]  just listens to this connection");
    2730  printf("\n");
    2831}
     
    3437  IPaddress ip;
    3538  SDLNet_ResolveHost(&ip, "127.0.0.1", 9999);
    36   NetworkSocket server;
     39  ServerSocket server;
    3740  server.listen(9999);
    38   SDL_Delay(20);
    39 
    40   NetworkSocket client(ip);
     41
     42  NetworkSocket client1(ip);
     43
     44  NetworkSocket server1 = server.getNewSocket();
     45
     46  NetworkSocket client2(ip);
     47
     48  NetworkSocket server2 = server.getNewSocket();
    4149
    4250  char buf[1024];
    4351
    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));
     52  printf("read from client1 before sending data\n");
     53  printf("result: %d bytes\n", client1.readPacket((byte*)buf, 1024));
     54
     55  printf("read from client2 before sending data\n");
     56  printf("result: %d bytes\n", client2.readPacket((byte*)buf, 1024));
    4957
    5058  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);
     59  char * str1 = "client1 to server";
     60  char * str2 = "server1 to client";
     61  char * str3 = "client2 to server";
     62  char * str4 = "server2 to client";
     63  n = client1.writePacket((byte*)str1, strlen(str1)+1);
     64  printf("%d bytes send from client1\n", n);
     65  n = server1.writePacket((byte*)str2, strlen(str2)+1);
     66  printf("%d bytes send from server1\n", n);
     67  n = client2.writePacket((byte*)str3, strlen(str3)+1);
     68  printf("%d bytes send from client2\n", n);
     69  n = server2.writePacket((byte*)str4, strlen(str4)+1);
     70  printf("%d bytes send from server2\n", n);
    5771  SDL_Delay(1000);
    5872
    59   printf("read from server\n");
    60   n = server.readBytes((byte*)buf, 1024);
     73  printf("read from server1\n");
     74  n = server1.readPacket((byte*)buf, 1024);
    6175  printf("read %d bytes\n", n);
    6276  if (n<0)
    6377    return -1;
    6478
    65   printf("data: %s\n", buf);
    66   printf("read from client\n");
    67   n = client.readBytes((byte*)buf, 1024);
    68 
     79  printf("data: '%s'\n", buf);
     80
     81  printf("read from server2\n");
     82  n = server2.readPacket((byte*)buf, 1024);
    6983  printf("read %d bytes\n", n);
    7084  if (n<0)
    7185    return -1;
    7286
    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   }
     87  printf("data: '%s'\n", buf);
     88
     89  printf("read from client1\n");
     90  n = client1.readPacket((byte*)buf, 1024);
     91  printf("read %d bytes\n", n);
     92  if (n<0)
     93    return -1;
     94
     95  printf("data: '%s'\n", buf);
     96
     97  printf("read from client2\n");
     98  n = client2.readPacket((byte*)buf, 1024);
     99  printf("read %d bytes\n", n);
     100  if (n<0)
     101    return -1;
     102
     103  printf("data: '%s'\n", buf);
     104
     105  //sending bigger packets than 255 is not supported
     106
     107  printf("try to send more than 255 bytes\n");
     108  printf("result: %d\n", client1.writePacket((byte*)buf, 1000));
     109
     110  server1.writePacket((byte*)str1, strlen(str1)+1);
     111  SDL_Delay(500);
     112  printf("try to read with a too small buffer\n");
     113  printf("result: %d\n", client1.readPacket((byte*)buf, strlen(str1)));
    116114
    117115  return 0;
     
    131129
    132130  /* create the network manager */
    133   NetworkManager* nm = new NetworkManager();
     131  NetworkManager* nm = NetworkManager::getInstance();
    134132
    135133  /* initialize the network manager */
     
    143141  int error = SDLNet_ResolveHost(&ip, "127.0.0.1", port);
    144142  //SDLNet_ResolveHost(&ip, "localhost", port);
    145   if(error == -1) printf("\n\nerror on address resolution, program inconsistancy\n\n");
     143  if(error == -1)
     144    printf("\n\nerror on address resolution, program inconsistancy\n\n");
    146145  nm->establishConnection(ip, *clientSync);
    147146  /* adding some break for connection setup */
     
    149148
    150149  /* synchronize the data 1 time (increment for longer tests) */
    151   for( int i = 0; i < 3; i++) {
     150  for( int i = 0; i < 3; i++)
     151  {
    152152    nm->synchronize();
    153153    /* simulate the network delay */
     
    177177int startServer(int argc, char** argv)
    178178{
    179   if( argc <= 2) {
     179  if( argc <= 2)
     180  {
    180181    printf(" Wrong arguments try following notations:\n");
    181182    printf("   --server [port number]\n");
     
    186187  printf("Starting Server on port %i\n", port);
    187188
    188   NetworkManager* netMan = new NetworkManager();
     189  NetworkManager* netMan = NetworkManager::getInstance();
    189190  Synchronizeable* ss = new SimpleSync("Server\0");
    190191
    191   //NetworkStream* server = new NetworkStream(port, ss, NET_SERVER);
    192192  netMan->createServer(*ss, port);
    193193  SDL_Delay(20);
    194194
    195   for(;;) {
     195  for(;;)
     196  {
    196197    netMan->synchronize();
    197198    SDL_Delay(500);
    198199  }
     200
     201  delete netMan;
     202  delete ss;
     203
     204
    199205  return 0;
    200206}
     
    203209int startClient(int argc, char** argv)
    204210{
    205   if( argc < 3) {
     211  if( argc < 3)
     212  {
    206213    printf(" Wrong arguments try following notations:\n");
    207214    printf("   --client [server ip] [port number]\n");
    208     printf("   --server [server name] [port number]\n");
     215    printf("   --client [server name] [port number]\n");
    209216    return 0;
    210217  }
     
    216223  IPaddress ip;
    217224  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();
     225  if(error == -1)
     226    printf("\n\nerror on address resolution, program inconsistancy\n\n");
     227
     228  NetworkManager* netMan = NetworkManager::getInstance();
    222229  Synchronizeable* ss = new SimpleSync("Client\0");
    223230
    224231  netMan->establishConnection(ip, *ss);
    225232
    226   for(;;) {
     233  for(;;)
     234  {
    227235    netMan->synchronize();
    228236    SDL_Delay(500);
    229237  }
    230238
    231   //NetworkStream* client = new NetworkStream(ip, ss, NET_CLIENT);
     239
     240  delete netMan;
     241  delete ss;
     242
     243  return 0;
     244}
     245
     246
     247
     248int startListen(int argc, char** argv)
     249{
     250  if( argc < 3)
     251  {
     252    printf(" Wrong arguments try following notations:\n");
     253    printf("   --listen [server ip] [port number]\n");
     254    printf("   --listen [server name] [port number]\n");
     255    return 0;
     256  }
     257
     258  char* name = argv[2];
     259  int port = atoi(argv[3]);
     260  printf("Connecting to %s, on port %i\n", name, port);
     261
     262  IPaddress ip;
     263  int error = SDLNet_ResolveHost(&ip, name, port);
     264  if(error == -1)
     265    printf("\n\nerror on address resolution, program inconsistancy\n\n");
     266
     267  NetworkManager* netMan = NetworkManager::getInstance();
     268  Synchronizeable* ss = new ReadSync("WriteSync\0");
     269
     270  netMan->establishConnection(ip, *ss);
     271
     272  for(;;)
     273  {
     274    netMan->synchronize();
     275    SDL_Delay(10);
     276  }
    232277
    233278
     
    253298  {
    254299    //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);
     300    if (! strcmp( "--sockettest", argv[i]) || !strcmp("-st", argv[i]))
     301      return testSocket(argc, argv);
     302    else if (! strcmp( "--frameworktest", argv[i]) || !strcmp("-ft", argv[i]))
     303      return testFramework(argc, argv);
     304    else if (! strcmp( "--server", argv[i]) || !strcmp("-s", argv[i]))
     305      return startServer(argc, argv);
     306    else if (! strcmp( "--client", argv[i]) || !strcmp("-c", argv[i]))
     307      return startClient(argc, argv);
     308    else if (! strcmp( "--listen", argv[i]) || !strcmp("-l", argv[i]))
     309      return startListen(argc, argv);
    259310  }
    260311
     
    270321  //vprintf(line, arguments);
    271322  printf("%s", line);
    272 
    273 }
     323}
  • trunk/src/subprojects/network/simple_sync.cc

    r5822 r5996  
    8686 *  read data from Synchronizeable
    8787 */
    88 int SimpleSync::readBytes(byte* data) const
     88int SimpleSync::readBytes(byte* data)
    8989{
    9090  PRINTF(0)("SimpleSync: sent %i bytes of data\n", this->outLength);
  • trunk/src/subprojects/network/simple_sync.h

    r5822 r5996  
    1717
    1818    virtual void writeBytes(const byte* data, int length);
    19     virtual int readBytes(byte* data) const;
     19    virtual int readBytes(byte* data);
    2020
    2121
Note: See TracChangeset for help on using the changeset viewer.