2008-02-03 01:29:02 -05:00
|
|
|
/*
|
|
|
|
* libretroshare/src/services p3ranking.cc
|
|
|
|
*
|
|
|
|
* 3P/PQI network interface for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2007-2008 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".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
#include "serialiser/rsrankitems.h"
|
2008-02-03 01:29:02 -05:00
|
|
|
#include "services/p3ranking.h"
|
2008-03-03 09:41:15 -05:00
|
|
|
#include <iomanip>
|
2008-02-05 08:45:04 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
#include "pqi/pqibin.h"
|
|
|
|
#include "pqi/p3authmgr.h"
|
|
|
|
|
|
|
|
const uint32_t RANK_MAX_FWD_OFFSET = (60 * 60 * 24 * 2); /* 2 Days */
|
|
|
|
|
2008-03-31 10:06:59 -04:00
|
|
|
//const uint32_t FRIEND_RANK_REPUBLISH_PERIOD = 60; /* every minute for testing */
|
|
|
|
const uint32_t FRIEND_RANK_REPUBLISH_PERIOD = 1800; /* every 30 minutes */
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
std::string generateRandomLinkId();
|
|
|
|
|
|
|
|
/*****
|
|
|
|
* TODO
|
|
|
|
* (1) Streaming.
|
|
|
|
* (2) Ranking.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-02-03 11:43:03 -05:00
|
|
|
#define RANK_DEBUG 1
|
2008-02-03 01:29:02 -05:00
|
|
|
|
2008-03-31 10:06:59 -04:00
|
|
|
p3Ranking::p3Ranking(p3ConnectMgr *connMgr,
|
|
|
|
uint16_t type, CacheStrapper *cs, CacheTransfer *cft,
|
2008-02-03 01:29:02 -05:00
|
|
|
std::string sourcedir, std::string storedir,
|
|
|
|
uint32_t storePeriod)
|
2008-02-09 07:47:45 -05:00
|
|
|
:CacheSource(type, true, cs, sourcedir),
|
|
|
|
CacheStore(type, true, cs, cft, storedir),
|
2008-04-15 12:35:46 -04:00
|
|
|
p3Config(CONFIG_TYPE_RANK_LINK),
|
2008-03-31 10:06:59 -04:00
|
|
|
mConnMgr(connMgr),
|
|
|
|
mRepublish(false), mRepublishFriends(false), mRepublishFriendTS(0),
|
2008-03-17 09:51:04 -04:00
|
|
|
mStorePeriod(storePeriod), mUpdated(true)
|
2008-02-03 01:29:02 -05:00
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
|
|
|
|
{ RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
2008-03-31 10:06:59 -04:00
|
|
|
mOwnId = mConnMgr->getOwnId();
|
2008-02-03 11:43:03 -05:00
|
|
|
mViewPeriod = 60 * 60 * 24 * 30; /* one Month */
|
|
|
|
mSortType = RS_RANK_ALG;
|
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
} RsStackMutex stack(mRankMtx);
|
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
// createDummyData();
|
2008-02-03 01:29:02 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool p3Ranking::loadLocalCache(const CacheData &data)
|
|
|
|
{
|
2008-02-09 07:47:45 -05:00
|
|
|
std::string filename = data.path + '/' + data.name;
|
|
|
|
std::string hash = data.hash;
|
|
|
|
//uint64_t size = data.size;
|
|
|
|
std::string source = data.pid;
|
|
|
|
|
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::loadLocalCache()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\tSource: " << source;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\tFilename: " << filename;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\tHash: " << hash;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\tSize: " << data.size;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
loadRankFile(filename, source);
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
mRepublish = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.size > 0) /* don't refresh zero sized caches */
|
|
|
|
{
|
|
|
|
refreshCache(data);
|
|
|
|
}
|
2008-02-03 01:29:02 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int p3Ranking::loadCache(const CacheData &data)
|
|
|
|
{
|
2008-02-05 08:45:04 -05:00
|
|
|
std::string filename = data.path + '/' + data.name;
|
|
|
|
std::string hash = data.hash;
|
|
|
|
//uint64_t size = data.size;
|
|
|
|
std::string source = data.pid;
|
|
|
|
|
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::loadCache()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\tSource: " << source;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\tFilename: " << filename;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\tHash: " << hash;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\tSize: " << data.size;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
loadRankFile(filename, source);
|
2008-02-03 01:29:02 -05:00
|
|
|
|
2008-02-09 07:47:45 -05:00
|
|
|
|
|
|
|
CacheStore::lockData(); /***** LOCK ****/
|
|
|
|
locked_storeCacheEntry(data);
|
|
|
|
CacheStore::unlockData(); /***** UNLOCK ****/
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
void p3Ranking::loadRankFile(std::string filename, std::string src)
|
|
|
|
{
|
|
|
|
/* create the serialiser to load info */
|
2008-02-05 08:45:04 -05:00
|
|
|
RsSerialiser *rsSerialiser = new RsSerialiser();
|
|
|
|
rsSerialiser->addSerialType(new RsRankSerialiser());
|
|
|
|
|
|
|
|
uint32_t bioflags = BIN_FLAGS_HASH_DATA | BIN_FLAGS_READABLE;
|
|
|
|
BinInterface *bio = new BinFileInterface(filename.c_str(), bioflags);
|
|
|
|
pqistreamer *stream = new pqistreamer(rsSerialiser, src, bio, 0);
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
time_t now = time(NULL);
|
2008-02-07 11:18:34 -05:00
|
|
|
time_t min, max;
|
|
|
|
|
|
|
|
{ RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
|
|
|
min = now - mStorePeriod;
|
|
|
|
max = now + RANK_MAX_FWD_OFFSET;
|
|
|
|
|
|
|
|
} /********** STACK LOCKED MTX ******/
|
2008-02-03 01:29:02 -05:00
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::loadRankFile()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\tSource: " << src;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\tFilename: " << filename;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
RsItem *item;
|
2008-02-05 08:45:04 -05:00
|
|
|
RsRankLinkMsg *newMsg;
|
|
|
|
|
|
|
|
stream->tick(); /* Tick to read! */
|
|
|
|
while(NULL != (item = stream->GetItem()))
|
2008-02-03 01:29:02 -05:00
|
|
|
{
|
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::loadRankFile() Got Item:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
item->print(std::cerr, 10);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (NULL == (newMsg = dynamic_cast<RsRankLinkMsg *>(item)))
|
2008-02-03 01:29:02 -05:00
|
|
|
{
|
2008-02-05 08:45:04 -05:00
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::loadRankFile() Item not LinkMsg (deleting):";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
/* check timestamp */
|
|
|
|
else if (((time_t) newMsg->timestamp < min) ||
|
|
|
|
((time_t) newMsg->timestamp > max))
|
|
|
|
{
|
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::loadRankFile() Outside TimeRange (deleting):";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-02-03 01:29:02 -05:00
|
|
|
/* if outside range -> remove */
|
|
|
|
delete newMsg;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-02-05 08:45:04 -05:00
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::loadRankFile() Loading Item";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-03-31 10:06:59 -04:00
|
|
|
/* correct the source (if is a message from a friend) */
|
|
|
|
newMsg->PeerId(newMsg->pid);
|
2008-02-03 01:29:02 -05:00
|
|
|
addRankMsg(newMsg);
|
|
|
|
}
|
2008-02-05 08:45:04 -05:00
|
|
|
|
|
|
|
stream->tick(); /* Tick to read! */
|
2008-02-03 01:29:02 -05:00
|
|
|
}
|
2008-02-05 08:45:04 -05:00
|
|
|
|
|
|
|
delete stream;
|
2008-02-03 01:29:02 -05:00
|
|
|
}
|
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
|
2008-03-31 10:06:59 -04:00
|
|
|
void p3Ranking::publishMsgs(bool own)
|
2008-02-03 01:29:02 -05:00
|
|
|
{
|
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::publishMsgs()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-02-03 01:29:02 -05:00
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
std::string path = CacheSource::getCacheDir();
|
|
|
|
std::ostringstream out;
|
2008-03-31 10:06:59 -04:00
|
|
|
uint16_t subid;
|
|
|
|
|
|
|
|
|
|
|
|
/* setup name / etc based on whether we're
|
|
|
|
* publishing own or friends...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (own)
|
|
|
|
{
|
|
|
|
/* setup to publish own messages */
|
|
|
|
out << "rank-links-" << time(NULL) << ".rsrl";
|
|
|
|
subid = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* setup to publish friend messages */
|
|
|
|
out << "rank-friend-links-" << time(NULL) << ".rsrl";
|
|
|
|
subid = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* determine filename */
|
2008-02-05 08:45:04 -05:00
|
|
|
std::string tmpname = out.str();
|
|
|
|
std::string fname = path + "/" + tmpname;
|
|
|
|
|
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::publishMsgs() Storing to: " << fname;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
RsSerialiser *rsSerialiser = new RsSerialiser();
|
|
|
|
rsSerialiser->addSerialType(new RsRankSerialiser());
|
|
|
|
|
|
|
|
uint32_t bioflags = BIN_FLAGS_HASH_DATA | BIN_FLAGS_WRITEABLE;
|
|
|
|
BinInterface *bio = new BinFileInterface(fname.c_str(), bioflags);
|
|
|
|
pqistreamer *stream = new pqistreamer(rsSerialiser, mOwnId, bio,
|
|
|
|
BIN_FLAGS_NO_DELETE);
|
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
{ RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
/* iterate through list */
|
|
|
|
std::map<std::string, RankGroup>::iterator it;
|
2008-03-31 10:06:59 -04:00
|
|
|
std::map<std::string, RsRankLinkMsg *>::iterator cit;
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
for(it = mData.begin(); it != mData.end(); it++)
|
|
|
|
{
|
2008-03-31 10:06:59 -04:00
|
|
|
if (own)
|
2008-02-03 01:29:02 -05:00
|
|
|
{
|
2008-03-31 10:06:59 -04:00
|
|
|
if (it->second.ownTag)
|
|
|
|
{
|
2008-02-03 01:29:02 -05:00
|
|
|
/* write to serialiser */
|
|
|
|
RsItem *item = it->second.comments[mOwnId];
|
|
|
|
if (item)
|
2008-02-05 08:45:04 -05:00
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::publishMsgs() Storing Item:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
item->print(std::cerr, 10);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-02-03 01:29:02 -05:00
|
|
|
stream->SendItem(item);
|
2008-02-05 08:45:04 -05:00
|
|
|
stream->tick(); /* Tick to write! */
|
|
|
|
|
|
|
|
}
|
2008-03-31 10:06:59 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::publishMsgs() Skipping Foreign item";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
2008-02-05 08:45:04 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-04-15 12:35:46 -04:00
|
|
|
/* if we have pushed it out already - don't bother */
|
|
|
|
if (it->second.ownTag)
|
2008-03-31 10:06:59 -04:00
|
|
|
{
|
2008-02-05 08:45:04 -05:00
|
|
|
#ifdef RANK_DEBUG
|
2008-04-15 12:35:46 -04:00
|
|
|
std::cerr << "p3Ranking::publishMsgs() (Friends) Skipping Own Item";
|
|
|
|
std::cerr << std::endl;
|
2008-02-05 08:45:04 -05:00
|
|
|
#endif
|
2008-04-15 12:35:46 -04:00
|
|
|
continue;
|
|
|
|
}
|
2008-03-31 10:06:59 -04:00
|
|
|
|
2008-04-15 12:35:46 -04:00
|
|
|
/* if we have some comments ... then a friend has recommended it
|
|
|
|
* serialise a sanitized version.
|
|
|
|
*/
|
|
|
|
if (it->second.comments.size() > 0)
|
|
|
|
{
|
|
|
|
RsRankLinkMsg *origmsg = (it->second.comments.begin())->second;
|
|
|
|
RsRankLinkMsg *msg = new RsRankLinkMsg();
|
|
|
|
|
|
|
|
/* copy anon data */
|
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
/****************************** LINK SPECIFIC ****************************/
|
|
|
|
/*************************************************************************/
|
|
|
|
msg->clear();
|
|
|
|
msg->PeerId("");
|
|
|
|
msg->pid = ""; /* Anon */
|
|
|
|
msg->rid = origmsg->rid;
|
|
|
|
msg->link = origmsg->link;
|
|
|
|
msg->title = origmsg->title;
|
|
|
|
msg->timestamp = origmsg->timestamp;
|
|
|
|
msg->score = 0;
|
|
|
|
|
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::publishMsgs() (Friends) Storing (Anon) Item:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
msg->print(std::cerr, 10);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
stream->SendItem(msg);
|
|
|
|
stream->tick(); /* Tick to write! */
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
delete msg;
|
2008-03-31 10:06:59 -04:00
|
|
|
}
|
2008-02-03 01:29:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-15 12:35:46 -04:00
|
|
|
|
|
|
|
/* now we also add our anon messages to the friends list */
|
|
|
|
if (!own)
|
|
|
|
{
|
|
|
|
std::list<RsRankLinkMsg *>::iterator ait;
|
|
|
|
for(ait=mAnon.begin(); ait != mAnon.end(); ait++)
|
|
|
|
{
|
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::publishMsgs() (Friends) Adding Own Anon Item:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
(*ait)->print(std::cerr, 10);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
stream->SendItem(*ait);
|
|
|
|
stream->tick(); /* Tick to write! */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
} /********** STACK LOCKED MTX ******/
|
|
|
|
|
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
stream->tick(); /* Tick for final write! */
|
|
|
|
|
|
|
|
/* flag as new info */
|
2008-02-03 01:29:02 -05:00
|
|
|
CacheData data;
|
2008-02-07 11:18:34 -05:00
|
|
|
|
|
|
|
{ RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
2008-02-03 01:29:02 -05:00
|
|
|
data.pid = mOwnId;
|
2008-02-07 11:18:34 -05:00
|
|
|
} /********** STACK LOCKED MTX ******/
|
|
|
|
|
2008-03-31 10:06:59 -04:00
|
|
|
data.cid = CacheId(CacheSource::getCacheType(), subid);
|
2008-02-03 01:29:02 -05:00
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
data.path = path;
|
|
|
|
data.name = tmpname;
|
|
|
|
|
|
|
|
data.hash = bio->gethash();
|
|
|
|
data.size = bio->bytecount();
|
|
|
|
data.recvd = time(NULL);
|
|
|
|
|
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::publishMsgs() refreshing Cache";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\tCache Path: " << data.path;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\tCache Name: " << data.name;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\tCache Hash: " << data.hash;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\tCache Size: " << data.size;
|
|
|
|
std::cerr << std::endl;
|
2008-02-03 01:29:02 -05:00
|
|
|
#endif
|
2008-02-05 08:45:04 -05:00
|
|
|
if (data.size > 0) /* don't refresh zero sized caches */
|
|
|
|
{
|
|
|
|
refreshCache(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete stream;
|
2008-02-03 01:29:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
void p3Ranking::addRankMsg(RsRankLinkMsg *msg)
|
2008-02-03 01:29:02 -05:00
|
|
|
{
|
|
|
|
/* find msg */
|
|
|
|
std::string id = msg->PeerId();
|
|
|
|
std::string rid = msg->rid;
|
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::addRankMsg() Item:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
msg->print(std::cerr, 10);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
std::map<std::string, RankGroup>::iterator it;
|
|
|
|
it = mData.find(rid);
|
|
|
|
if (it == mData.end())
|
|
|
|
{
|
|
|
|
/* add a new one */
|
|
|
|
RankGroup grp;
|
|
|
|
grp.rid = rid;
|
|
|
|
grp.ownTag = false;
|
|
|
|
|
2008-04-15 12:35:46 -04:00
|
|
|
/*************************************************************************/
|
|
|
|
/****************************** LINK SPECIFIC ****************************/
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
grp.link = msg->link;
|
|
|
|
grp.title = msg->title;
|
|
|
|
|
2008-04-15 12:35:46 -04:00
|
|
|
/*************************************************************************/
|
|
|
|
/****************************** LINK SPECIFIC ****************************/
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
mData[rid] = grp;
|
|
|
|
it = mData.find(rid);
|
2008-04-15 12:35:46 -04:00
|
|
|
|
|
|
|
|
|
|
|
if (id == "")
|
|
|
|
{
|
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::addRankMsg() New Anon Link: mUpdated = true";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
locked_reSortGroup(it->second);
|
|
|
|
mUpdated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**** If it is an anonymous Link (ie Friend of a Friend) Drop out now ***/
|
|
|
|
if (id == "")
|
|
|
|
{
|
|
|
|
return;
|
2008-02-03 01:29:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check for old comment */
|
2008-02-05 08:45:04 -05:00
|
|
|
std::map<std::string, RsRankLinkMsg *>::iterator cit;
|
2008-02-03 01:29:02 -05:00
|
|
|
cit = (it->second).comments.find(id);
|
2008-02-05 08:45:04 -05:00
|
|
|
|
|
|
|
/* Check that it is different! */
|
|
|
|
bool newComment = false;
|
|
|
|
if ((it->second).comments.end() == cit)
|
2008-02-03 01:29:02 -05:00
|
|
|
{
|
2008-02-05 08:45:04 -05:00
|
|
|
newComment = true;
|
2008-02-03 01:29:02 -05:00
|
|
|
}
|
2008-02-05 08:45:04 -05:00
|
|
|
else
|
2008-02-03 01:29:02 -05:00
|
|
|
{
|
2008-02-05 08:45:04 -05:00
|
|
|
RsRankLinkMsg *old = cit->second;
|
|
|
|
if ((msg->timestamp != old->timestamp) ||
|
|
|
|
(msg->comment != old->comment))
|
|
|
|
{
|
|
|
|
newComment = true;
|
|
|
|
}
|
2008-02-03 01:29:02 -05:00
|
|
|
}
|
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
if (newComment)
|
|
|
|
{
|
2008-03-31 10:06:59 -04:00
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::addRankMsg() New Comment";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-02-05 08:45:04 -05:00
|
|
|
/* clean up old */
|
|
|
|
if ((it->second).comments.end() != cit)
|
|
|
|
{
|
|
|
|
delete (cit->second);
|
|
|
|
(it->second).comments.erase(cit);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add in */
|
|
|
|
(it->second).comments[id] = msg;
|
|
|
|
|
|
|
|
/* republish? */
|
|
|
|
if (id == mOwnId)
|
|
|
|
{
|
|
|
|
it->second.ownTag = true;
|
|
|
|
mRepublish = true;
|
2008-03-31 10:06:59 -04:00
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::addRankMsg() Own Comment: mRepublish = true";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mRepublishFriends = true;
|
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::addRankMsg() Other Comment: mRepublishFriends = true";
|
|
|
|
std::cerr << "p3Ranking::addRankMsg() Old Comment ignoring";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-02-05 08:45:04 -05:00
|
|
|
}
|
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
locked_reSortGroup(it->second);
|
2008-03-17 09:51:04 -04:00
|
|
|
|
|
|
|
mUpdated = true;
|
2008-02-05 08:45:04 -05:00
|
|
|
}
|
2008-03-31 10:06:59 -04:00
|
|
|
else
|
|
|
|
{
|
2008-04-02 09:55:45 -04:00
|
|
|
delete msg;
|
2008-03-31 10:06:59 -04:00
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::addRankMsg() Old Comment ignoring";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
2008-02-03 01:29:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***************** Sorting ****************/
|
|
|
|
|
|
|
|
bool p3Ranking::setSortPeriod(uint32_t period)
|
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
bool reSort = false;
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
reSort = (mViewPeriod != period);
|
|
|
|
mViewPeriod = period;
|
|
|
|
}
|
|
|
|
|
2008-02-03 11:43:03 -05:00
|
|
|
|
|
|
|
if (reSort)
|
|
|
|
{
|
|
|
|
sortAllMsgs();
|
|
|
|
}
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool p3Ranking::setSortMethod(uint32_t type)
|
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
bool reSort = false;
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
reSort = (mSortType != type);
|
|
|
|
mSortType = type;
|
|
|
|
}
|
2008-02-03 11:43:03 -05:00
|
|
|
|
|
|
|
if (reSort)
|
|
|
|
{
|
|
|
|
sortAllMsgs();
|
|
|
|
}
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool p3Ranking::clearPeerFilter()
|
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
bool reSort = false;
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
reSort = (mPeerFilter.size() > 0);
|
|
|
|
mPeerFilter.clear();
|
|
|
|
}
|
2008-02-03 11:43:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
if (reSort)
|
|
|
|
{
|
|
|
|
sortAllMsgs();
|
|
|
|
}
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool p3Ranking::setPeerFilter(std::list<std::string> peers)
|
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
mPeerFilter = peers;
|
|
|
|
}
|
2008-02-03 11:43:03 -05:00
|
|
|
|
|
|
|
sortAllMsgs();
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-02-03 11:43:03 -05:00
|
|
|
float p3Ranking::locked_calcRank(RankGroup &grp)
|
2008-02-03 01:29:02 -05:00
|
|
|
{
|
2008-02-03 11:43:03 -05:00
|
|
|
/* Ranking Calculations .....
|
|
|
|
*/
|
2008-02-03 01:29:02 -05:00
|
|
|
|
2008-02-03 11:43:03 -05:00
|
|
|
time_t now = time(NULL);
|
|
|
|
time_t minTime = now-mViewPeriod;
|
2008-02-03 01:29:02 -05:00
|
|
|
bool doFilter = (mPeerFilter.size() > 0);
|
2008-02-03 11:43:03 -05:00
|
|
|
bool doScore = (mSortType & RS_RANK_SCORE);
|
|
|
|
bool doTime = (mSortType & RS_RANK_TIME);
|
|
|
|
|
|
|
|
uint32_t count = 0;
|
|
|
|
float algScore = 0;
|
|
|
|
|
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::string normlink(grp.link.begin(), grp.link.end());
|
|
|
|
std::cerr << "p3Ranking::locked_calcRank() for: " << normlink;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "Period: " << mViewPeriod;
|
|
|
|
std::cerr << " doFilter: " << doFilter;
|
|
|
|
std::cerr << " doScore: " << doScore;
|
|
|
|
std::cerr << " doTime: " << doTime;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-02-03 01:29:02 -05:00
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
std::map<std::string, RsRankLinkMsg *>::iterator it;
|
2008-02-03 01:29:02 -05:00
|
|
|
for(it = grp.comments.begin(); it != grp.comments.end(); it++)
|
|
|
|
{
|
2008-02-03 11:43:03 -05:00
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "Comment by:" << it->first << " age: " << now - it->second->timestamp;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
if (doFilter)
|
|
|
|
{
|
|
|
|
if (mPeerFilter.end() ==
|
|
|
|
std::find(mPeerFilter.begin(), mPeerFilter.end(), it->first))
|
|
|
|
{
|
|
|
|
continue; /* skip it */
|
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "\tFiltered Out";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-02-03 01:29:02 -05:00
|
|
|
|
2008-02-03 11:43:03 -05:00
|
|
|
}
|
|
|
|
}
|
2008-02-03 01:29:02 -05:00
|
|
|
|
2008-02-03 11:43:03 -05:00
|
|
|
/* if Scoring is involved... drop old ones */
|
2008-02-05 08:45:04 -05:00
|
|
|
if ((doScore) && ((time_t) it->second->timestamp < minTime))
|
2008-02-03 11:43:03 -05:00
|
|
|
{
|
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "\tToo Old";
|
|
|
|
std::cerr << std::endl;
|
2008-02-03 01:29:02 -05:00
|
|
|
#endif
|
2008-02-03 11:43:03 -05:00
|
|
|
continue;
|
|
|
|
}
|
2008-02-03 01:29:02 -05:00
|
|
|
|
2008-02-03 11:43:03 -05:00
|
|
|
time_t deltaT;
|
2008-02-05 08:45:04 -05:00
|
|
|
if ((time_t) it->second->timestamp > now)
|
2008-02-03 11:43:03 -05:00
|
|
|
{
|
|
|
|
deltaT = it->second->timestamp - now;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
deltaT = now - it->second->timestamp;
|
|
|
|
}
|
|
|
|
float timeScore = ((float) mViewPeriod - deltaT) / (mViewPeriod + 0.01);
|
|
|
|
|
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "\tTimeScore: " << timeScore;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* algScore is sum of (filtered) timeScores */
|
|
|
|
/* timeScore is average of (all) timeScores */
|
|
|
|
/* popScore is just count of valid scores */
|
|
|
|
|
|
|
|
algScore += timeScore;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::locked_calcRank() algScore: " << algScore;
|
|
|
|
std::cerr << " Count: " << count;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-04-15 12:35:46 -04:00
|
|
|
if ((count <= 0) || (algScore <= 0))
|
2008-02-03 11:43:03 -05:00
|
|
|
{
|
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "Final score: 0";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((doScore) && (doTime))
|
|
|
|
{
|
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "Final (alg) score:" << algScore;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return algScore;
|
|
|
|
}
|
|
|
|
else if (doScore)
|
|
|
|
{
|
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "Final (pop) score:" << count;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
else if (doTime)
|
|
|
|
{
|
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "Final (time) score:" << algScore / count;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return algScore / count;
|
|
|
|
}
|
|
|
|
return 0;
|
2008-02-03 01:29:02 -05:00
|
|
|
}
|
|
|
|
|
2008-02-03 11:43:03 -05:00
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
void p3Ranking::locked_reSortGroup(RankGroup &grp)
|
2008-02-03 01:29:02 -05:00
|
|
|
{
|
|
|
|
std::string rid = grp.rid;
|
|
|
|
|
|
|
|
/* remove from existings rankings */
|
|
|
|
std::multimap<float, std::string>::iterator rit;
|
|
|
|
rit = mRankings.lower_bound(grp.rank);
|
|
|
|
for(; (rit != mRankings.end()) && (rit->first == grp.rank); rit++)
|
|
|
|
{
|
|
|
|
if (rit->second == rid)
|
|
|
|
{
|
|
|
|
mRankings.erase(rit);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add it back in */
|
|
|
|
grp.rank = locked_calcRank(grp);
|
|
|
|
mRankings.insert(
|
|
|
|
std::pair<float, std::string>(grp.rank, rid));
|
|
|
|
}
|
|
|
|
|
|
|
|
void p3Ranking::sortAllMsgs()
|
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
/* iterate through list and re-score each one */
|
|
|
|
std::map<std::string, RankGroup>::iterator it;
|
|
|
|
|
|
|
|
mRankings.clear();
|
|
|
|
|
|
|
|
for(it = mData.begin(); it != mData.end(); it++)
|
|
|
|
{
|
|
|
|
(it->second).rank = locked_calcRank(it->second);
|
2008-04-15 12:35:46 -04:00
|
|
|
if (it->second.rank < 0)
|
2008-02-03 01:29:02 -05:00
|
|
|
{
|
2008-04-15 12:35:46 -04:00
|
|
|
it->second.rank = 0;
|
2008-02-03 01:29:02 -05:00
|
|
|
}
|
2008-04-15 12:35:46 -04:00
|
|
|
|
|
|
|
mRankings.insert(
|
|
|
|
std::pair<float, std::string>
|
|
|
|
(it->second.rank, it->first));
|
2008-02-03 01:29:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******** ACCESS *************/
|
|
|
|
|
|
|
|
/* get Ids */
|
|
|
|
uint32_t p3Ranking::getRankingsCount()
|
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
return mRankings.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
float p3Ranking::getMaxRank()
|
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
if (mRankings.size() == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return mRankings.rbegin()->first;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool p3Ranking::getRankings(uint32_t first, uint32_t count, std::list<std::string> &rids)
|
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
2008-04-15 12:35:46 -04:00
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::getRankings() First: " << first << " Count: " << count;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
uint32_t i = 0;
|
2008-02-03 11:43:03 -05:00
|
|
|
std::multimap<float, std::string>::reverse_iterator rit;
|
2008-04-15 12:35:46 -04:00
|
|
|
for(rit = mRankings.rbegin(); (i < first) && (rit != mRankings.rend()); rit++, i++);
|
2008-02-03 01:29:02 -05:00
|
|
|
|
|
|
|
i = 0;
|
2008-04-15 12:35:46 -04:00
|
|
|
for(; (i < count) && (rit != mRankings.rend()); rit++, i++)
|
2008-02-03 01:29:02 -05:00
|
|
|
{
|
|
|
|
rids.push_back(rit->second);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
bool p3Ranking::getRankDetails(std::string rid, RsRankDetails &details)
|
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
/* get the details. */
|
2008-02-07 11:18:34 -05:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
std::map<std::string, RankGroup>::iterator it;
|
|
|
|
it = mData.find(rid);
|
|
|
|
if (mData.end() == it)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-04-15 12:35:46 -04:00
|
|
|
/*************************************************************************/
|
|
|
|
/****************************** LINK SPECIFIC ****************************/
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
details.rid = it->first;
|
|
|
|
details.link = (it->second).link;
|
|
|
|
details.title = (it->second).title;
|
|
|
|
details.rank = (it->second).rank;
|
|
|
|
details.ownTag = (it->second).ownTag;
|
|
|
|
|
2008-04-15 12:35:46 -04:00
|
|
|
/*************************************************************************/
|
|
|
|
/****************************** LINK SPECIFIC ****************************/
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
std::map<std::string, RsRankLinkMsg *>::iterator cit;
|
2008-02-03 01:29:02 -05:00
|
|
|
for(cit = (it->second).comments.begin();
|
|
|
|
cit != (it->second).comments.end(); cit++)
|
|
|
|
{
|
|
|
|
RsRankComment comm;
|
|
|
|
comm.id = (cit->second)->PeerId();
|
|
|
|
comm.timestamp = (cit->second)->timestamp;
|
|
|
|
comm.comment = (cit->second)->comment;
|
2008-04-15 12:35:46 -04:00
|
|
|
comm.score = (cit->second)->score;
|
2008-02-03 01:29:02 -05:00
|
|
|
|
|
|
|
details.comments.push_back(comm);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void p3Ranking::tick()
|
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
bool repub = false;
|
2008-03-31 10:06:59 -04:00
|
|
|
bool repubFriends = false;
|
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
repub = mRepublish;
|
2008-03-31 10:06:59 -04:00
|
|
|
repubFriends = mRepublishFriends && (time(NULL) > mRepublishFriendTS);
|
2008-02-07 11:18:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (repub)
|
2008-02-03 01:29:02 -05:00
|
|
|
{
|
2008-03-31 10:06:59 -04:00
|
|
|
publishMsgs(true);
|
2008-02-07 11:18:34 -05:00
|
|
|
|
|
|
|
RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
2008-02-03 01:29:02 -05:00
|
|
|
mRepublish = false;
|
|
|
|
}
|
2008-03-31 10:06:59 -04:00
|
|
|
|
|
|
|
|
|
|
|
if (repubFriends)
|
|
|
|
{
|
|
|
|
publishMsgs(false);
|
|
|
|
|
|
|
|
RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
mRepublishFriends = false;
|
|
|
|
mRepublishFriendTS = time(NULL) + FRIEND_RANK_REPUBLISH_PERIOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
}
|
|
|
|
|
2008-03-17 09:51:04 -04:00
|
|
|
bool p3Ranking::updated()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
2008-02-03 01:29:02 -05:00
|
|
|
|
2008-03-17 09:51:04 -04:00
|
|
|
if (mUpdated)
|
|
|
|
{
|
|
|
|
mUpdated = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2008-02-03 01:29:02 -05:00
|
|
|
|
|
|
|
/***** NEW CONTENT *****/
|
2008-04-15 12:35:46 -04:00
|
|
|
/*************************************************************************/
|
|
|
|
/****************************** LINK SPECIFIC ****************************/
|
|
|
|
/*************************************************************************/
|
|
|
|
std::string p3Ranking::newRankMsg(std::wstring link, std::wstring title, std::wstring comment, int32_t score)
|
2008-02-03 01:29:02 -05:00
|
|
|
{
|
|
|
|
/* generate an id */
|
|
|
|
std::string rid = generateRandomLinkId();
|
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
RsRankLinkMsg *msg = new RsRankLinkMsg();
|
2008-02-03 01:29:02 -05:00
|
|
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
msg->PeerId(mOwnId);
|
2008-03-31 10:06:59 -04:00
|
|
|
msg->pid = mOwnId;
|
2008-02-07 11:18:34 -05:00
|
|
|
}
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
msg->rid = rid;
|
|
|
|
msg->title = title;
|
|
|
|
msg->timestamp = now;
|
|
|
|
msg->comment = comment;
|
2008-04-15 12:35:46 -04:00
|
|
|
msg->score = score;
|
2008-02-03 01:29:02 -05:00
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
msg->linktype = RS_LINK_TYPE_WEB;
|
|
|
|
msg->link = link;
|
|
|
|
|
2008-04-15 12:35:46 -04:00
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
addRankMsg(msg);
|
|
|
|
|
|
|
|
return rid;
|
|
|
|
}
|
|
|
|
|
2008-04-15 12:35:46 -04:00
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
/****************************** LINK SPECIFIC ****************************/
|
|
|
|
/*************************************************************************/
|
|
|
|
bool p3Ranking::updateComment(std::string rid, std::wstring comment, int32_t score)
|
2008-02-03 01:29:02 -05:00
|
|
|
{
|
2008-02-05 08:45:04 -05:00
|
|
|
|
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::updateComment() rid:" << rid;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-02-07 11:18:34 -05:00
|
|
|
RsRankLinkMsg *msg = NULL;
|
|
|
|
|
|
|
|
{ RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
2008-02-05 08:45:04 -05:00
|
|
|
|
|
|
|
std::map<std::string, RankGroup>::iterator it;
|
|
|
|
it = mData.find(rid);
|
|
|
|
if (it == mData.end())
|
|
|
|
{
|
|
|
|
/* missing group -> fail */
|
|
|
|
|
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::updateComment() Failed - noData";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
msg = new RsRankLinkMsg();
|
2008-02-05 08:45:04 -05:00
|
|
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
|
|
|
msg->PeerId(mOwnId);
|
2008-03-31 10:06:59 -04:00
|
|
|
msg->pid = mOwnId;
|
2008-02-05 08:45:04 -05:00
|
|
|
msg->rid = rid;
|
|
|
|
msg->timestamp = now;
|
|
|
|
msg->title = (it->second).title;
|
|
|
|
msg->comment = comment;
|
2008-04-15 12:35:46 -04:00
|
|
|
msg->score = score;
|
2008-02-05 08:45:04 -05:00
|
|
|
|
|
|
|
msg->linktype = RS_LINK_TYPE_WEB;
|
|
|
|
msg->link = (it->second).link;
|
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
} /********** STACK UNLOCKED MTX ******/
|
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::updateComment() Item:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
msg->print(std::cerr, 10);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
addRankMsg(msg);
|
2008-02-03 01:29:02 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-04-15 12:35:46 -04:00
|
|
|
/*************************************************************************/
|
|
|
|
/****************************** LINK SPECIFIC ****************************/
|
|
|
|
/*************************************************************************/
|
|
|
|
std::string p3Ranking::anonRankMsg(std::wstring link, std::wstring title)
|
|
|
|
{
|
|
|
|
/* generate an id */
|
|
|
|
std::string rid = generateRandomLinkId();
|
|
|
|
|
|
|
|
RsRankLinkMsg *msg1 = new RsRankLinkMsg();
|
|
|
|
RsRankLinkMsg *msg2 = new RsRankLinkMsg();
|
|
|
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
msg1->PeerId("");
|
|
|
|
msg1->pid = "";
|
|
|
|
|
|
|
|
msg2->PeerId("");
|
|
|
|
msg2->pid = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
msg1->rid = rid;
|
|
|
|
msg1->title = title;
|
|
|
|
msg1->timestamp = now;
|
|
|
|
msg1->comment.clear();
|
|
|
|
msg1->score = 0;
|
|
|
|
|
|
|
|
msg1->linktype = RS_LINK_TYPE_WEB;
|
|
|
|
msg1->link = link;
|
|
|
|
|
|
|
|
msg2->rid = rid;
|
|
|
|
msg2->title = title;
|
|
|
|
msg2->timestamp = now;
|
|
|
|
msg2->comment.clear();
|
|
|
|
msg2->score = 0;
|
|
|
|
|
|
|
|
msg2->linktype = RS_LINK_TYPE_WEB;
|
|
|
|
msg2->link = link;
|
|
|
|
|
|
|
|
addRankMsg(msg1);
|
|
|
|
addAnonToList(msg2);
|
|
|
|
|
|
|
|
return rid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
pqistreamer *createStreamer(std::string file, std::string src, bool reading)
|
2008-02-03 01:29:02 -05:00
|
|
|
{
|
|
|
|
|
|
|
|
RsSerialiser *rsSerialiser = new RsSerialiser();
|
2008-02-05 08:45:04 -05:00
|
|
|
rsSerialiser->addSerialType(new RsRankSerialiser());
|
2008-02-03 01:29:02 -05:00
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
uint32_t bioflags = BIN_FLAGS_HASH_DATA;
|
|
|
|
if (reading)
|
|
|
|
{
|
|
|
|
bioflags |= BIN_FLAGS_READABLE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bioflags |= BIN_FLAGS_WRITEABLE;
|
|
|
|
}
|
2008-02-03 01:29:02 -05:00
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
/* bin flags: READ | WRITE | HASH_DATA */
|
|
|
|
BinInterface *bio = new BinFileInterface(file.c_str(), bioflags);
|
|
|
|
/* streamer flags: NO_DELETE (yes) | NO_CLOSE (no) */
|
|
|
|
pqistreamer *streamer = new pqistreamer(rsSerialiser, src, bio,
|
|
|
|
BIN_FLAGS_NO_DELETE);
|
2008-02-03 01:29:02 -05:00
|
|
|
|
|
|
|
return streamer;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string generateRandomLinkId()
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
out << std::hex;
|
2008-03-03 09:41:15 -05:00
|
|
|
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
|
|
|
#ifndef WINDOWS_SYS
|
2008-02-03 01:29:02 -05:00
|
|
|
/* 4 bytes per random number: 4 x 4 = 16 bytes */
|
|
|
|
for(int i = 0; i < 4; i++)
|
|
|
|
{
|
2008-03-03 09:41:15 -05:00
|
|
|
out << std::setw(8) << std::setfill('0');
|
2008-02-03 01:29:02 -05:00
|
|
|
uint32_t rint = random();
|
|
|
|
out << rint;
|
|
|
|
}
|
2008-03-03 09:41:15 -05:00
|
|
|
#else
|
|
|
|
srand(time(NULL));
|
|
|
|
/* 2 bytes per random number: 8 x 2 = 16 bytes */
|
|
|
|
for(int i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
out << std::setw(4) << std::setfill('0');
|
|
|
|
uint16_t rint = rand(); /* only gives 16 bits */
|
|
|
|
out << rint;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
2008-02-03 01:29:02 -05:00
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-15 12:35:46 -04:00
|
|
|
/*************************************************************************/
|
|
|
|
/****************************** LINK SPECIFIC ****************************/
|
|
|
|
/*************************************************************************/
|
2008-02-03 01:29:02 -05:00
|
|
|
void p3Ranking::createDummyData()
|
|
|
|
{
|
2008-02-05 08:45:04 -05:00
|
|
|
RsRankLinkMsg *msg = new RsRankLinkMsg();
|
2008-02-03 01:29:02 -05:00
|
|
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
|
|
|
msg->PeerId(mOwnId);
|
2008-03-31 10:06:59 -04:00
|
|
|
msg->pid = mOwnId;
|
2008-02-03 01:29:02 -05:00
|
|
|
msg->rid = "0001";
|
|
|
|
msg->title = L"Original Awesome Site!";
|
2008-02-03 11:43:03 -05:00
|
|
|
msg->timestamp = now - 60 * 60 * 24 * 15;
|
2008-02-03 01:29:02 -05:00
|
|
|
msg->link = L"http://www.retroshare.org";
|
|
|
|
msg->comment = L"Retroshares Website";
|
2008-04-15 12:35:46 -04:00
|
|
|
msg->score = 1;
|
2008-02-03 01:29:02 -05:00
|
|
|
|
|
|
|
addRankMsg(msg);
|
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
msg = new RsRankLinkMsg();
|
2008-02-03 01:29:02 -05:00
|
|
|
msg->PeerId(mOwnId);
|
2008-03-31 10:06:59 -04:00
|
|
|
msg->pid = mOwnId;
|
2008-02-03 01:29:02 -05:00
|
|
|
msg->rid = "0002";
|
|
|
|
msg->title = L"Awesome Site!";
|
|
|
|
msg->timestamp = now - 123;
|
|
|
|
msg->link = L"http://www.lunamutt.org";
|
|
|
|
msg->comment = L"Lunamutt's Website";
|
2008-04-15 12:35:46 -04:00
|
|
|
msg->score = 1;
|
2008-02-03 01:29:02 -05:00
|
|
|
|
|
|
|
addRankMsg(msg);
|
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
msg = new RsRankLinkMsg();
|
2008-02-03 01:29:02 -05:00
|
|
|
msg->PeerId("ALTID");
|
2008-03-31 10:06:59 -04:00
|
|
|
msg->pid = "ALTID";
|
2008-02-03 01:29:02 -05:00
|
|
|
msg->rid = "0002";
|
|
|
|
msg->title = L"Awesome Site!";
|
2008-02-03 11:43:03 -05:00
|
|
|
msg->timestamp = now - 60 * 60 * 24 * 29;
|
2008-02-03 01:29:02 -05:00
|
|
|
msg->link = L"http://www.lunamutt.org";
|
|
|
|
msg->comment = L"Lunamutt's Website (TWO) How Long can this comment be!\n";
|
|
|
|
msg->comment += L"What happens to the second line?\n";
|
|
|
|
msg->comment += L"And a 3rd!";
|
2008-04-15 12:35:46 -04:00
|
|
|
msg->score = 1;
|
2008-02-03 01:29:02 -05:00
|
|
|
|
|
|
|
addRankMsg(msg);
|
2008-02-03 11:43:03 -05:00
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
msg = new RsRankLinkMsg();
|
2008-02-03 11:43:03 -05:00
|
|
|
msg->PeerId("ALTID2");
|
2008-03-31 10:06:59 -04:00
|
|
|
msg->pid = "ALTID2";
|
2008-02-03 11:43:03 -05:00
|
|
|
msg->rid = "0002";
|
|
|
|
msg->title = L"Awesome Site!";
|
|
|
|
msg->timestamp = now - 60 * 60 * 7;
|
|
|
|
msg->link = L"http://www.lunamutt.org";
|
|
|
|
msg->comment += L"A Short Comment";
|
2008-04-15 12:35:46 -04:00
|
|
|
msg->score = 1;
|
2008-02-03 11:43:03 -05:00
|
|
|
|
|
|
|
addRankMsg(msg);
|
|
|
|
|
|
|
|
|
|
|
|
/***** Third one ****/
|
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
msg = new RsRankLinkMsg();
|
2008-02-03 11:43:03 -05:00
|
|
|
msg->PeerId(mOwnId);
|
2008-03-31 10:06:59 -04:00
|
|
|
msg->pid = mOwnId;
|
2008-02-03 11:43:03 -05:00
|
|
|
msg->rid = "0003";
|
|
|
|
msg->title = L"Weird Site!";
|
|
|
|
msg->timestamp = now - 60 * 60;
|
|
|
|
msg->link = L"http://www.lunamutt.com";
|
|
|
|
msg->comment = L"";
|
2008-04-15 12:35:46 -04:00
|
|
|
msg->score = 1;
|
2008-02-03 11:43:03 -05:00
|
|
|
|
|
|
|
addRankMsg(msg);
|
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
msg = new RsRankLinkMsg();
|
2008-02-03 11:43:03 -05:00
|
|
|
msg->PeerId("ALTID");
|
2008-03-31 10:06:59 -04:00
|
|
|
msg->pid = "ALTID";
|
2008-02-03 11:43:03 -05:00
|
|
|
msg->rid = "0003";
|
|
|
|
msg->title = L"Weird Site!";
|
|
|
|
msg->timestamp = now - 60 * 60 * 24 * 2;
|
|
|
|
msg->link = L"http://www.lunamutt.com";
|
|
|
|
msg->comment = L"";
|
2008-04-15 12:35:46 -04:00
|
|
|
msg->score = 1;
|
2008-02-03 11:43:03 -05:00
|
|
|
|
|
|
|
addRankMsg(msg);
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
}
|
|
|
|
|
2008-04-15 12:35:46 -04:00
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
/****************************** CONFIGURATION HANDLING *********************/
|
|
|
|
/***************************************************************************/
|
|
|
|
|
|
|
|
/**** Store Anon Links: OVERLOADED FROM p3Config ****/
|
|
|
|
|
|
|
|
RsSerialiser *p3Ranking::setupSerialiser()
|
|
|
|
{
|
|
|
|
RsSerialiser *rss = new RsSerialiser();
|
|
|
|
|
|
|
|
/* add in the types we need! */
|
|
|
|
rss->addSerialType(new RsRankSerialiser());
|
|
|
|
return rss;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool p3Ranking::addAnonToList(RsRankLinkMsg *msg)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
mAnon.push_back(msg);
|
|
|
|
mRepublishFriends = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
IndicateConfigChanged(); /**** INDICATE CONFIG CHANGED! *****/
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::list<RsItem *> p3Ranking::saveList(bool &cleanup)
|
|
|
|
{
|
|
|
|
std::list<RsItem *> saveData;
|
|
|
|
|
|
|
|
mRankMtx.lock(); /*********************** LOCK *******/
|
|
|
|
|
|
|
|
cleanup = false;
|
|
|
|
|
|
|
|
std::list<RsRankLinkMsg *>::iterator it;
|
|
|
|
for(it = mAnon.begin(); it != mAnon.end(); it++)
|
|
|
|
{
|
|
|
|
saveData.push_back(*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* list completed! */
|
|
|
|
return saveData;
|
|
|
|
}
|
|
|
|
|
|
|
|
void p3Ranking::saveDone()
|
|
|
|
{
|
|
|
|
mRankMtx.unlock(); /*********************** UNLOCK *******/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool p3Ranking::loadList(std::list<RsItem *> load)
|
|
|
|
{
|
|
|
|
std::list<RsItem *>::iterator it;
|
|
|
|
RsRankLinkMsg *msg;
|
|
|
|
|
|
|
|
#ifdef SERVER_DEBUG
|
|
|
|
std::cerr << "p3Ranking::loadList() Item Count: " << load.size();
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
time_t min, max;
|
|
|
|
|
|
|
|
{ RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
|
|
|
min = now - mStorePeriod;
|
|
|
|
max = now + RANK_MAX_FWD_OFFSET;
|
|
|
|
|
|
|
|
} /********** STACK LOCKED MTX ******/
|
|
|
|
|
|
|
|
for(it = load.begin(); it != load.end(); it++)
|
|
|
|
{
|
|
|
|
/* switch on type */
|
|
|
|
if (NULL != (msg = dynamic_cast<RsRankLinkMsg *>(*it)))
|
|
|
|
{
|
|
|
|
/* check date -> if old expire */
|
|
|
|
if (((time_t) msg->timestamp < min) ||
|
|
|
|
((time_t) msg->timestamp > max))
|
|
|
|
{
|
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::loadList() Outside TimeRange (deleting Own Anon):";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
/* if outside range -> remove */
|
|
|
|
delete msg;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef RANK_DEBUG
|
|
|
|
std::cerr << "p3Ranking::loadList() Anon TimeRange ok";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
msg->PeerId("");
|
|
|
|
msg->pid = "";
|
|
|
|
|
|
|
|
RsRankLinkMsg *msg2 = new RsRankLinkMsg();
|
|
|
|
msg2->clear();
|
|
|
|
msg2->PeerId(msg->PeerId());
|
|
|
|
msg2->pid = msg->pid;
|
|
|
|
msg2->rid = msg->rid;
|
|
|
|
msg2->title = msg->title;
|
|
|
|
msg2->timestamp = msg->timestamp;
|
|
|
|
msg2->comment.clear();
|
|
|
|
msg2->score = 0;
|
|
|
|
|
|
|
|
msg2->linktype = msg->linktype;
|
|
|
|
msg2->link = msg->link;
|
|
|
|
|
|
|
|
/* make a copy to add into standard map */
|
|
|
|
addRankMsg(msg);
|
|
|
|
|
|
|
|
RsStackMutex stack(mRankMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
mAnon.push_back(msg2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* cleanup */
|
|
|
|
delete (*it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|