fixed some reputation sending bugs

This commit is contained in:
csoler 2015-10-08 23:54:18 -04:00
parent 8c6c9344fe
commit bb7a8f2732
4 changed files with 51 additions and 61 deletions

View File

@ -109,7 +109,7 @@ std::ostream& RsGxsReputationUpdateItem::print(std::ostream &out, uint16_t inden
uint16_t int_Indent = indent + 2; uint16_t int_Indent = indent + 2;
out << "from: " << PeerId() << std::endl; out << "from: " << PeerId() << std::endl;
out << "last update: " << time(NULL) - mLatestUpdate << " secs ago." << std::endl; //out << "last update: " << time(NULL) - mLatestUpdate << " secs ago." << std::endl;
for(std::map<RsGxsId,uint32_t>::const_iterator it(mOpinions.begin());it!=mOpinions.end();++it) for(std::map<RsGxsId,uint32_t>::const_iterator it(mOpinions.begin());it!=mOpinions.end();++it)
out << " " << it->first << ": " << it->second << std::endl; out << " " << it->first << ": " << it->second << std::endl;
@ -159,7 +159,7 @@ uint32_t RsGxsReputationUpdateItem::serial_size() const
{ {
uint32_t s = 8; /* header */ uint32_t s = 8; /* header */
s += 4 ; // mLatestUpdate //s += 4 ; // mLatestUpdate
s += 4 ; // mOpinions.size(); s += 4 ; // mOpinions.size();
s += (RsGxsId::serial_size() + 4) * mOpinions.size() ; s += (RsGxsId::serial_size() + 4) * mOpinions.size() ;
@ -239,7 +239,7 @@ bool RsGxsReputationUpdateItem::serialise(void *data, uint32_t& pktsize) const
bool ok = true; bool ok = true;
ok &= setRawUInt32(data, tlvsize, &offset, mLatestUpdate); //ok &= setRawUInt32(data, tlvsize, &offset, mLatestUpdate);
ok &= setRawUInt32(data, tlvsize, &offset, mOpinions.size()); ok &= setRawUInt32(data, tlvsize, &offset, mOpinions.size());
for(std::map<RsGxsId,uint32_t>::const_iterator it(mOpinions.begin());ok && it!=mOpinions.end();++it) for(std::map<RsGxsId,uint32_t>::const_iterator it(mOpinions.begin());ok && it!=mOpinions.end();++it)
@ -349,7 +349,7 @@ RsGxsReputationUpdateItem *RsGxsReputationSerialiser::deserialiseReputationUpdat
RsGxsReputationUpdateItem *item = new RsGxsReputationUpdateItem() ; RsGxsReputationUpdateItem *item = new RsGxsReputationUpdateItem() ;
/* add mandatory parts first */ /* add mandatory parts first */
ok &= getRawUInt32(data, tlvsize, &offset, &item->mLatestUpdate); //ok &= getRawUInt32(data, tlvsize, &offset, &item->mLatestUpdate);
uint32_t S ; uint32_t S ;
ok &= getRawUInt32(data, tlvsize, &offset, &S) ; ok &= getRawUInt32(data, tlvsize, &offset, &S) ;

View File

@ -105,7 +105,7 @@ public:
virtual bool serialise(void *data,uint32_t& size) const ; virtual bool serialise(void *data,uint32_t& size) const ;
virtual uint32_t serial_size() const ; virtual uint32_t serial_size() const ;
uint32_t mLatestUpdate; //uint32_t mLatestUpdate;
std::map<RsGxsId, uint32_t> mOpinions; // GxsId -> Opinion. std::map<RsGxsId, uint32_t> mOpinions; // GxsId -> Opinion.
}; };

View File

@ -39,16 +39,6 @@
****/ ****/
#define DEBUG_REPUTATION 1 #define DEBUG_REPUTATION 1
/* DEFINE INTERFACE POINTER! */
//RsGxsReputation *rsGxsReputation = NULL;
static const int kMaximumPeerAge = 180; // half a year.
static const int kMaximumSetSize = 100;
static const int ACTIVE_FRIENDS_UPDATE_PERIOD = 600 ; // 10 minutes
static const int ACTIVE_FRIENDS_ONLINE_DELAY = 86400*7 ; // 1 week.
/************ IMPLEMENTATION NOTES ********************************* /************ IMPLEMENTATION NOTES *********************************
* *
* p3GxsReputation shares opinions / reputations with peers. * p3GxsReputation shares opinions / reputations with peers.
@ -135,9 +125,14 @@ static const int ACTIVE_FRIENDS_ONLINE_DELAY = 86400*7 ; // 1 week.
* [X] Implement a system to allow not storing info when we don't have it * [X] Implement a system to allow not storing info when we don't have it
*/ */
const uint32_t LOWER_LIMIT = 0; static const uint32_t LOWER_LIMIT = 0; // used to filter valid Opinion values from serialized data
const uint32_t UPPER_LIMIT = 2; static const uint32_t UPPER_LIMIT = 2; // used to filter valid Opinion values from serialized data
const float REPUTATION_ASSESSMENT_THRESHOLD_X1 = 0.5f ; static const float REPUTATION_ASSESSMENT_THRESHOLD_X1 = 0.5f ; // reputation under which the peer gets killed
static const int kMaximumPeerAge = 180; // half a year.
static const int kMaximumSetSize = 100; // max set of updates to send at once.
static const int ACTIVE_FRIENDS_UPDATE_PERIOD = 60;//600 ;// 10 minutes
static const int ACTIVE_FRIENDS_ONLINE_DELAY = 86400*7 ; // 1 week.
p3GxsReputation::p3GxsReputation(p3LinkMgr *lm) p3GxsReputation::p3GxsReputation(p3LinkMgr *lm)
:p3Service(), p3Config(), :p3Service(), p3Config(),
@ -149,6 +144,7 @@ p3GxsReputation::p3GxsReputation(p3LinkMgr *lm)
mStoreTime = 0; mStoreTime = 0;
mReputationsUpdated = false; mReputationsUpdated = false;
mLastActiveFriendsUpdate = 0 ; mLastActiveFriendsUpdate = 0 ;
mAverageActiveFriends = 0 ;
} }
const std::string GXS_REPUTATION_APP_NAME = "gxsreputation"; const std::string GXS_REPUTATION_APP_NAME = "gxsreputation";
@ -167,8 +163,6 @@ RsServiceInfo p3GxsReputation::getServiceInfo()
GXS_REPUTATION_MIN_MINOR_VERSION); GXS_REPUTATION_MIN_MINOR_VERSION);
} }
int p3GxsReputation::tick() int p3GxsReputation::tick()
{ {
processIncoming(); processIncoming();
@ -308,12 +302,13 @@ bool p3GxsReputation::SendReputations(RsGxsReputationRequestItem *request)
int count = 0; int count = 0;
int totalcount = 0; int totalcount = 0;
RsGxsReputationUpdateItem *pkt = new RsGxsReputationUpdateItem(); RsGxsReputationUpdateItem *pkt = new RsGxsReputationUpdateItem();
pkt->PeerId(peerId); pkt->PeerId(peerId);
for(;tit != mUpdated.end(); ++tit) for(;tit != mUpdated.end(); ++tit)
{ {
/* find */ /* find */
std::map<RsGxsId, Reputation>::iterator rit; std::map<RsGxsId, Reputation>::iterator rit = mReputations.find(tit->second);
rit = mReputations.find(tit->second);
if (rit == mReputations.end()) if (rit == mReputations.end())
{ {
std::cerr << "p3GxsReputation::SendReputations() ERROR Missing Reputation"; std::cerr << "p3GxsReputation::SendReputations() ERROR Missing Reputation";
@ -332,13 +327,14 @@ bool p3GxsReputation::SendReputations(RsGxsReputationRequestItem *request)
RsGxsId gxsId = rit->first; RsGxsId gxsId = rit->first;
pkt->mOpinions[gxsId] = rit->second.mOwnOpinion; pkt->mOpinions[gxsId] = rit->second.mOwnOpinion;
pkt->mLatestUpdate = rit->second.mOwnOpinionTs;
if (pkt->mLatestUpdate == (uint32_t) now) //pkt->mLatestUpdate = rit->second.mOwnOpinionTs;
{ //if (pkt->mLatestUpdate == (uint32_t) now)
// if we could possibly get another Update at this point (same second). //{
// then set Update back one second to ensure there are none missed. // // if we could possibly get another Update at this point (same second).
pkt->mLatestUpdate--; // // then set Update back one second to ensure there are none missed.
} // pkt->mLatestUpdate--;
//}
count++; count++;
totalcount++; totalcount++;
@ -349,6 +345,7 @@ bool p3GxsReputation::SendReputations(RsGxsReputationRequestItem *request)
std::cerr << std::endl; std::cerr << std::endl;
sendItem(pkt); sendItem(pkt);
pkt = new RsGxsReputationUpdateItem(); pkt = new RsGxsReputationUpdateItem();
pkt->PeerId(peerId); pkt->PeerId(peerId);
count = 0; count = 0;
@ -477,16 +474,13 @@ bool p3GxsReputation::RecvReputations(RsGxsReputationUpdateItem *item)
locked_updateOpinion(peerid,it->first,safe_convert_uint32t_to_opinion(it->second)); locked_updateOpinion(peerid,it->first,safe_convert_uint32t_to_opinion(it->second));
} }
updateLatestUpdate(peerid);
updateLatestUpdate(peerid, item->mLatestUpdate);
// now update all reputations of IDs for which some opinions have changed.
return true; return true;
} }
bool p3GxsReputation::updateLatestUpdate(RsPeerId peerid, time_t ts) bool p3GxsReputation::updateLatestUpdate(RsPeerId peerid)
{ {
RsStackMutex stack(mReputationMtx); /****** LOCKED MUTEX *******/ RsStackMutex stack(mReputationMtx); /****** LOCKED MUTEX *******/
@ -497,11 +491,12 @@ bool p3GxsReputation::updateLatestUpdate(RsPeerId peerid, time_t ts)
mConfig[peerid] = ReputationConfig(peerid); mConfig[peerid] = ReputationConfig(peerid);
it = mConfig.find(peerid) ; it = mConfig.find(peerid) ;
} }
it->second.mLatestUpdate = ts; it->second.mLatestUpdate = time(NULL);
mReputationsUpdated = true; mReputationsUpdated = true;
// Switched to periodic save due to scale of data. // Switched to periodic save due to scale of data.
//IndicateConfigChanged();
IndicateConfigChanged();
return true; return true;
} }
@ -758,7 +753,7 @@ bool p3GxsReputation::loadReputationSet(RsGxsReputationSetItem *item, const std:
* Send Requests. * Send Requests.
****/ ****/
const int kReputationRequestPeriod = 3600; const int kReputationRequestPeriod = 60; // 3600; // 1 hour
const int kReputationStoreWait = 180; // 3 minutes. const int kReputationStoreWait = 180; // 3 minutes.
int p3GxsReputation::sendPackets() int p3GxsReputation::sendPackets()
@ -771,7 +766,7 @@ int p3GxsReputation::sendPackets()
storeTime = mStoreTime; storeTime = mStoreTime;
} }
if (now - requestTime > kReputationRequestPeriod) if (now > requestTime + kReputationRequestPeriod)
{ {
sendReputationRequests(); sendReputationRequests();
@ -815,46 +810,41 @@ void p3GxsReputation::sendReputationRequests()
mLinkMgr->getOnlineList(idList); mLinkMgr->getOnlineList(idList);
#ifdef DEBUG_REPUTATION
std::cerr << "p3GxsReputation::sendReputationRequests()";
std::cerr << std::endl;
#endif
/* prepare packets */ /* prepare packets */
std::list<RsPeerId>::iterator it; std::list<RsPeerId>::iterator it;
for(it = idList.begin(); it != idList.end(); ++it) for(it = idList.begin(); it != idList.end(); ++it)
{
#ifdef DEBUG_REPUTATION
std::cerr << "p3GxsReputation::sendReputationRequest() To: " << *it;
std::cerr << std::endl;
#endif
sendReputationRequest(*it); sendReputationRequest(*it);
}
} }
int p3GxsReputation::sendReputationRequest(RsPeerId peerid) int p3GxsReputation::sendReputationRequest(RsPeerId peerid)
{ {
std::cerr << "p3GxsReputation::sendReputationRequest(" << peerid << ")"; #ifdef DEBUG_REPUTATION
std::cerr << std::endl; std::cerr << " p3GxsReputation::sendReputationRequest(" << peerid << ") " ;
#endif
time_t now = time(NULL) ;
/* */ /* */
RsGxsReputationRequestItem *requestItem = RsGxsReputationRequestItem *requestItem = new RsGxsReputationRequestItem();
new RsGxsReputationRequestItem();
requestItem->PeerId(peerid); requestItem->PeerId(peerid);
{ {
RsStackMutex stack(mReputationMtx); /****** LOCKED MUTEX *******/ RsStackMutex stack(mReputationMtx); /****** LOCKED MUTEX *******/
/* find the last timestamp we have */ /* find the last timestamp we have */
std::map<RsPeerId, ReputationConfig>::iterator it; std::map<RsPeerId, ReputationConfig>::iterator it = mConfig.find(peerid);
it = mConfig.find(peerid);
if (it != mConfig.end()) if (it != mConfig.end())
{ {
#ifdef DEBUG_REPUTATION
std::cerr << " lastUpdate = " << now - it->second.mLatestUpdate << " secs ago. Requesting only more recent." << std::endl;
#endif
requestItem->mLastUpdate = it->second.mLatestUpdate; requestItem->mLastUpdate = it->second.mLatestUpdate;
} }
else else
{ {
#ifdef DEBUG_REPUTATION
std::cerr << " lastUpdate = never. Requesting all!" << std::endl;
#endif
// get whole list. // get whole list.
requestItem->mLastUpdate = 0; requestItem->mLastUpdate = 0;
} }

View File

@ -111,7 +111,7 @@ class p3GxsReputation: public p3Service, public p3Config, public RsReputations /
bool SendReputations(RsGxsReputationRequestItem *request); bool SendReputations(RsGxsReputationRequestItem *request);
bool RecvReputations(RsGxsReputationUpdateItem *item); bool RecvReputations(RsGxsReputationUpdateItem *item);
bool updateLatestUpdate(RsPeerId peerid, time_t ts); bool updateLatestUpdate(RsPeerId peerid);
void updateActiveFriends() ; void updateActiveFriends() ;
// internal update of data. Takes care of cleaning empty boxes. // internal update of data. Takes care of cleaning empty boxes.