#include <SimMud.h>

Definition at line 33 of file SimMud.h.
Public Member Functions | |
| SimMud () | |
| ~SimMud () | |
| virtual void | initializeApp (int stage) |
| Initialize class attributes. | |
| virtual void | handleTimerEvent (cMessage *msg) |
| processes self-messages | |
| virtual void | handleUpperMessage (cMessage *msg) |
| handleUpperMessage gets called of handleMessage(cMessage* msg) if msg arrivedOn from_upperTier (currently msg gets deleted in this function) | |
| virtual void | handleLowerMessage (cMessage *msg) |
| method to handle non-commonAPI messages from the overlay | |
| virtual void | handleReadyMessage (CompReadyMessage *msg) |
| method to handle ready messages from the overlay | |
| virtual bool | handleRpc (BaseCallMessage *msg) |
| Processes Remote-Procedure-Call invokation messages. | |
| virtual void | handleRpcResponse (BaseResponseMessage *msg, cPolymorphic *context, int rpcId, simtime_t rtt) |
| This method is called if an RPC response has been received. | |
| virtual void | finishApp () |
| collect statistical data | |
Protected Member Functions | |
| void | handleMove (GameAPIPositionMessage *msg) |
| void | handleOtherPlayerMove (SimMudMoveMessage *msg) |
Private Attributes | |
| int | currentRegionX |
| int | currentRegionY |
| OverlayKey | currentRegionID |
| std::set< OverlayKey > | subscribedRegions |
| int | fieldSize |
| int | numSubspaces |
| int | regionSize |
| int | AOIWidth |
| int | receivedMovementLists |
| int | lostMovementLists |
| simtime_t | maxMoveDelay |
| int | playerTimeout |
| cMessage * | playerTimer |
| std::map< NodeHandle, PlayerInfo > | playerMap |
Classes | |
| struct | PlayerInfo |
| SimMud::SimMud | ( | ) |
Definition at line 33 of file SimMud.cc.
00034 { 00035 currentRegionX = currentRegionY = INT_MIN; 00036 currentRegionID = OverlayKey::UNSPECIFIED_KEY; 00037 playerTimer = new cMessage("playerTimeout"); 00038 }
| SimMud::~SimMud | ( | ) |
| void SimMud::initializeApp | ( | int | stage | ) | [virtual] |
Initialize class attributes.
Reimplemented from BaseApp.
Definition at line 56 of file SimMud.cc.
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 }
| void SimMud::handleTimerEvent | ( | cMessage * | msg | ) | [virtual] |
processes self-messages
method to handle self-messages should be overwritten in derived application if needed
| msg | self-message |
Reimplemented from BaseApp.
Definition at line 96 of file SimMud.cc.
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 }
| void SimMud::handleUpperMessage | ( | cMessage * | msg | ) | [virtual] |
handleUpperMessage gets called of handleMessage(cMessage* msg) if msg arrivedOn from_upperTier (currently msg gets deleted in this function)
| msg | the message to handle |
Reimplemented from BaseApp.
Definition at line 161 of file SimMud.cc.
00162 { 00163 if( GameAPIPositionMessage* moveMsg = 00164 dynamic_cast<GameAPIPositionMessage*>(msg) ) { 00165 handleMove( moveMsg ); 00166 delete msg; 00167 } 00168 }
| void SimMud::handleLowerMessage | ( | cMessage * | msg | ) | [virtual] |
method to handle non-commonAPI messages from the overlay
| msg | message to handle |
Reimplemented from BaseApp.
Definition at line 124 of file SimMud.cc.
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 }
| void SimMud::handleReadyMessage | ( | CompReadyMessage * | msg | ) | [virtual] |
method to handle ready messages from the overlay
| msg | message to handle |
Reimplemented from BaseApp.
Definition at line 142 of file SimMud.cc.
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 }
| bool SimMud::handleRpc | ( | BaseCallMessage * | msg | ) | [virtual] |
Processes Remote-Procedure-Call invokation messages.
This method should be overloaded when the overlay provides RPC functionality.
Reimplemented from BaseRpc.
Definition at line 80 of file SimMud.cc.
00081 { 00082 // RPC_SWITCH_START(msg); 00083 // RPC_SWITCH_END( ); 00084 // return RPC_HANDLED; 00085 return false; 00086 }
| void SimMud::handleRpcResponse | ( | BaseResponseMessage * | msg, | |
| cPolymorphic * | context, | |||
| int | rpcId, | |||
| simtime_t | rtt | |||
| ) | [virtual] |
This method is called if an RPC response has been received.
| msg | The response message. | |
| context | Pointer to an optional state object. The object has to be handled/deleted by the handleRpcResponse() code | |
| rpcId | The RPC id. | |
| rtt | The Round-Trip-Time of this RPC |
Reimplemented from RpcListener.
Definition at line 88 of file SimMud.cc.
| void SimMud::finishApp | ( | ) | [virtual] |
collect statistical data
Reimplemented from BaseApp.
Definition at line 45 of file SimMud.cc.
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 }
| void SimMud::handleMove | ( | GameAPIPositionMessage * | msg | ) | [protected] |
Definition at line 170 of file SimMud.cc.
Referenced by handleUpperMessage().
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 }
| void SimMud::handleOtherPlayerMove | ( | SimMudMoveMessage * | msg | ) | [protected] |
Definition at line 275 of file SimMud.cc.
Referenced by handleLowerMessage().
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 }
int SimMud::currentRegionX [private] |
int SimMud::currentRegionY [private] |
OverlayKey SimMud::currentRegionID [private] |
std::set<OverlayKey> SimMud::subscribedRegions [private] |
int SimMud::fieldSize [private] |
int SimMud::numSubspaces [private] |
int SimMud::regionSize [private] |
int SimMud::AOIWidth [private] |
Definition at line 44 of file SimMud.h.
Referenced by handleMove(), handleReadyMessage(), and initializeApp().
int SimMud::receivedMovementLists [private] |
Definition at line 46 of file SimMud.h.
Referenced by finishApp(), handleOtherPlayerMove(), and initializeApp().
int SimMud::lostMovementLists [private] |
Definition at line 47 of file SimMud.h.
Referenced by finishApp(), handleOtherPlayerMove(), and initializeApp().
simtime_t SimMud::maxMoveDelay [private] |
int SimMud::playerTimeout [private] |
Definition at line 50 of file SimMud.h.
Referenced by handleReadyMessage(), handleTimerEvent(), and initializeApp().
cMessage* SimMud::playerTimer [private] |
Definition at line 51 of file SimMud.h.
Referenced by handleReadyMessage(), handleTimerEvent(), SimMud(), and ~SimMud().
std::map<NodeHandle, PlayerInfo> SimMud::playerMap [private] |
Definition at line 58 of file SimMud.h.
Referenced by handleOtherPlayerMove(), and handleTimerEvent().
1.5.5