2014-04-05 03:27:18 -04:00
|
|
|
#include "nxstesthub.h"
|
|
|
|
|
2015-12-13 11:22:31 -05:00
|
|
|
#include <unistd.h>
|
2014-04-05 03:27:18 -04:00
|
|
|
|
2014-04-13 17:52:53 -04:00
|
|
|
class NotifyWithPeerId : public RsNxsObserver
|
|
|
|
{
|
|
|
|
public:
|
2014-04-05 03:27:18 -04:00
|
|
|
|
2014-04-18 17:58:14 -04:00
|
|
|
NotifyWithPeerId(RsPeerId val, rs_nxs_test::NxsTestHub& hub)
|
2014-04-13 17:52:53 -04:00
|
|
|
: mPeerId(val), mTestHub(hub){
|
2014-04-05 03:27:18 -04:00
|
|
|
|
2014-04-13 17:52:53 -04:00
|
|
|
}
|
2014-04-05 03:27:18 -04:00
|
|
|
|
2014-04-13 17:52:53 -04:00
|
|
|
void notifyNewMessages(std::vector<RsNxsMsg*>& messages)
|
|
|
|
{
|
|
|
|
mTestHub.notifyNewMessages(mPeerId, messages);
|
2014-04-05 03:27:18 -04:00
|
|
|
}
|
|
|
|
|
2014-04-13 17:52:53 -04:00
|
|
|
void notifyNewGroups(std::vector<RsNxsGrp*>& groups)
|
2014-04-05 03:27:18 -04:00
|
|
|
{
|
2014-04-13 17:52:53 -04:00
|
|
|
mTestHub.notifyNewGroups(mPeerId, groups);
|
2014-04-05 03:27:18 -04:00
|
|
|
}
|
|
|
|
|
2015-12-13 11:22:31 -05:00
|
|
|
void notifyReceivePublishKey(const RsGxsGroupId& )
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void notifyChangedGroupStats(const RsGxsGroupId&)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-04-13 17:52:53 -04:00
|
|
|
private:
|
|
|
|
|
|
|
|
RsPeerId mPeerId;
|
|
|
|
rs_nxs_test::NxsTestHub& mTestHub;
|
|
|
|
};
|
|
|
|
|
2014-04-18 17:58:14 -04:00
|
|
|
using namespace rs_nxs_test;
|
|
|
|
|
|
|
|
class NxsTestHubConnection : public p3ServiceServerIface
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
NxsTestHubConnection(const RsPeerId& id, RecvPeerItemIface* recvIface) : mPeerId(id), mRecvIface(recvIface) {}
|
|
|
|
|
|
|
|
bool recvItem(RsRawItem * i)
|
|
|
|
{
|
|
|
|
return mRecvIface->recvItem(i, mPeerId);
|
|
|
|
}
|
|
|
|
bool sendItem(RsRawItem * i)
|
|
|
|
{
|
|
|
|
return recvItem(i);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
RsPeerId mPeerId;
|
|
|
|
RecvPeerItemIface* mRecvIface;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
rs_nxs_test::NxsTestHub::NxsTestHub(NxsTestScenario::pointer testScenario)
|
2015-12-13 14:50:08 -05:00
|
|
|
: mTestScenario(testScenario), mMtx("NxsTestHub Mutex")
|
2014-04-05 03:27:18 -04:00
|
|
|
{
|
2014-04-13 17:52:53 -04:00
|
|
|
std::list<RsPeerId> peers;
|
|
|
|
mTestScenario->getPeers(peers);
|
2014-04-05 03:27:18 -04:00
|
|
|
|
2014-04-13 17:52:53 -04:00
|
|
|
// for each peer get initialise a nxs net instance
|
|
|
|
// and pass this to the simulator
|
2014-04-05 03:27:18 -04:00
|
|
|
|
2014-04-13 17:52:53 -04:00
|
|
|
std::list<RsPeerId>::const_iterator cit = peers.begin();
|
2014-04-05 03:27:18 -04:00
|
|
|
|
2014-04-13 17:52:53 -04:00
|
|
|
for(; cit != peers.end(); cit++)
|
|
|
|
{
|
2014-04-18 17:58:14 -04:00
|
|
|
RsGxsNetService::pointer ns = RsGxsNetService::pointer(
|
|
|
|
new RsGxsNetService(
|
|
|
|
mTestScenario->getServiceType(),
|
|
|
|
mTestScenario->getDataService(*cit),
|
|
|
|
mTestScenario->getDummyNetManager(*cit),
|
2014-04-13 17:52:53 -04:00
|
|
|
new NotifyWithPeerId(*cit, *this),
|
2014-04-18 17:58:14 -04:00
|
|
|
mTestScenario->getServiceInfo(),
|
|
|
|
mTestScenario->getDummyReputations(*cit),
|
2014-05-27 17:14:05 -04:00
|
|
|
mTestScenario->getDummyCircles(*cit),
|
|
|
|
mTestScenario->getDummyPgpUtils(),
|
|
|
|
true
|
2014-04-18 17:58:14 -04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
NxsTestHubConnection *connection =
|
|
|
|
new NxsTestHubConnection(*cit, this);
|
|
|
|
ns->setServiceServer(connection);
|
2014-04-05 03:27:18 -04:00
|
|
|
|
2014-04-13 17:52:53 -04:00
|
|
|
mPeerNxsMap.insert(std::make_pair(*cit, ns));
|
|
|
|
}
|
2015-01-11 17:19:32 -05:00
|
|
|
}
|
2014-04-05 03:27:18 -04:00
|
|
|
|
|
|
|
|
2014-04-13 17:52:53 -04:00
|
|
|
rs_nxs_test::NxsTestHub::~NxsTestHub() {
|
2015-01-11 17:19:32 -05:00
|
|
|
}
|
2014-04-05 03:27:18 -04:00
|
|
|
|
|
|
|
|
2014-04-13 17:52:53 -04:00
|
|
|
bool rs_nxs_test::NxsTestHub::testsPassed()
|
|
|
|
{
|
|
|
|
return mTestScenario->checkTestPassed();
|
2015-01-11 17:19:32 -05:00
|
|
|
}
|
2014-04-05 03:27:18 -04:00
|
|
|
|
|
|
|
|
2015-12-13 11:22:31 -05:00
|
|
|
void rs_nxs_test::NxsTestHub::runloop()
|
2014-04-13 17:52:53 -04:00
|
|
|
{
|
|
|
|
double timeDelta = .2;
|
2015-12-13 11:22:31 -05:00
|
|
|
while(!shouldStop())
|
2014-04-13 17:52:53 -04:00
|
|
|
{
|
|
|
|
#ifndef WINDOWS_SYS
|
|
|
|
usleep((int) (timeDelta * 1000000));
|
|
|
|
#else
|
|
|
|
Sleep((int) (timeDelta * 1000));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
tick();
|
|
|
|
}
|
|
|
|
|
2015-01-11 17:19:32 -05:00
|
|
|
}
|
2014-04-05 03:27:18 -04:00
|
|
|
|
|
|
|
|
2014-04-13 17:52:53 -04:00
|
|
|
void rs_nxs_test::NxsTestHub::StartTest()
|
|
|
|
{
|
|
|
|
// get all services up and running
|
|
|
|
PeerNxsMap::iterator mit = mPeerNxsMap.begin();
|
|
|
|
for(; mit != mPeerNxsMap.end(); mit++)
|
|
|
|
{
|
2015-01-11 17:19:32 -05:00
|
|
|
(mit->second)->start() ;
|
2014-04-13 17:52:53 -04:00
|
|
|
}
|
|
|
|
|
2015-01-11 17:19:32 -05:00
|
|
|
start() ;
|
|
|
|
}
|
2014-04-05 03:27:18 -04:00
|
|
|
|
|
|
|
|
2014-04-13 17:52:53 -04:00
|
|
|
void rs_nxs_test::NxsTestHub::EndTest()
|
|
|
|
{
|
|
|
|
// then stop this thread
|
2015-12-13 11:22:31 -05:00
|
|
|
ask_for_stop();
|
2014-04-13 17:52:53 -04:00
|
|
|
|
|
|
|
// stop services
|
|
|
|
PeerNxsMap::iterator mit = mPeerNxsMap.begin();
|
|
|
|
for(; mit != mPeerNxsMap.end(); mit++)
|
|
|
|
{
|
|
|
|
mit->second->join();
|
|
|
|
}
|
|
|
|
}
|
2014-04-05 03:27:18 -04:00
|
|
|
|
2014-04-13 17:52:53 -04:00
|
|
|
void rs_nxs_test::NxsTestHub::notifyNewMessages(const RsPeerId& pid,
|
|
|
|
std::vector<RsNxsMsg*>& messages)
|
|
|
|
{
|
2015-12-13 14:50:08 -05:00
|
|
|
RS_STACK_MUTEX(mMtx); /***** MTX LOCKED *****/
|
2014-04-05 03:27:18 -04:00
|
|
|
|
2014-04-13 17:52:53 -04:00
|
|
|
std::map<RsNxsMsg*, RsGxsMsgMetaData*> toStore;
|
|
|
|
std::vector<RsNxsMsg*>::iterator it = messages.begin();
|
|
|
|
for(; it != messages.end(); it++)
|
|
|
|
{
|
|
|
|
RsNxsMsg* msg = *it;
|
|
|
|
RsGxsMsgMetaData* meta = new RsGxsMsgMetaData();
|
2014-04-18 17:58:14 -04:00
|
|
|
bool ok = meta->deserialise(msg->meta.bin_data, &(msg->meta.bin_len));
|
2014-04-13 17:52:53 -04:00
|
|
|
toStore.insert(std::make_pair(msg, meta));
|
|
|
|
}
|
2014-04-05 03:27:18 -04:00
|
|
|
|
2014-04-18 17:58:14 -04:00
|
|
|
RsGeneralDataService* ds = mTestScenario->getDataService(pid);
|
2014-04-13 17:52:53 -04:00
|
|
|
ds->storeMessage(toStore);
|
2015-01-11 17:19:32 -05:00
|
|
|
}
|
2014-04-05 03:27:18 -04:00
|
|
|
|
|
|
|
|
2014-04-13 17:52:53 -04:00
|
|
|
void rs_nxs_test::NxsTestHub::notifyNewGroups(const RsPeerId& pid, std::vector<RsNxsGrp*>& groups)
|
|
|
|
{
|
2015-12-13 14:50:08 -05:00
|
|
|
RS_STACK_MUTEX(mMtx); /***** MTX LOCKED *****/
|
|
|
|
|
2014-04-13 17:52:53 -04:00
|
|
|
std::map<RsNxsGrp*, RsGxsGrpMetaData*> toStore;
|
|
|
|
std::vector<RsNxsGrp*>::iterator it = groups.begin();
|
|
|
|
for(; it != groups.end(); it++)
|
|
|
|
{
|
|
|
|
RsNxsGrp* grp = *it;
|
|
|
|
RsGxsGrpMetaData* meta = new RsGxsGrpMetaData();
|
|
|
|
bool ok = meta->deserialise(grp->meta.bin_data, grp->meta.bin_len);
|
|
|
|
toStore.insert(std::make_pair(grp, meta));
|
2014-04-05 03:27:18 -04:00
|
|
|
}
|
2014-04-13 17:52:53 -04:00
|
|
|
|
2014-04-18 17:58:14 -04:00
|
|
|
RsGeneralDataService* ds = mTestScenario->getDataService(pid);
|
2014-04-13 17:52:53 -04:00
|
|
|
ds->storeGroup(toStore);
|
2014-04-05 03:27:18 -04:00
|
|
|
}
|
|
|
|
|
2014-04-13 17:52:53 -04:00
|
|
|
void rs_nxs_test::NxsTestHub::Wait(int seconds) {
|
2014-04-05 03:27:18 -04:00
|
|
|
|
2014-04-18 17:58:14 -04:00
|
|
|
double dsecs = seconds;
|
|
|
|
|
2014-04-13 17:52:53 -04:00
|
|
|
#ifndef WINDOWS_SYS
|
2014-04-18 17:58:14 -04:00
|
|
|
usleep((int) (dsecs * 1000000));
|
2014-04-13 17:52:53 -04:00
|
|
|
#else
|
2014-04-18 17:58:14 -04:00
|
|
|
Sleep((int) (dsecs * 1000));
|
2014-04-13 17:52:53 -04:00
|
|
|
#endif
|
2014-04-05 03:27:18 -04:00
|
|
|
}
|
|
|
|
|
2014-04-18 17:58:14 -04:00
|
|
|
bool rs_nxs_test::NxsTestHub::recvItem(RsRawItem* item, const RsPeerId& peerFrom)
|
|
|
|
{
|
2015-12-13 14:50:08 -05:00
|
|
|
RS_STACK_MUTEX(mMtx); /***** MTX LOCKED *****/
|
2014-04-19 06:20:07 -04:00
|
|
|
PayLoad p(peerFrom, item);
|
|
|
|
mPayLoad.push(p);
|
|
|
|
return true;
|
2014-04-18 17:58:14 -04:00
|
|
|
}
|
|
|
|
|
2014-05-27 17:14:05 -04:00
|
|
|
void rs_nxs_test::NxsTestHub::CleanUpTest()
|
|
|
|
{
|
|
|
|
mTestScenario->cleanTestScenario();
|
|
|
|
}
|
|
|
|
|
2014-04-13 17:52:53 -04:00
|
|
|
void rs_nxs_test::NxsTestHub::tick()
|
2014-04-05 03:27:18 -04:00
|
|
|
{
|
2014-04-13 17:52:53 -04:00
|
|
|
// for each nxs instance pull out all items from each and then move to destination peer
|
|
|
|
|
|
|
|
PeerNxsMap::iterator it = mPeerNxsMap.begin();
|
2014-04-18 17:58:14 -04:00
|
|
|
|
2014-04-19 06:20:07 -04:00
|
|
|
// deliver payloads to peer's net services
|
2015-12-13 14:50:08 -05:00
|
|
|
mMtx.lock();
|
2014-04-19 06:20:07 -04:00
|
|
|
while(!mPayLoad.empty())
|
|
|
|
{
|
|
|
|
PayLoad& p = mPayLoad.front();
|
|
|
|
|
|
|
|
RsRawItem* item = p.second;
|
|
|
|
RsPeerId peerFrom = p.first;
|
|
|
|
RsPeerId peerTo = item->PeerId();
|
|
|
|
item->PeerId(peerFrom);
|
2015-12-13 14:50:08 -05:00
|
|
|
mMtx.unlock();
|
|
|
|
mPeerNxsMap[peerTo]->recv(item);
|
|
|
|
mMtx.lock();
|
2014-04-19 06:20:07 -04:00
|
|
|
mPayLoad.pop();
|
|
|
|
}
|
2015-12-13 14:50:08 -05:00
|
|
|
mMtx.unlock();
|
2014-04-19 06:20:07 -04:00
|
|
|
|
|
|
|
// then tick net services
|
2014-04-13 17:52:53 -04:00
|
|
|
for(; it != mPeerNxsMap.end(); it++)
|
|
|
|
{
|
2014-04-18 17:58:14 -04:00
|
|
|
RsGxsNetService::pointer s = it->second;
|
2014-04-13 17:52:53 -04:00
|
|
|
s->tick();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-11 17:19:32 -05:00
|
|
|
}
|
|
|
|
|
2014-04-13 17:52:53 -04:00
|
|
|
|