SimMud.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 //
00018 
00024 #include "SimMud.h"
00025 #include "ScribeMessage_m.h"
00026 #include <limits.h>
00027 #include <GlobalStatistics.h>
00028 
00029 Define_Module(SimMud);
00030 
00031 using namespace std;
00032 
00033 SimMud::SimMud()
00034 {
00035     currentRegionX = currentRegionY = INT_MIN;
00036     currentRegionID = OverlayKey::UNSPECIFIED_KEY;
00037     playerTimer = new cMessage("playerTimeout");
00038 }
00039 
00040 SimMud::~SimMud()
00041 {
00042     cancelAndDelete(playerTimer);
00043 }
00044 
00045 void SimMud::finishApp()
00046 {
00047     simtime_t time = globalStatistics->calcMeasuredLifetime(creationTime);
00048     if(time == 0) return;
00049 
00050     globalStatistics->addStdDev("SimMUD: Lost or too long delayed MoveLists/s",
00051                                 lostMovementLists / time);
00052     globalStatistics->addStdDev("SimMUD: Received valid MoveLists/s",
00053                                 receivedMovementLists / time);
00054 }
00055 
00056 void SimMud::initializeApp(int stage)
00057 {
00058     if( stage != (numInitStages()-1)) {
00059         return;
00060     }
00061 
00062     // FIXME: areaDimension is not used consistently between all overlays!
00063     fieldSize = par("areaDimension");
00064     numSubspaces = par("numSubspaces");
00065     regionSize = fieldSize / numSubspaces;
00066     playerTimeout = par("playerTimeout");
00067 
00068     receivedMovementLists = 0;
00069     lostMovementLists = 0;
00070 
00071     maxMoveDelay = par("maxMoveDelay");
00072     AOIWidth = par("AOIWidth");
00073 
00074     WATCH(currentRegionX);
00075     WATCH(currentRegionY);
00076     WATCH(currentRegionID);
00077 //    WATCH_MAP( playerMap );
00078 }
00079 
00080 bool SimMud::handleRpc( BaseCallMessage* msg )
00081 {
00082 //    RPC_SWITCH_START(msg);
00083 //    RPC_SWITCH_END( );
00084 //    return RPC_HANDLED;
00085       return false;
00086 }
00087 
00088 void SimMud::handleRpcResponse( BaseResponseMessage* msg,
00089                                 cPolymorphic* context,
00090                                 int rpcId, simtime_t rtt )
00091 {
00092 //    RPC_SWITCH_START(msg);
00093 //    RPC_SWITCH_END( );
00094 }
00095 
00096 void SimMud::handleTimerEvent( cMessage *msg )
00097 {
00098     if( msg == playerTimer ) {
00099         // Check if any player didn't send any updates since last check
00100         map<NodeHandle, PlayerInfo>::iterator it;
00101         list<NodeHandle> erasePlayers;
00102         for( it = playerMap.begin(); it != playerMap.end(); ++it ) {
00103             if( it->second.update == false ) {
00104                 erasePlayers.push_back( it->first );
00105             }
00106             it->second.update = false;
00107         }
00108         for( list<NodeHandle>::iterator it = erasePlayers.begin(); it != erasePlayers.end(); ++it) {
00109             // Delete all neighbors that didn't update (and inform app)
00110             GameAPIListMessage *scMsg = new GameAPIListMessage("NEIGHBOR_UPDATE");
00111             scMsg->setCommand(NEIGHBOR_UPDATE);
00112             scMsg->setRemoveNeighborArraySize(1);
00113             scMsg->setRemoveNeighbor(0, *it);
00114             send(scMsg, "to_upperTier");
00115 
00116             playerMap.erase( *it );
00117         }
00118         erasePlayers.clear();
00119 
00120         scheduleAt( simTime() + playerTimeout, msg );
00121     }
00122 }
00123 
00124 void SimMud::handleLowerMessage( cMessage *msg )
00125 {
00126     if (ALMMulticastMessage* mcastMsg =
00127             dynamic_cast<ALMMulticastMessage*>(msg) ){
00128 
00129         cMessage* innerMsg = mcastMsg->decapsulate();
00130         SimMudMoveMessage* moveMsg = NULL;
00131         if( innerMsg ) {
00132                 moveMsg = dynamic_cast<SimMudMoveMessage*>(innerMsg);
00133         }
00134         if( moveMsg ) {
00135             handleOtherPlayerMove( moveMsg );
00136         }
00137         delete innerMsg;
00138         delete mcastMsg;
00139     }
00140 }
00141 
00142 void SimMud::handleReadyMessage( CompReadyMessage *msg )
00143 {
00144     if( msg->getReady() ) {
00145         // TODO/FIXME: maybe ugly? send CompReadyMessage directly from BaseApp to SimpleGameClient?
00146         send(msg, "to_upperTier");
00147         // also send AOI size to SimpleGameClient
00148         GameAPIResizeAOIMessage* gameMsg = new GameAPIResizeAOIMessage("RESIZE_AOI");
00149         gameMsg->setCommand(RESIZE_AOI);
00150         gameMsg->setAOIsize(AOIWidth);
00151         send(gameMsg, "to_upperTier");
00152 
00153         if( playerTimeout > 0 ) {
00154             cancelEvent( playerTimer );
00155             scheduleAt( simTime() + playerTimeout, playerTimer );
00156         }
00157     }
00158     //delete msg;
00159 }
00160 
00161 void SimMud::handleUpperMessage( cMessage *msg )
00162 {
00163     if( GameAPIPositionMessage* moveMsg =
00164         dynamic_cast<GameAPIPositionMessage*>(msg) ) {
00165         handleMove( moveMsg );
00166         delete msg;
00167     }
00168 }
00169 
00170 void SimMud::handleMove( GameAPIPositionMessage* msg )
00171 {
00172     if( (int) (msg->getPosition().x/regionSize) != currentRegionX ||
00173             (int) (msg->getPosition().y/regionSize) != currentRegionY ) {
00174         // New position is outside of current region; change currentRegion
00175 
00176         // get region ID
00177         currentRegionX = (int) (msg->getPosition().x/regionSize);
00178         currentRegionY = (int) (msg->getPosition().y/regionSize);
00179         std::stringstream regionstr;
00180         regionstr << currentRegionX << ":" << currentRegionY;
00181         OverlayKey region = OverlayKey::sha1( BinaryValue(regionstr.str() ));
00182         currentRegionID = region;
00183     }
00184 
00185     set<OverlayKey> expectedRegions;
00186     set<OverlayKey> allowedRegions;
00187     int minX = (int) ((msg->getPosition().x - AOIWidth)/regionSize);
00188     if( minX < 0 ) minX = 0;
00189     int maxX = (int) ((msg->getPosition().x + AOIWidth)/regionSize);
00190     if( maxX >= numSubspaces ) maxX = numSubspaces -1;
00191     int minY = (int) ((msg->getPosition().y - AOIWidth)/regionSize);
00192     if( minY < 0 ) minY = 0;
00193     int maxY = (int) ((msg->getPosition().y + AOIWidth)/regionSize);
00194     if( maxY >= numSubspaces ) maxY = numSubspaces -1;
00195 
00196     // FIXME: make parameter: unsubscription size
00197     int minUnsubX = (int) ((msg->getPosition().x - 1.5*AOIWidth)/regionSize);
00198     if( minUnsubX < 0 ) minUnsubX = 0;
00199     int maxUnsubX = (int) ((msg->getPosition().x + 1.5*AOIWidth)/regionSize);
00200     if( maxUnsubX >= numSubspaces ) maxUnsubX = numSubspaces -1;
00201     int minUnsubY = (int) ((msg->getPosition().y - 1.5*AOIWidth)/regionSize);
00202     if( minUnsubY < 0 ) minUnsubY = 0;
00203     int maxUnsubY = (int) ((msg->getPosition().y + 1.5+AOIWidth)/regionSize);
00204     if( maxUnsubY >= numSubspaces ) maxUnsubY = numSubspaces -1;
00205 
00206     for( int x = minUnsubX; x <= maxUnsubX; ++x ){
00207         for( int y = minUnsubY; y <= maxUnsubY; ++y ){
00208             std::stringstream regionstr;
00209             regionstr << x << ":" << y;
00210             if( x >= minX && x <=maxX && y >= minY && y <= maxY ){
00211                 expectedRegions.insert( OverlayKey::sha1( BinaryValue(regionstr.str() )));
00212             }
00213             allowedRegions.insert( OverlayKey::sha1( BinaryValue(regionstr.str() )));
00214         }
00215     }
00216 
00217     set<OverlayKey>::iterator subIt = subscribedRegions.begin();
00218     while( subIt != subscribedRegions.end() ){
00219 
00220         expectedRegions.erase( *subIt );
00221 
00222         // unsubscribe region if to far away
00223         if( allowedRegions.find( *subIt ) == allowedRegions.end() ){
00224             // Inform other players about region leave
00225             SimMudMoveMessage* moveMsg = new SimMudMoveMessage("MOVE/LEAVE_REGION");
00226             moveMsg->setSrc( overlay->getThisNode() );
00227             moveMsg->setPosX( msg->getPosition().x );
00228             moveMsg->setPosY( msg->getPosition().y );
00229             moveMsg->setTimestamp( simTime() );
00230             moveMsg->setLeaveRegion( true );
00231             ALMMulticastMessage* mcastMsg = new ALMMulticastMessage("MOVE/LEAVE_REGION");
00232             mcastMsg->setGroupId(*subIt);
00233             mcastMsg->encapsulate( moveMsg );
00234 
00235             send(mcastMsg, "to_lowerTier");
00236 
00237              // leave old region's multicastGroup
00238             ALMLeaveMessage* leaveMsg = new ALMLeaveMessage("LEAVE_REGION_GROUP");
00239             leaveMsg->setGroupId(*subIt);
00240             send(leaveMsg, "to_lowerTier");
00241             // TODO: leave old simMud region
00242 
00243            // Erase subspace from subscribedList and increase iterator
00244             subscribedRegions.erase( subIt++ );
00245         } else {
00246             ++subIt;
00247         }
00248     }
00249 
00250     // if any "near" region is not yet subscribed, subscribe
00251     for( set<OverlayKey>::iterator regionIt = expectedRegions.begin(); regionIt != expectedRegions.end(); ++regionIt ){
00252         // join region's multicast group
00253         ALMSubscribeMessage* subMsg = new ALMSubscribeMessage;
00254         subMsg->setGroupId(*regionIt);
00255         send( subMsg, "to_lowerTier" );
00256 
00257         subscribedRegions.insert( *regionIt );
00258         // TODO: join simMud region
00259     }
00260 
00261     // publish movement
00262     SimMudMoveMessage* moveMsg = new SimMudMoveMessage("MOVE");
00263     moveMsg->setSrc( overlay->getThisNode() );
00264     moveMsg->setPosX( msg->getPosition().x );
00265     moveMsg->setPosY( msg->getPosition().y );
00266     moveMsg->setTimestamp( simTime() );
00267     moveMsg->setBitLength( SIMMUD_MOVE_L( moveMsg ));
00268     ALMMulticastMessage* mcastMsg = new ALMMulticastMessage("MOVE");
00269     mcastMsg->setGroupId(currentRegionID);
00270     mcastMsg->encapsulate( moveMsg );
00271 
00272     send(mcastMsg, "to_lowerTier");
00273 }
00274 
00275 void SimMud::handleOtherPlayerMove( SimMudMoveMessage* msg )
00276 {
00277     GameAPIListMessage *scMsg = new GameAPIListMessage("NEIGHBOR_UPDATE");
00278     scMsg->setCommand(NEIGHBOR_UPDATE);
00279 
00280     NodeHandle& src = msg->getSrc();
00281     RECORD_STATS(
00282             if( msg->getTimestamp() < simTime() - maxMoveDelay ){
00283                 ++lostMovementLists;
00284                 globalStatistics->addStdDev("SimMUD: LOST MOVE Delay",
00285                     SIMTIME_DBL(simTime() - msg->getTimestamp()) );
00286             } else {
00287                 ++receivedMovementLists;
00288             }
00289 
00290             if( src != overlay->getThisNode() ){
00291                 globalStatistics->addStdDev("SimMUD: MOVE Delay",
00292                     SIMTIME_DBL(simTime() - msg->getTimestamp()) );
00293             }
00294             );
00295 
00296     if( msg->getLeaveRegion() ) {
00297         // Player leaves region
00298         scMsg->setRemoveNeighborArraySize(1);
00299         scMsg->setRemoveNeighbor(0, src);
00300         playerMap.erase( src );
00301 
00302     } else {
00303         PlayerInfo player;
00304         player.pos = Vector2D( msg->getPosX(), msg->getPosY() );
00305         player.update = true;
00306 
00307         pair< map<NodeHandle, PlayerInfo>::iterator, bool> inserter =
00308             playerMap.insert( make_pair(src, player) );
00309 
00310         /*    if( inserter.second ) {
00311             // new player
00312 
00313         } else {
00314             // move player
00315         }*/
00316 
00317         // Ordinary move
00318         scMsg->setAddNeighborArraySize(1);
00319         scMsg->setNeighborPositionArraySize(1);
00320         scMsg->setAddNeighbor(0, src);
00321         scMsg->setNeighborPosition(0, Vector2D(msg->getPosX(), msg->getPosY()) );
00322     }
00323     send(scMsg, "to_upperTier");
00324 
00325 }
00326 

Generated on Wed Mar 25 16:32:49 2009 for OverSim by  doxygen 1.5.5