2018-11-04 15:17:22 -05:00
|
|
|
/*******************************************************************************
|
|
|
|
* unittests/libretroshare/gxs/nxs_test/nxstesthub.cc *
|
|
|
|
* *
|
|
|
|
* Copyright (C) 2014, Crispy <retroshare.team@gmailcom> *
|
|
|
|
* *
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU Affero General Public License as *
|
|
|
|
* published by the Free Software Foundation, either version 3 of the *
|
|
|
|
* License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU Lesser General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
|
|
|
* *
|
|
|
|
******************************************************************************/
|
|
|
|
|
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
|
|
|
}
|
2018-02-15 12:57:16 -05:00
|
|
|
virtual ~NotifyWithPeerId(){}
|
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);
|
|
|
|
}
|
2017-04-21 12:50:36 -04:00
|
|
|
bool getServiceItemNames(uint32_t /*service_type*/, std::map<uint8_t,std::string>& /*names*/) { return false; }
|
2014-04-18 17:58:14 -04:00
|
|
|
private:
|
|
|
|
RsPeerId mPeerId;
|
|
|
|
RecvPeerItemIface* mRecvIface;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-07-24 18:07:53 -04:00
|
|
|
rs_nxs_test::NxsTestHub::NxsTestHub(NxsTestScenario *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++)
|
|
|
|
{
|
2017-07-24 18:07:53 -04:00
|
|
|
NotifyWithPeerId *noti = new NotifyWithPeerId(*cit, *this) ;
|
|
|
|
|
|
|
|
mNotifys.push_back(noti) ;
|
|
|
|
|
|
|
|
RsGxsNetService *ns = new RsGxsNetService(
|
|
|
|
mTestScenario->getServiceType(),
|
|
|
|
mTestScenario->getDataService(*cit),
|
|
|
|
mTestScenario->getDummyNetManager(*cit),
|
|
|
|
noti,
|
|
|
|
mTestScenario->getServiceInfo(),
|
|
|
|
mTestScenario->getDummyReputations(*cit),
|
|
|
|
mTestScenario->getDummyCircles(*cit),
|
|
|
|
NULL,
|
|
|
|
mTestScenario->getDummyPgpUtils(),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
NxsTestHubConnection *connection = new NxsTestHubConnection(*cit, this);
|
2014-04-18 17:58:14 -04:00
|
|
|
ns->setServiceServer(connection);
|
2014-04-05 03:27:18 -04:00
|
|
|
|
2017-07-24 18:07:53 -04:00
|
|
|
mConnections.push_back(connection) ;
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2017-07-24 18:07:53 -04:00
|
|
|
rs_nxs_test::NxsTestHub::~NxsTestHub()
|
|
|
|
{
|
|
|
|
for(PeerNxsMap::const_iterator it(mPeerNxsMap.begin());it!=mPeerNxsMap.end();++it)
|
|
|
|
delete it->second ;
|
|
|
|
|
|
|
|
for(std::list<NotifyWithPeerId*>::const_iterator it(mNotifys.begin());it!=mNotifys.end();++it)
|
|
|
|
delete *it ;
|
|
|
|
|
|
|
|
for(std::list<NxsTestHubConnection*>::const_iterator it(mConnections.begin());it!=mConnections.end();++it)
|
|
|
|
delete *it ;
|
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
|
|
|
|
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-20 08:31:26 -05:00
|
|
|
join();
|
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
|
|
|
|
2017-07-24 18:07:53 -04:00
|
|
|
RsNxsMsgDataTemporaryList toStore;
|
2014-04-13 17:52:53 -04:00
|
|
|
std::vector<RsNxsMsg*>::iterator it = messages.begin();
|
|
|
|
for(; it != messages.end(); it++)
|
|
|
|
{
|
|
|
|
RsNxsMsg* msg = *it;
|
|
|
|
RsGxsMsgMetaData* meta = new RsGxsMsgMetaData();
|
2015-12-20 08:29:24 -05:00
|
|
|
// local meta is not touched by the deserialisation routine
|
|
|
|
// have to initialise it
|
2017-07-24 18:07:53 -04:00
|
|
|
|
|
|
|
msg->metaData = meta ;
|
|
|
|
|
2015-12-20 08:29:24 -05:00
|
|
|
meta->mMsgStatus = 0;
|
|
|
|
meta->mMsgSize = 0;
|
|
|
|
meta->mChildTs = 0;
|
|
|
|
meta->recvTS = 0;
|
|
|
|
meta->validated = false;
|
2017-01-21 10:31:22 -05:00
|
|
|
meta->deserialise(msg->meta.bin_data, &(msg->meta.bin_len));
|
2017-07-24 18:07:53 -04:00
|
|
|
|
|
|
|
toStore.push_back(msg);
|
2014-04-13 17:52:53 -04:00
|
|
|
}
|
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 *****/
|
|
|
|
|
2017-07-24 18:07:53 -04:00
|
|
|
RsNxsGrpDataTemporaryList toStore;
|
2014-04-13 17:52:53 -04:00
|
|
|
std::vector<RsNxsGrp*>::iterator it = groups.begin();
|
|
|
|
for(; it != groups.end(); it++)
|
|
|
|
{
|
|
|
|
RsNxsGrp* grp = *it;
|
|
|
|
RsGxsGrpMetaData* meta = new RsGxsGrpMetaData();
|
2017-07-24 18:07:53 -04:00
|
|
|
grp->metaData = meta ;
|
2017-01-21 10:31:22 -05:00
|
|
|
meta->deserialise(grp->meta.bin_data, grp->meta.bin_len);
|
2017-07-24 18:07:53 -04:00
|
|
|
toStore.push_back(grp);
|
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();
|
|
|
|
}
|
|
|
|
|
2015-12-20 08:31:26 -05:00
|
|
|
void rs_nxs_test::NxsTestHub::data_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++)
|
|
|
|
{
|
2017-07-24 18:07:53 -04:00
|
|
|
RsGxsNetService *s = it->second;
|
2014-04-13 17:52:53 -04:00
|
|
|
s->tick();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-12-20 08:31:26 -05:00
|
|
|
double timeDelta = .2;
|
|
|
|
usleep(timeDelta * 1000000);
|
2015-01-11 17:19:32 -05:00
|
|
|
}
|
|
|
|
|
2014-04-13 17:52:53 -04:00
|
|
|
|