Add second testing Simulator.

It has a number of PeerNodes, and captures traffic for examination.


git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@7271 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2014-04-13 12:13:55 +00:00
parent 3a13b2ac65
commit 6be4a464a5
6 changed files with 599 additions and 0 deletions

View File

@ -40,8 +40,13 @@ SOURCES += peer/PeerNode.cc \
###################### Unit Tests ###############################
HEADERS += testing/IsolatedServiceTester.h \
testing/SetServiceTester.h \
testing/SetPacket.h \
testing/SetFilter.h \
SOURCES += testing/IsolatedServiceTester.cc \
testing/SetServiceTester.cc \
testing/SetFilter.cc \
##################### Network Sims ##############################
# to be ported over.

View File

@ -0,0 +1,67 @@
#include "SetFilter.h"
bool SetFilter::filter(const SetPacket &pkt)
{
switch(mFilterMode)
{
case FILTER_NONE:
return false;
break;
case FILTER_ALL:
return true;
break;
case FILTER_PARAMS:
// fall through to below.
break;
}
// use params.
if (mUseSource)
{
std::set<RsPeerId>::const_iterator it;
it = mSources.find(pkt.mSrcId);
if (it == mSources.end())
{
return false;
}
}
if (mUseDest)
{
std::set<RsPeerId>::const_iterator it;
it = mDests.find(pkt.mDestId);
if (it == mDests.end())
{
return false;
}
}
if (mUseFullTypes)
{
std::set<uint32_t>::const_iterator it;
it = mFullTypes.find(pkt.mItem->PacketId());
if (it == mFullTypes.end())
{
return false;
}
}
if (mUseServiceTypes)
{
std::set<uint16_t>::const_iterator it;
it = mServiceTypes.find(pkt.mItem->PacketService());
if (it == mServiceTypes.end())
{
return false;
}
}
return true;
}

View File

@ -0,0 +1,55 @@
#pragma once
#include <list>
#include "retroshare/rsids.h"
#include "SetPacket.h"
class RsItem;
class SetFilter
{
public:
enum FilterMode {
FILTER_NONE,
FILTER_ALL,
FILTER_PARAMS
};
SetFilter()
:mFilterMode(FILTER_ALL),
mUseSource(false), mUseDest(false),
mUseFullTypes(false), mUseServiceTypes(false) { return; }
SetFilter(FilterMode mode)
:mFilterMode(mode),
mUseSource(false), mUseDest(false),
mUseFullTypes(false), mUseServiceTypes(false) { return; }
void setFilterMode(FilterMode mode) { mFilterMode = mode; }
void setUseSource(bool toUse) { mUseSource = toUse; }
void setUseDest(bool toUse) { mUseDest = toUse; }
void setUseFullTypes(bool toUse) { mUseFullTypes = toUse; }
void setUseServiceTypes(bool toUse) { mUseServiceTypes = toUse; }
void addSource(const RsPeerId &id) { mSources.insert(id); }
void addDest(const RsPeerId &id) { mDests.insert(id); }
void addFullType(const uint32_t t) { mFullTypes.insert(t); }
void addServiceType(const uint16_t t) { mServiceTypes.insert(t); }
virtual bool filter(const SetPacket &pkt);
private:
// capture parameters.
FilterMode mFilterMode;
bool mUseSource;
std::set<RsPeerId> mSources;
bool mUseDest;
std::set<RsPeerId> mDests;
bool mUseFullTypes;
std::set<uint32_t> mFullTypes;
bool mUseServiceTypes;
std::set<uint16_t> mServiceTypes;
};

View File

@ -0,0 +1,27 @@
#pragma once
#include "pqi/p3linkmgr.h" // for RsPeerId ??
#include <list>
#include "retroshare/rsids.h"
class RsItem;
class SetPacket
{
public:
SetPacket(double t, const RsPeerId &srcId,
const RsPeerId &destId, RsItem *item)
:mTime(t), mSrcId(srcId), mDestId(destId), mItem(item)
{
return;
}
SetPacket() :mTime(), mItem(NULL) { return; }
double mTime; // relative to test creation;
RsPeerId mSrcId;
RsPeerId mDestId;
RsItem *mItem;
};

View File

@ -0,0 +1,367 @@
#include <list>
#include "retroshare/rsids.h"
#include "serialiser/rsserial.h"
#include "SetServiceTester.h"
#include "peer/PeerNode.h"
SetServiceTester::SetServiceTester()
:mDropFilter(SetFilter::FILTER_NONE),
mCaptureFilter(SetFilter::FILTER_ALL),
mFinishFilter(SetFilter::FILTER_NONE)
{
mRsSerialiser = new RsSerialiser();
mRefTime = time(NULL);
}
SetServiceTester::~SetServiceTester()
{
std::map<RsPeerId, PeerNode *>::iterator pit;
for(pit = mNodes.begin(); pit != mNodes.end(); ++pit)
{
delete (pit->second);
}
mNodes.clear();
std::vector<SetPacket>::iterator it;
for(it = mPackets.begin(); it != mPackets.end(); ++it)
{
delete it->mItem;
}
mPackets.clear();
delete mRsSerialiser;
}
bool SetServiceTester::addNode(const RsPeerId &peerId, std::list<RsPeerId> friendIds)
{
PeerNode *node = new PeerNode(peerId, friendIds, false);
mNodes[peerId] = node;
return true;
}
bool SetServiceTester::addNode(const RsPeerId &peerId, PeerNode *node)
{
mNodes[peerId] = node;
return true;
}
bool SetServiceTester::startup()
{
std::map<RsPeerId, PeerNode *>::iterator pit;
for(pit = mNodes.begin(); pit != mNodes.end(); ++pit)
{
pit->second->notifyOfFriends();
}
return true;
}
bool SetServiceTester::bringOnline(const RsPeerId &peerId, std::list<RsPeerId> peers)
{
std::map<RsPeerId, PeerNode *>::iterator pit;
pit = mNodes.find(peerId);
if (pit != mNodes.end())
{
pit->second->bringOnline(peers);
}
else
{
throw std::logic_error("SetServiceTester::bringOnline() invalid index");
}
return true;
}
/***************************************************************************************************/
/***************************************************************************************************/
uint32_t SetServiceTester::getPacketCount()
{
return mPackets.size();
}
SetPacket &SetServiceTester::examinePacket(uint32_t idx)
{
if (idx >= mPackets.size())
{
throw std::logic_error("SetServiceTester::examinePacket() invalid index");
}
return mPackets[idx];
}
bool SetServiceTester::injectPacket(const SetPacket &pkt)
{
std::cerr << "SetServiceTester::injectPacket() Incomplete";
std::cerr << std::endl;
throw std::logic_error("SetServiceTester::injectPacket() incomplete");
return false;
}
uint32_t SetServiceTester::getNodeCount()
{
return mNodes.size();
}
PeerNode * SetServiceTester::getPeerNode(const RsPeerId &id)
{
std::map<RsPeerId, PeerNode *>::iterator pit;
pit = mNodes.find(id);
if (pit == mNodes.end())
{
throw std::logic_error("SetServiceTester::getPeerNode() invalid index");
}
return pit->second;
}
/***************************************************************************************************/
/***************************************************************************************************/
bool SetServiceTester::tick()
{
tickUntilEvent(1, UNTIL_NONE);
return true;
}
bool SetServiceTester::tickUntilCapturedPacket(int max_ticks, uint32_t& idx)
{
bool eventOccured = tickUntilEvent(max_ticks, UNTIL_CAPTURE);
if (eventOccured)
{
idx = mPackets.size() - 1;
}
return eventOccured;
}
bool SetServiceTester::tickUntilFinish(int max_ticks)
{
return tickUntilEvent(max_ticks, UNTIL_FINISH);
}
bool SetServiceTester::tickUntilEvent(int max_ticks, EventType eventType)
{
std::cerr << "SetServiceTester::tickUntilEvent()";
std::cerr << std::endl;
for(int i = 0; i < max_ticks; i++)
{
std::map<RsPeerId, PeerNode *>::iterator pit;
for(pit = mNodes.begin(); pit != mNodes.end(); ++pit)
{
pit->second->tick();
while (pit->second->haveOutgoingPackets())
{
std::cerr << "SetServiceTester::tickUntilEvent() ";
std::cerr << "packet from: " << pit->first.toStdString();
std::cerr << std::endl;
bool finished = false;
double ts = time(NULL) - mRefTime;
RsRawItem *rawItem = pit->second->outgoing();
RsItem *item = convertToRsItem(rawItem, false);
RsPeerId destId = rawItem->PeerId();
RsPeerId srcId = pit->second->id();
SetPacket pkt(ts, srcId, destId, item);
if (filter(pkt))
{
std::cerr << "Dropping Packet: ";
std::cerr << std::endl;
item->print(std::cerr);
std::cerr << std::endl;
delete rawItem;
delete item;
continue;
}
if (eventType == UNTIL_FINISH)
{
if (finish(pkt))
{
std::cerr << "Finish Packet: ";
std::cerr << std::endl;
item->print(std::cerr);
std::cerr << std::endl;
finished = true;
}
}
if (capture(pkt))
{
std::cerr << "Capture Packet: ";
std::cerr << std::endl;
item->print(std::cerr);
std::cerr << std::endl;
mPackets.push_back(pkt);
if (eventType == UNTIL_CAPTURE)
{
finished = true;
}
}
else
{
delete item;
}
// Pass on Item.
if (rawItem)
{
rawItem->PeerId(srcId);
std::map<RsPeerId, PeerNode *>::iterator pit2;
pit2 = mNodes.find(destId);
if (pit2 != mNodes.end())
{
pit2->second->incoming(rawItem);
}
else
{
// Error.
delete rawItem;
throw std::logic_error("SetServiceTester::tickUntilEvent() invalid destId");
}
}
if (finished)
{
return true;
}
}
}
}
return false;
}
/***************************************************************************************************/
/***************************************************************************************************/
bool SetServiceTester::filter(const SetPacket& packet)
{
return mDropFilter.filter(packet);
}
bool SetServiceTester::capture(const SetPacket& packet)
{
return mCaptureFilter.filter(packet);
}
bool SetServiceTester::finish(const SetPacket& packet)
{
return mFinishFilter.filter(packet);
}
/***************************************************************************************************/
/***************************************************************************************************/
void SetServiceTester::addSerialType(RsSerialType *st)
{
mRsSerialiser->addSerialType(st);
}
RsItem *SetServiceTester::convertToRsItem(RsRawItem *rawitem, bool toDelete)
{
if (rawitem)
{
/* convert back to standard item for convenience */
std::cerr << "SetServiceTester::getPacket() have RsRawItem";
std::cerr << std::endl;
/* convert to RsServiceItem */
uint32_t size = rawitem->getRawLength();
RsItem *item = mRsSerialiser->deserialise(rawitem->getRawData(), &size);
if ((!item) || (size != rawitem->getRawLength()))
{
/* error in conversion */
std::cerr << "SetServiceTester::getPacket() Error";
std::cerr << std::endl;
std::cerr << "SetServiceTester::getPacket() Size: " << size;
std::cerr << std::endl;
std::cerr << "SetServiceTester::getPacket() RawLength: " << rawitem->getRawLength();
std::cerr << std::endl;
if (item)
{
std::cerr << "p3Service::recv() Bad Item:";
std::cerr << std::endl;
item->print(std::cerr, 0);
std::cerr << std::endl;
delete item;
item=NULL ;
}
}
else
{
item->PeerId(rawitem->PeerId());
}
if (toDelete)
{
delete rawitem;
}
return item;
}
return NULL;
}
RsRawItem *SetServiceTester::convertToRsRawItem(RsItem *item, bool toDelete)
{
std::cerr << "SetServiceTester::convertToRawItem()";
std::cerr << std::endl;
/* try to convert */
uint32_t size = mRsSerialiser->size(item);
if (!size)
{
std::cerr << "SetServiceTesterconvertToRsRawItem() ERROR size == 0";
std::cerr << std::endl;
/* can't convert! */
if (toDelete)
{
delete item;
}
return NULL;
}
RsRawItem *raw = new RsRawItem(item->PacketId(), size);
if (!mRsSerialiser->serialise(item, raw->getRawData(), &size))
{
std::cerr << "SetServiceTesterconvertToRsRawItem() ERROR serialise failed";
std::cerr << std::endl;
delete raw;
raw = NULL;
}
if ((raw) && (size != raw->getRawLength()))
{
std::cerr << "SetServiceTesterconvertToRsRawItem() ERROR serialise size mismatch";
std::cerr << std::endl;
delete raw;
raw = NULL;
}
if (raw)
{
raw->PeerId(item->PeerId());
}
if (toDelete)
{
delete item;
}
return raw;
}

View File

@ -0,0 +1,78 @@
#pragma once
#include <list>
#include "retroshare/rsids.h"
#include "pqi/p3linkmgr.h"
#include "SetPacket.h"
#include "SetFilter.h"
class RsItem;
class PeerNode;
class RsSerialType;
class RsSerialiser;
class SetServiceTester
{
public:
SetServiceTester();
~SetServiceTester();
enum EventType {
UNTIL_CAPTURE = 0,
UNTIL_FINISH,
UNTIL_NONE
};
bool addNode(const RsPeerId &peerId, std::list<RsPeerId> friendIds);
bool addNode(const RsPeerId &peerId, PeerNode *node);
void addSerialType(RsSerialType *st);
bool startup();
bool bringOnline(const RsPeerId &peerId, std::list<RsPeerId> peers);
bool tick();
bool tickUntilCapturedPacket(int max_ticks, uint32_t &idx);
bool tickUntilFinish(int max_ticks);
// return true, if we not transmit it.
virtual bool filter(const SetPacket& packet);
// return true, if we should save it.
virtual bool capture(const SetPacket& packet);
// return true to finish.
virtual bool finish(const SetPacket& packet);
uint32_t getPacketCount();
SetPacket &examinePacket(uint32_t idx);
bool injectPacket(const SetPacket &pkt);
uint32_t getNodeCount();
PeerNode *getPeerNode(const RsPeerId &id);
SetFilter &getDropFilter() { return mDropFilter; }
SetFilter &getCaptureFilter() { return mCaptureFilter; }
SetFilter &getFinishFilter() { return mFinishFilter; }
private:
bool tickUntilEvent(int max_ticks, EventType eventType);
RsItem *convertToRsItem(RsRawItem *rawitem, bool toDelete);
RsRawItem *convertToRsRawItem(RsItem *item, bool toDelete);
time_t mRefTime;
std::map<RsPeerId, PeerNode *> mNodes;
RsSerialiser *mRsSerialiser;
std::vector<SetPacket> mPackets;
SetFilter mDropFilter;
SetFilter mCaptureFilter;
SetFilter mFinishFilter;
};