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

412 lines
14 KiB
C++

/*
* File: PartitionApplication.cc
* Author: gschneid
*
* Created on October 22, 2012, 11:09 AM
*/
#include "PartitionApplication.h"
#include "Dispatcher.h"
#include "Moversight.h"
#include "ms/PeerResources.h"
#include "common/transport/TransportAddress.h"
#include "mt/msg/GroupData.h"
#include "ms/Invitation.h"
#include "simutils/OmnetppIniUtils.h"
#include "StreamMessage.h"
#include "ms/events/GroupCreatedEvent.h"
#include "ms/events/GroupClosedEvent.h"
#include "ms/events/PeerJoinedEvent.h"
#include "ms/events/JoinRequestEvent.h"
#include "ms/events/JoinConfirmEvent.h"
#include "ms/events/JoinRejectedEvent.h"
#include "ms/events/JoinAbortedEvent.h"
#include "ms/events/RejoinDoneEvent.h"
#include "ms/events/RejoinFailedEvent.h"
#include "mt/events/PendingPeersEvent.h"
#include "ms/events/LocalPeerUpdatedEvent.h"
#include "ms/events/PeerLeftEvent.h"
#include "simutils/events/StartMeasuringEvent.h"
#include "simutils/events/StopMeasuringEvent.h"
#include "fd/events/PeerReconnectedEvent.h"
#undef DEBUG
#define DEBUG(msg) if (module.isPrintDebugAPP()) \
MOV_DEBUG << "APP@"<<(dis->getLocalState()==DISJOINED?"TA_":"");\
if(dis->getLocalState()==DISJOINED){MOV_DEBUG << module.getLocalAddress();}\
else{MOV_DEBUG<< dis->getMembershipService().getLocalID();}MOV_DEBUG<<" "<<msg<<std::endl;
namespace ubeeme {
namespace moversight {
/**
* @brief Constructor
* @param m A reference to the moversight instance
*/
PartitionApplication::PartitionApplication(Moversight & m) : Application(m, "PartitionApplication"), rejoinSucessDuration(m, "rejoinSuccess"), leaveDelay(m, "leaveDelay"), groupSize(0) {
numberOfPeers = module.par("numberOfPeers");
numberOfMobilePeers = module.par("numberOfMobilePeers");
clusterSize = (numberOfPeers + numberOfMobilePeers) / module.getMaxPeerCount();
if ((numberOfPeers + numberOfMobilePeers) % module.getMaxPeerCount() != 0) {
clusterSize++;
}
}
/**
* @brief Destructor
*/
PartitionApplication::~PartitionApplication() {
// joinDelayStat, leaveDelayStat, dtDelayStat,groupSizeStat removen?!
}
void
PartitionApplication::initialise() {
dis->subscribe<GroupCreatedEvent>(this);
dis->subscribe<PeerJoinedEvent>(this);
dis->subscribe<PendingPeersEvent>(this);
dis->subscribe<PeerReconnectedEvent>(this);
dis->subscribe<PeerLeftEvent>(this);
dis->subscribe<JoinRequestEvent>(this);
dis->subscribe<JoinConfirmEvent>(this);
dis->subscribe<JoinRejectedEvent>(this);
dis->subscribe<JoinAbortedEvent>(this);
dis->subscribe<RejoinDoneEvent>(this);
dis->subscribe<RejoinFailedEvent>(this);
// MeasurementEvent
dis->subscribe<StartMeasuringEvent>(this);
dis->subscribe<StopMeasuringEvent>(this);
}
void
PartitionApplication::finalise() {
}
/**
* @brief Invites a peer to a the moversight group.
* @param ta The transport address of the peer to invite.
*/
void
PartitionApplication::invitePeer(TransportAddress & ta) {
std::stringstream buf;
buf << "invitePeer - invite peer at address " << ta;
DEBUG(buf.str().c_str());
PeerDescription pDesc;
dis->invitePeer(ta, pDesc);
}
/**
* @brief Leaves the current group. The peer leafs the current group be
* emitting a LeaveAnnounce within the current group.
*/
void
PartitionApplication::leaveGroup() {
DEBUG("leaveGroup - peer leave the group");
dis->leaveGroup();
}
/**
* @brief Sends dummy data to the group to simulate application traffic.
*/
void
PartitionApplication::sendData() {
DEBUG("sendData - send 24 byte dummy data to the group");
GroupData data;
VectorStatistic vs("sendGroupData");
vs.record(GenericTime::currentTime());
dis->sendMessage(data);
}//End
/**
* @brief Starts the given test case.
* @param i The number of the test case to start.
*/
void
PartitionApplication::startTestCase(unsigned int i) {
switch (i) {
case 0:
testCase00();
break;
default:
break;
}//End switch
}
/**
* @brief Executes test case 01 - 6 peers, testcase1
*/
void
PartitionApplication::testCase00() {
DEBUG("partitionApplication tc00");
if (initApp) {
createRoster();
initApp = false;
module.scheduleTestCase(80);
}
}
/**
* @brief Method to create a roster from which the group is set up.
*/
void
PartitionApplication::createRoster() {
int numberOfAllPeers = numberOfPeers + numberOfMobilePeers;
Roster roster;
roster.setNextPeerID(numberOfAllPeers + 1);
roster.setViewID(numberOfAllPeers + 1);
PeerDescription dummyPDesc;
PeerResources dummyPRes;
ClusterID cID = 0;
if (clusterSize <= 1) {
// necessary to make sure if the numberOfPeers is equal or smaller than
// the maxPeerCount that only one cluster is created!
roster.setViewID(numberOfAllPeers + 1);
roster.setNextPeerID(numberOfAllPeers + 1);
}
for (int i = 1; i <= numberOfAllPeers; i++) {
TransportAddress ta = getNewTransportAddress(i);
ta.setPort(module.getLocalAddress().getPort());
MemberDescription mDesc(i, ta, JOINED, cID, dummyPDesc, dummyPRes);
roster.addMemberDescription(mDesc);
if ((i % module.getMaxPeerCount()) == 0) {
cID++;
}//End if
}
dis->setupGroupFromRoster(roster, module.getLocalAddress().getHostAddress().get4().getDByte(3));
}
/**
* @brief Getting a transportAddress for a given index.
* @param i The index of the peer a TA is searched for.
* @return The created TA.
*/
TransportAddress
PartitionApplication::getNewTransportAddress(int i) {
std::stringstream taStream;
taStream << "192.168.0." << i;
std::string s(taStream.str());
return TransportAddress(IPvXAddress(s.c_str()));
}
/**
* @brief Callback method. This method is called by moversight to signal that the local peer have receive a group data message.
* @param dat The received group data.
* @param sender The sending peer ID.
*/
void
PartitionApplication::receiveGroupData(const GroupData & dat, const PeerID sender) {
std::stringstream buf;
buf << "receiveGroupData - receive group data from peer ID " << sender;
DEBUG(buf.str().c_str());
VectorStatistic vs("receiveGroupData");
vs.record(GenericTime::currentTime());
}
/**
* @brief Handle an incoming JoinRequestEvent.
* @param e The event.
*
* The local instance has received an invitation.
*/
void
PartitionApplication::handleEvent(const JoinRequestEvent & e) {
DEBUG("invitationReceived - invitation received");
std::stringstream buf;
buf << "invitationReceived - invitationID: " << e.getInvitation().getInvitationID() << " inviterID: " << e.getInvitation().getInviterID();
DEBUG(buf.str().c_str());
DEBUG("invitationReceived - accept invitation");
PeerDescription pDesc;
PeerResources resources;
dis->acceptInvitation(e.getInvitation(), "accept invitation", pDesc, resources);
}
/**
* @brief Handle an incoming JonConfirmEvent.
* @param e The event.
*
* The remote peer has accepted the invitation from the local peer.
*/
void
PartitionApplication::handleEvent(const JoinConfirmEvent & e) {
std::stringstream buf;
buf << "receiveInvitationResponse - peer at TA: " << e.getInvitation().getInviteeAddress() << " accept invitation " << e.getInvitation().getInvitationID();
buf << " message: " << e.getMessage() << ", peer description: " << e.getDescription().getDescription();
DEBUG(buf.str().c_str());
}
/**
* @brief Handle an incoming JoinRejectedEvent.
* @param e The event.
*
* The invitation process was aborted.
*/
void
PartitionApplication::handleEvent(const JoinRejectedEvent & e) {
std::stringstream buf;
buf << "invitationAborted - invitation ID " << e.getInvitation().getInvitationID() << " aborted";
buf << " reason: " << e.getReason();
DEBUG(buf.str().c_str());
}
/**
* @brief Handle an incoming JoinAbortedEvent.
* @param e The event.
*
* The invitation process could not be executed.
*/
void
PartitionApplication::handleEvent(const JoinAbortedEvent & e) {
std::stringstream buf;
buf << "invitationAborted - invitation to peer " << e.getTransportAddress() << " aborted";
buf << " reason: " << e.getReason();
DEBUG(buf.str().c_str());
}
/**
* @brief Handle an incoming GroupCreatedEvent.
* @param e The event.
*/
void
PartitionApplication::handleEvent(const GroupCreatedEvent & e) {
groupSize = 1;
}
void
PartitionApplication::handleEvent(const PeerJoinedEvent & e) {
std::stringstream buf;
buf << "peerJoined - peer " << e.getPeerID()
<< "@TA_" << e.getTransportAddress() << " joined group successfully ("
<< e.getDescription().getDescription() << ")";
DEBUG(buf.str().c_str());
}
/**
* @brief Handle an incoming PendingPeersEvent.
* @param e The event.
*
* Some peers are pending.
*/
void
PartitionApplication::handleEvent(const PendingPeersEvent & e) {
std::stringstream buf;
buf << "peerIsPending - peer " << e.getPeerIDList() << " is pending";
DEBUG(buf.str().c_str());
}
/**
* @brief Handle an incoming PeerReconnectedEvent.
* @param e The event.
*
* A pending peers has reconnected.
*/
void
PartitionApplication::handleEvent(const PeerReconnectedEvent & e) {
std::stringstream buf;
buf << "peerReconnected - peer " << e.getPeerID() << " has reconnected";
DEBUG(buf.str().c_str());
}
/**
* @brief Handle an incoming PeerLeftEvent.
* @param e The event.
*
* A dedicated peer has left the group.
*/
void
PartitionApplication::handleEvent(const PeerLeftEvent & e) {
std::stringstream buf;
buf << "peerLeft - peer " << e.getPeer().getPeerID() << "@TA_" << e.getPeer().getLocalAddress() << " left the group";
DEBUG(buf.str().c_str());
}
/**
* @brief Handle an incoming RejoinDoneEvent.
* @param e The event.
*/
void
PartitionApplication::handleEvent(const RejoinDoneEvent & e) {
dis->signal(new StopMeasuringEvent("REJOIN"));
}
/**
* @brief Handle an incoming RejoinFailedEvent.
* @param e The event.
*/
void
PartitionApplication::handleEvent(const RejoinFailedEvent & e) {
}
/**
* @brief Handle an incoming StartMeasuringEvent.
* @param e The event.
*
* Starts the measuring of the given parameter.
*/
void
PartitionApplication::handleEvent(const StartMeasuringEvent & e) {
std::string startS = "start" + e.getMeasurement();
VectorStatistic vs(startS.c_str());
if (e.getMeasurement() == "REJOIN") {
vs.record(GenericTime::currentTime());
rejoinSucessDuration.startRecord();
}
if (e.getMeasurement() == "TG") {
vs.record(GenericTime::currentTime());
// leaveDelay.startRecord();
}
}
/**
* @brief Handle an incoming StopMeasuringEvent.
* @param e The event.
*
* Stops the measuring of the given parameter.
*/
void
PartitionApplication::handleEvent(const StopMeasuringEvent & e) {
std::string startS = "stop " + e.getMeasurement();
VectorStatistic vs(startS.c_str());
if (e.getMeasurement() == "REJOIN") {
vs.record(GenericTime::currentTime());
rejoinSucessDuration.startRecord();
}
if (e.getMeasurement() == "TG") {
vs.record(GenericTime::currentTime());
leaveDelay.stopRecord();
}
}
}//End namespace moversight
}//End namespace ubeeme