Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 1008


Ignore:
Timestamp:
Apr 10, 2008, 12:55:35 PM (16 years ago)
Author:
dumenim
Message:

changed some comments and catched some return values and maybe some stuff we have to unchange

Location:
code/branches/network/src/network
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • code/branches/network/src/network/Client.cc

    r913 r1008  
    204204    if(id!=NULL)
    205205      id->setNetworkID(clid->clid);
    206     COUT(4) << "received and set network id: " << clid->clid << "; classname: " << clid->message << std::endl;
     206    COUT(4) << "Client: received and set network id: " << clid->clid << "; classname: " << clid->message << std::endl;
    207207    return;
    208208  }
  • code/branches/network/src/network/ClientInformation.cc

    r790 r1008  
    189189  * This function should only be applied to the head of the list
    190190  * @param clientID id to look for
    191   * @return pointer to the element in the list or 0 if the search was unsuccessfull
     191  * @return pointer to the last element in the list or 0 if the search was unsuccessfull
    192192  */
    193193  ClientInformation *ClientInformation::findClient(int clientID, bool look_backwards) {
     
    195195    if (temp->head)
    196196      temp=temp->next();
    197     while(temp!=0 && temp->getID()!=clientID){
     197    //bugfix: temp to temp->next(), get last elem if not found, not segflt
     198    while(temp->next()!=0 && temp->getID()!=clientID){
    198199      temp = temp->next();
    199200    }
     
    210211  ClientInformation *ClientInformation::findClient(ENetAddress *address, bool look_backwards) {
    211212    ClientInformation *temp = this;
    212     while(temp!=0){
     213    //bugfix: temp to temp->next(), get last elem if not found, not segflt
     214    while(temp->next()!=0){
    213215      if(temp->head){
    214216        temp = temp->next();
  • code/branches/network/src/network/ClientInformation.h

    r790 r1008  
    4747    bool removeClient(int clientID);
    4848    bool removeClient(ENetPeer *peer);
     49    //## add bool mask-function eventually
    4950    ClientInformation *findClient(int clientID, bool look_backwards=false);
     51    //## add bool mask-function eventually
    5052    ClientInformation *findClient(ENetAddress *address, bool look_backwards=false);
    5153
  • code/branches/network/src/network/ConnectionManager.cc

    r988 r1008  
    5959{
    6060  boost::thread_group network_threads;
    61 
     61 
     62  ConnectionManager::ConnectionManager(){}
     63 
    6264  ConnectionManager::ConnectionManager(ClientInformation *head) {
    6365    quit=false;
     
    8789      return NULL;
    8890  }
    89 
     91/**
     92This function only pops the first element in PacketBuffer (first in first out)
     93used by processQueue in Server.cc
     94*/
    9095  ENetPacket *ConnectionManager::getPacket(int &clientID) {
    9196    ENetAddress address;
     
    173178        case ENET_EVENT_TYPE_CONNECT:
    174179          addClient(&event);
     180          COUT(5) << "Con.Man: connection event has occured" << std::endl;
    175181          break;
    176182        case ENET_EVENT_TYPE_RECEIVE:
    177183          //std::cout << "received data" << std::endl;
     184          COUT(5) << "Con.Man: receive event has occured" << std::endl;
    178185          processData(&event);
    179186          break;
     
    190197    enet_host_destroy(server);
    191198  }
    192 
     199 
     200  //### added some bugfixes here, but we cannot test them because
     201  //### the server crashes everytime because of some gamestates
     202  //### (trying to resolve that now)
    193203  void ConnectionManager::disconnectClients() {
    194204    ENetEvent event;
     
    198208      temp = temp->next();
    199209    }
    200     temp = temp->next();
     210    //bugfix: might be the reason why server crashes when clients disconnects
     211    //temp = temp->next();
     212    temp = head_->next();
    201213    while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){
    202214      switch (event.type)
    203215      {
    204       case ENET_EVENT_TYPE_NONE:
    205       case ENET_EVENT_TYPE_CONNECT:
     216      case ENET_EVENT_TYPE_NONE: break;
     217      case ENET_EVENT_TYPE_CONNECT: break;
    206218      case ENET_EVENT_TYPE_RECEIVE:
    207219        enet_packet_destroy(event.packet);
    208220        break;
    209221      case ENET_EVENT_TYPE_DISCONNECT:
    210         COUT(4) << "disconnecting client" << std::endl;
     222        COUT(4) << "disconnecting all clients" << std::endl;
    211223        delete head_->findClient(&(event.peer->address));
     224        //maybe needs bugfix: might also be a reason for the server to crash
    212225        temp = temp->next();
    213226        break;
     
    231244    return head_->removeClient(peer);
    232245  }
    233 
     246/**
     247This function adds a client that connects to the clientlist of the server
     248NOTE: if you change this, don't forget to change the test function
     249addClientTest in diffTest.cc since addClient is not good for testing because of syncClassid
     250*/
    234251  bool ConnectionManager::addClient(ENetEvent *event) {
    235252    ClientInformation *temp = head_->insertBack(new ClientInformation);
    236     if(temp->prev()->head)
     253    if(temp->prev()->head) { //not good if you use anything else than insertBack
     254      temp->prev()->setID(0); //bugfix: not necessary but usefull
    237255      temp->setID(1);
     256    }
    238257    else
    239258      temp->setID(temp->prev()->getID()+1);
    240259    temp->setPeer(event->peer);
    241     std::cout << "added client id: " << temp->getID() << std::endl;
     260    COUT(4) << "Con.Man: added client id: " << temp->getID() << std::endl;
    242261    syncClassid(temp->getID());
    243262    temp->setSynched(true);
     
    268287      classname = id->getName();
    269288      network_id = id->getNetworkID();
    270       COUT(4) << "network_id: " << network_id << ", classname: " << classname << std::endl;
     289      COUT(4) << "Con.Man:syncClassid:\tnetwork_id: " << network_id << ", classname: " << classname << std::endl;
    271290     
    272291      addPacket(packet_gen.clid( (int)network_id, classname ), clientID);
     
    275294    }
    276295    sendPackets();
     296    COUT(4) << "syncClassid:\tall synchClassID packets have been sent" << std::endl;
    277297  }
    278298
     
    289309
    290310  int ConnectionManager::getObjectsClientID( int objectID ) {
    291     std::map<int, int>::iterator iter = clientsShip.begin();
    292     while( iter != clientsShip.end() ) {
     311    std::map<int, int>::iterator iter;
     312    for( iter = clientsShip.begin(); iter != clientsShip.end(); iter++ ) {
    293313      if( iter->second == objectID ) return iter->first;
    294314    }
     
    302322  void ConnectionManager::deleteObjectIDReg( int objectID ) {
    303323    std::map<int, int>::iterator iter = clientsShip.begin();
    304     while( iter != clientsShip.end() ) {
     324    for( iter = clientsShip.begin(); iter != clientsShip.end(); iter++ ) {
    305325      if( iter->second == objectID ) break;
    306326    }
    307327    clientsShip.erase( iter->first );
    308328  }
    309  
     329  int ConnectionManager::getNumberOfClients() {
     330    return clientsShip.size();
     331  }
    310332}
  • code/branches/network/src/network/ConnectionManager.h

    r888 r1008  
    5555    bool sendPackets(ENetEvent *event);
    5656    bool sendPackets();
     57
     58    //##### for testing purpose only #####
     59    ConnectionManager();
     60    std::map<int, int> testGetClientsShip() {
     61      return clientsShip;
     62    }
     63    void testAddClientsShipID( int clientID, int objectID ) {
     64      addClientsObjectID( clientID, objectID );
     65    }
     66    int testGetClientsShipID( int clientID ) {
     67      return getClientsShipID( clientID );
     68    }
     69    int testGetObjectsClientID( int objectID ) {
     70       return getObjectsClientID( objectID );
     71    }
     72    void testDeleteClientsIDReg( int clientID ) {
     73      deleteClientIDReg( clientID );
     74    }
     75    void testDeleteObjectIDReg( int objectID ) {
     76      deleteObjectIDReg( objectID );
     77    }
     78    //##### for testing purpose only #####
    5779  private:
    5880    bool clientDisconnect(ENetPeer *peer);
     
    82104    void deleteClientIDReg( int clientID );
    83105    void deleteObjectIDReg( int objectID );
     106    int getNumberOfClients();
    84107  };
    85 
    86 
    87 
    88 
    89 
    90 
    91 
    92108
    93109}
  • code/branches/network/src/network/GameStateClient.cc

    r1005 r1008  
    109109       
    110110        if(!it){
    111           COUT(5) << "classid: " << sync.classID << ", name: " << ID((unsigned int) sync.classID)->getName() << std::endl;
     111          COUT(4) << "loadSnapshot:\tclassid: " << sync.classID << ", name: " << ID((unsigned int) sync.classID)->getName() << std::endl;
    112112          Synchronisable *no = (Synchronisable*)(ID((unsigned int) sync.classID)->fabricate());
    113113          no->objectID=sync.objectID;
     
    197197      case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return NULL;
    198198      case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer" << std::endl; return NULL;
    199       case Z_DATA_ERROR: COUT(2) << "data corrupted" << std::endl; return NULL;
     199      case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return NULL;
    200200    }
    201201
  • code/branches/network/src/network/GameStateManager.cc

    r1007 r1008  
    9393
    9494  GameStateCompressed *GameStateManager::popGameState(int clientID) {
     95    //why are we searching the same client's gamestate id as we searched in
     96    //Server::sendGameState?
    9597    int gID = head_->findClient(clientID)->getGamestateID();
    96     COUT(4) << "popgamestate: sending gstate id: " << id << "diffed from: " << gID << std::endl;
    97     if(gID!=GAMESTATEID_INITIAL){
     98    COUT(4) << "G.St.Man: popgamestate: sending gstate_id: " << id << " diffed from: " << gID << " (not diffed yet)" << std::endl;
     99   
     100    //chose wheather the next gamestate is the first or not
     101    if(gID != GAMESTATEID_INITIAL){
    98102      GameState *client = gameStateMap[gID];
    99103      GameState *server = reference;
     
    115119  GameState *GameStateManager::getSnapshot(int id)
    116120  {
     121    //std::cout << "begin getSnapshot" << std::endl;
    117122    //the size of the gamestate
    118123    int totalsize=0;
     
    127132    GameState *retval=new GameState; //return value
    128133    retval->id=id++;
    129     COUT(4) << "producing gamestate with id: " << retval->id << std::endl;
     134    COUT(4) << "G.ST.Man: producing gamestate with id: " << retval->id << std::endl;
    130135    // reserve a little memory and increase it later on
    131     COUT(5) << "mallocing" << std::endl;
     136    COUT(5) << "G.ST.Man: mallocing: " << memsize << std::endl;
    132137    retval->data = (unsigned char*)malloc(memsize);
    133     COUT(5) << "malloced" << std::endl;
     138    COUT(5) << "G.ST.Man: malloced: " << memsize << std::endl;
    134139
    135140    // offset of memory functions
     
    137142    // go through all Synchronisables
    138143    for(it = orxonox::ObjectList<Synchronisable>::start(); it; ++it){
     144      //std::cout << "begin inner loop" << std::endl;
    139145      //std::cout << "gamestatemanager: in for loop" << std::endl;
    140146      //get size of the synchronisable
    141147      tempsize=it->getSize();
    142 //       COUT(5) << "size of temp gamestate: " << tempsize << std::endl;
     148      //COUT(5) << "size of temp gamestate: " << tempsize << std::endl;
    143149      //COUT(2) << "size of synchronisable: " << tempsize << std::endl;
    144150      // add place for data and 3 ints (length,classid,objectid)
    145151      totalsize+=tempsize+3*sizeof(int);
    146152      //std::cout << "totalsize: " << totalsize << std::endl;
     153      //COUT(5) << "G.St.Man: current totalsize=" << totalsize << std::endl;
     154      //COUT(5) << "G.St.Man: current it->classID=" << it->classID << " it->objectID=" << it->objectID << std::endl;
    147155      // allocate additional space
    148       if(totalsize+tempsize>memsize){
    149         if(tempsize<1000){
     156      if((totalsize+tempsize) > memsize){
     157        COUT(5) << "G.St.Man: need additional memory" << std::endl;
     158        if(tempsize < 1000){
    150159          retval->data = (unsigned char *)realloc((void *)retval->data, totalsize+1000);
    151160          memsize+=1000;
     
    154163          memsize+=tempsize+1000;
    155164        }
     165        COUT(5) << "G.St.Man: additional space allocation finished" << std::endl;
    156166      }
    157167
     
    166176      // increase data pointer
    167177      offset+=tempsize+3*sizeof(int);
    168     }
    169     COUT(5) << "Gamestate size: " << totalsize << std::endl;
     178      //std::cout << "end inner loop" << std::endl;
     179    }
    170180    retval->size=totalsize;
    171181    //#### bugfix
    172182    retval->diffed = false;
     183    //std::cout << "end snapShot" << std::endl;
     184    COUT(5) << "G.ST.Man: Gamestate size: " << totalsize << std::endl;
    173185    return retval;
    174186  }
     
    185197
    186198  GameStateCompressed *GameStateManager::encode(GameState *a, GameState *b) {
     199    COUT(5) << "G.St.Man: this will be a DIFFED gamestate" << std::endl;
    187200    //GameState r = diff(a,b);
    188201    //r.diffed = true;
     
    193206
    194207  GameStateCompressed *GameStateManager::encode(GameState *a) {
     208    COUT(5) << "G.St.Man: this will be a not diffed gamestate" << std::endl;
    195209    a->diffed=false;
    196210    return compress_(a);
     
    235249
    236250  GameStateCompressed *GameStateManager::compress_(GameState *a) {
    237     COUT(5) << "compressing gamestate" << std::endl;
     251    COUT(5) << "G.St.Man: compressing gamestate" << std::endl;
    238252    int size = a->size;
    239253    uLongf buffer = (uLongf)((a->size + 12)*1.01)+1;
     
    245259
    246260    switch ( retval ) {
    247       case Z_OK: COUT(5) << "successfully compressed" << std::endl; break;
    248       case Z_MEM_ERROR: COUT(1) << "not enough memory available in gamestate.compress" << std::endl;
     261      case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break;
     262      case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl;
    249263      return NULL;
    250       case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer in gamestate.compress" << std::endl;
     264      case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl;
    251265      return NULL;
    252       case Z_DATA_ERROR: COUT(2) << "decompress: data corrupted in gamestate.compress" << std::endl;
     266      case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl;
    253267      return NULL;
    254268    }
     
    263277    compressedGamestate->diffed = a->diffed;
    264278    compressedGamestate->base_id = a->base_id;
    265 
     279    COUT(5) << "G.St.Man: saved compressed data in GameStateCompressed" << std::endl;
    266280    return compressedGamestate;
    267281  }
  • code/branches/network/src/network/PacketBuffer.cc

    r790 r1008  
    6060      last=first;
    6161      last->next=NULL;
    62       // change this!!!!!!!
     62      // change this!!!!!!! 
    6363      last->packet = ev->packet;
    6464      last->address = ev->peer->address;
  • code/branches/network/src/network/PacketDecoder.cc

    r986 r1008  
    5252  {
    5353    int client = clientId;
    54     COUT(5) << "clientId: " << client << std::endl; //control cout, not important, just debugging info
     54    COUT(5) << "PacketDecoder: clientId: " << client << std::endl; //control cout, not important, just debugging info
    5555    int id = (int)*packet->data; //the first 4 bytes are always the enet packet id
    56     COUT(5) << "packet id: " << id << std::endl;
    57 //     COUT(5) << "packet size inside packetdecoder: " << packet->dataLength << std::endl;
     56    COUT(5) << "PacketDecoder: packet id: " << id << std::endl;
     57    //COUT(5) << "packet size inside packetdecoder: " << packet->dataLength << std::endl;
     58
     59    if ( packet == NULL ) {
     60      COUT(4) << "PacketDecoder: no packets->packetbuffer queue is empty" << std::endl;
     61      return false;
     62    }
    5863    switch( id ) {
    5964  case ACK:
     
    9398
    9499
    95     COUT(5) << "got ack id: " << a->id << std::endl;
     100    COUT(5) << "PacketDecoder: got ack id: " << a->id << std::endl;
    96101    processAck( a, clientId ); //debug info
    97102
     
    147152    currentState = new GameStateCompressed;
    148153    if(currentState == NULL){
    149       COUT(3) << "could not generate new GameStateCompressed" << std::endl;
     154      COUT(3) << "PacketDecoder: could not generate new GameStateCompressed" << std::endl;
    150155      return;
    151156    }
     
    156161    //currentState->id = *((int *)packet->data+sizeof(int));
    157162    memcpy( (void*)&(currentState->id), (const void*)(packet->data+1*sizeof( int )), sizeof( int) );
    158     COUT(5) << "decoder: received gs id: " << currentState->id << std::endl;
     163    COUT(5) << "PacketDecoder: received gs id: " << currentState->id << std::endl;
    159164//     std::cout << "id: " << currentState->id << std::endl;
    160165    //copy the size of the GameStateCompressed compressed data into the new GameStateCompressed struct, located at 3th
     
    174179    //since data is not allocated, because it's just a pointer, allocate it with size of gamestatedatastream
    175180    if(currentState->compsize==0)
    176       COUT(2) << "compsize is 0" << std::endl;
     181      COUT(2) << "PacketDecoder: compsize is 0" << std::endl;
    177182    currentState->data = (unsigned char*)(malloc( currentState->compsize ));
    178183    if(currentState->data==NULL)
    179       COUT(2) << "Gamestatepacket-decoder: memory leak" << std::endl;
     184      COUT(2) << "PacketDecoder: Gamestatepacket-decoder: memory leak" << std::endl;
    180185    //copy the GameStateCompressed data
    181186    //std::cout << "packet size (enet): " << packet->dataLength << std::endl;
     
    197202    void *data  = (void *)cid->message;
    198203    memcpy(data, (const void*)(packet->data+3*sizeof(int)), cid->length);
    199     COUT(4) << "classid: " << cid->clid << ", name: " << cid->message << std::endl;
     204    COUT(4) << "PacketDecoder: classid: " << cid->clid << ", name: " << cid->message << std::endl;
    200205    enet_packet_destroy( packet );
    201206    processClassid(cid);
  • code/branches/network/src/network/PacketGenerator.cc

    r986 r1008  
    5050  ENetPacket* PacketGenerator::acknowledgement( int state, int reliable )
    5151  {
    52     COUT(4) << "generating new acknowledgement, id: " << state << std::endl;
     52    COUT(4) << "PacketGenerator: generating new acknowledgement, id: " << state << std::endl;
    5353    ack* ackreq = new ack;
    5454    ackreq->id = ACK;
     
    6363  ENetPacket* PacketGenerator::mousem( double x, double y, int reliable )
    6464  {
    65     COUT(4) << "generating new mouse" << std::endl;
     65    COUT(4) << "PacketGenerator: generating new mouse" << std::endl;
    6666    mouse* mousemove = new mouse;
    6767    mousemove->id = MOUSE;
     
    7777  ENetPacket* PacketGenerator::keystrike( char press, int reliable )
    7878  {
    79     COUT(4) << "generating new keyboard" << std::endl;
     79    COUT(4) << "PacketGenerator: generating new keyboard" << std::endl;
    8080    keyboard* key = new keyboard;
    8181    key->id = KEYBOARD;
     
    126126  {
    127127    unsigned char* data = (unsigned char *)malloc(3*sizeof(int)+classname.length()+1);
    128     std::cout << "classid: " << classid << ", name: " << classname << std::endl;
     128    std::cout << "PacketGenerator: classid: " << classid << ", name: " << classname << std::endl;
    129129    *(int *)data = CLASSID;
    130130    *((int *)data+1) = classname.length()+1;
  • code/branches/network/src/network/Server.cc

    r1007 r1008  
    9595  bool Server::sendMSG(const char *msg) {
    9696    ENetPacket *packet = packet_gen.chatMessage(msg);
    97     std::cout <<"adding Packets" << std::endl;
     97    COUT(4) <<"Server: adding Packets" << std::endl;
    9898    connection->addPacketAll(packet);
    9999    //std::cout <<"added packets" << std::endl;
    100100    if (connection->sendPackets()){
    101       std::cout << "Sucessfully" << std::endl;
     101      COUT(4) << "Server: Sucessfully" << std::endl;
    102102      return true;
    103103    }
     
    126126    while(!connection->queueEmpty()){
    127127      //std::cout << "Client " << clientID << " sent: " << std::endl;
     128      //clientID here is a reference to grab clientID from ClientInformation
    128129      packet = connection->getPacket(clientID);
    129       elaborate(packet, clientID);
     130      //if statement to catch case that packetbuffer is empty
     131      if( !elaborate(packet, clientID) )
     132        COUT(4) << "Server: PacketBuffer empty" << std::endl;
    130133    }
    131134  }
     
    136139  void Server::updateGamestate() {
    137140    gamestates->update();
     141    COUT(4) << "Server: one gamestate update complete, goig to sendGameState" << std::endl;
    138142    //std::cout << "updated gamestate, sending it" << std::endl;
    139143    //if(clients->getGamestateID()!=GAMESTATEID_INITIAL)
    140       sendGameState();
     144    sendGameState();
     145    COUT(4) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl;
    141146    //std::cout << "sent gamestate" << std::endl;
    142147  }
     
    146151  */
    147152  bool Server::sendGameState() {
    148     COUT(5) << "starting sendGameState" << std::endl;
     153    COUT(5) << "Server: starting function sendGameState" << std::endl;
    149154    ClientInformation *temp = clients;
    150155    bool added=false;
    151     while(temp!=NULL){
     156    while(temp != NULL){
    152157      if(temp->head){
    153158        temp=temp->next();
     159        //think this works without continue
    154160        continue;
    155161      }
    156162      if( !(temp->getSynched()) ){
    157         COUT(5) << "not sending gamestate" << std::endl;
     163        COUT(5) << "Server: not sending gamestate" << std::endl;
    158164        temp=temp->next();
     165        //think this works without continue
    159166        continue;
    160167      }
    161       COUT(5) << "doing gamestate gamestate preparation" << std::endl;
    162       int gid = temp->getGamestateID();
    163       int cid = temp->getID();
    164       COUT(5) << "server, got acked (gamestate) ID: " << gid << std::endl;
     168      COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl;
     169      int gid = temp->getGamestateID(); //get gamestate id
     170      int cid = temp->getID(); //get client id
     171      COUT(5) << "Server: got acked (gamestate) ID from clientlist: " << gid << std::endl;
    165172      GameStateCompressed *gs = gamestates->popGameState(cid);
    166173      if(gs==NULL){
    167         COUT(2) << "could not generate gamestate" << std::endl;
     174        COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl;
    168175        return false;
    169176      }
    170177      //std::cout << "adding gamestate" << std::endl;
    171       connection->addPacket(packet_gen.gstate(gs), cid);
     178      if ( !(connection->addPacket(packet_gen.gstate(gs), cid)) )
     179        COUT(4) << "Server: packet with client id (cid): " << cid << " not sended" << std::endl;
    172180      //std::cout << "added gamestate" << std::endl;
    173181      added=true;
    174182      temp=temp->next();
    175183    }
    176     if(added)
     184    if(added) {
     185      //std::cout << "send gamestates from server.cc in sendGameState" << std::endl;
    177186      return connection->sendPackets();
    178     COUT(5) << "had no gamestates to send" << std::endl;
     187    }
     188    COUT(5) << "Server: had no gamestates to send" << std::endl;
    179189    return false;
    180190  }
    181191
    182192  void Server::processAck( ack *data, int clientID) {
    183     COUT(5) << "processing ack from client: " << clientID << "; ack-id: " << data->id << std::endl;
     193    COUT(5) << "Server: processing ack from client: " << clientID << "; ack-id: " << data->id << std::endl;
    184194    clients->findClient(clientID)->setGamestateID(data->a);
    185195  }
  • code/branches/network/src/network/Synchronisable.cc

    r871 r1008  
    4444    // increase datasize
    4545    datasize+=sizeof(int)+size;
    46     // push temp to syncList (at the bottom)
     46    //std::cout << "push temp to syncList (at the bottom) " << datasize << std::endl;
    4747    syncList.push_back(temp);
    4848  }
     
    104104  */
    105105  syncData Synchronisable::getData(unsigned char *mem){
    106     std::list<synchronisableVariable>::iterator i;
     106    //std::cout << "inside getData" << std::endl;
     107    std::list<SYNCVAR>::iterator i;
    107108    syncData retVal;
    108109    retVal.objectID=this->objectID;
     
    113114    int n=0;
    114115    for(i=syncList.begin(); n<datasize && i!=syncList.end(); ++i){
    115       //COUT(2) << "size of variable: " << i->size << std::endl;
    116116      //(std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int));
    117117      memcpy( (void *)(retVal.data+n), (const void*)&(i->size), sizeof(int) );
     
    166166  int Synchronisable::getSize(){
    167167    int tsize=0;
    168     std::list<synchronisableVariable>::iterator i;
     168    std::list<SYNCVAR>::iterator i;
    169169    for(i=syncList.begin(); i!=syncList.end(); i++){
    170170      switch(i->type){
    171     case DATA:
    172       tsize+=sizeof(int);
    173       tsize+=i->size;
    174       break;
    175     case STRING:
    176       tsize+=sizeof(int);
    177       tsize+=((std::string *)i->var)->length()+1;
    178       break;
     171      case DATA:
     172        tsize+=sizeof(int);
     173        tsize+=i->size;
     174        break;
     175      case STRING:
     176        tsize+=sizeof(int);
     177        tsize+=((std::string *)i->var)->length()+1;
     178        break;
    179179      }
    180180    }
  • code/branches/network/src/network/Synchronisable.h

    r927 r1008  
    4747  public:
    4848
    49       virtual ~Synchronisable();
     49    virtual ~Synchronisable();
    5050    int objectID;
    5151    int classID;
  • code/branches/network/src/network/diffTest.cc

    r984 r1008  
     1#include "enet/enet.h"
     2#include "Orxonox.h"
     3#include "NetworkPrereqs.h"
     4#include "PacketTypes.h"
    15#include "GameStateManager.h"
    26#include "Synchronisable.h"
    37#include "GameStateClient.h"
    4 #include "NetworkPrereqs.h"
    5 #include "PacketTypes.h"
    68#include "iostream"
    79#include "core/CoreIncludes.h"
    810#include "time.h"
     11#include "ConnectionManager.h"
     12#include "ClientInformation.h"
     13#include <boost/thread/thread.hpp>
     14#include <boost/bind.hpp>
     15#include "util/Sleep.h"
    916
    1017using namespace network;
     
    1724
    1825void printGameStateCompressed( GameStateCompressed* gc ) {
    19   std::cout << "=================================================" << std::endl;
     26  //std::cout << "=================================================" << std::endl;
    2027  std::cout << "GameStateCompressed id:\t\t" << gc->id << std::endl;
    2128  std::cout << "GameStateCompressed normsize:\t" << gc->normsize << std::endl;
     
    2431  //std::cout << "GameState data:\t" << gc->data << std::endl;
    2532  std::cout << "GameStateCompressed compressing rate:\t" << 100.0-((100.0/(gc->normsize))*(gc->compsize)) << "%" << std::endl;
    26   //std::cout << "=================================================" << std::endl;
     33  std::cout << "=================================================" << std::endl;
    2734  return;
    2835}
     
    3037bool compareData( GameState* g1, GameState* g2 ) {
    3138  if ( g1->id != g2->id ) {
    32     std::cout << "GameStates are not comparable -> not same id" << std::endl;
     39    std::cout << "\t--> GameStates are not comparable -> not same id" << std::endl;
    3340    return 1;
    3441  }
    3542  else if ( g1->size != g2->size ) {
    36     std::cout << "GameStates are not the same size!!" << std::endl;
     43    std::cout << "\t--> GameStates are not the same size!!" << std::endl;
    3744    std::cout << g1->size << " != " << g2->size << std::endl;
    3845  }
     
    4047  for ( int i=0; i<length; i++ ) {
    4148    if ( g1->data[i] != g2->data[i] ) {
    42       std::cout << "data of both GameStates are not identical" << std::endl;
     49      std::cout << "\t--> data of both GameStates are not identical" << std::endl;
    4350      return false;
    4451    }
    4552  }
    46   std::cout << "GameStates are identical" << std::endl;
     53  std::cout << "\t--> GameStates are identical (compareData)" << std::endl;
    4754  return true;
    4855}
     
    5057bool compareGameStates( GameState* g1, GameState* g2 ) {
    5158  if ( g1->id != g2->id ) {
    52     std::cout << "GameState id's not identical" << std::endl;
     59    std::cout << "\t==> GameState id's not identical (GameStateCompare)" << std::endl;
     60  }
     61  if( g1->size != g2->size ) {
     62    std::cout << "\t==> GameState sizes are not identical (GameStateCompare)" << std::endl;
    5363    return false;
    5464  }
    55   else if( g1->size != g2->size ) {
    56     std::cout << "GameState sizes are not identical" << std::endl;
     65  else if ( g1->diffed != g2->diffed ) {
     66    std::cout << "\t==> GameState diffed params not identical (GameStateCompare)" << std::endl;
    5767    return false;
    5868  }
    59   else if ( g1->diffed != g2->diffed ) {
    60     std::cout << "GameState diffed params not identical" << std::endl;
     69  else if ( !compareData( g1, g2 ) ) {
     70    std::cout << "\t==> GameState data are not identical (GameStateCompare)" << std::endl;
    6171    return false;
    6272  }
    63   else if ( !compareData( g1, g2 ) ) {
    64     std::cout << "GameState data are not identical" << std::endl;
    65     return false;
    66   }
    67   std::cout << "==>GameStates are identical (GameStateCompare)" << std::endl;
     73  std::cout << "\t==> GameStates are identical (GameStateCompare)" << std::endl;
    6874  return true;
    6975}
    7076
    7177void printGameState( GameState* gstate ) {
    72   std::cout << "=================================================" << std::endl;
     78  //std::cout << "=================================================" << std::endl;
    7379  std::cout << "GameState id:\t\t" << gstate->id << std::endl;
    7480  std::cout << "GameState size:\t\t" << gstate->size << std::endl;
    7581  std::cout << "GameState diffed:\t" << gstate->diffed << std::endl;
    7682  //std::cout << "GameState data:\t" << gstate->data << std::endl;
    77   //std::cout << "=================================================" << std::endl;
     83  std::cout << "=================================================" << std::endl;
    7884  return;
    7985}
     
    8187unsigned char* createData( int length, int mode ) {
    8288  char* data = new char[ length ];
    83  
    8489  if ( mode == 1 ) {
    8590    for ( int i=0; i<length; i++ )
     
    9398  }
    9499  else if ( mode == 3 ) {
    95     for ( int i=0; i<length; i++ ) {
    96       data[i] = (char)(i%255);
    97     }
    98   }
    99   else if ( mode == 4 ) {
    100100    for ( int i=0; i<length; i++ ){
    101101      data[i] = (char)(rand()%255);
    102102    }
    103103  }
    104   else if ( mode == 5 ) {
     104  else if ( mode == 4 ) {
    105105    for ( int i=0; i<length; i++ ){
    106106      data[i] = (char)(rand()%127);
     
    121121
    122122  if ( mode == 1 ) {
    123     b->data = new unsigned char[a->size];
     123    b->data = new unsigned char[length];
    124124    b->size = a->size;
    125125    for ( int i=0; i<length; i++ ) {
     
    129129  }
    130130  else if ( mode == 2 ) {
    131     b->data = new unsigned char[a->size];
    132     b->size = a->size;
     131    b->data = new unsigned char[length];
     132    b->size = length;
    133133    for ( int i=0; i<length; i++ ) {
    134       if ( i%5 == 0 ) b->data[i] = rand()%255;
     134      if ( i%(rand()%((length)/11)) == 0 ) b->data[i] = rand()%255;
    135135      else b->data[i] = a->data[i];
    136136    }
    137137  }
    138138  else if ( mode == 3 ) {
    139     int s = a->size + (a->size)/3;
     139    int s = length + (rand()%(length));
    140140    b->data = new unsigned char[s];
    141141    b->size = s;
     
    148148    }
    149149  }
     150  else if ( mode == 4 ) {
     151    int s = length + (rand()%(length));
     152    b->data = new unsigned char[s];
     153    b->size = s;
     154    for ( int i=0; i<length; i++ ) {
     155      if ( i%(rand()%(length)) == 0 ) b->data[i] = rand()%255;
     156      else b->data[i] = a->data[i];
     157    }
     158    for( int i=length; i<s; i++ ) {
     159      b->data[i] = rand()%255;
     160    }
     161  }
     162    else if ( mode == 5 ) {
     163    int s = (length)/2;
     164    b->data = new unsigned char[s];
     165    b->size = s;
     166    for ( int i=0; i<s; i++ ) {
     167      if ( i%10 == 0 ) b->data[i] = rand()%255;
     168      else b->data[i] = a->data[i];
     169    }
     170  }
    150171
    151172  return b;
     
    182203            << modeCreateData << " modeChangeData = " << modeChangeData << std::endl;
    183204  GameStateClient* g_client;
    184   GameStateManager* g_manager;;
     205  GameStateManager* g_manager;
    185206 
    186207  GameState* g_undiff1 = new GameState;
     
    267288}
    268289
     290void printClientObjectMapping( ConnectionManager* cmanager, int clients ) {
     291  std::map<int, int>::iterator iter;
     292  std::map<int, int> clientsmap = cmanager->testGetClientsShip();   
     293  for( iter = clientsmap.begin(); iter != clientsmap.end(); iter++ ) {
     294    std::cout << "clientID: " << iter->first << "\t-> objectID: " << iter->second << std::endl;
     295  }
     296  return;
     297}
     298
     299bool is( int a[], int b, int size ) {
     300  for ( int i=0; i<size; i++ ) {
     301    if ( a[i] == b ) return true;
     302  }
     303  return false;
     304}
     305
     306void testClientObjectMapping( int clients ) {
     307  ConnectionManager* cmanager = new ConnectionManager();
     308  int shift = 2;
     309  std::cout << "create a map length [clients]" << std::endl;
     310  for ( int i=0; i<clients; i++ ) {
     311    cmanager->testAddClientsShipID( i, i+shift );
     312  }
     313  printClientObjectMapping( cmanager, clients );
     314 
     315  std::cout << "get random client's ship id" << std::endl;
     316  int id;
     317  for ( int i=0; i<(clients/3); i++ ) {
     318    id = rand()%clients;
     319    std::cout << "client: " << id << "\t-> ship: " << cmanager->testGetClientsShipID( id ) << std::endl; 
     320  }
     321
     322  std::cout <<"get random ship's client id" << std::endl;
     323  for ( int i=0; i<(clients/3); i++ ) {
     324    id = (rand()%clients)+shift;
     325    std::cout << "ship:   " << id << "\t-> client: " << cmanager->testGetObjectsClientID( id ) << std::endl; 
     326  }
     327
     328  std::cout << "delete random client from map" << std::endl;
     329  int deleted[clients/3];
     330  for ( int i=0; i<(clients/3); i++ ) {
     331    id = rand()%clients;
     332    if ( !is( deleted, id, clients/3 ) ) {
     333      std::cout << "delete client " << id << std::endl;
     334      cmanager->testDeleteClientsIDReg( id );
     335    }
     336    deleted[i] = id;
     337  }
     338  std::cout << "resulting list:" << std::endl;
     339  printClientObjectMapping( cmanager, clients-(clients/3));
     340 
     341  std::cout << "delete random object from map" << std::endl;
     342  int jap = 0;
     343  while( jap < 3 ) {
     344    id = (rand()%clients) + shift;
     345    if ( !is( deleted, id, clients/3 ) ) {
     346      std::cout << "delete object: " << id << std::endl;
     347      cmanager->testDeleteObjectIDReg( id );
     348      jap++;
     349    }
     350  }
     351  std::cout << "resulting list:" << std::endl;
     352  printClientObjectMapping( cmanager, clients-(clients/3)-3);
     353}
     354
     355bool addClientTest( ENetEvent* event, ClientInformation*& head ) {
     356  ClientInformation *temp = head->insertBack(new ClientInformation);
     357  if(temp->prev()->head) {
     358    temp->prev()->setID(0);
     359    temp->setID(1);
     360  }
     361  else
     362    temp->setID(temp->prev()->getID()+1);
     363  temp->setPeer(event->peer);
     364  std::cout << "added client id: " << temp->getID() << std::endl;
     365
     366  temp->setSynched(true);
     367  return true;
     368}
     369
     370void printClientInformationBox( ClientInformation* box ) {
     371  std::cout << "ClientList: id: " << box->getID() << "\t";
     372  std::cout << "g_id: " << box->getGamestateID() << " \t";
     373  std::cout << "synched: " << box->getSynched() << "\t";
     374  std::cout << "is head: " << box->head << std::endl;
     375}
     376
     377void printClientInformationList( ClientInformation* list ) {
     378  printClientInformationBox( list );
     379  list = list->next();
     380 
     381  while( list != 0 ) {
     382    printClientInformationBox( list );
     383    list = list->next();
     384  }
     385  return;
     386}
     387
     388void testClientInformation( int numberOfClients ) {
     389  ClientInformation* head = new ClientInformation( true );
     390  ConnectionManager* connectionManager;
     391  ENetEvent event;
     392 
     393  for ( int i=0; i<numberOfClients; i++ ) {
     394    if ( !addClientTest( &event, head ) ) {
     395      std::cout << "addClientTest didn't work with: " << i << std::endl;
     396    }
     397  }
     398  std::cout << "(now id should be synched, since that works and this is test of list, this step is left out)" << std::endl;
     399
     400  printClientInformationList( head );
     401
     402  std::cout << "remove some clients" << std::endl;
     403  if ( head->removeClient( numberOfClients/3 ) ) std::cout << "client " << numberOfClients/3 << " removed" << std::endl;
     404  else std::cout << "could not remove client: " << numberOfClients/3 << std::endl;
     405  if ( head->removeClient( numberOfClients ) ) std::cout << "client " << numberOfClients << " removed" << std::endl;
     406  else std::cout << "could not remove client: " << numberOfClients << std::endl;
     407  if ( head->removeClient( 1 ) ) std::cout << "client " << 1 << " removed" << std::endl;
     408  else std::cout << "could not remove client: " << 1 << std::endl;
     409  if ( head->removeClient( 1 ) ) std::cout << "client " << 1 << " removed a second time" << std::endl;
     410  else std::cout << "could not remove client: " << 1 << std::endl;
     411  if ( head->removeClient( numberOfClients + 100 ) ) std::cout << "client " << numberOfClients + 100 << " removed a second time" << std::endl;
     412  else std::cout << "could not remove client: " << numberOfClients + 100 << std::endl;
     413
     414  printClientInformationList( head );
     415 
     416  std::cout << "try to find some clients with findClient(..., false)" << std::endl;
     417  ClientInformation* temp = head->findClient( 2 );
     418  printClientInformationBox( temp );
     419  temp = head->findClient( numberOfClients/3 );
     420  printClientInformationBox( temp );
     421  temp = head->findClient( 0 );
     422  printClientInformationBox( temp );
     423  temp = head->findClient( 8 );
     424  printClientInformationBox( temp );
     425
     426  std::cout << "find the same, output should be identical with above but with findClient(..., TRUE)" << std::endl;
     427  temp = head->findClient( 2, true );
     428  printClientInformationBox( temp );
     429  temp = head->findClient( numberOfClients/3, true );
     430  printClientInformationBox( temp );
     431  temp = head->findClient( 0, true );
     432  printClientInformationBox( temp );
     433  temp = head->findClient( 8, true );
     434  printClientInformationBox( temp );
     435
     436  std::cout << "test setGamestateID" << std::endl;
     437  temp->setGamestateID( 8 );
     438  printClientInformationBox( temp );
     439
     440  std::cout << "test insertAfter() and insertBefore()" << std::endl;
     441  ClientInformation* newInfo = new ClientInformation;
     442  ClientInformation* toool = new ClientInformation;
     443  newInfo->setGamestateID( 200 );
     444  newInfo->setID( numberOfClients+2);
     445  newInfo->setSynched( true );
     446  newInfo->setPeer( NULL );
     447  toool->setGamestateID( 199 );
     448  toool->setID( numberOfClients+1);
     449  toool->setSynched( true );
     450  toool->setPeer( NULL );
     451
     452  //never use the same ClientInformation box in this situation
     453  //-> results in endless loop
     454  temp->insertAfter( newInfo );
     455  temp->insertBefore( toool );
     456
     457  printClientInformationList( head );
     458  return;
     459}
     460
     461//### following stuff is to test buffer, took from PacketBufferTestExt.cc ###
     462
     463void write(PacketBuffer *test){
     464  ENetEvent event;
     465  ENetPacket *packet;
     466  if(test->isEmpty())
     467    std::cout << "buffer is empty" << std::endl;
     468  for(int i=0; i<10; i++){
     469    std::string temp = "packet ";
     470    packet = enet_packet_create("packet", strlen("packet ")+1,
     471      ENET_PACKET_FLAG_RELIABLE);
     472    std::cout << i << ": pushing " << packet->data << std::endl;
     473    event.packet=packet;
     474    test->push(&event);
     475    if(i==5)
     476      usleep(200000);
     477  }
     478  test->setClosed(true);
     479  return;
     480}
     481
     482void read(PacketBuffer *test){
     483  //test->print();
     484  // exit if the queue is closed and empty
     485  while(!test->isClosed() || !test->isEmpty()){
     486    // only pop if the queue isn't empty
     487    while(!test->isEmpty()){
     488      std::cout << "We popped the value " << test->pop()->data << std::endl;
     489    }
     490  }
     491  return;
     492}
     493
     494void testPacketBuffer() {
     495  PacketBuffer test = PacketBuffer();
     496  boost::thread thrd1(boost::bind(&write, &test));
     497  boost::thread thrd2(boost::bind(&read, &test));
     498
     499  thrd1.join();
     500  thrd2.join();
     501  return;
     502}
     503
     504//### end packetbuffer test stuff ###
     505
     506void displayModes() {
     507  std::cout << "mode datalength: length of data array to create" << std::endl;
     508  std::cout << "mode Data:" << std::endl;
     509  std::cout << "\t-1 -> array[length] with numbers length%255" << std::endl;
     510  std::cout << "\t-2 -> array[length] with numbers length%255, every %98 is != 0" << std::endl;
     511  std::cout << "\t-3 -> array[length] with random numbers (-126:127) no modulo zeros" << std::endl;
     512  std::cout << "\t-4 -> array[length] with random numbers (0:127) no modulo zeros" << std::endl;
     513  std::cout << "---------------------------------------------------------------------------------" << std::endl;
     514  std::cout << "mode Change:" << std::endl;
     515  std::cout << "\t-1 -> every %10 == 0 index is different from original" << std::endl;
     516  std::cout << "\t-2 -> every %(rand()%(length/11)) is different from original" << std::endl;
     517  std::cout << "\t-3 -> every %10 == 0 index is different and randomly longer till 2xlonger" << std::endl;
     518  std::cout << "\t-4 -> random differences and randomly longer" << std::endl;
     519  std::cout << "\t-5 -> only half as long and ever %10 == 0 index is different" << std::endl;
     520}
     521
    269522int main( int argc, char* argv[] ) {
    270523  int a,b,c;
    271524  std::string dec = "nothing";
    272525  std::cout << "############### START TEST (quit q) ###############" << std::endl;
     526  std::cout << "possible tests: " << std::endl;
     527  std::cout << "displayModes:\t\t modes" << std::endl;
     528  std::cout << "testCompression:\t tc [datalength] [mode Data]" << std::endl;
     529  std::cout << "testDifferentiation:\t td [datalength] [mode Data] [mode Change]" << std::endl;
     530  std::cout << "testCompressWithDiff:\t tcd [datalength] [mode Data] [mode Change]" << std::endl;
     531  std::cout << "testClientObjectMapping: tcom [#clients]" << std::endl;
     532  std::cout << "testClientInformation:\t tci [#clients] (call with >10)" << std::endl;
     533  std::cout << "testPacketBuffer:\t tbuf (comment address assignements in PacketBuffer.cc!)" << std::endl;
    273534  while ( dec.compare("q") != 0 ) {
    274     std::cout << "possible tests: testcompression [datalength] [mode]" << std::endl;
    275     std::cout << "testdiff [datalength] [mode Data] [mode Change]" << std::endl;
    276     std::cout << "tcd [datalength] [mode Data] [mode Change]" << std::endl;
    277535    std::cin >> dec;
    278     if ( dec.compare("testcompression") == 0 ) {
     536    if ( dec.compare("tc") == 0 ) {
    279537      std::cin >> a; std::cin >> b;
    280538      testCompression( a, b );
    281539    }
    282     else if ( dec.compare("testdiff") == 0 ) {
     540    else if ( dec.compare("td") == 0 ) {
    283541      std::cin>> a; std::cin >> b; std::cin >> c;
    284542      testDifferentiation( a, b, c );
     
    288546      testCompressWithDiff( a, b, c );
    289547    }
     548    else if ( dec.compare("modes") == 0 )
     549      displayModes();
     550    else if ( dec.compare("tcom") == 0 ) {
     551      std::cin>> a;
     552      testClientObjectMapping( a );
     553    }
     554    else if ( dec.compare("tci") == 0 ) {
     555      std::cin >> a;
     556      testClientInformation( a );
     557    }
     558    else if ( dec.compare("tbuf") == 0 ) {
     559      testPacketBuffer();
     560    }   
     561    std::cout << "################## END ONE TURN ##################@" << std::endl;
    290562  }
    291563  return 0;
     
    294566int main() {
    295567  std::cout << "############### START TEST (quit q) ###############" << std::endl;
    296   testCompressWithDiff( 5000, 5, 1 );
     568  testClientInformation( 10 );
    297569}*/
Note: See TracChangeset for help on using the changeset viewer.