OverSim
SimMud Class Reference

#include <SimMud.h>

Inheritance diagram for SimMud:
BaseApp BaseRpc BaseTcpSupport RpcListener

Classes

struct  PlayerInfo

Public Member Functions

 SimMud ()
 ~SimMud ()
virtual void initializeApp (int stage)
 Initialize class attributes.
virtual void handleTimerEvent (cMessage *msg)
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)
 processes self-messages
virtual void handleReadyMessage (CompReadyMessage *msg)
 method to handle ready messages from the overlay
virtual bool handleRpcCall (BaseCallMessage *msg)
 Processes Remote-Procedure-Call invocation 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
- Public Member Functions inherited from BaseApp
 BaseApp ()
virtual ~BaseApp ()
 virtual destructor
- Public Member Functions inherited from BaseRpc
 BaseRpc ()
const NodeHandlegetThisNode ()
 Returns the NodeHandle of this node.
simtime_t getUdpTimeout ()
- Public Member Functions inherited from RpcListener
virtual ~RpcListener ()
 destructor
- Public Member Functions inherited from BaseTcpSupport
virtual void socketDataArrived (int connId, void *yourPtr, cPacket *msg, bool urgent)
virtual void socketEstablished (int connId, void *yourPtr)
virtual void socketPeerClosed (int connId, void *yourPtr)
virtual void socketFailure (int connId, void *yourPtr, int code)
virtual void socketStatusArrived (int connId, void *yourPtr, TCPStatusInfo *status)

Protected Member Functions

void handleMove (GameAPIPositionMessage *msg)
void handleOtherPlayerMove (SimMudMoveMessage *msg)
- Protected Member Functions inherited from BaseApp
int numInitStages () const
 method to set InitStage
void initialize (int stage)
 initializes base class-attributes
void handleMessage (cMessage *msg)
 checks for message type and calls corresponding method
virtual void receiveChangeNotification (int category, const cPolymorphic *details)
 callback-method for events at the NotificationBoard
virtual void handleTransportAddressChangedNotification ()
 This method gets call if the node has a new TransportAddress (IP address) because he changed his access network.
virtual void handleNodeLeaveNotification ()
 This method gets call **.gracefulLeaveDelay seconds before it is killed.
virtual void handleNodeGracefulLeaveNotification ()
 This method gets call **.gracefulLeaveDelay seconds before it is killed if this node is among the gracefulLeaveProbability nodes.
void finish ()
 collects statistical data
void callRoute (const OverlayKey &key, cPacket *msg, const TransportAddress &hint=TransportAddress::UNSPECIFIED_NODE, RoutingType routingType=DEFAULT_ROUTING)
 Common API function: calls route-method in overlay.
void callRoute (const OverlayKey &key, cPacket *msg, const std::vector< TransportAddress > &sourceRoute, RoutingType routingType=DEFAULT_ROUTING)
virtual void deliver (OverlayKey &key, cMessage *msg)
 Common API function: handles delivered messages from overlay.
virtual void forward (OverlayKey *key, cPacket **msg, NodeHandle *nextHopNode)
 Common API function: handles messages from overlay to be forwarded.
virtual void update (const NodeHandle &node, bool joined)
 Common API function: informs application about neighbors and own nodeID.
NodeVectorcallLocalLookup (const OverlayKey &key, int num, bool safe)
 Common API function: produces a list of nodes that can be used as next hops towards key.
NodeVectorcallNeighborSet (int num)
 Common API function: produces a list of neighbor nodes.
bool isSiblingFor (const NodeHandle &node, const OverlayKey &key, int numSiblings, bool *err)
 Query if a node is among the siblings for a given key.
virtual void handleUDPMessage (cMessage *msg)
 method to handle messages that come directly from the UDP gate
virtual void bindToPort (int port)
 Tells UDP we want to get all packets arriving on the given port.
virtual void sendMessageToUDP (const TransportAddress &destAddr, cPacket *msg, simtime_t delay=SIMTIME_ZERO)
 Sends a packet over UDP.
virtual void handleTraceMessage (cMessage *msg)
 handleTraceMessage gets called of handleMessage(cMessage* msg) if a message arrives at trace_in.
void sendMessageToLowerTier (cPacket *msg)
 sends non-commonAPI message to the lower tier
bool internalHandleRpcCall (BaseCallMessage *msg)
 Handles internal rpc requests.
void internalHandleRpcResponse (BaseResponseMessage *msg, cPolymorphic *context, int rpcId, simtime_t rtt)
 Handles rpc responses internal in base classes

void internalSendRouteRpc (BaseRpcMessage *message, const OverlayKey &destKey, const std::vector< TransportAddress > &sourceRoute, RoutingType routingType)
virtual CompType getThisCompType ()
 Return the component type of this module.
void sendReadyMessage (bool ready=true, const OverlayKey &nodeId=OverlayKey::UNSPECIFIED_KEY)
- Protected Member Functions inherited from BaseRpc
virtual void internalHandleRpcTimeout (BaseCallMessage *msg, const TransportAddress &dest, cPolymorphic *context, int rpcId, const OverlayKey &destKey)
 Handles rpc timeouts internal in base classes

void initRpcs ()
 Initializes Remote-Procedure state.
void finishRpcs ()
 Deinitializes Remote-Procedure state.
virtual void internalHandleRpcMessage (BaseRpcMessage *msg)
 Handles incoming rpc messages and delegates them to the corresponding listeners or handlers.
uint32_t sendRouteRpcCall (CompType destComp, const TransportAddress &dest, const OverlayKey &destKey, BaseCallMessage *msg, cPolymorphic *context=NULL, RoutingType routingType=DEFAULT_ROUTING, simtime_t timeout=-1, int retries=0, int rpcId=-1, RpcListener *rpcListener=NULL)
 Routes a Remote-Procedure-Call message to an OverlayKey.
uint32_t sendRouteRpcCall (CompType destComp, const OverlayKey &destKey, BaseCallMessage *msg, cPolymorphic *context=NULL, RoutingType routingType=DEFAULT_ROUTING, simtime_t timeout=-1, int retries=0, int rpcId=-1, RpcListener *rpcListener=NULL)
 Routes a Remote-Procedure-Call message to an OverlayKey.
uint32_t sendRouteRpcCall (CompType destComp, const TransportAddress &dest, BaseCallMessage *msg, cPolymorphic *context=NULL, RoutingType routingType=DEFAULT_ROUTING, simtime_t timeout=-1, int retries=0, int rpcId=-1, RpcListener *rpcListener=NULL)
 Sends a Remote-Procedure-Call message using the overlay's UDP port
This replaces ROUTE_DIRECT calls!
uint32_t sendUdpRpcCall (const TransportAddress &dest, BaseCallMessage *msg, cPolymorphic *context=NULL, simtime_t timeout=-1, int retries=0, int rpcId=-1, RpcListener *rpcListener=NULL)
 Sends a Remote-Procedure-Call message to the underlay

uint32_t sendInternalRpcCall (CompType destComp, BaseCallMessage *msg, cPolymorphic *context=NULL, simtime_t timeout=-1, int retries=0, int rpcId=-1, RpcListener *rpcListener=NULL)
 Sends an internal Remote-Procedure-Call between two tiers

void cancelRpcMessage (uint32_t nonce)
 Cancels a Remote-Procedure-Call.
void cancelAllRpcs ()
 Cancels all RPCs.
void sendRpcResponse (TransportType transportType, CompType destComp, const TransportAddress &dest, const OverlayKey &destKey, BaseCallMessage *call, BaseResponseMessage *response)
 Send Remote-Procedure response message and deletes call message.
void sendRpcResponse (BaseCallMessage *call, BaseResponseMessage *response)
 Send Remote-Procedure response message to UDP and deletes call message.
int pingNode (const TransportAddress &dest, simtime_t timeout=-1, int retries=0, cPolymorphic *context=NULL, const char *caption="PING", RpcListener *rpcListener=NULL, int rpcId=-1, TransportType transportType=INVALID_TRANSPORT)
 ping a node by its TransportAddress
virtual void pingResponse (PingResponse *pingResponse, cPolymorphic *context, int rpcId, simtime_t rtt)
virtual void pingTimeout (PingCall *pingCall, const TransportAddress &dest, cPolymorphic *context, int rpcId)
bool internalHandleMessage (cMessage *msg)
- Protected Member Functions inherited from RpcListener
virtual void handleRpcResponse (BaseResponseMessage *msg, const RpcState &rpcState, simtime_t rtt)
 This method is called if an RPC response has been received.
virtual void handleRpcTimeout (BaseCallMessage *msg, const TransportAddress &dest, cPolymorphic *context, int rpcId, const OverlayKey &destKey)
 This method is called if an RPC timeout has been reached.
virtual void handleRpcTimeout (const RpcState &rpcState)
 This method is called if an RPC timeout has been reached.
- Protected Member Functions inherited from BaseTcpSupport
void handleTCPMessage (cMessage *msg)
 Member function to handle incoming TCP messages.
void bindAndListenTcp (int port)
 Member function to bind service to the specified port and listen afterwards.
bool isAlreadyConnected (TransportAddress address)
 Member function to check if the service is already connected.
void establishTcpConnection (TransportAddress address)
 Member function to establish a connection to the specified node.
void sendTcpData (cPacket *msg, TransportAddress address)
 Member function to send TCP data to the specified node.
virtual void handleConnectionEvent (EvCode code, TransportAddress address)
 Member function to handle passive connection events.
virtual void handleDataReceived (TransportAddress address, cPacket *msg, bool urgent)
 Member function to handle incoming data.
virtual void handleIncomingConnection (TransportAddress address)
 Member function to handle newly opened connections.
void closeTcpConnection (TransportAddress address)
 Member function to close an established connection.
void setTcpOut (cGate *gate)
 Member function to set local gate towards the TCP module during init phase.
cGate * getTcpOut ()
 Member function to get local gate towards the TCP module.

Private Attributes

int currentRegionX
int currentRegionY
OverlayKey currentRegionID
std::set< OverlayKeysubscribedRegions
int fieldSize
int numSubspaces
int regionSize
int AOIWidth
int receivedMovementLists
int lostMovementLists
simtime_t maxMoveDelay
int playerTimeout
cMessage * playerTimer
std::map< NodeHandle, PlayerInfoplayerMap

Additional Inherited Members

- Public Types inherited from BaseTcpSupport
enum  EvCode {
  NO_EST_CONNECTION, PEER_CLOSED, PEER_TIMEDOUT, PEER_REFUSED,
  CONNECTION_RESET, CONNECTION_SUCC_ClOSED
}
- Protected Attributes inherited from BaseApp
UnderlayConfiguratorunderlayConfigurator
 pointer to UnderlayConfigurator in this node
GlobalNodeListglobalNodeList
 pointer to GlobalNodeList in this node
GlobalStatisticsglobalStatistics
 pointer to GlobalStatistics module in this node
NotificationBoard * notificationBoard
 pointer to NotificationBoard in this node
bool debugOutput
 debug output yes/no?
int numOverlaySent
 number of sent packets to overlay
int bytesOverlaySent
 number of sent bytes to overlay
int numOverlayReceived
 number of received packets from overlay
int bytesOverlayReceived
 number of received bytes from overlay
int numUdpSent
 number of sent packets to UDP
int bytesUdpSent
 number of sent bytes to UDP
int numUdpReceived
 number of received packets from UDP
int bytesUdpReceived
 number of received bytes from UDP
simtime_t creationTime
 simTime when the App has been created

Detailed Description

Definition at line 33 of file SimMud.h.

Constructor & Destructor Documentation

SimMud::SimMud ( )

Definition at line 33 of file SimMud.cc.

{
playerTimer = new cMessage("playerTimeout");
}
SimMud::~SimMud ( )

Definition at line 40 of file SimMud.cc.

{
cancelAndDelete(playerTimer);
}

Member Function Documentation

void SimMud::finishApp ( )
virtual

collect statistical data

Reimplemented from BaseApp.

Definition at line 45 of file SimMud.cc.

{
if (time < GlobalStatistics::MIN_MEASURED) return;
globalStatistics->addStdDev("SimMUD: Lost or too long delayed MoveLists/s",
globalStatistics->addStdDev("SimMUD: Received valid MoveLists/s",
}
void SimMud::handleLowerMessage ( cMessage *  msg)
virtual

processes self-messages

method to handle self-messages should be overwritten in derived application if needed

Parameters
msgself-message method to handle non-commonAPI messages from the overlay
msgmessage to handle

Reimplemented from BaseApp.

Definition at line 124 of file SimMud.cc.

{
if (ALMMulticastMessage* mcastMsg =
dynamic_cast<ALMMulticastMessage*>(msg) ){
cMessage* innerMsg = mcastMsg->decapsulate();
SimMudMoveMessage* moveMsg = NULL;
if( innerMsg ) {
moveMsg = dynamic_cast<SimMudMoveMessage*>(innerMsg);
}
if( moveMsg ) {
}
delete innerMsg;
delete mcastMsg;
}
}
void SimMud::handleMove ( GameAPIPositionMessage msg)
protected

Definition at line 175 of file SimMud.cc.

{
if( (int) (msg->getPosition().x/regionSize) != currentRegionX ||
(int) (msg->getPosition().y/regionSize) != currentRegionY ) {
// New position is outside of current region; change currentRegion
// get region ID
std::stringstream regionstr;
regionstr << currentRegionX << ":" << currentRegionY;
OverlayKey region = OverlayKey::sha1( BinaryValue(regionstr.str() ));
currentRegionID = region;
}
set<OverlayKey> expectedRegions;
set<OverlayKey> allowedRegions;
int minX = (int) ((msg->getPosition().x - AOIWidth)/regionSize);
if( minX < 0 ) minX = 0;
int maxX = (int) ((msg->getPosition().x + AOIWidth)/regionSize);
if( maxX >= numSubspaces ) maxX = numSubspaces -1;
int minY = (int) ((msg->getPosition().y - AOIWidth)/regionSize);
if( minY < 0 ) minY = 0;
int maxY = (int) ((msg->getPosition().y + AOIWidth)/regionSize);
if( maxY >= numSubspaces ) maxY = numSubspaces -1;
// FIXME: make parameter: unsubscription size
int minUnsubX = (int) ((msg->getPosition().x - 1.5*AOIWidth)/regionSize);
if( minUnsubX < 0 ) minUnsubX = 0;
int maxUnsubX = (int) ((msg->getPosition().x + 1.5*AOIWidth)/regionSize);
if( maxUnsubX >= numSubspaces ) maxUnsubX = numSubspaces -1;
int minUnsubY = (int) ((msg->getPosition().y - 1.5*AOIWidth)/regionSize);
if( minUnsubY < 0 ) minUnsubY = 0;
int maxUnsubY = (int) ((msg->getPosition().y + 1.5+AOIWidth)/regionSize);
if( maxUnsubY >= numSubspaces ) maxUnsubY = numSubspaces -1;
for( int x = minUnsubX; x <= maxUnsubX; ++x ){
for( int y = minUnsubY; y <= maxUnsubY; ++y ){
std::stringstream regionstr;
regionstr << x << ":" << y;
if( x >= minX && x <=maxX && y >= minY && y <= maxY ){
expectedRegions.insert( OverlayKey::sha1( BinaryValue(regionstr.str() )));
}
allowedRegions.insert( OverlayKey::sha1( BinaryValue(regionstr.str() )));
}
}
set<OverlayKey>::iterator subIt = subscribedRegions.begin();
while( subIt != subscribedRegions.end() ){
expectedRegions.erase( *subIt );
// unsubscribe region if to far away
if( allowedRegions.find( *subIt ) == allowedRegions.end() ){
// Inform other players about region leave
SimMudMoveMessage* moveMsg = new SimMudMoveMessage("MOVE/LEAVE_REGION");
moveMsg->setSrc( overlay->getThisNode() );
moveMsg->setPosX( msg->getPosition().x );
moveMsg->setPosY( msg->getPosition().y );
moveMsg->setTimestamp( simTime() );
moveMsg->setLeaveRegion( true );
ALMMulticastMessage* mcastMsg = new ALMMulticastMessage("MOVE/LEAVE_REGION");
mcastMsg->setGroupId(*subIt);
mcastMsg->encapsulate( moveMsg );
send(mcastMsg, "to_lowerTier");
// leave old region's multicastGroup
ALMLeaveMessage* leaveMsg = new ALMLeaveMessage("LEAVE_REGION_GROUP");
leaveMsg->setGroupId(*subIt);
send(leaveMsg, "to_lowerTier");
// TODO: leave old simMud region
// Erase subspace from subscribedList and increase iterator
subscribedRegions.erase( subIt++ );
} else {
++subIt;
}
}
// if any "near" region is not yet subscribed, subscribe
for( set<OverlayKey>::iterator regionIt = expectedRegions.begin(); regionIt != expectedRegions.end(); ++regionIt ){
// join region's multicast group
subMsg->setGroupId(*regionIt);
send( subMsg, "to_lowerTier" );
subscribedRegions.insert( *regionIt );
// TODO: join simMud region
}
// publish movement
SimMudMoveMessage* moveMsg = new SimMudMoveMessage("MOVE");
moveMsg->setSrc( overlay->getThisNode() );
moveMsg->setPosX( msg->getPosition().x );
moveMsg->setPosY( msg->getPosition().y );
moveMsg->setTimestamp( simTime() );
moveMsg->setBitLength( SIMMUD_MOVE_L( moveMsg ));
ALMMulticastMessage* mcastMsg = new ALMMulticastMessage("MOVE");
mcastMsg->encapsulate( moveMsg );
send(mcastMsg, "to_lowerTier");
}
void SimMud::handleOtherPlayerMove ( SimMudMoveMessage msg)
protected

Definition at line 280 of file SimMud.cc.

{
GameAPIListMessage *scMsg = new GameAPIListMessage("NEIGHBOR_UPDATE");
NodeHandle& src = msg->getSrc();
if( msg->getTimestamp() < simTime() - maxMoveDelay ){
globalStatistics->addStdDev("SimMUD: LOST MOVE Delay",
SIMTIME_DBL(simTime() - msg->getTimestamp()) );
} else {
}
if( src != overlay->getThisNode() ){
globalStatistics->addStdDev("SimMUD: MOVE Delay",
SIMTIME_DBL(simTime() - msg->getTimestamp()) );
}
);
if( msg->getLeaveRegion() ) {
// Player leaves region
scMsg->setRemoveNeighbor(0, src);
playerMap.erase( src );
} else {
PlayerInfo player;
player.pos = Vector2D( msg->getPosX(), msg->getPosY() );
player.update = true;
// pair< map<NodeHandle, PlayerInfo>::iterator, bool> inserter =
playerMap.insert( make_pair(src, player) );
/* if( inserter.second ) {
// new player
} else {
// move player
}*/
// Ordinary move
scMsg->setAddNeighbor(0, src);
scMsg->setNeighborPosition(0, Vector2D(msg->getPosX(), msg->getPosY()) );
}
send(scMsg, "to_upperTier");
}
void SimMud::handleReadyMessage ( CompReadyMessage msg)
virtual

method to handle ready messages from the overlay

Parameters
msgmessage to handle

Reimplemented from BaseApp.

Definition at line 142 of file SimMud.cc.

{
// process only ready messages from the tier below
if( getThisCompType() - msg->getComp() == 1 ){
if( msg->getReady() ) {
// TODO/FIXME: use overlay->sendMessageToAllComp(msg, getThisCompType())?
send(msg, "to_upperTier");
// also send AOI size to SimpleGameClient
GameAPIResizeAOIMessage* gameMsg = new GameAPIResizeAOIMessage("RESIZE_AOI");
gameMsg->setAOIsize(AOIWidth);
send(gameMsg, "to_upperTier");
if( playerTimeout > 0 ) {
cancelEvent( playerTimer );
scheduleAt( simTime() + playerTimeout, playerTimer );
}
}
} else {
delete msg;
}
}
bool SimMud::handleRpcCall ( BaseCallMessage msg)
virtual

Processes Remote-Procedure-Call invocation messages.


This method should be overloaded when the overlay provides RPC functionality.

Returns
true, if rpc has been handled

Reimplemented from BaseRpc.

Definition at line 80 of file SimMud.cc.

{
// RPC_SWITCH_START(msg);
// RPC_SWITCH_END( );
// return RPC_HANDLED;
return false;
}
void SimMud::handleRpcResponse ( BaseResponseMessage msg,
cPolymorphic *  context,
int  rpcId,
simtime_t  rtt 
)
virtual

This method is called if an RPC response has been received.

Parameters
msgThe response message.
contextPointer to an optional state object. The object has to be handled/deleted by the handleRpcResponse() code
rpcIdThe RPC id.
rttThe Round-Trip-Time of this RPC

Reimplemented from RpcListener.

Definition at line 88 of file SimMud.cc.

{
// RPC_SWITCH_START(msg);
// RPC_SWITCH_END( );
}
void SimMud::handleTimerEvent ( cMessage *  msg)
virtual

Reimplemented from BaseRpc.

Definition at line 96 of file SimMud.cc.

{
if( msg == playerTimer ) {
// Check if any player didn't send any updates since last check
map<NodeHandle, PlayerInfo>::iterator it;
list<NodeHandle> erasePlayers;
for( it = playerMap.begin(); it != playerMap.end(); ++it ) {
if( it->second.update == false ) {
erasePlayers.push_back( it->first );
}
it->second.update = false;
}
for( list<NodeHandle>::iterator it = erasePlayers.begin(); it != erasePlayers.end(); ++it) {
// Delete all neighbors that didn't update (and inform app)
GameAPIListMessage *scMsg = new GameAPIListMessage("NEIGHBOR_UPDATE");
scMsg->setRemoveNeighbor(0, *it);
send(scMsg, "to_upperTier");
playerMap.erase( *it );
}
erasePlayers.clear();
scheduleAt( simTime() + playerTimeout, msg );
}
}
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)

Parameters
msgthe message to handle

Reimplemented from BaseApp.

Definition at line 166 of file SimMud.cc.

{
if( GameAPIPositionMessage* moveMsg =
dynamic_cast<GameAPIPositionMessage*>(msg) ) {
handleMove( moveMsg );
delete msg;
}
}
void SimMud::initializeApp ( int  stage)
virtual

Initialize class attributes.

Reimplemented from BaseApp.

Definition at line 56 of file SimMud.cc.

{
if( stage != (numInitStages()-1)) {
return;
}
// FIXME: areaDimension is not used consistently between all overlays!
fieldSize = par("areaDimension");
numSubspaces = par("numSubspaces");
playerTimeout = par("playerTimeout");
maxMoveDelay = par("maxMoveDelay");
AOIWidth = par("AOIWidth");
// WATCH_MAP( playerMap );
}

Member Data Documentation

int SimMud::AOIWidth
private

Definition at line 44 of file SimMud.h.

OverlayKey SimMud::currentRegionID
private

Definition at line 38 of file SimMud.h.

int SimMud::currentRegionX
private

Definition at line 36 of file SimMud.h.

int SimMud::currentRegionY
private

Definition at line 37 of file SimMud.h.

int SimMud::fieldSize
private

Definition at line 41 of file SimMud.h.

int SimMud::lostMovementLists
private

Definition at line 47 of file SimMud.h.

simtime_t SimMud::maxMoveDelay
private

Definition at line 48 of file SimMud.h.

int SimMud::numSubspaces
private

Definition at line 42 of file SimMud.h.

std::map<NodeHandle, PlayerInfo> SimMud::playerMap
private

Definition at line 58 of file SimMud.h.

int SimMud::playerTimeout
private

Definition at line 50 of file SimMud.h.

cMessage* SimMud::playerTimer
private

Definition at line 51 of file SimMud.h.

int SimMud::receivedMovementLists
private

Definition at line 46 of file SimMud.h.

int SimMud::regionSize
private

Definition at line 43 of file SimMud.h.

std::set<OverlayKey> SimMud::subscribedRegions
private

Definition at line 39 of file SimMud.h.


The documentation for this class was generated from the following files: