added grouter as a msg sending service. Not enable yet. Added serialisation for grouter items and test methods in tests/serialiser/. Added saveList/loadList for grouter. set connectToTurtleRouter() and connectToGRouter() to be mandatory methods

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@6970 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2013-12-27 20:06:47 +00:00
parent da1b6ac845
commit 5c52890ad5
17 changed files with 784 additions and 80 deletions

View File

@ -100,7 +100,7 @@ class ftServer: public RsFiles, public ftDataSend, public RsTurtleClientService,
/* Final Setup (once everything is assigned) */
//void SetupFtServer();
void SetupFtServer(NotifyBase *cb);
void connectToTurtleRouter(p3turtle *p) ;
virtual void connectToTurtleRouter(p3turtle *p) ;
void StartupThreads();
void StopThreads();

View File

@ -16,8 +16,6 @@ bool RsGRouterItem::serialise_header(void *data,uint32_t& pktsize,uint32_t& tlvs
pktsize = tlvsize;
bool ok = true;
if(!setRsItemHeader(data, tlvsize, PacketId(), tlvsize))
{
std::cerr << "RsFileTransferItem::serialise_header(): ERROR. Not enough size!" << std::endl;
@ -86,6 +84,9 @@ RsItem *RsGRouterSerialiser::deserialise(void *data, uint32_t *pktsize)
case RS_PKT_SUBTYPE_GROUTER_PUBLISH_KEY: return deserialise_RsGRouterPublishKeyItem(data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_DATA: return deserialise_RsGRouterGenericDataItem(data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_ACK: return deserialise_RsGRouterACKItem(data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_MATRIX_CLUES: return deserialise_RsGRouterMatrixCluesItem(data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_FRIENDS_LIST: return deserialise_RsGRouterMatrixFriendListItem(data, *pktsize);
case RS_PKT_SUBTYPE_GROUTER_ROUTING_INFO: return deserialise_RsGRouterRoutingInfoItem(data, *pktsize);
default:
std::cerr << "RsGRouterSerialiser::deserialise(): Could not de-serialise item. SubPacket id = " << std::hex << getRsItemSubType(rstype) << " id = " << rstype << std::dec << std::endl;
return NULL;
@ -166,10 +167,116 @@ RsGRouterItem *RsGRouterSerialiser::deserialise_RsGRouterACKItem(void *data, uin
return item;
}
RsGRouterItem *RsGRouterSerialiser::deserialise_RsGRouterRoutingInfoItem(void *data, uint32_t pktsize) const
{
uint32_t offset = 8; // skip the header
uint32_t rssize = getRsItemSize(data);
bool ok = true ;
RsGRouterRoutingInfoItem *item = new RsGRouterRoutingInfoItem() ;
ok &= getRawUInt32(data, pktsize, &offset, &item->status_flags);
ok &= getRawSSLId(data, pktsize, &offset, item->origin);
ok &= getRawTimeT(data, pktsize, &offset, item->received_time);
uint32_t s = 0 ;
ok &= getRawUInt32(data, pktsize, &offset, &s) ;
for(uint32_t i=0;i<s;++i)
{
FriendTrialRecord ftr ;
ok &= getRawSSLId(data, pktsize, &offset, ftr.friend_id);
ok &= getRawTimeT(data, pktsize, &offset, ftr.time_stamp) ;
item->tried_friends.push_back(ftr) ;
}
item->data_item = new RsGRouterGenericDataItem ;
ok &= getRawUInt32(data, pktsize, &offset, &item->data_item->routing_id);
ok &= getRawSha1(data, pktsize, &offset, item->data_item->destination_key) ;
ok &= getRawUInt32(data, pktsize, &offset, &item->data_item->data_size) ;
item->data_item->data_bytes = (uint8_t*)malloc(item->data_item->data_size) ;
memcpy(item->data_item->data_bytes,&((uint8_t*)data)[offset],item->data_item->data_size) ;
offset += item->data_item->data_size ;
if (offset != rssize || !ok)
{
std::cerr << __PRETTY_FUNCTION__ << ": error while deserialising! Item will be dropped." << std::endl;
return NULL ;
}
return item;
}
RsGRouterItem *RsGRouterSerialiser::deserialise_RsGRouterMatrixFriendListItem(void *data, uint32_t pktsize) const
{
uint32_t offset = 8; // skip the header
uint32_t rssize = getRsItemSize(data);
bool ok = true ;
RsGRouterMatrixFriendListItem *item = new RsGRouterMatrixFriendListItem() ;
uint32_t nb_friends = 0 ;
ok &= getRawUInt32(data, pktsize, &offset, &nb_friends); // file hash
item->reverse_friend_indices.resize(nb_friends) ;
for(uint32_t i=0;ok && i<nb_friends;++i)
ok &= getRawSSLId(data, pktsize, &offset, item->reverse_friend_indices[i]) ;
if (offset != rssize || !ok)
{
std::cerr << __PRETTY_FUNCTION__ << ": error while deserialising! Item will be dropped." << std::endl;
return NULL ;
}
return item;
}
RsGRouterItem *RsGRouterSerialiser::deserialise_RsGRouterMatrixCluesItem(void *data, uint32_t pktsize) const
{
uint32_t offset = 8; // skip the header
uint32_t rssize = getRsItemSize(data);
bool ok = true ;
RsGRouterMatrixCluesItem *item = new RsGRouterMatrixCluesItem() ;
ok &= getRawSha1(data,pktsize,&offset,item->destination_key) ;
uint32_t nb_clues = 0 ;
ok &= getRawUInt32(data, pktsize, &offset, &nb_clues);
item->clues.clear() ;
for(uint32_t j=0;j<nb_clues;++j)
{
RoutingMatrixHitEntry HitE ;
ok &= getRawUInt32(data, pktsize, &offset, &HitE.friend_id);
ok &= getRawUFloat32(data, pktsize, &offset, HitE.weight);
ok &= getRawTimeT(data, pktsize, &offset, HitE.time_stamp);
item->clues.push_back(HitE) ;
}
if (offset != rssize || !ok)
{
std::cerr << __PRETTY_FUNCTION__ << ": error while deserialising! Item will be dropped." << std::endl;
return NULL ;
}
return item;
}
RsGRouterGenericDataItem *RsGRouterGenericDataItem::duplicate() const
{
RsGRouterGenericDataItem *item = new RsGRouterGenericDataItem ;
*item = *this ; // copies everything.
item->routing_id = routing_id ;
item->destination_key = destination_key ;
item->data_size = data_size ;
// then duplicate the memory chunk
@ -242,6 +349,127 @@ bool RsGRouterACKItem::serialise(void *data,uint32_t& size) const
return ok;
}
/* serialise the data to the buffer */
uint32_t RsGRouterMatrixCluesItem::serial_size() const
{
uint32_t s = 8 ; // header
s += 20 ; // Key size
s += 4 ; // list<RoutingMatrixHitEntry>::size()
s += (4+4+8) * clues.size() ;
return s ;
}
uint32_t RsGRouterMatrixFriendListItem::serial_size() const
{
uint32_t s = 8 ; // header
s += 4 ; // reverse_friend_indices.size()
s += 16 * reverse_friend_indices.size() ; // sha1 for published_key
return s ;
}
uint32_t RsGRouterRoutingInfoItem::serial_size() const
{
uint32_t s = 8 ; // header
s += 4 ; // status_flags
s += 16 ; // origin
s += 8 ; // received_time
s += 4 ; // tried_friends.size() ;
s += tried_friends.size() * ( 16 + 8 ) ; // FriendTrialRecord
s += 4; // data_item->routing_id
s += 20; // data_item->destination_key
s += 4; // data_item->data_size
s += data_item->data_size; // data_item->data_bytes
return s ;
}
bool RsGRouterMatrixFriendListItem::serialise(void *data,uint32_t& size) const
{
uint32_t tlvsize,offset=0;
bool ok = true;
if(!serialise_header(data,size,tlvsize,offset))
return false ;
/* add mandatory parts first */
ok &= setRawUInt32(data, tlvsize, &offset, reverse_friend_indices.size());
for(uint32_t i=0;ok && i<reverse_friend_indices.size();++i)
ok &= setRawSSLId(data,tlvsize,&offset,reverse_friend_indices[i]) ;
if (offset != tlvsize)
{
ok = false;
std::cerr << "rsfileitemserialiser::serialisedata() size error! " << std::endl;
}
return ok;
}
bool RsGRouterMatrixCluesItem::serialise(void *data,uint32_t& size) const
{
uint32_t tlvsize,offset=0;
bool ok = true;
if(!serialise_header(data,size,tlvsize,offset))
return false ;
/* add mandatory parts first */
ok &= setRawSha1(data,tlvsize,&offset,destination_key) ;
ok &= setRawUInt32(data, tlvsize, &offset, clues.size());
for(std::list<RoutingMatrixHitEntry>::const_iterator it2(clues.begin());it2!=clues.end();++it2)
{
ok &= setRawUInt32(data, tlvsize, &offset, (*it2).friend_id) ;
ok &= setRawUFloat32(data, tlvsize, &offset, (*it2).weight) ;
ok &= setRawTimeT(data, tlvsize, &offset, (*it2).time_stamp) ;
}
if (offset != tlvsize)
{
ok = false;
std::cerr << "rsfileitemserialiser::serialisedata() size error! " << std::endl;
}
return ok;
}
bool RsGRouterRoutingInfoItem::serialise(void *data,uint32_t& size) const
{
uint32_t tlvsize,offset=0;
bool ok = true;
if(!serialise_header(data,size,tlvsize,offset))
return false ;
ok &= setRawUInt32(data, tlvsize, &offset, status_flags) ;
ok &= setRawSSLId(data, tlvsize, &offset, origin) ;
ok &= setRawTimeT(data, tlvsize, &offset, received_time) ;
ok &= setRawUInt32(data, tlvsize, &offset, tried_friends.size()) ;
for(std::list<FriendTrialRecord>::const_iterator it(tried_friends.begin());it!=tried_friends.end();++it)
{
ok &= setRawSSLId(data, tlvsize, &offset, (*it).friend_id) ;
ok &= setRawTimeT(data, tlvsize, &offset, (*it).time_stamp) ;
}
ok &= setRawUInt32(data, tlvsize, &offset, data_item->routing_id) ;
ok &= setRawSha1(data, tlvsize, &offset, data_item->destination_key) ;
ok &= setRawUInt32(data, tlvsize, &offset, data_item->data_size) ;
memcpy(&((uint8_t*)data)[offset],data_item->data_bytes,data_item->data_size) ;
offset += data_item->data_size ;
if (offset != tlvsize)
{
ok = false;
std::cerr << "rsfileitemserialiser::serialisedata() size error! " << std::endl;
}
return ok;
}
// -----------------------------------------------------------------------------------//
// ------------------------------------- IO --------------------------------------- //
// -----------------------------------------------------------------------------------//
@ -277,3 +505,35 @@ std::ostream& RsGRouterGenericDataItem::print(std::ostream& o, uint16_t)
return o ;
}
std::ostream& RsGRouterRoutingInfoItem::print(std::ostream& o, uint16_t)
{
o << "RsGRouterRoutingInfoItem:" << std::endl ;
o << " direct origin: \""<< PeerId() << "\"" << std::endl ;
o << " origin: "<< origin.toStdString() << std::endl ;
o << " recv time: "<< received_time << std::endl ;
o << " flags: "<< std::hex << status_flags << std::dec << std::endl ;
o << " Key: "<< data_item->destination_key.toStdString() << std::endl ;
o << " Data size: "<< data_item->data_size << std::endl ;
o << " Tried friends: "<< tried_friends.size() << std::endl;
return o ;
}
std::ostream& RsGRouterMatrixCluesItem::print(std::ostream& o, uint16_t)
{
o << "RsGRouterMatrixCluesItem:" << std::endl ;
o << " destination k: " << destination_key.toStdString() << std::endl;
o << " routing clues: " << clues.size() << std::endl;
for(std::list<RoutingMatrixHitEntry>::const_iterator it(clues.begin());it!=clues.end();++it)
o << " " << (*it).friend_id << " " << (*it).time_stamp << " " << (*it).weight << std::endl;
return o ;
}
std::ostream& RsGRouterMatrixFriendListItem::print(std::ostream& o, uint16_t)
{
o << "RsGRouterMatrixCluesItem:" << std::endl ;
o << " friends: " << reverse_friend_indices.size() << std::endl;
return o ;
}

View File

@ -27,6 +27,7 @@
#include "serialiser/rsserial.h"
#include "rsgrouter.h"
#include "p3grouter.h"
// To be put in serialiser/rsserviceids.h
static const uint8_t RS_SERVICE_TYPE_GROUTER = 0x0016 ;
@ -35,6 +36,10 @@ const uint8_t RS_PKT_SUBTYPE_GROUTER_PUBLISH_KEY = 0x01 ; // used to publish a
const uint8_t RS_PKT_SUBTYPE_GROUTER_DATA = 0x02 ; // used to send data to a destination
const uint8_t RS_PKT_SUBTYPE_GROUTER_ACK = 0x03 ; // acknowledgement of data received
const uint8_t RS_PKT_SUBTYPE_GROUTER_MATRIX_CLUES = 0x80 ; // item to save matrix clues
const uint8_t RS_PKT_SUBTYPE_GROUTER_ROUTING_INFO = 0x81 ; // item to save routing info
const uint8_t RS_PKT_SUBTYPE_GROUTER_FRIENDS_LIST = 0x82 ; // item to save friend lists
const uint8_t QOS_PRIORITY_RS_GROUTER_PUBLISH_KEY = 3 ; // slow items. No need to congest the network with this.
const uint8_t QOS_PRIORITY_RS_GROUTER_ACK = 3 ;
const uint8_t QOS_PRIORITY_RS_GROUTER_DATA = 3 ;
@ -88,16 +93,29 @@ class RsGRouterPublishKeyItem: public RsGRouterItem
std::string description_string ;
};
class RsGRouterGenericDataItem: public RsGRouterItem
class RsGRouterNonCopyableObject
{
public:
RsGRouterNonCopyableObject() {}
private:
RsGRouterNonCopyableObject(const RsGRouterNonCopyableObject&) {}
RsGRouterNonCopyableObject operator=(const RsGRouterNonCopyableObject&) { return *this ;}
};
class RsGRouterGenericDataItem: public RsGRouterItem, public RsGRouterNonCopyableObject
{
public:
RsGRouterGenericDataItem() : RsGRouterItem(RS_PKT_SUBTYPE_GROUTER_DATA) { setPriorityLevel(QOS_PRIORITY_RS_GROUTER_DATA) ; }
virtual ~RsGRouterGenericDataItem() { free(data_bytes); data_bytes=NULL;}
virtual ~RsGRouterGenericDataItem() { clear() ; }
virtual bool serialise(void *data,uint32_t& size) const ;
virtual uint32_t serial_size() const ;
virtual void clear() {}
virtual void clear()
{
free(data_bytes);
data_bytes=NULL;
}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) ;
RsGRouterGenericDataItem *duplicate() const ;
@ -128,6 +146,64 @@ class RsGRouterACKItem: public RsGRouterItem
uint32_t state ; // packet was delivered, not delivered, bounced, etc
};
// Items for saving the routing matrix information.
class RsGRouterMatrixCluesItem: public RsGRouterItem
{
public:
RsGRouterMatrixCluesItem() : RsGRouterItem(RS_PKT_SUBTYPE_GROUTER_MATRIX_CLUES)
{ setPriorityLevel(0) ; } // this item is never sent through the network
virtual bool serialise(void *data,uint32_t& size) const ;
virtual uint32_t serial_size() const ;
virtual void clear() {}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) ;
// packet data
//
GRouterKeyId destination_key ;
std::list<RoutingMatrixHitEntry> clues ;
};
class RsGRouterMatrixFriendListItem: public RsGRouterItem
{
public:
RsGRouterMatrixFriendListItem() : RsGRouterItem(RS_PKT_SUBTYPE_GROUTER_FRIENDS_LIST)
{ setPriorityLevel(0) ; } // this item is never sent through the network
virtual bool serialise(void *data,uint32_t& size) const ;
virtual uint32_t serial_size() const ;
virtual void clear() {}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) ;
// packet data
//
std::vector<SSLIdType> reverse_friend_indices ;
};
class RsGRouterRoutingInfoItem: public RsGRouterItem, public GRouterRoutingInfo, public RsGRouterNonCopyableObject
{
public:
RsGRouterRoutingInfoItem() : RsGRouterItem(RS_PKT_SUBTYPE_GROUTER_ROUTING_INFO)
{ setPriorityLevel(0) ; } // this item is never sent through the network
virtual ~RsGRouterRoutingInfoItem() { clear() ; }
virtual bool serialise(void *data,uint32_t& size) const ;
virtual uint32_t serial_size() const ;
virtual void clear()
{
if(data_item != NULL)
delete data_item ;
data_item = NULL ;
tried_friends.clear() ;
}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) ;
};
/***********************************************************************************/
/* Serialisation */
/***********************************************************************************/
@ -151,6 +227,9 @@ class RsGRouterSerialiser: public RsSerialType
RsGRouterItem *deserialise_RsGRouterPublishKeyItem(void *data,uint32_t size) const ;
RsGRouterItem *deserialise_RsGRouterGenericDataItem(void *data,uint32_t size) const ;
RsGRouterItem *deserialise_RsGRouterACKItem(void *data,uint32_t size) const ;
RsGRouterItem *deserialise_RsGRouterMatrixCluesItem(void *data,uint32_t size) const ;
RsGRouterItem *deserialise_RsGRouterMatrixFriendListItem(void *data,uint32_t size) const ;
RsGRouterItem *deserialise_RsGRouterRoutingInfoItem(void *data,uint32_t size) const ;
};

View File

@ -25,6 +25,7 @@
#include "groutertypes.h"
#include "groutermatrix.h"
#include "grouteritems.h"
GRouterMatrix::GRouterMatrix()
{
@ -177,8 +178,13 @@ bool GRouterMatrix::computeRoutingProbabilities(const GRouterKeyId& key_id, cons
bool GRouterMatrix::updateRoutingProbabilities()
{
std::cerr << "Updating routing probabilities..." << std::endl;
if(!_proba_need_updating)
{
std::cerr << " not needed." << std::endl;
return false ;
}
time_t now = time(NULL) ;
@ -196,11 +202,61 @@ bool GRouterMatrix::updateRoutingProbabilities()
v[(*it2).friend_id] += (*it2).weight / (time_difference_in_days*time_difference_in_days) ;
}
}
std::cerr << " done." << std::endl;
_proba_need_updating = false ;
return true ;
}
bool GRouterMatrix::saveList(std::list<RsItem*>& items)
{
std::cerr << " GRoutingMatrix::saveList()" << std::endl;
RsGRouterMatrixFriendListItem *item = new RsGRouterMatrixFriendListItem ;
item->reverse_friend_indices = _reverse_friend_indices ;
items.push_back(item) ;
for(std::map<GRouterKeyId,std::list<RoutingMatrixHitEntry> >::const_iterator it(_routing_clues.begin());it!=_routing_clues.end();++it)
{
RsGRouterMatrixCluesItem *item = new RsGRouterMatrixCluesItem ;
item->destination_key = it->first ;
item->clues = it->second ;
items.push_back(item) ;
}
return true ;
}
bool GRouterMatrix::loadList(std::list<RsItem*>& items)
{
RsGRouterMatrixFriendListItem *itm1 = NULL ;
RsGRouterMatrixCluesItem *itm2 = NULL ;
std::cerr << " GRoutingMatrix::loadList()" << std::endl;
for(std::list<RsItem*>::const_iterator it(items.begin());it!=items.end();++it)
{
if(NULL != (itm2 = dynamic_cast<RsGRouterMatrixCluesItem*>(*it)))
{
std::cerr << " initing routing clues." << std::endl;
_routing_clues[itm2->destination_key] = itm2->clues ;
_proba_need_updating = true ; // notifies to re-compute all the info.
}
if(NULL != (itm1 = dynamic_cast<RsGRouterMatrixFriendListItem*>(*it)))
{
_reverse_friend_indices = itm1->reverse_friend_indices ;
_friend_indices.clear() ;
for(uint32_t i=0;i<_reverse_friend_indices.size();++i)
_friend_indices[_reverse_friend_indices[i]] = i ;
_proba_need_updating = true ; // notifies to re-compute all the info.
}
}
return true ;
}

View File

@ -31,6 +31,8 @@
#include "groutertypes.h"
#include "rsgrouter.h"
class RsItem ;
// The routing matrix records the event clues received from each friend
//
struct RoutingMatrixHitEntry
@ -64,6 +66,9 @@ class GRouterMatrix
//
void debugDump() const ;
bool saveList(std::list<RsItem*>& items) ;
bool loadList(std::list<RsItem*>& items) ;
private:
// returns the friend id, possibly creating a new id.
//
@ -75,8 +80,8 @@ class GRouterMatrix
// List of events received and computed routing probabilities
//
std::map<GRouterKeyId, std::list<RoutingMatrixHitEntry> > _routing_clues ;
std::map<GRouterKeyId, std::vector<float> > _time_combined_hits ; // hit matrix after time-convolution filter
std::map<GRouterKeyId, std::list<RoutingMatrixHitEntry> > _routing_clues ; // received routing clues. Should be saved.
std::map<GRouterKeyId, std::vector<float> > _time_combined_hits ; // hit matrix after time-convolution filter
// This is used to avoid re-computing probas when new events have been received.
//

View File

@ -27,6 +27,10 @@
#include <stdint.h>
#include <time.h>
#include <list>
#include "pgp/rscertificate.h"
class RsGRouterGenericDataItem ;
typedef uint32_t GRouterServiceId ;
typedef uint32_t GRouterKeyPropagationId ;
@ -35,14 +39,43 @@ typedef uint32_t GRouterMsgPropagationId ;
static const uint32_t RS_GROUTER_MATRIX_MAX_HIT_ENTRIES = 5;
static const uint32_t RS_GROUTER_MATRIX_MIN_TIME_BETWEEN_HITS = 60; // can be set to up to half the publish time interval. Prevents flooding routes.
static const time_t RS_GROUTER_DEBUG_OUTPUT_PERIOD = 20 ; // Output everything
static const time_t RS_GROUTER_AUTOWASH_PERIOD = 60 ; // Autowash every minute. Not a costly operation.
static const time_t RS_GROUTER_DEBUG_OUTPUT_PERIOD = 20 ; // Output everything
static const time_t RS_GROUTER_AUTOWASH_PERIOD = 60 ; // Autowash every minute. Not a costly operation.
//static const time_t RS_GROUTER_PUBLISH_CAMPAIGN_PERIOD = 10*60 ; // Check for key advertising every 10 minutes
//static const time_t RS_GROUTER_PUBLISH_KEY_TIME_INTERVAL = 24*60*60 ; // Advertise each key once a day at most.
static const time_t RS_GROUTER_PUBLISH_CAMPAIGN_PERIOD = 1 *60 ; // Check for key advertising every 10 minutes
static const time_t RS_GROUTER_PUBLISH_KEY_TIME_INTERVAL = 2 *60 ; // Advertise each key once a day at most.
static const time_t RS_GROUTER_PUBLISH_CAMPAIGN_PERIOD = 1 *60 ; // Check for key advertising every 10 minutes
static const time_t RS_GROUTER_PUBLISH_KEY_TIME_INTERVAL = 2 *60 ; // Advertise each key once a day at most.
static const time_t RS_GROUTER_ROUTING_WAITING_TIME = 3600 ; // time between two trial of sending a given message
static const time_t RS_GROUTER_KEY_DIFFUSION_MAX_KEEP = 7200 ; // time to keep key diffusion items in cache, to avoid multiple diffusion.
static const uint32_t RS_GROUTER_ROUTING_STATE_UNKN = 0x0000 ; // unknown. Unused.
static const uint32_t RS_GROUTER_ROUTING_STATE_PEND = 0x0001 ; // item is pending. Should be sent asap.
static const uint32_t RS_GROUTER_ROUTING_STATE_SENT = 0x0002 ; // item is sent. Waiting for answer
static const uint32_t RS_GROUTER_ROUTING_STATE_ARVD = 0x0003 ; // item is at destination. The cache only holds it to avoid duplication.
class GRouterPublishedKeyInfo
{
public:
GRouterServiceId service_id ;
std::string description_string ;
time_t last_published_time ;
time_t validity_time ;
};
struct FriendTrialRecord
{
SSLIdType friend_id ; // id of the friend
time_t time_stamp ; // time of the last tried
};
class GRouterRoutingInfo
{
public:
RsGRouterGenericDataItem *data_item ;
uint32_t status_flags ; // pending, waiting, etc.
std::list<FriendTrialRecord> tried_friends ; // list of friends to which the item was sent ordered with time.
SSLIdType origin ; // which friend sent us that item
time_t received_time ; // time at which the item was received
};

View File

@ -32,8 +32,6 @@
#include "groutertypes.h"
#include "grouterclientservice.h"
static const uint32_t RS_GROUTER_ROUTING_WAITING_TIME = 3600 ; // time between two trial of sending a message
p3GRouter::p3GRouter(p3LinkMgr *lm)
: p3Service(RS_SERVICE_TYPE_GROUTER), p3Config(CONFIG_TYPE_GROUTER), mLinkMgr(lm), grMtx("GRouter")
{
@ -75,6 +73,7 @@ int p3GRouter::tick()
if(now > last_publish_campaign_time + RS_GROUTER_PUBLISH_CAMPAIGN_PERIOD)
{
last_publish_campaign_time = now ;
publishKeys() ;
_routing_matrix.updateRoutingProbabilities() ;
}
@ -103,13 +102,34 @@ void p3GRouter::autoWash()
{
RsStackMutex mtx(grMtx) ;
std::cerr << "p3GRouter::autoWash() Unimplemented !!" << std::endl;
std::cerr << "p3GRouter::autoWash(): cleaning old entried." << std::endl;
// cleanup cache
time_t now = time(NULL) ;
for(std::map<GRouterKeyPropagationId,time_t>::iterator it(_key_diffusion_time_stamps.begin());it!=_key_diffusion_time_stamps.end();)
if(it->second + RS_GROUTER_KEY_DIFFUSION_MAX_KEEP < now)
{
std::cerr << " Removing key diffusion time stamp " << it->second << " for diffusion id " << std::hex << it->first << std::dec << std::endl;
std::map<GRouterKeyPropagationId,time_t>::iterator tmp(it) ;
++tmp ;
_key_diffusion_time_stamps.erase(it) ;
it = tmp ;
}
else
++it ;
// look into pending items.
std::cerr << " Pending key diffusion items: " << _key_diffusion_items.size() << std::endl;
std::cerr << " Pending messages to route : " << _pending_messages.size() << std::endl;
}
void p3GRouter::routePendingObjects()
{
RsStackMutex mtx(grMtx) ;
// Go through list of published keys
// broadcast a publishKeyItem for each of them.
//
@ -157,7 +177,7 @@ void p3GRouter::routePendingObjects()
lst.push_back(SSLIdType(*it)) ;
for(std::map<GRouterMsgPropagationId, GRouterRoutingInfo>::iterator it(_pending_messages.begin());it!=_pending_messages.end();)
if((it->second.status_flags & RS_GROUTER_ROUTING_STATE_PEND) || it->second.status_flags == RS_GROUTER_ROUTING_STATE_SENT && it->second.tried_friends.front().time_stamp+RS_GROUTER_ROUTING_WAITING_TIME < now)
if((it->second.status_flags & RS_GROUTER_ROUTING_STATE_PEND) || (it->second.status_flags == RS_GROUTER_ROUTING_STATE_SENT && it->second.tried_friends.front().time_stamp+RS_GROUTER_ROUTING_WAITING_TIME < now))
{
std::cerr << " Msg id: " << std::hex << it->first << std::dec << std::endl;
std::cerr << " Origin: " << it->second.origin.toStdString() << std::endl;
@ -265,12 +285,12 @@ void p3GRouter::routePendingObjects()
void p3GRouter::publishKeys()
{
RsStackMutex mtx(grMtx) ;
// Go through list of published keys
// broadcast a publishKeyItem for each of them.
time_t now = time(NULL) ;
std::list<std::string> connected_peers ;
mLinkMgr->getOnlineList(connected_peers) ;
for(std::map<GRouterKeyId, GRouterPublishedKeyInfo>::iterator it(_owned_key_ids.begin());it!=_owned_key_ids.end();++it)
{
@ -291,25 +311,41 @@ void p3GRouter::publishKeys()
item.service_id = info.service_id ;
item.randomized_distance = drand48() ;
item.description_string = info.description_string ;
item.PeerId("") ; // no peer id => key is forwarded to all friends.
// get list of connected friends, and broadcast to all of them
//
for(std::list<std::string>::const_iterator it(connected_peers.begin());it!=connected_peers.end();++it)
{
std::cerr << " sending to " << (*it) << std::endl;
locked_forwardKey(item) ;
RsGRouterPublishKeyItem *itm = new RsGRouterPublishKeyItem(item) ;
itm->PeerId(*it) ;
// we should randomise the depth
sendItem(itm) ;
}
info.last_published_time = now ;
}
}
}
void p3GRouter::locked_forwardKey(const RsGRouterPublishKeyItem& item)
{
std::list<std::string> connected_peers ;
mLinkMgr->getOnlineList(connected_peers) ;
std::cerr << " Forwarding key item to all available friends..." << std::endl;
// get list of connected friends, and broadcast to all of them
//
for(std::list<std::string>::const_iterator it(connected_peers.begin());it!=connected_peers.end();++it)
if(item.PeerId() != *it)
{
std::cerr << " sending to " << (*it) << std::endl;
RsGRouterPublishKeyItem *itm = new RsGRouterPublishKeyItem(item) ;
itm->PeerId(*it) ;
// we should randomise the depth
sendItem(itm) ;
}
else
std::cerr << " Not forwarding to source id " << item.PeerId() << std::endl;
}
bool p3GRouter::registerKey(const GRouterKeyId& key,const GRouterServiceId& client_id,const std::string& description)
{
RsStackMutex mtx(grMtx) ;
@ -355,6 +391,8 @@ void p3GRouter::handleIncoming()
void p3GRouter::handleRecvPublishKeyItem(RsGRouterPublishKeyItem *item)
{
RsStackMutex mtx(grMtx) ;
std::cerr << "Received key publish item for key :" << std::endl ;
std::cerr << " diffusion = " << std::hex << item->diffusion_id << std::dec << std::endl ;
std::cerr << " key id = " << item->published_key.toStdString() << std::endl ;
@ -369,25 +407,27 @@ void p3GRouter::handleRecvPublishKeyItem(RsGRouterPublishKeyItem *item)
// forward the key to other peers according to key forwarding cache
std::map<GRouterKeyPropagationId,time_t>::iterator it = _key_diffusion_time_stamps.find(item->diffusion_id) ;
bool found = false ;
if(it != _key_diffusion_time_stamps.end()) // found. We don't propagate further
found = true ;
bool found = (it != _key_diffusion_time_stamps.end()); // found. We don't propagate further
_key_diffusion_time_stamps[item->diffusion_id] = time(NULL) ; // always stamp
if(found)
{
std::cerr << " Key diffusion item already in cache. Not forwardign further." << std::endl;
return ;
}
// Propagate the item to all other online friends. We don't do this right now, but push items in a queue.
// Doing this we can control the amount of key propagation and avoid flooding.
_key_diffusion_items.push(item) ;
locked_forwardKey(*item) ;
}
void p3GRouter::handleRecvACKItem(RsGRouterACKItem *item)
{
std::cerr << "Received ACK item, mid=" << (void*)item->mid << std::endl;
RsStackMutex mtx(grMtx) ;
std::cerr << "Received ACK item, mid=" << std::hex << item->mid << std::dec << std::endl;
// find the item in the pendign list,
// - if not found, drop.
@ -438,6 +478,7 @@ void p3GRouter::handleRecvACKItem(RsGRouterACKItem *item)
void p3GRouter::handleRecvDataItem(RsGRouterGenericDataItem *item)
{
RsStackMutex mtx(grMtx) ;
std::cerr << "Received data item for key " << item->destination_key.toStdString() << std::endl;
// Do we have this item in the cache already?
@ -497,8 +538,16 @@ void p3GRouter::handleRecvDataItem(RsGRouterGenericDataItem *item)
std::cerr << " Item is not for us. Leaving in pending msgs to be routed later." << std::endl;
}
bool p3GRouter::registerClientService(const GRouterServiceId& id,GRouterClientService *service)
{
RsStackMutex mtx(grMtx) ;
_registered_services[id] = service ;
return true ;
}
void p3GRouter::sendData(const GRouterKeyId& destination, RsGRouterGenericDataItem *item)
{
RsStackMutex mtx(grMtx) ;
// push the item into pending messages.
//
GRouterRoutingInfo info ;
@ -511,7 +560,7 @@ void p3GRouter::sendData(const GRouterKeyId& destination, RsGRouterGenericDataIt
// Make sure we have a unique id (at least locally).
//
GRouterMsgPropagationId propagation_id ;
do propagation_id = RSRandom::random_u32() ; while(_pending_messages.find(propagation_id) != _pending_messages.end()) ;
do { propagation_id = RSRandom::random_u32(); } while(_pending_messages.find(propagation_id) != _pending_messages.end()) ;
std::cerr << "p3GRouter::sendGRouterData(): pushing the followign item in the msg pending list:" << std::endl;
std::cerr << " data_item.size = " << info.data_item->data_size << std::endl;
@ -536,24 +585,71 @@ void p3GRouter::sendACK(const SSLIdType& peer, GRouterMsgPropagationId mid, uint
bool p3GRouter::loadList(std::list<RsItem*>& items)
{
std::cerr << "(WW) " << __PRETTY_FUNCTION__ << ": not implemented." << std::endl;
return false ;
}
bool p3GRouter::saveList(bool&,std::list<RsItem*>& items)
{
std::cerr << "(WW) " << __PRETTY_FUNCTION__ << ": not implemented." << std::endl;
RsStackMutex mtx(grMtx) ;
std::cerr << "p3GRouter::loadList() : " << std::endl;
_routing_matrix.loadList(items) ;
// remove all existing objects.
//
std::cerr << " removing all existing items (" << _pending_messages.size() << " items to delete)." << std::endl;
for(std::map<GRouterMsgPropagationId,GRouterRoutingInfo>::iterator it(_pending_messages.begin());it!=_pending_messages.end();++it)
delete it->second.data_item ;
_pending_messages.clear() ;
for(std::list<RsItem*>::const_iterator it(items.begin());it!=items.end();++it)
{
RsGRouterRoutingInfoItem *itm1 = NULL ;
if(NULL != (itm1 = dynamic_cast<RsGRouterRoutingInfoItem*>(*it)))
{
_pending_messages[itm1->data_item->routing_id] = *itm1 ;
_pending_messages[itm1->data_item->routing_id].data_item = itm1->data_item ; // avoids duplication.
itm1->data_item = NULL ; // prevents deletion.
}
delete *it ;
}
return true ;
}
bool p3GRouter::saveList(bool& cleanup,std::list<RsItem*>& items)
{
// We save
// - the routing clues
// - the pending items
return false ;
cleanup = true ; // the client should delete the items.
std::cerr << "p3GRouter::saveList()..." << std::endl;
std::cerr << " saving routing clues." << std::endl;
_routing_matrix.saveList(items) ;
std::cerr << " saving pending items." << std::endl;
for(std::map<GRouterMsgPropagationId,GRouterRoutingInfo>::const_iterator it(_pending_messages.begin());it!=_pending_messages.end();++it)
{
RsGRouterRoutingInfoItem *item = new RsGRouterRoutingInfoItem ;
*(GRouterRoutingInfo*)item = it->second ; // copy all members
item->data_item = it->second.data_item->duplicate() ; // deep copy, because we call delete on the object, and the item might be removed before we handle it in the client.
items.push_back(item) ;
}
return true ;
}
// Dump everything
//
void p3GRouter::debugDump()
{
RsStackMutex mtx(grMtx) ;
time_t now = time(NULL) ;
std::cerr << "Full dump of Global Router state: " << std::endl;

View File

@ -43,32 +43,8 @@
static const uint32_t CONFIG_TYPE_GROUTER = 0x0016 ;
class p3LinkMgr ;
class GRouterPublishedKeyInfo
{
public:
GRouterServiceId service_id ;
std::string description_string ;
time_t last_published_time ;
time_t validity_time ;
};
struct FriendTrialRecord
{
SSLIdType friend_id ; // id of the friend
time_t time_stamp ; // time of the last tried
};
class GRouterRoutingInfo
{
public:
RsGRouterGenericDataItem *data_item ;
uint32_t status_flags ; // pending, waiting, etc.
std::list<FriendTrialRecord> tried_friends ; // list of friends to which the item was sent ordered with time.
SSLIdType origin ; // which friend sent us that item
time_t received_time ; // time at which the item was received
};
class RsGRouterPublishKeyItem ;
class RsGRouterACKItem ;
class p3GRouter: public RsGRouter, public p3Service, public p3Config
{
@ -149,6 +125,7 @@ class p3GRouter: public RsGRouter, public p3Service, public p3Config
void handleIncoming() ;
void publishKeys() ;
void debugDump() ;
void locked_forwardKey(const RsGRouterPublishKeyItem&) ;
//===================================================//
// p3Config methods //
@ -157,7 +134,7 @@ class p3GRouter: public RsGRouter, public p3Service, public p3Config
// Load/save the routing info, the pending items in transit, and the config variables.
//
virtual bool loadList(std::list<RsItem*>& items) ;
virtual bool saveList(bool&,std::list<RsItem*>& items) ;
virtual bool saveList(bool& cleanup,std::list<RsItem*>& items) ;
virtual RsSerialiser *setupSerialiser() ;

View File

@ -67,6 +67,8 @@ class Sha1CheckSum
explicit Sha1CheckSum(const uint8_t *twenty_bytes_digest) ; // inits form a 20-bytes digest.
explicit Sha1CheckSum(const std::string& fourty_bytes_string) ; // inits form a 40 bytes hexadecimal string.
static Sha1CheckSum random() ;
std::string toStdString() const ;
bool operator==(const Sha1CheckSum& s) const ;

View File

@ -2238,6 +2238,9 @@ int RsServer::StartupRetroShare()
ftserver->connectToTurtleRouter(tr) ;
chatSrv->connectToTurtleRouter(tr) ;
#ifdef GROUTER
msgSrv->connectToGlobalRouter(gr) ;
#endif
msgSrv->connectToTurtleRouter(tr) ;
pqih -> addService(ad);

View File

@ -322,7 +322,7 @@ class p3ChatService: public p3Service, public p3Config, public pqiMonitor, publi
// ===========================================================//
public:
void connectToTurtleRouter(p3turtle *) ;
virtual void connectToTurtleRouter(p3turtle *) ;
// Creates the invite if the public key of the distant peer is available.
// Om success, stores the invite in the map above, so that we can respond to tunnel requests.

View File

@ -34,7 +34,9 @@
#include "services/p3msgservice.h"
#include "pgp/pgpkeyutil.h"
#include "pqi/p3cfgmgr.h"
#include "pqi/pqinotify.h"
#include "serialiser/rsconfigitems.h"
#include "util/rsdebug.h"
#include "util/rsdir.h"
@ -320,9 +322,12 @@ int p3MsgService::checkOutgoingMessages()
{
// Do we have a tunnel already?
//
#ifdef GROUTER
tunnel_is_ok = true ;
#else
const std::string& hash = mit->second->PeerId() ;
std::map<std::string,DistantMessengingContact>::iterator it = _messenging_contacts.find(hash) ;
if(it != _messenging_contacts.end())
{
tunnel_is_ok = (it->second.status == RS_DISTANT_MSG_STATUS_TUNNEL_OK) ;
@ -343,6 +348,7 @@ int p3MsgService::checkOutgoingMessages()
contact.status = RS_DISTANT_MSG_STATUS_TUNNEL_DN ;
contact.pending_messages = true ;
}
#endif
}
if(tunnel_is_ok || mLinkMgr->isOnline(pid) || pid == ownId) /* FEEDBACK Msg to Ourselves */
@ -2057,11 +2063,19 @@ bool p3MsgService::decryptMessage(const std::string& mId)
return true ;
}
#ifdef GROUTER
void p3MsgService::connectToGlobalRouter(p3GRouter *gr)
{
mGRouter = gr ;
gr->registerClientService(RS_SERVICE_TYPE_MSG,this) ;
}
#endif
void p3MsgService::connectToTurtleRouter(p3turtle *pt)
{
mTurtle = pt ;
pt->registerTunnelService(this) ;
}
bool p3MsgService::createDistantOfflineMessengingInvite(time_t time_of_validity,TurtleFileHash& hash)
{
unsigned char hash_bytes[DISTANT_MSG_HASH_SIZE] ;
@ -2080,6 +2094,7 @@ bool p3MsgService::createDistantOfflineMessengingInvite(time_t time_of_validity,
return true ;
}
void p3MsgService::enableDistantMessaging(bool b)
{
// compute the hash
@ -2156,6 +2171,7 @@ bool p3MsgService::getDistantMessageHash(const std::string& pgp_id,std::string&
return AuthGPG::getAuthGPG()->isKeySupported(pgp_id) ;
}
bool p3MsgService::getDistantOfflineMessengingInvites(std::vector<DistantOfflineMessengingInvite>& invites)
{
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
@ -2175,6 +2191,7 @@ bool p3MsgService::getDistantOfflineMessengingInvites(std::vector<DistantOffline
}
return true ;
}
bool p3MsgService::handleTunnelRequest(const std::string& hash,const std::string& /*peer_id*/)
{
@ -2281,6 +2298,7 @@ void p3MsgService::removeVirtualPeer(const TurtleFileHash& hash, const TurtleVir
mTurtle->stopMonitoringTunnels(hash) ;
}
}
#ifdef DEBUG_DISTANT_MSG
static void printBinaryData(void *data,uint32_t size)
{
@ -2351,7 +2369,6 @@ void p3MsgService::sendTurtleData(const std::string& hash,RsMsgItem *msgitem)
mTurtle->sendTurtleData(virtual_peer_id,item) ;
}
void p3MsgService::receiveTurtleData(RsTurtleGenericTunnelItem *gitem,const std::string& hash,
const std::string& virtual_peer_id,RsTurtleGenericTunnelItem::Direction /*direction*/)
{
@ -2407,17 +2424,42 @@ void p3MsgService::receiveTurtleData(RsTurtleGenericTunnelItem *gitem,const std:
delete itm ;
}
}
#ifdef GROUTER
void p3MsgService::sendGRouterData(const std::string& hash,RsMsgItem *msgitem)
{
// The item is serialized and turned into a generic turtle item.
uint32_t msg_serialized_rssize = _serialiser->size(msgitem) ;
unsigned char *msg_serialized_data = new unsigned char[msg_serialized_rssize] ;
if(!_serialiser->serialise(msgitem,msg_serialized_data,&msg_serialized_rssize))
{
std::cerr << "(EE) p3MsgService::sendTurtleData(): Serialization error." << std::endl;
delete[] msg_serialized_data ;
return ;
}
RsGRouterGenericDataItem *item = new RsGRouterGenericDataItem ;
item->data_bytes = (uint8_t*)malloc(msg_serialized_rssize) ;
item->data_size = msg_serialized_rssize ;
memcpy(item->data_bytes,msg_serialized_data,msg_serialized_rssize) ;
delete[] msg_serialized_data ;
mGRouter->sendData(GRouterKeyId(hash),item) ;
}
void p3MsgService::receiveGRouterData(RsGRouterGenericDataItem *gitem,const GRouterKeyId& key)
{
std::cerr << "(WW) p3msgservice::receiveGRouterData(): received message. Not handled yet. Needs implementing." << std::endl;
}
#endif
void p3MsgService::sendPrivateMsgItem(RsMsgItem *msgitem)
{
#ifdef DEBUG_DISTANT_MSG
std::cerr << "p3MsgService::sendDistanteMsgItem(): sending distant msg item to peer " << msgitem->PeerId() << std::endl;
// std::cerr << " asking for tunnels" << std::endl;
// std::cerr << " recording msg info" << std::endl;
#endif
// const std::string& hash = msgitem->PeerId() ;
// rsTurtle->monitorTunnels(hash,this) ; // create a tunnel for it, and put the msg on the waiting list.
{
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
@ -2444,7 +2486,11 @@ void p3MsgService::sendPrivateMsgItem(RsMsgItem *msgitem)
#ifdef DEBUG_DISTANT_MSG
std::cerr << " Flushing msg " << msgitem->msgId << " for peer id " << msgitem->PeerId() << std::endl;
#endif
#ifdef GROUTER
sendGRouterData(msgitem->PeerId(),msgitem) ;
#else
sendTurtleData(msgitem->PeerId(),msgitem) ;
#endif
}

View File

@ -42,12 +42,21 @@
#include "services/p3service.h"
#include "serialiser/rsmsgitems.h"
#include "util/rsthreads.h"
#ifdef GROUTER
#include "grouter/p3grouter.h"
#include "grouter/grouterclientservice.h"
#endif
#include "turtle/p3turtle.h"
#include "turtle/turtleclientservice.h"
class p3LinkMgr;
// Temp tweak to test grouter
class p3MsgService: public p3Service, public p3Config, public pqiMonitor, public RsTurtleClientService
#ifdef GROUTER
, public GRouterClientService
#endif
{
public:
p3MsgService(p3LinkMgr *lm);
@ -102,7 +111,10 @@ int checkOutgoingMessages();
/*** overloaded from p3turtle ***/
void connectToTurtleRouter(p3turtle *) ;
#ifdef GROUTER
virtual void connectToGlobalRouter(p3GRouter *) ;
#endif
virtual void connectToTurtleRouter(p3turtle *) ;
struct DistantMessengingInvite
{
@ -115,7 +127,6 @@ int checkOutgoingMessages();
uint32_t status ;
bool pending_messages ;
};
bool createDistantOfflineMessengingInvite(time_t time_of_validity,TurtleFileHash& hash) ;
bool getDistantOfflineMessengingInvites(std::vector<DistantOfflineMessengingInvite>& invites) ;
@ -136,6 +147,9 @@ int checkOutgoingMessages();
// Overloaded from RsTurtleClientService
#ifdef GROUTER
virtual void receiveGRouterData(RsGRouterGenericDataItem *item, const GRouterKeyId& key) ;
#endif
virtual bool handleTunnelRequest(const std::string& hash,const std::string& peer_id) ;
virtual void receiveTurtleData(RsTurtleGenericTunnelItem *item,const std::string& hash,const std::string& virtual_peer_id,RsTurtleGenericTunnelItem::Direction direction) ;
void addVirtualPeer(const TurtleFileHash&, const TurtleVirtualPeerId&,RsTurtleGenericTunnelItem::Direction dir) ;
@ -147,8 +161,14 @@ int checkOutgoingMessages();
void manageDistantPeers() ;
void sendTurtleData(const std::string& hash,RsMsgItem *) ;
#ifdef GROUTER
void sendGRouterData(const std::string& hash,RsMsgItem *) ;
#endif
void handleIncomingItem(RsMsgItem *) ;
#ifdef GROUTER
p3GRouter *mGRouter ;
#endif
p3turtle *mTurtle ;
uint32_t getNewUniqueMsgId();

View File

@ -68,6 +68,55 @@ RsSerialType* init_item(RsGRouterPublishKeyItem& cmi)
return new RsGRouterSerialiser();
}
RsSerialType* init_item(RsGRouterRoutingInfoItem& cmi)
{
cmi.origin = SSLIdType::random() ;
cmi.received_time = RSRandom::random_u64() ;
cmi.status_flags = RSRandom::random_u32() ;
cmi.data_item = new RsGRouterGenericDataItem ;
uint32_t n = 10+(RSRandom::random_u32()%30) ;
for(uint32_t i=0;i<n;++i)
{
FriendTrialRecord ftr ;
ftr.friend_id = SSLIdType::random() ;
ftr.time_stamp = RSRandom::random_u64() ;
cmi.tried_friends.push_back(ftr) ;
}
return init_item(*cmi.data_item) ;
}
RsSerialType* init_item(RsGRouterMatrixFriendListItem& cmi)
{
uint32_t n = 10+(RSRandom::random_u32()%30) ;
cmi.reverse_friend_indices.clear() ;
for(uint32_t i=0;i<n;++i)
cmi.reverse_friend_indices.push_back(SSLIdType::random()) ;
return new RsGRouterSerialiser();
}
RsSerialType* init_item(RsGRouterMatrixCluesItem& cmi)
{
cmi.destination_key = Sha1CheckSum::random() ;
uint32_t n = 10+(RSRandom::random_u32()%30) ;
for(uint32_t i=0;i<n;++i)
{
RoutingMatrixHitEntry rmhe ;
rmhe.friend_id = RSRandom::random_u32() ;
rmhe.weight = RSRandom::random_f32() ;
rmhe.time_stamp = RSRandom::random_u64() ;
cmi.clues.push_back(rmhe) ;
}
return new RsGRouterSerialiser();
}
bool operator ==(const RsGRouterGenericDataItem& cmiLeft,const RsGRouterGenericDataItem& cmiRight)
{
if(cmiLeft.routing_id != cmiRight.routing_id) return false;
@ -94,12 +143,66 @@ bool operator ==(const RsGRouterACKItem& cmiLeft,const RsGRouterACKItem& cmiRigh
return true;
}
bool operator ==(const RsGRouterMatrixCluesItem& cmiLeft,const RsGRouterMatrixCluesItem& cmiRight)
{
if(!(cmiLeft.destination_key == cmiRight.destination_key)) return false;
if(cmiLeft.clues.size() != cmiRight.clues.size()) return false;
std::list<RoutingMatrixHitEntry>::const_iterator itl = cmiLeft.clues.begin() ;
std::list<RoutingMatrixHitEntry>::const_iterator itr = cmiRight.clues.begin() ;
while(itl != cmiLeft.clues.end())
{
if( (*itl).friend_id != (*itr).friend_id) return false ;
if( (*itl).time_stamp != (*itr).time_stamp) return false ;
++itl ;
++itr ;
}
return true;
}
bool operator ==(const RsGRouterMatrixFriendListItem& cmiLeft,const RsGRouterMatrixFriendListItem& cmiRight)
{
if(cmiLeft.reverse_friend_indices.size() != cmiRight.reverse_friend_indices.size()) return false;
for(uint32_t i=0;i<cmiLeft.reverse_friend_indices.size();++i)
if(cmiLeft.reverse_friend_indices[i] != cmiRight.reverse_friend_indices[i])
return false ;
return true;
}
bool operator ==(const RsGRouterRoutingInfoItem& cmiLeft,const RsGRouterRoutingInfoItem& cmiRight)
{
if(cmiLeft.status_flags != cmiRight.status_flags) return false ;
if(cmiLeft.origin != cmiRight.origin) return false ;
if(cmiLeft.received_time != cmiRight.received_time) return false ;
if(cmiLeft.tried_friends.size() != cmiRight.tried_friends.size()) return false ;
std::list<FriendTrialRecord>::const_iterator itl(cmiLeft.tried_friends.begin()) ;
std::list<FriendTrialRecord>::const_iterator itr(cmiRight.tried_friends.begin()) ;
while(itl != cmiLeft.tried_friends.end())
{
if( (*itl).friend_id != (*itr).friend_id) return false ;
if( (*itl).time_stamp != (*itr).time_stamp) return false ;
++itl ;
++itr ;
}
if(!(*cmiLeft.data_item == *cmiRight.data_item)) return false ;
return true;
}
int main()
{
test_RsItem<RsGRouterGenericDataItem >(); REPORT("Serialise/Deserialise RsGRouterGenericDataItem");
test_RsItem<RsGRouterACKItem >(); REPORT("Serialise/Deserialise RsGRouterACKItem");
test_RsItem<RsGRouterPublishKeyItem >(); REPORT("Serialise/Deserialise RsGRouterPublishKeyItem");
test_RsItem<RsGRouterRoutingInfoItem >(); REPORT("Serialise/Deserialise RsGRouterRoutingInfoItem");
test_RsItem<RsGRouterMatrixFriendListItem >(); REPORT("Serialise/Deserialise RsGRouterMatrixFriendListItem");
test_RsItem<RsGRouterMatrixCluesItem >(); REPORT("Serialise/Deserialise RsGRouterMatrixCluesItem");
std::cerr << std::endl;

View File

@ -142,9 +142,16 @@ template<class T> int test_RsItem()
CHECK(outfi != NULL);
if(!(*outfi == rsfi))
{
std::cerr << "Items differ: "<< std::endl;
outfi->print(std::cerr,0) ;
rsfi.print(std::cerr,0) ;
}
if (outfi)
CHECK(*outfi == rsfi) ;
sersize2 = MAX_BUFSIZE;
bool done2 = srl.serialise(outfi, (void *) &(buffer[16*8]), &sersize2);

View File

@ -37,6 +37,7 @@
#include <turtle/rsturtleitem.h>
class RsItem ;
class p3turtle ;
class RsTurtleClientService
{
@ -81,6 +82,12 @@ class RsTurtleClientService
//
virtual void addVirtualPeer(const TurtleFileHash& hash,const TurtleVirtualPeerId& virtual_peer_id,RsTurtleGenericTunnelItem::Direction dir) = 0 ;
virtual void removeVirtualPeer(const TurtleFileHash& hash,const TurtleVirtualPeerId& virtual_peer_id) = 0 ;
// This function is mandatory. It should do two things:
// 1 - keep a pointer to the turtle router, so as to be able to send data (e.g. copy pt into a local variable)
// 2 - call pt->registerTunnelService(this), so that the TR knows that service and can send back information to it.
//
virtual void connectToTurtleRouter(p3turtle *pt) = 0 ;
};

View File

@ -32,6 +32,7 @@
#include "util/rsdir.h"
#include "util/rsstring.h"
#include "util/rsrandom.h"
#include "pqi/pqinotify.h"
#include "retroshare/rstypes.h"
#include "rsthreads.h"
@ -799,6 +800,15 @@ bool Sha1CheckSum::operator<(const Sha1CheckSum& s) const
return false ;
}
Sha1CheckSum Sha1CheckSum::random()
{
Sha1CheckSum s ;
for(int i=0;i<5;++i)
s.fourbytes[i] = RSRandom::random_u32() ;
return s;
}
std::string Sha1CheckSum::toStdString() const
{
static const char outl[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' } ;