/* * ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Author: * Oliver Scheuss , (C) 2008 * Co-authors: * ... * */ #include "TrafficControl.h" #include #include namespace network { /** *Initializing protected members */ TrafficControl *TrafficControl::instance_=0; /** * @brief Constructor: assures that only one reference will be created and sets the pointer */ TrafficControl::TrafficControl() { assert(instance_=0); instance_=this; targetSize = 5000;//5000bytes } /** * @brief Destructor: resets the instance pointer to 0 */ TrafficControl::~TrafficControl() { //was macht das genau? da instance ja gleich this ist im moment instance_=0; //muss ich alles deallozieren was ich im constructor aufgebaut habe? } /** *Definition of public members */ /** *eigener sortieralgorithmus */ // bool TrafficControl::priodiffer(obj i, obj j) // { // std::map::iterator iti; // std::map::iterator itj; // iti=listToProcess_.find(i.objID); // itj=listToProcess_.find(j.objID); // return iti->second.objValuePerm < itj->second.objValuePerm; // } /** * eigener sortieralgorithmus */ bool TrafficControl::priodiffer(uint32_t clientID, obj i, obj j) { assert(clientListPerm_.find(clientID) != clientListPerm_.end()); //make sure the client exists in our list assert(clientListPerm_[clientID].find(i.objID) != clientListPerm_[clientID].end()); // make sure the object i is in the client list assert(clientListPerm_[clientID].find(j.objID) != clientListPerm_[clientID].end()); // make sure the object j is in the client list int prio1 = clientListPerm_[clientID][i.objID].objValuePerm + clientListPerm_[clientID][i.objID].objValueSched; int prio2 = clientListPerm_[clientID][j.objID].objValuePerm + clientListPerm_[clientID][j.objID].objValueSched; // NOTE: smaller priority is better return prio1 < prio2; } void TrafficControl::processObjectList(unsigned int clientID, unsigned int gamestateID, std::vector *list) { // copiedVector = *list; currentClientID=clientID; currentGamestateID=gamestateID; evaluateList(clientID, list); //list hatte vorher ja vielmehr elemente, nach zuweisung nicht mehr... speicherplatz?? // *list=copiedVector; //später wird copiedVector ja überschrieben, ist das ein problem für list-dh. für gamestatemanager? return; } void TrafficControl::processAck(unsigned int clientID, unsigned int gamestateID) { std::vector::iterator itvec; // iterator to iterate through the acked objects //assertions to make sure the maps already exist assert(clientListTemp_.find(clientID) != clientListTemp_.end() ); assert(clientListPerm_.find(clientID) != clientListPerm_.end() ); assert( clientListTemp_[clientID].find(gamestateID) != clientListTemp_[clientID].end() ); for(itvec = clientListTemp_[clientID][gamestateID].begin(); itvec != clientListTemp_[clientID][gamestateID].end(); itvec++) { if(clientListPerm_[clientID].find((*itvec).objID) != clientListPerm_[clientID].end()) // check whether the obj already exists in our lists { clientListPerm_[clientID][(*itvec).objID].objID = gamestateID; } else { clientListPerm_[clientID][(*itvec).objID].objCurGS = gamestateID; clientListPerm_[clientID][(*itvec).objID].objID = (*itvec).objID; clientListPerm_[clientID][(*itvec).objID].objCreatorID = (*itvec).objCreatorID; clientListPerm_[clientID][(*itvec).objID].objSize = (*itvec).objSize; } } // remove temporary vector (with acked objects) from the map clientListTemp_[clientID].erase( clientListTemp_[clientID].find(gamestateID) ); } /** *Definition of private members */ /** *copyList gets vector of Gamestate Manager and turns it to *listToProcess */ // void TrafficControl::copyList(std::vector *list) // { // std::vector::iterator itvec; // for(itvec = (*list).begin(); itvec != (*list).end(); itvec++) // { // objInfo objectA; // objectA.objCreatorID=(*itvec).objCreatorID; // objectA.objSize = (*itvec).objSize; // listToProcess_.insert(std::pair>);//unsicher: ob map<...> so richtig ist // } // } /** *updateReferenceList compares the sent list by GSmanager with the current *reference list and updates it. *returns void */ // void TrafficControl::updateReferenceList(std::map *list) // { // std::map::iterator itref; // std::map::iterator itproc; // for(itproc=listToProcess_.begin(); itproc != listToProcess_.end(); itproc++) // { // //itproc->first=objectid that is looked for // if(referenceList_->find(itproc->first)) // { // continue; // } // else // { // (*referenceList_).insert(pair((*itproc).first,Synchronisable::getSynchronisable((*itproc).first)));//important: how to get adress of an object! // insertinClientListPerm(currentClientID,itproc->first,itproc->second); // } // } // } /** *updateClientListPerm *returns void */ void TrafficControl::insertinClientListPerm(unsigned int clientID, obj objinf) { std::map >::iterator itperm;//iterator clientListPerm over clientIDs // itperm = (clientListPerm_).find(clientID); // assert(itperm != clientListPerm_.end() ); unsigned int gsid=GAMESTATEID_INITIAL, gsdiff=currentGamestateID, prioperm=Synchronisable::getSynchronisable(objinf.objID)->getPriority(), priomom=0; clientListPerm_[clientID][objinf.objID] = objInfo(objinf.objID, objinf.objCreatorID,gsid,gsdiff, objinf.objSize,prioperm,priomom); // itperm->second.insert(std::pair(objid,objinf)); // permObjPrio_.insert(objid, objinf.objValuePerm); } /** * updateClientListTemp * takes the shortened vector which will be sent to the gsmanager and puts the *info into clientListTemp */ void TrafficControl::updateClientListTemp(std::vector *list) { clientListTemp_[currentClientID][currentGamestateID] = std::vector(*list); // std::vector::iterator itvec; // std::map >::iterator ittemp; // std::map::iterator ittempgs; // ittemp = clientListTemp_.find(currentClientID); // ittempgs = (*ittemp).find(currentGamestateID); // for(itvec = list.begin(); itvec!=list.end(), itvec++) // { // ittempgs.insert(itvec); // } } /** *cut *takes the current vector that has to be returned to the gsmanager and shortens it in criteria of bandwidth of clientID(XY) */ void TrafficControl::cut(std::vector *list, unsigned int targetsize) { unsigned int size=0; std::vector::iterator itvec; itvec = list->begin(); for(itvec = list->begin(); itvec != list->end() && sizeerase(itvec++); } } } /** *evaluateList evaluates whether new obj are there, whether there are things to be updatet and manipulates all this. */ void TrafficControl::evaluateList(unsigned int clientID, std::vector *list) { //copyList(list); //now the sorting //compare listToProcess vs clientListPerm //if listToProcess contains new Objects, add them to clientListPerm // std::map::iterator itproc; std::vector::iterator itvec; // std::map >::iterator itperm; // std::map::iterator itpermobj; for( itvec=list->begin(); itvec != list->end(); itvec++) { // itperm = clientListPerm_.find(clientID); if ( clientListPerm_[clientID].find( (*itvec).objID) != clientListPerm_[clientID].end() ) { // we already have the object in our map //obj bleibt in liste und permanente prio wird berechnet clientListPerm_[clientID][(*itvec).objID].objDiffGS = currentGamestateID - clientListPerm_[clientID][(*itvec).objID].objCurGS; // ((*itpermobj).second).objDiffGS = ((*itpermobj).second).objCurGS - currentGamestateID; // permprio = clientListPerm_[clientID][(*itproc).first].objValuePerm; // itpermprio = (permObjPrio_).find((*itproc).first); // ((*itpermobj).second).objValuePerm = ((*itpermobj).second).objDiffGS * (*itpermprio).second; continue;//check next objId } else { // insert the object into clientListPerm insertinClientListPerm(clientID,*itvec); // itpermobj=(*itperm).find((*itproc).first) // ((*itpermobj).second).objDiffGS = ((*itpermobj).second).objCurGS - currentGamestateID; // itpermprio = (permObjPrio_).find((*itproc).first); // ((*itpermobj).second).objValuePerm = ((*itpermobj).second).objDiffGS * (*itpermprio).second; continue;//check next objId } } //end compare listToProcess vs clientListPerm //listToProc vs clientListTemp //TODO: uncomment it again and change some things /* std::map >::iterator ittemp; std::map::iterator ittempgs; for( itproc=listToProcess_.begin(); itproc != listToProcess_.end();itproc++) { ittemp = clientListTemp_->find(currentClientID); if( ittempgs = (*ittemp).find(currentGamestateID)) { if((*itproc).first == (*ittempgs).find((*itproc).first))//ja, dann ist objekt schon in der zu sendenden liste-muss nicht nochmal gesendet werden { (listToProcess_).erase (itproc); } else continue; } else continue; }*/ //end listToProc vs clientListTemp //TODO: check whether we need this, cause i don't think so. //listToProcess contains obj to send now, and since we give gsmanager the copiedvector and not listToProcess shorten copiedvector therefor too. /*std::vector::iterator itvec; for(itvec = copiedVector.begin(); itvec != copiedVector.end(); itvec++) { if ( listToProcess_.find(itvec->objID) ) { continue;//therefore object wasnt thrown out yet and has to be sent back to gsmanager } else { copiedVector.remove(itvec); } }*/ //sort copied vector aufgrund der objprioperm in clientlistperm // use boost bind here because we need to pass a memberfunction to stl sort sort(list->begin(), list->end(), boost::bind(&TrafficControl::priodiffer, this, clientID, _1, _2) ); //now we check, that the creator of an object always exists on a client std::vector::iterator itcreator; for(itvec = list->begin(); itvec != list->end(); itvec++) { if ( (*itvec).objCreatorID != OBJECTID_UNKNOWN ) { if( clientListPerm_[clientID][(*itvec).objCreatorID].objCurGS != GAMESTATEID_INITIAL ) continue; for( itcreator = list->begin(); itcreator != list->end(); itcreator++) { if ( (*itcreator).objID == (*itvec).objCreatorID ) break; } // if the creator is before the object everything is fine if( itcreator < itvec ) continue; // otherwise insert the creator right before our object and delete it at the old position list->insert(itvec, *itcreator); list->erase(itcreator); } } //end of sorting //now the cutting, work the same obj out in processobjectlist and copiedvector, compression rate muss noch festgelegt werden. cut(list, targetSize); //diese Funktion updateClientList muss noch gemacht werden updateClientListTemp(list); //end of sorting } /* void bvlabla(vector *a){ //sort a vector *cache; cache = new vector(*a); return a; } */ }//namespace network