367 lines
13 KiB
C++
367 lines
13 KiB
C++
/*
|
|
* PeerplacingApplication.cc
|
|
*
|
|
* Created on: Apr 26, 2011
|
|
* Author: gschneid
|
|
*/
|
|
|
|
#include "PeerplacingApplication.h"
|
|
|
|
#include "Application.h"
|
|
#include "../Dispatcher.h"
|
|
#include "../Moversight.h"
|
|
#include "../common/transport/TransportAddress.h"
|
|
#include "../mt/msg/GroupData.h"
|
|
#include "../ms/Invitation.h"
|
|
|
|
#include "ms/events/JoinRequestEvent.h"
|
|
#include "ms/events/JoinConfirmEvent.h"
|
|
#include "ms/events/JoinRejectedEvent.h"
|
|
#include "ms/events/JoinAbortedEvent.h"
|
|
#include "ms/events/PeerJoinedEvent.h"
|
|
#include "ms/events/PeerLeftEvent.h"
|
|
#include "mt/events/PendingPeersEvent.h"
|
|
#include "fd/events/PeerReconnectedEvent.h"
|
|
#include "ms/events/PeerJoinedEvent.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
|
|
*/
|
|
PeerplacingApplication::PeerplacingApplication(Moversight & m) : Application(m, "PeerPlacingApplication") {
|
|
WATCH(state);
|
|
}
|
|
|
|
/**
|
|
* @brief Destructor
|
|
*/
|
|
PeerplacingApplication::~PeerplacingApplication() {
|
|
}
|
|
|
|
void
|
|
PeerplacingApplication::initialise() {
|
|
dis->subscribe<PeerJoinedEvent>(this);
|
|
dis->subscribe<JoinRequestEvent>(this);
|
|
dis->subscribe<JoinConfirmEvent>(this);
|
|
dis->subscribe<JoinRejectedEvent>(this);
|
|
dis->subscribe<JoinAbortedEvent>(this);
|
|
dis->subscribe<PendingPeersEvent>(this);
|
|
dis->subscribe<PeerReconnectedEvent>(this);
|
|
dis->subscribe<PeerLeftEvent>(this);
|
|
}
|
|
|
|
void
|
|
PeerplacingApplication::finalise() {
|
|
}
|
|
|
|
/**
|
|
* @brief Invites a peer to a the moversight group.
|
|
* @param ta The transport address of the peer to invite.
|
|
*/
|
|
void
|
|
PeerplacingApplication::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
|
|
& emmiting a LeaveAnnounce within the current group.
|
|
*/
|
|
void
|
|
PeerplacingApplication::leaveGroup() {
|
|
|
|
DEBUG("leaveGroup - peer leave the group");
|
|
dis->leaveGroup();
|
|
}
|
|
|
|
/**
|
|
* @brief Sends dummy data to the group to simulate application traffic.
|
|
*/
|
|
void
|
|
PeerplacingApplication::sendData() {
|
|
|
|
DEBUG("sendData - send 24 byte dummy data to the group");
|
|
GroupData data;
|
|
dis->sendMessage(data);
|
|
|
|
}//End
|
|
|
|
/**
|
|
* @brief Starst the given test case.
|
|
* @param i The number of the test case to start.
|
|
*/
|
|
void
|
|
PeerplacingApplication::startTestCase(unsigned int i) {
|
|
|
|
switch (i) {
|
|
case 0:
|
|
testCase00();
|
|
break;
|
|
case 1:
|
|
testCase01();
|
|
break;
|
|
case 2:
|
|
testCase02();
|
|
break;
|
|
default:
|
|
break;
|
|
}//End switch
|
|
}//End startTestCase
|
|
|
|
/**
|
|
* @brief Executes test case 00
|
|
*/
|
|
void
|
|
PeerplacingApplication::testCase00() {
|
|
// state = dis->getLocalState();
|
|
// int moduleIndex = module.getLocalAddress().getHostAddress();
|
|
// if (initApp) {
|
|
//
|
|
// const char *candidateString = module.par("candidates");
|
|
// cStringTokenizer tokenizer(candidateString);
|
|
// while (tokenizer.hasMoreTokens()) {
|
|
// candidates.add(atoi(tokenizer.nextToken()));
|
|
// }//End while
|
|
// waitCounter = 5;
|
|
// initApp = false;
|
|
// count = 1;
|
|
//
|
|
// if (moduleIndex == 0) {
|
|
// count = 0;
|
|
// module.scheduleTestCase(45);
|
|
// }//End if
|
|
// }//End if
|
|
//
|
|
// //------------------------------------------------------------------
|
|
// //run the app
|
|
// //------------------------------------------------------------------
|
|
// if (moduleIndex == 0) {
|
|
// //do we have more candidates?
|
|
//
|
|
// if (waitCounter > 0) {
|
|
// waitCounter--;
|
|
// module.scheduleTestCase(45);
|
|
// }
|
|
// if (candidatesIndex < candidates.size()) {
|
|
// invitePeer(candidates.get(candidatesIndex++));
|
|
// if (candidatesIndex == candidates.size()) {
|
|
// module.scheduleTestCase(240);
|
|
// waitCounter = 50;
|
|
// }
|
|
// else {
|
|
// module.scheduleTestCase(45);
|
|
// }
|
|
// count = 1;
|
|
// }
|
|
// else {
|
|
// //Only leave if all the others are in their cluster
|
|
// if (waitCounter > 0) {
|
|
// waitCounter--;
|
|
// module.scheduleTestCase(90);
|
|
// }
|
|
// // else{
|
|
// // leaveGroup();
|
|
// // initApp = true;
|
|
// // module.scheduleTestCase(45);
|
|
// // waitCounter = 8;
|
|
// // }
|
|
// }//End else
|
|
// }//End if
|
|
// else if (moduleIndex > 0) {
|
|
// if (count == 1 && moduleIndex == 5) {
|
|
// waitCounter = 60;
|
|
// module.scheduleTestCase(250);
|
|
// count = 0;
|
|
// }
|
|
// else if (count == 1 && moduleIndex != 1) {
|
|
// waitCounter = (moduleIndex - 1) * 8;
|
|
// module.scheduleTestCase((moduleIndex - 1)*45);
|
|
// count = 0;
|
|
// }
|
|
// else {
|
|
// if (waitCounter > 0) {
|
|
// waitCounter--;
|
|
// module.scheduleTestCase(45);
|
|
// }
|
|
// else {
|
|
// //do we have more candidates?
|
|
// if (candidatesIndex < candidates.size()) {
|
|
// invitePeer(candidates.get(candidatesIndex++));
|
|
// count = 0;
|
|
// module.scheduleTestCase(45);
|
|
// if (module.getLocalAddress().getHostAddress() == 5) {
|
|
// waitCounter = 15;
|
|
// }
|
|
// }//End if
|
|
// }
|
|
// }
|
|
// }
|
|
}
|
|
|
|
/**
|
|
* @brief Executes test case 01.
|
|
*/
|
|
void
|
|
PeerplacingApplication::testCase01() {
|
|
throw NotImplementedYetException("run test case 01 not implemented ");
|
|
}
|
|
|
|
/**
|
|
* @brief Executes test case 02.
|
|
*/
|
|
void
|
|
PeerplacingApplication::testCase02() {
|
|
throw NotImplementedYetException("run test case 02 not implemented ");
|
|
}
|
|
|
|
/**
|
|
* @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
|
|
PeerplacingApplication::receiveGroupData(const GroupData & data, const PeerID sender) {
|
|
std::stringstream buf;
|
|
buf << "receiveGroupData - receive group data from peer ID " << sender;
|
|
DEBUG(buf.str().c_str());
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief Handle an incoming JoinRequestEvent.
|
|
* @param e The event.
|
|
*
|
|
* The local instance has received an invitation.
|
|
*/
|
|
void
|
|
PeerplacingApplication::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 JoinConfirmEvent.
|
|
* @param e The event.
|
|
*
|
|
* The remote peer has accepted the invitation from the local peer.
|
|
*/
|
|
void
|
|
PeerplacingApplication::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 was rejected by the remote peer.
|
|
*/
|
|
void
|
|
PeerplacingApplication::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
|
|
PeerplacingApplication::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 PeerJoinedEvent.
|
|
* @param e The event.
|
|
*
|
|
* A peer has joined the group.
|
|
*/
|
|
void
|
|
PeerplacingApplication::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.
|
|
*
|
|
* A list of peers is pending.
|
|
*/
|
|
void
|
|
PeerplacingApplication::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 peer has reconnected.
|
|
*/
|
|
void
|
|
PeerplacingApplication::handleEvent(const PeerReconnectedEvent & e) {
|
|
|
|
std::stringstream buf;
|
|
buf << "peerReconnected - peer " << e.getPeerID() << " is reconnected";
|
|
DEBUG(buf.str().c_str());
|
|
}
|
|
|
|
/**
|
|
* @brief Handle an incoming PeerLeftEvent.
|
|
* @param e The event.
|
|
*
|
|
* A dedicated peer has left the group.
|
|
*/
|
|
void
|
|
PeerplacingApplication::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());
|
|
}
|
|
}//End namespace moversight
|
|
}//End namespace ubeeme
|