Added Round Trip Time service (revived old p3VoRS service).

- Fixed gxs priority level.



git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.6-initdev@6776 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2013-09-29 07:11:02 +00:00
parent ba157e4891
commit 41daabe1ef
10 changed files with 1147 additions and 9 deletions

View File

@ -3,7 +3,6 @@ CONFIG += staticlib bitdht
CONFIG -= qt CONFIG -= qt
TARGET = retroshare TARGET = retroshare
CONFIG += test_voip
#GXS Stuff. #GXS Stuff.
# This should be disabled for releases until further notice. # This should be disabled for releases until further notice.
@ -91,6 +90,7 @@ PUBLIC_HEADERS = retroshare/rsdisc.h \
retroshare/rstypes.h \ retroshare/rstypes.h \
retroshare/rsdht.h \ retroshare/rsdht.h \
retroshare/rsdsdv.h \ retroshare/rsdsdv.h \
retroshare/rsrtt.h \
retroshare/rsconfig.h retroshare/rsconfig.h
@ -387,6 +387,7 @@ HEADERS += serialiser/rsbaseitems.h \
serialiser/rsbwctrlitems.h \ serialiser/rsbwctrlitems.h \
serialiser/rsdiscovery2items.h \ serialiser/rsdiscovery2items.h \
serialiser/rsheartbeatitems.h \ serialiser/rsheartbeatitems.h \
serialiser/rsrttitems.h \
HEADERS += services/p3chatservice.h \ HEADERS += services/p3chatservice.h \
services/p3msgservice.h \ services/p3msgservice.h \
@ -397,6 +398,7 @@ HEADERS += services/p3chatservice.h \
services/p3bwctrl.h \ services/p3bwctrl.h \
services/p3discovery2.h \ services/p3discovery2.h \
services/p3heartbeat.h \ services/p3heartbeat.h \
services/p3rtt.h \
HEADERS += turtle/p3turtle.h \ HEADERS += turtle/p3turtle.h \
turtle/rsturtleitem.h \ turtle/rsturtleitem.h \
@ -514,6 +516,7 @@ SOURCES += serialiser/rsbaseitems.cc \
serialiser/rsbwctrlitems.cc \ serialiser/rsbwctrlitems.cc \
serialiser/rsdiscovery2items.cc \ serialiser/rsdiscovery2items.cc \
serialiser/rsheartbeatitems.cc \ serialiser/rsheartbeatitems.cc \
serialiser/rsrttitems.cc \
SOURCES += services/p3chatservice.cc \ SOURCES += services/p3chatservice.cc \
services/p3msgservice.cc \ services/p3msgservice.cc \
@ -524,6 +527,7 @@ SOURCES += services/p3chatservice.cc \
services/p3bwctrl.cc \ services/p3bwctrl.cc \
services/p3discovery2.cc \ services/p3discovery2.cc \
services/p3heartbeat.cc \ services/p3heartbeat.cc \
services/p3rtt.cc \
SOURCES += turtle/p3turtle.cc \ SOURCES += turtle/p3turtle.cc \
turtle/rsturtleitem.cc turtle/rsturtleitem.cc

View File

@ -0,0 +1,63 @@
#ifndef RETROSHARE_RTT_INTERFACE_H
#define RETROSHARE_RTT_INTERFACE_H
/*
* libretroshare/src/retroshare: rsvoip.h
*
* RetroShare C++ Interface.
*
* Copyright 2011-2011 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include <inttypes.h>
#include <string>
#include <list>
/* The Main Interface Class - for information about your Peers */
class RsRtt;
extern RsRtt *rsRtt;
class RsRttPongResult
{
public:
RsRttPongResult()
:mTS(0), mRTT(0), mOffset(0) { return; }
RsRttPongResult(double ts, double rtt, double offset)
:mTS(ts), mRTT(rtt), mOffset(offset) { return; }
double mTS;
double mRTT;
double mOffset;
};
class RsRtt
{
public:
RsRtt() { return; }
virtual ~RsRtt() { return; }
virtual uint32_t getPongResults(std::string id, int n, std::list<RsRttPongResult> &results) = 0;
};
#endif

View File

@ -1842,9 +1842,17 @@ RsTurtle *rsTurtle = NULL ;
/**** /****
* #define RS_RELEASE 1 * #define RS_RELEASE 1
* #define RS_RTT 1
****/ ****/
#define RS_RELEASE 1 #define RS_RELEASE 1
#define RS_RTT 1
#ifdef RS_RTT
#include "services/p3rtt.h"
#endif
#include "services/p3banlist.h" #include "services/p3banlist.h"
#include "services/p3bwctrl.h" #include "services/p3bwctrl.h"
@ -2452,10 +2460,10 @@ int RsServer::StartupRetroShare()
#endif // RS_ENABLE_GXS. #endif // RS_ENABLE_GXS.
#ifdef RS_VOIPTEST #ifdef RS_RTT
p3VoRS *mVoipTest = new p3VoRS(mLinkMgr); p3rtt *mRtt = new p3rtt(mLinkMgr);
pqih -> addService(mVoipTest); pqih -> addService(mRtt);
rsVoip = mVoipTest; rsRtt = mRtt;
#endif #endif
// new services to test. // new services to test.

View File

@ -87,9 +87,9 @@ const uint8_t QOS_PRIORITY_RS_CHAT_AVATAR_ITEM = 2 ;
const uint8_t QOS_PRIORITY_RS_MSG_ITEM = 2 ; const uint8_t QOS_PRIORITY_RS_MSG_ITEM = 2 ;
const uint8_t QOS_PRIORITY_RS_STATUS_ITEM = 2 ; const uint8_t QOS_PRIORITY_RS_STATUS_ITEM = 2 ;
// VOIP // RTT
// //
const uint8_t QOS_PRIORITY_RS_VOIP_PING = 9 ; const uint8_t QOS_PRIORITY_RS_RTT_PING = 9 ;
// BanList // BanList
// //

View File

@ -70,7 +70,7 @@ public:
RsNxsItem(uint16_t servtype, uint8_t subtype) RsNxsItem(uint16_t servtype, uint8_t subtype)
: RsItem(RS_PKT_VERSION_SERVICE, servtype, subtype), transactionNumber(0) : RsItem(RS_PKT_VERSION_SERVICE, servtype, subtype), transactionNumber(0)
{ {
setPriorityLevel(QOS_PRIORITY_RS_VOIP_PING); setPriorityLevel(QOS_PRIORITY_RS_GXS_NET);
return; return;
} }
virtual ~RsNxsItem(){ return; } virtual ~RsNxsItem(){ return; }

View File

@ -0,0 +1,373 @@
/*
* libretroshare/src/serialiser: rsrttitems.cc
*
* RetroShare Serialiser.
*
* Copyright 2011-2013 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2.1 as published by the Free Software Foundation.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include "serialiser/rsbaseserial.h"
#include "serialiser/rsrttitems.h"
#include "serialiser/rstlvbase.h"
/***
#define RSSERIAL_DEBUG 1
***/
#include <iostream>
/*************************************************************************/
RsRttPingItem::~RsRttPingItem()
{
return;
}
void RsRttPingItem::clear()
{
mSeqNo = 0;
mPingTS = 0;
}
std::ostream& RsRttPingItem::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsRttPingItem", indent);
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << "SeqNo: " << mSeqNo << std::endl;
printIndent(out, int_Indent);
out << "PingTS: " << std::hex << mPingTS << std::dec << std::endl;
printRsItemEnd(out, "RsRttPingItem", indent);
return out;
}
RsRttPongItem::~RsRttPongItem()
{
return;
}
void RsRttPongItem::clear()
{
mSeqNo = 0;
mPingTS = 0;
mPongTS = 0;
}
std::ostream& RsRttPongItem::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsRttPongItem", indent);
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << "SeqNo: " << mSeqNo << std::endl;
printIndent(out, int_Indent);
out << "PingTS: " << std::hex << mPingTS << std::dec << std::endl;
printIndent(out, int_Indent);
out << "PongTS: " << std::hex << mPongTS << std::dec << std::endl;
printRsItemEnd(out, "RsRttPongItem", indent);
return out;
}
/*************************************************************************/
uint32_t RsRttSerialiser::sizeRttPingItem(RsRttPingItem */*item*/)
{
uint32_t s = 8; /* header */
s += 4; /* seqno */
s += 8; /* pingTS */
return s;
}
/* serialise the data to the buffer */
bool RsRttSerialiser::serialiseRttPingItem(RsRttPingItem *item, void *data, uint32_t *pktsize)
{
uint32_t tlvsize = sizeRttPingItem(item);
uint32_t offset = 0;
if (*pktsize < tlvsize)
return false; /* not enough space */
*pktsize = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
#ifdef RSSERIAL_DEBUG
std::cerr << "RsRttSerialiser::serialiseRttPingItem() Header: " << ok << std::endl;
std::cerr << "RsRttSerialiser::serialiseRttPingItem() Size: " << tlvsize << std::endl;
#endif
/* skip the header */
offset += 8;
/* add mandatory parts first */
ok &= setRawUInt32(data, tlvsize, &offset, item->mSeqNo);
ok &= setRawUInt64(data, tlvsize, &offset, item->mPingTS);
if (offset != tlvsize)
{
ok = false;
std::cerr << "RsRttSerialiser::serialiseRttPingItem() Size Error! " << std::endl;
}
return ok;
}
RsRttPingItem *RsRttSerialiser::deserialiseRttPingItem(void *data, uint32_t *pktsize)
{
/* get the type and size */
uint32_t rstype = getRsItemId(data);
uint32_t rssize = getRsItemSize(data);
uint32_t offset = 0;
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(RS_SERVICE_TYPE_RTT != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_RTT_PING != getRsItemSubType(rstype)))
{
return NULL; /* wrong type */
}
if (*pktsize < rssize) /* check size */
return NULL; /* not enough data */
/* set the packet length */
*pktsize = rssize;
bool ok = true;
/* ready to load */
RsRttPingItem *item = new RsRttPingItem();
item->clear();
/* skip the header */
offset += 8;
/* get mandatory parts first */
ok &= getRawUInt32(data, rssize, &offset, &(item->mSeqNo));
ok &= getRawUInt64(data, rssize, &offset, &(item->mPingTS));
if (offset != rssize)
{
/* error */
delete item;
return NULL;
}
if (!ok)
{
delete item;
return NULL;
}
return item;
}
/*************************************************************************/
/*************************************************************************/
uint32_t RsRttSerialiser::sizeRttPongItem(RsRttPongItem */*item*/)
{
uint32_t s = 8; /* header */
s += 4; /* seqno */
s += 8; /* pingTS */
s += 8; /* pongTS */
return s;
}
/* serialise the data to the buffer */
bool RsRttSerialiser::serialiseRttPongItem(RsRttPongItem *item, void *data, uint32_t *pktsize)
{
uint32_t tlvsize = sizeRttPongItem(item);
uint32_t offset = 0;
if (*pktsize < tlvsize)
return false; /* not enough space */
*pktsize = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
#ifdef RSSERIAL_DEBUG
std::cerr << "RsRttSerialiser::serialiseRttPongItem() Header: " << ok << std::endl;
std::cerr << "RsRttSerialiser::serialiseRttPongItem() Size: " << tlvsize << std::endl;
#endif
/* skip the header */
offset += 8;
/* add mandatory parts first */
ok &= setRawUInt32(data, tlvsize, &offset, item->mSeqNo);
ok &= setRawUInt64(data, tlvsize, &offset, item->mPingTS);
ok &= setRawUInt64(data, tlvsize, &offset, item->mPongTS);
if (offset != tlvsize)
{
ok = false;
std::cerr << "RsRttSerialiser::serialiseRttPongItem() Size Error! " << std::endl;
}
return ok;
}
RsRttPongItem *RsRttSerialiser::deserialiseRttPongItem(void *data, uint32_t *pktsize)
{
/* get the type and size */
uint32_t rstype = getRsItemId(data);
uint32_t rssize = getRsItemSize(data);
uint32_t offset = 0;
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(RS_SERVICE_TYPE_RTT != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_RTT_PONG != getRsItemSubType(rstype)))
{
return NULL; /* wrong type */
}
if (*pktsize < rssize) /* check size */
return NULL; /* not enough data */
/* set the packet length */
*pktsize = rssize;
bool ok = true;
/* ready to load */
RsRttPongItem *item = new RsRttPongItem();
item->clear();
/* skip the header */
offset += 8;
/* get mandatory parts first */
ok &= getRawUInt32(data, rssize, &offset, &(item->mSeqNo));
ok &= getRawUInt64(data, rssize, &offset, &(item->mPingTS));
ok &= getRawUInt64(data, rssize, &offset, &(item->mPongTS));
if (offset != rssize)
{
/* error */
delete item;
return NULL;
}
if (!ok)
{
delete item;
return NULL;
}
return item;
}
/*************************************************************************/
uint32_t RsRttSerialiser::size(RsItem *i)
{
RsRttPingItem *ping;
RsRttPongItem *pong;
if (NULL != (ping = dynamic_cast<RsRttPingItem *>(i)))
{
return sizeRttPingItem(ping);
}
else if (NULL != (pong = dynamic_cast<RsRttPongItem *>(i)))
{
return sizeRttPongItem(pong);
}
return 0;
}
bool RsRttSerialiser::serialise(RsItem *i, void *data, uint32_t *pktsize)
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsMsgSerialiser::serialise()" << std::endl;
#endif
RsRttPingItem *ping;
RsRttPongItem *pong;
if (NULL != (ping = dynamic_cast<RsRttPingItem *>(i)))
{
return serialiseRttPingItem(ping, data, pktsize);
}
else if (NULL != (pong = dynamic_cast<RsRttPongItem *>(i)))
{
return serialiseRttPongItem(pong, data, pktsize);
}
return false;
}
RsItem* RsRttSerialiser::deserialise(void *data, uint32_t *pktsize)
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsRttSerialiser::deserialise()" << std::endl;
#endif
/* get the type and size */
uint32_t rstype = getRsItemId(data);
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(RS_SERVICE_TYPE_RTT != getRsItemService(rstype)))
{
return NULL; /* wrong type */
}
switch(getRsItemSubType(rstype))
{
case RS_PKT_SUBTYPE_RTT_PING:
return deserialiseRttPingItem(data, pktsize);
break;
case RS_PKT_SUBTYPE_RTT_PONG:
return deserialiseRttPongItem(data, pktsize);
break;
default:
return NULL;
break;
}
return NULL;
}
/*************************************************************************/

View File

@ -0,0 +1,108 @@
#ifndef RS_RTT_ITEMS_H
#define RS_RTT_ITEMS_H
/*
* libretroshare/src/serialiser: rsrttitems.h
*
* RetroShare Serialiser.
*
* Copyright 2011-2013 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2.1 as published by the Free Software Foundation.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include <map>
#include "serialiser/rsserviceids.h"
#include "serialiser/rsserial.h"
#include "serialiser/rstlvtypes.h"
/**************************************************************************/
const uint8_t RS_PKT_SUBTYPE_RTT_PING = 0x01;
const uint8_t RS_PKT_SUBTYPE_RTT_PONG = 0x02;
class RsRttItem: public RsItem
{
public:
RsRttItem(uint8_t chat_subtype) : RsItem(RS_PKT_VERSION_SERVICE,RS_SERVICE_TYPE_RTT,chat_subtype)
{ setPriorityLevel(QOS_PRIORITY_RS_RTT_PING) ;} // should be refined later.
virtual ~RsRttItem() {};
virtual void clear() {};
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) = 0 ;
};
class RsRttPingItem: public RsRttItem
{
public:
RsRttPingItem() :RsRttItem(RS_PKT_SUBTYPE_RTT_PING) {}
virtual ~RsRttPingItem();
virtual void clear();
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0);
uint32_t mSeqNo;
uint64_t mPingTS;
};
class RsRttPongItem: public RsRttItem
{
public:
RsRttPongItem() :RsRttItem(RS_PKT_SUBTYPE_RTT_PONG) {}
virtual ~RsRttPongItem();
virtual void clear();
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0);
uint32_t mSeqNo;
uint64_t mPingTS;
uint64_t mPongTS;
};
class RsRttSerialiser: public RsSerialType
{
public:
RsRttSerialiser()
:RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_RTT)
{ return; }
virtual ~RsRttSerialiser() { return; }
virtual uint32_t size(RsItem *);
virtual bool serialise (RsItem *item, void *data, uint32_t *size);
virtual RsItem * deserialise(void *data, uint32_t *size);
private:
virtual uint32_t sizeRttPingItem(RsRttPingItem *);
virtual bool serialiseRttPingItem (RsRttPingItem *item, void *data, uint32_t *size);
virtual RsRttPingItem *deserialiseRttPingItem(void *data, uint32_t *size);
virtual uint32_t sizeRttPongItem(RsRttPongItem *);
virtual bool serialiseRttPongItem (RsRttPongItem *item, void *data, uint32_t *size);
virtual RsRttPongItem *deserialiseRttPongItem(void *data, uint32_t *size);
};
/**************************************************************************/
#endif /* RS_RTT_ITEMS_H */

View File

@ -104,12 +104,15 @@ const uint16_t RS_SERVICE_TYPE_PROXY = 0xf030;
/* DSDV Testing at the moment - Service Only */ /* DSDV Testing at the moment - Service Only */
const uint16_t RS_SERVICE_TYPE_DSDV = 0xf050; const uint16_t RS_SERVICE_TYPE_DSDV = 0xf050;
/* Latency RTT Measurements */
const uint16_t RS_SERVICE_TYPE_RTT = 0xf051;
/* Bandwidth Testing at the moment - Service Only */ /* Bandwidth Testing at the moment - Service Only */
const uint16_t RS_SERVICE_TYPE_BWCTRL = 0xf060; const uint16_t RS_SERVICE_TYPE_BWCTRL = 0xf060;
//const uint16_t RS_SERVICE_TYPE_DISTRIB = 0xf110; //const uint16_t RS_SERVICE_TYPE_DISTRIB = 0xf110;
//const uint16_t RS_SERVICE_TYPE_FORUM = 0xf120; //const uint16_t RS_SERVICE_TYPE_FORUM = 0xf120;
//const uint16_t RS_SERVICE_TYPE_CHANNEL = 0xf130; //const uint16_t RS_SERVICE_TYPE_CHANNEL = 0xf130;

View File

@ -0,0 +1,461 @@
/*
* libretroshare/src/services p3rtt.cc
*
* Round Trip Time Measurement for RetroShare.
*
* Copyright 2011-2013 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2.1 as published by the Free Software Foundation.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include "util/rsdir.h"
#include "retroshare/rsiface.h"
#include "pqi/pqibin.h"
#include "pqi/pqinotify.h"
#include "pqi/pqistore.h"
#include "pqi/p3linkmgr.h"
#include "services/p3rtt.h"
#include "serialiser/rsrttitems.h"
#include <sys/time.h>
/****
* #define DEBUG_RTT 1
****/
/* DEFINE INTERFACE POINTER! */
RsRtt *rsRtt = NULL;
#define MAX_PONG_RESULTS 150
#define RTT_PING_PERIOD 10
/************ IMPLEMENTATION NOTES *********************************
*
* Voice over Retroshare ;)
*
* This will be a simple test VoIP system aimed at testing out the possibilities.
*
* Important things to test:
* 1) lag, and variability in data rate
* - To do this we time tag every packet..., the destination can use this info to calculate the results.
* - Like imixitup. Dt = clock_diff + lag.
* we expect clock_diff to be relatively constant, but lag to vary.
* lag cannot be negative, so minimal Dt is ~clock_diff, and delays on this are considered +lag.
*
* 2) we could directly measure lag. ping back and forth with Timestamps.
*
* 3) we also want to measure bandwidth...
* - not sure the best method?
* one way: send a ping, then a large amount of data (5 seconds worth), then another ping.
* the delta in timestamps should be a decent indication of bandwidth.
* say we have a 100kb/s connection... need 500kb.
* actually the amount of data should be based on a reasonable maximum that we require.
* what does decent video require?
* Audio we can test for 64kb/s - which seems like a decent rate: e.g. mono, 16bit 22k = 1 x 2 x 22k = 44 kilobytes/sec
* best to do this without a VoIP call going on ;)
*
*
*/
#ifdef WINDOWS_SYS
#include <time.h>
#include <sys/timeb.h>
#endif
static double getCurrentTS()
{
#ifndef WINDOWS_SYS
struct timeval cts_tmp;
gettimeofday(&cts_tmp, NULL);
double cts = (cts_tmp.tv_sec) + ((double) cts_tmp.tv_usec) / 1000000.0;
#else
struct _timeb timebuf;
_ftime( &timebuf);
double cts = (timebuf.time) + ((double) timebuf.millitm) / 1000.0;
#endif
return cts;
}
static uint64_t convertTsTo64bits(double ts)
{
uint32_t secs = (uint32_t) ts;
uint32_t usecs = (uint32_t) ((ts - (double) secs) * 1000000);
uint64_t bits = (((uint64_t) secs) << 32) + usecs;
return bits;
}
static double convert64bitsToTs(uint64_t bits)
{
uint32_t usecs = (uint32_t) (bits & 0xffffffff);
uint32_t secs = (uint32_t) ((bits >> 32) & 0xffffffff);
double ts = (secs) + ((double) usecs) / 1000000.0;
return ts;
}
p3rtt::p3rtt(p3LinkMgr *lm)
:p3Service(RS_SERVICE_TYPE_RTT), /* p3Config(CONFIG_TYPE_RTT), */ mRttMtx("p3rtt"), mLinkMgr(lm)
{
addSerialType(new RsRttSerialiser());
mSentPingTime = 0;
mCounter = 0;
}
int p3rtt::tick()
{
processIncoming();
sendPackets();
return 0;
}
int p3rtt::status()
{
return 1;
}
int p3rtt::sendPackets()
{
time_t now = time(NULL);
time_t pt;
{
RsStackMutex stack(mRttMtx); /****** LOCKED MUTEX *******/
pt = mSentPingTime;
}
if (now - pt > RTT_PING_PERIOD)
{
sendPingMeasurements();
RsStackMutex stack(mRttMtx); /****** LOCKED MUTEX *******/
mSentPingTime = now;
}
return true ;
}
void p3rtt::sendPingMeasurements()
{
/* we ping our peers */
/* who is online? */
std::list<std::string> idList;
mLinkMgr->getOnlineList(idList);
double ts = getCurrentTS();
#ifdef DEBUG_RTT
std::cerr << "p3rtt::sendPingMeasurements() @ts: " << ts;
std::cerr << std::endl;
#endif
/* prepare packets */
std::list<std::string>::iterator it;
for(it = idList.begin(); it != idList.end(); it++)
{
#ifdef DEBUG_RTT
std::cerr << "p3rtt::sendPingMeasurements() Pinging: " << *it;
std::cerr << std::endl;
#endif
/* create the packet */
RsRttPingItem *pingPkt = new RsRttPingItem();
pingPkt->PeerId(*it);
pingPkt->mSeqNo = mCounter;
pingPkt->mPingTS = convertTsTo64bits(ts);
storePingAttempt(*it, ts, mCounter);
#ifdef DEBUG_RTT
std::cerr << "p3rtt::sendPingMeasurements() With Packet:";
std::cerr << std::endl;
pingPkt->print(std::cerr, 10);
#endif
sendItem(pingPkt);
}
RsStackMutex stack(mRttMtx); /****** LOCKED MUTEX *******/
mCounter++;
}
int p3rtt::processIncoming()
{
/* for each packet - pass to specific handler */
RsItem *item = NULL;
while(NULL != (item = recvItem()))
{
switch(item->PacketSubType())
{
default:
break;
case RS_PKT_SUBTYPE_RTT_PING:
{
handlePing(item);
}
break;
case RS_PKT_SUBTYPE_RTT_PONG:
{
handlePong(item);
}
break;
#if 0
/* THESE ARE ALL FUTURISTIC DATA TYPES */
case RS_DATA_ITEM:
{
handleData(item);
}
break;
case RS_BANDWIDTH_PING_ITEM:
{
handleBandwidthPing(item);
}
break;
case RS_BANDWIDTH_PONG_ITEM:
{
handleBandwidthPong(item);
}
break;
#endif
}
/* clean up */
delete item;
}
return true ;
}
int p3rtt::handlePing(RsItem *item)
{
/* cast to right type */
RsRttPingItem *ping = (RsRttPingItem *) item;
#ifdef DEBUG_RTT
std::cerr << "p3rtt::handlePing() Recvd Packet from: " << ping->PeerId();
std::cerr << std::endl;
#endif
/* with a ping, we just respond as quickly as possible - they do all the analysis */
RsRttPongItem *pong = new RsRttPongItem();
pong->PeerId(ping->PeerId());
pong->mPingTS = ping->mPingTS;
pong->mSeqNo = ping->mSeqNo;
// add our timestamp.
double ts = getCurrentTS();
pong->mPongTS = convertTsTo64bits(ts);
#ifdef DEBUG_RTT
std::cerr << "p3rtt::handlePing() With Packet:";
std::cerr << std::endl;
pong->print(std::cerr, 10);
#endif
sendItem(pong);
return true ;
}
int p3rtt::handlePong(RsItem *item)
{
/* cast to right type */
RsRttPongItem *pong = (RsRttPongItem *) item;
#ifdef DEBUG_RTT
std::cerr << "p3rtt::handlePong() Recvd Packet from: " << pong->PeerId();
std::cerr << std::endl;
pong->print(std::cerr, 10);
#endif
/* with a pong, we do the maths! */
double recvTS = getCurrentTS();
double pingTS = convert64bitsToTs(pong->mPingTS);
double pongTS = convert64bitsToTs(pong->mPongTS);
double rtt = recvTS - pingTS;
double offset = pongTS - (recvTS - rtt / 2.0); // so to get to their time, we go ourTS + offset.
#ifdef DEBUG_RTT
std::cerr << "p3rtt::handlePong() Timing:";
std::cerr << std::endl;
std::cerr << "\tpingTS: " << pingTS;
std::cerr << std::endl;
std::cerr << "\tpongTS: " << pongTS;
std::cerr << std::endl;
std::cerr << "\trecvTS: " << recvTS;
std::cerr << std::endl;
std::cerr << "\t ==> rtt: " << rtt;
std::cerr << std::endl;
std::cerr << "\t ==> offset: " << offset;
std::cerr << std::endl;
#endif
storePongResult(pong->PeerId(), pong->mSeqNo, pingTS, rtt, offset);
return true ;
}
int p3rtt::storePingAttempt(std::string id, double ts, uint32_t seqno)
{
RsStackMutex stack(mRttMtx); /****** LOCKED MUTEX *******/
/* find corresponding local data */
RttPeerInfo *peerInfo = locked_GetPeerInfo(id);
peerInfo->mCurrentPingTS = ts;
peerInfo->mCurrentPingCounter = seqno;
peerInfo->mSentPings++;
if (!peerInfo->mCurrentPongRecvd)
{
peerInfo->mLostPongs++;
}
peerInfo->mCurrentPongRecvd = true;
return 1;
}
int p3rtt::storePongResult(std::string id, uint32_t counter, double ts, double rtt, double offset)
{
RsStackMutex stack(mRttMtx); /****** LOCKED MUTEX *******/
/* find corresponding local data */
RttPeerInfo *peerInfo = locked_GetPeerInfo(id);
if (peerInfo->mCurrentPingCounter != counter)
{
#ifdef DEBUG_RTT
std::cerr << "p3rtt::storePongResult() ERROR Severly Delayed Measurements!" << std::endl;
#endif
}
else
{
peerInfo->mCurrentPongRecvd = true;
}
peerInfo->mPongResults.push_back(RsRttPongResult(ts, rtt, offset));
while(peerInfo->mPongResults.size() > MAX_PONG_RESULTS)
{
peerInfo->mPongResults.pop_front();
}
/* should do calculations */
return 1;
}
uint32_t p3rtt::getPongResults(std::string id, int n, std::list<RsRttPongResult> &results)
{
RsStackMutex stack(mRttMtx); /****** LOCKED MUTEX *******/
RttPeerInfo *peer = locked_GetPeerInfo(id);
std::list<RsRttPongResult>::reverse_iterator it;
int i = 0;
for(it = peer->mPongResults.rbegin(); (it != peer->mPongResults.rend()) && (i < n); it++, i++)
{
/* reversing order - so its easy to trim later */
results.push_back(*it);
}
return i ;
}
RttPeerInfo *p3rtt::locked_GetPeerInfo(std::string id)
{
std::map<std::string, RttPeerInfo>::iterator it;
it = mPeerInfo.find(id);
if (it == mPeerInfo.end())
{
/* add it in */
RttPeerInfo pinfo;
/* initialise entry */
pinfo.initialisePeerInfo(id);
mPeerInfo[id] = pinfo;
it = mPeerInfo.find(id);
}
return &(it->second);
}
bool RttPeerInfo::initialisePeerInfo(std::string id)
{
mId = id;
/* reset variables */
mCurrentPingTS = 0;
mCurrentPingCounter = 0;
mCurrentPongRecvd = true;
mSentPings = 0;
mLostPongs = 0;
mPongResults.clear();
return true;
}

View File

@ -0,0 +1,118 @@
/*
* libretroshare/src/services/p3rtt.h
*
* Round Trip Time Measurement for RetroShare.
*
* Copyright 2011-2013 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2.1 as published by the Free Software Foundation.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#ifndef SERVICE_RSRTT_HEADER
#define SERVICE_RSRTT_HEADER
#include <list>
#include <string>
#include "serialiser/rsrttitems.h"
#include "services/p3service.h"
#include "retroshare/rsrtt.h"
class p3LinkMgr;
class RttPeerInfo
{
public:
bool initialisePeerInfo(std::string id);
std::string mId;
double mCurrentPingTS;
double mCurrentPingCounter;
bool mCurrentPongRecvd;
uint32_t mLostPongs;
uint32_t mSentPings;
std::list<RsRttPongResult> mPongResults;
};
//!The RS Rtt Test service.
/**
*
* Used to test Latency.
*/
class p3rtt: public RsRtt, public p3Service
{
public:
p3rtt(p3LinkMgr *cm);
/***** overloaded from rsRtt *****/
virtual uint32_t getPongResults(std::string id, int n, std::list<RsRttPongResult> &results);
/***** overloaded from p3Service *****/
virtual int tick();
virtual int status();
int sendPackets();
void sendPingMeasurements();
int processIncoming();
int handlePing(RsItem *item);
int handlePong(RsItem *item);
int storePingAttempt(std::string id, double ts, uint32_t mCounter);
int storePongResult(std::string id, uint32_t counter, double ts, double rtt, double offset);
/*!
* This retrieves all public chat msg items
*/
//bool getPublicChatQueue(std::list<ChatInfo> &chats);
/*************** pqiMonitor callback ***********************/
//virtual void statusChange(const std::list<pqipeer> &plist);
/************* from p3Config *******************/
//virtual RsSerialiser *setupSerialiser() ;
//virtual bool saveList(bool& cleanup, std::list<RsItem*>&) ;
//virtual void saveDone();
//virtual bool loadList(std::list<RsItem*>& load) ;
private:
RsMutex mRttMtx;
RttPeerInfo *locked_GetPeerInfo(std::string id);
std::map<std::string, RttPeerInfo> mPeerInfo;
time_t mSentPingTime;
uint32_t mCounter;
p3LinkMgr *mLinkMgr;
};
#endif // SERVICE_RSRTT_HEADER