Files
scandocs/uni/masterarbeit/source/moversight/simutils/MoversightSimulationPeerStateVisualization.cc
2014-06-30 13:58:10 +02:00

200 lines
6.4 KiB
C++

/*
* File: MoversightSimulationPeerStateVisualization.cc
* Author: jgaebler
*
* Created on November 1, 2012, 4:30 PM
*/
#include "MoversightSimulationPeerStateVisualization.h"
#include "Moversight.h"
#include "MoversightService.h"
#include "ModuleAccess.h"
#include "Dispatcher.h"
#include "event/MoversightEvent.h"
#include "event/events/ConnectionLostEvent.h"
#include "event/events/ConnectionReEstablishedEvent.h"
#include "ms/events/PeerJoinedEvent.h"
#include "ms/events/PeerLeftEvent.h"
#include "fd/events/PeerReconnectedEvent.h"
#include "mt/events/PendingPeersEvent.h"
namespace ubeeme {
namespace moversight {
#undef DEBUG
#define DEBUG(msg) if (module.isPrintDebugVISU()){ MOV_DEBUG <<"VISU@"<<getLocalID()<<" "<<msg<<endl; }
/**
* @brier Constructor
* @param mod A reference to the omnet module, which is located in the module that should be decorated.
* @param dis A reference to the moversight dispatcher.
*/
MoversightSimulationPeerStateVisualization::MoversightSimulationPeerStateVisualization(Dispatcher & dis)
: MoversightService(dis, "VISU") {
}
/**
* @brief Destructor.
*/
MoversightSimulationPeerStateVisualization::~MoversightSimulationPeerStateVisualization() {
finalise();
}
void
MoversightSimulationPeerStateVisualization::initialise() {
dispatcher.subscribe<MoversightEvent>(this);
dispatcher.subscribe<ConnectionLostEvent>(this);
dispatcher.subscribe<ConnectionReEstablishedEvent>(this);
dispatcher.subscribe<PeerJoinedEvent>(this);
dispatcher.subscribe<PeerLeftEvent>(this);
dispatcher.subscribe<PeerReconnectedEvent>(this);
dispatcher.subscribe<PendingPeersEvent>(this);
}
void
MoversightSimulationPeerStateVisualization::finalise() {
dispatcher.unsubscribeAll(this);
}
void
MoversightSimulationPeerStateVisualization::handleEvent( const MoversightEvent& e) {
updateSimUI();
}
void
MoversightSimulationPeerStateVisualization::handleEvent( const ConnectionLostEvent& e) {
module.bubble( "connection lost");
}
void
MoversightSimulationPeerStateVisualization::handleEvent( const ConnectionReEstablishedEvent& e) {
module.bubble( "connection re-established");
}
/**
* @brief Handle PeerJoinedEvent.
*
* Draw a bubble with information about the joined peer.
*
* @param e The event.
*/
void
MoversightSimulationPeerStateVisualization::handleEvent( const PeerJoinedEvent& e) {
std::ostringstream buf;
buf << "peer " << e.getPeerID() << " (" << e.getDescription().getDescription() << ") added";
module.bubble(buf.str().c_str());
}
void
MoversightSimulationPeerStateVisualization::handleEvent( const PeerLeftEvent& e) {
std::ostringstream buf;
buf << "peer " << e.getPeer().getPeerID() << " left the group";
module.bubble(buf.str().c_str());
}
void
MoversightSimulationPeerStateVisualization::handleEvent( const PeerReconnectedEvent& e) {
std::ostringstream buf;
buf << "peer " << e.getPeerID() << " reconnect to group";
module.bubble(buf.str().c_str());
}
void
MoversightSimulationPeerStateVisualization::handleEvent( const PendingPeersEvent& e) {
std::ostringstream buf;
buf << "peers " << e.getPeerIDList() << " are pending";
module.bubble(buf.str().c_str());
}
/**
* @brief Returns the display string of the parent module used to display the moversight state.
* @return The display string of the parent module.
*/
cDisplayString&
MoversightSimulationPeerStateVisualization::getMoversightDisplayString() {
return module.getParentModule()->getDisplayString();
}
/**
* @brief Updates the UI of the simulation.
*/
void
MoversightSimulationPeerStateVisualization::updateSimUI() {
updateRole();
updateState();
}
/**
* @brief Updates the role of the peer.
*/
void
MoversightSimulationPeerStateVisualization::updateRole() {
cDisplayString & dis = getMoversightDisplayString();
//display role
if (dispatcher.getMembershipService().getLocalPeer().isMaster()) {
dis.setTagArg("i", 0, "device/laptop");
}
else {
dis.setTagArg("i", 0, "device/pocketpc");
}
}
/**
* @brief Updates the state of the peer.
*/
void
MoversightSimulationPeerStateVisualization::updateState() {
cDisplayString & dis = getMoversightDisplayString();
State state = dispatcher.getMembershipService().getLocalPeer().getState();
switch (state) {
case INVITING:
case WAITING_FOR_ROSTER:
{
dis.setTagArg("i2", 0, "status/hourglass");
break;
}
case JOINED:
{
dis.setTagArg("i2", 0, "status/green");
break;
}
case LEAVING:
{
dis.setTagArg("i2", 0, "status/down");
break;
}
case PENDING:
{
dis.setTagArg("i2", 0, "status/excl3");
break;
}
case EXCLUDED:
{
dis.setTagArg("i", 0, "block/process");
dis.setTagArg("i2", 0, "status/noentry");
break;
}
case DEAD:
{
dis.setTagArg("i", 0, "block/process");
dis.setTagArg("i2", 0, "status/red");
break;
}
default: // DISJOINED case
{
dis.setTagArg("i", 0, "block/process");
dis.setTagArg("i2", 0, "status/off");
break;
}
}//End switch
}
}
}