From 129c07a553261b94c13046b178c45e49e9010500 Mon Sep 17 00:00:00 2001 From: drbob Date: Tue, 15 Apr 2008 16:35:46 +0000 Subject: [PATCH] Third attempt at link rank system. * Added Anonymous links, stored in configuration file. * Friends recommendations are also shared anonymously with peers. * Own recommendations are shared with friends. * TODO include ranking (+2 <-> -2) in score. * some bugfixes too. Added RandomId to p3service file. Improved forum interface. Added dummy forum system (not transmitted) for testing. Switched on new features as well. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@505 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/pqi/p3cfgmgr.h | 2 + libretroshare/src/rsiface/rsforums.h | 17 +- libretroshare/src/rsiface/rsrank.h | 7 +- libretroshare/src/rsserver/p3face-startup.cc | 5 +- libretroshare/src/rsserver/p3rank.cc | 12 +- libretroshare/src/rsserver/p3rank.h | 5 +- libretroshare/src/serialiser/rsrankitems.cc | 15 +- libretroshare/src/serialiser/rsrankitems.h | 10 +- libretroshare/src/services/p3forums.cc | 94 +++++- libretroshare/src/services/p3forums.h | 8 + libretroshare/src/services/p3ranking.cc | 328 +++++++++++++++++-- libretroshare/src/services/p3ranking.h | 24 +- libretroshare/src/services/p3service.cc | 29 ++ libretroshare/src/services/p3service.h | 3 + 14 files changed, 513 insertions(+), 46 deletions(-) diff --git a/libretroshare/src/pqi/p3cfgmgr.h b/libretroshare/src/pqi/p3cfgmgr.h index dc860d35a..666bcb0fd 100644 --- a/libretroshare/src/pqi/p3cfgmgr.h +++ b/libretroshare/src/pqi/p3cfgmgr.h @@ -64,6 +64,8 @@ const uint32_t CONFIG_TYPE_FSERVER = 0x0003; const uint32_t CONFIG_TYPE_MSGS = 0x0004; const uint32_t CONFIG_TYPE_CACHE = 0x0005; +const uint32_t CONFIG_TYPE_RANK_LINK = 0x0011; + class p3ConfigMgr; class p3AuthMgr; diff --git a/libretroshare/src/rsiface/rsforums.h b/libretroshare/src/rsiface/rsforums.h index 4fa85790c..80c40941c 100644 --- a/libretroshare/src/rsiface/rsforums.h +++ b/libretroshare/src/rsiface/rsforums.h @@ -37,6 +37,9 @@ #define RS_FORUM_PRIVATE 0x0002 /* anyone with key can publish */ #define RS_FORUM_ENCRYPTED 0x0004 /* need admin key */ +#define RS_FORUM_MSG_AUTH 0x0010 /* you must sign messages */ +#define RS_FORUM_MSG_ANON 0x0020 /* you can send anonymous messages */ + #define RS_FORUM_ADMIN 0x0100 /* anyone can publish */ #define RS_FORUM_SUBSCRIBED 0x0200 /* anyone can publish */ @@ -61,8 +64,11 @@ class ForumMsgInfo ForumMsgInfo() {} std::string forumId; std::string threadId; + std::string parentId; std::string msgId; + std::string srcId; /* if Authenticated -> signed here */ + unsigned int msgflags; std::wstring title; @@ -75,13 +81,15 @@ class ThreadInfoSummary { public: ThreadInfoSummary() {} - + std::string forumId; + std::string threadId; + std::string parentId; std::string msgId; - std::string srcId; uint32_t msgflags; std::wstring title; + std::wstring msg; int count; /* file count */ time_t ts; @@ -105,9 +113,12 @@ virtual ~RsForums() { return; } virtual bool forumsChanged(std::list &forumIds) = 0; + +virtual std::string createForum(std::wstring forumName, std::wstring forumDesc, uint32_t forumFlags) = 0; + virtual bool getForumList(std::list &forumList) = 0; virtual bool getForumThreadList(std::string fId, std::list &msgs) = 0; -virtual bool getForumThreadMsgList(std::string fId, std::string tId, std::list &msgs) = 0; +virtual bool getForumThreadMsgList(std::string fId, std::string pId, std::list &msgs) = 0; virtual bool getForumMessage(std::string fId, std::string mId, ForumMsgInfo &msg) = 0; virtual bool ForumMessageSend(ForumMsgInfo &info) = 0; diff --git a/libretroshare/src/rsiface/rsrank.h b/libretroshare/src/rsiface/rsrank.h index f99bab4d5..4c56d513e 100644 --- a/libretroshare/src/rsiface/rsrank.h +++ b/libretroshare/src/rsiface/rsrank.h @@ -40,6 +40,7 @@ class RsRankComment std::string id; std::wstring comment; + int32_t score; time_t timestamp; }; @@ -85,8 +86,10 @@ virtual bool getRankings(uint32_t first, uint32_t count, std::list virtual bool getRankDetails(std::string rid, RsRankDetails &details) = 0; /* Add New Comment / Msg */ -virtual std::string newRankMsg(std::wstring link, std::wstring title, std::wstring comment) = 0; -virtual bool updateComment(std::string rid, std::wstring comment) = 0; +virtual std::string newRankMsg(std::wstring link, std::wstring title, std::wstring comment, int32_t score) = 0; +virtual bool updateComment(std::string rid, std::wstring comment, int32_t score) = 0; + +virtual std::string anonRankMsg(std::wstring link, std::wstring title) = 0; }; diff --git a/libretroshare/src/rsserver/p3face-startup.cc b/libretroshare/src/rsserver/p3face-startup.cc index d78241dcf..fc4f5176a 100644 --- a/libretroshare/src/rsserver/p3face-startup.cc +++ b/libretroshare/src/rsserver/p3face-startup.cc @@ -74,8 +74,6 @@ #define RS_RELEASE 1 ****/ -#define RS_RELEASE 1 - /**************** PQI_USE_XPGP ******************/ #if defined(PQI_USE_XPGP) @@ -590,6 +588,9 @@ int RsServer::StartupRetroShare(RsInit *config) mConfigMgr->addConfiguration("general.cfg", mGeneralConfig); mConfigMgr->addConfiguration("msgs.cfg", msgSrv); mConfigMgr->addConfiguration("cache.cfg", mCacheStrapper); +#ifndef RS_RELEASE + mConfigMgr->addConfiguration("ranklink.cfg", mRanking); +#endif /**************************************************************************/ diff --git a/libretroshare/src/rsserver/p3rank.cc b/libretroshare/src/rsserver/p3rank.cc index 58180ecc4..88ec276b1 100644 --- a/libretroshare/src/rsserver/p3rank.cc +++ b/libretroshare/src/rsserver/p3rank.cc @@ -87,14 +87,18 @@ bool p3Rank::getRankDetails(std::string rid, RsRankDetails &details) /* Add New Comment / Msg */ -std::string p3Rank::newRankMsg(std::wstring link, std::wstring title, std::wstring comment) +std::string p3Rank::newRankMsg(std::wstring link, std::wstring title, std::wstring comment, int32_t score) { - return mRank->newRankMsg(link, title, comment); + return mRank->newRankMsg(link, title, comment, score); } -bool p3Rank::updateComment(std::string rid, std::wstring comment) +bool p3Rank::updateComment(std::string rid, std::wstring comment, int32_t score) { - return mRank->updateComment(rid, comment); + return mRank->updateComment(rid, comment, score); } +std::string p3Rank::anonRankMsg(std::wstring link, std::wstring title) +{ + return mRank->anonRankMsg(link, title); +} diff --git a/libretroshare/src/rsserver/p3rank.h b/libretroshare/src/rsserver/p3rank.h index cbb380dc8..305495769 100644 --- a/libretroshare/src/rsserver/p3rank.h +++ b/libretroshare/src/rsserver/p3rank.h @@ -52,8 +52,9 @@ virtual bool getRankings(uint32_t first, uint32_t count, std::listtitle); s += GetTlvWideStringSize(item->comment); + s += 4; /* score */ s += 4; /* linktype */ s += GetTlvWideStringSize(item->link); @@ -158,6 +167,9 @@ bool RsRankSerialiser::serialiseLink(RsRankLinkMsg *item, void *data, uint32 ok &= SetTlvWideString(data, tlvsize, &offset, TLV_TYPE_WSTR_COMMENT, item->comment); std::cerr << "RsRankLinkSerialiser::serialiseLink() Comment: " << ok << std::endl; + ok &= setRawUInt32(data, tlvsize, &offset, *((uint32_t *) &(item->score))); + std::cerr << "RsRankLinkSerialiser::serialiseLink() timestamp: " << ok << std::endl; + ok &= setRawUInt32(data, tlvsize, &offset, item->linktype); std::cerr << "RsRankLinkSerialiser::serialiseLink() linktype: " << ok << std::endl; @@ -184,7 +196,7 @@ RsRankLinkMsg *RsRankSerialiser::deserialiseLink(void *data, uint32_t *pktsize) if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) || (RS_SERVICE_TYPE_RANK != getRsItemService(rstype)) || - (RS_PKT_SUBTYPE_RANK_LINK2 != getRsItemSubType(rstype))) + (RS_PKT_SUBTYPE_RANK_LINK3 != getRsItemSubType(rstype))) { return NULL; /* wrong type */ } @@ -210,6 +222,7 @@ RsRankLinkMsg *RsRankSerialiser::deserialiseLink(void *data, uint32_t *pktsize) ok &= getRawUInt32(data, rssize, &offset, &(item->timestamp)); ok &= GetTlvWideString(data, rssize, &offset, TLV_TYPE_WSTR_TITLE, item->title); ok &= GetTlvWideString(data, rssize, &offset, TLV_TYPE_WSTR_COMMENT, item->comment); + ok &= getRawUInt32(data, rssize, &offset, (uint32_t *) &(item->score)); ok &= getRawUInt32(data, rssize, &offset, &(item->linktype)); ok &= GetTlvWideString(data, rssize, &offset, TLV_TYPE_WSTR_LINK, item->link); diff --git a/libretroshare/src/serialiser/rsrankitems.h b/libretroshare/src/serialiser/rsrankitems.h index 9edecd8da..22dbdfa0b 100644 --- a/libretroshare/src/serialiser/rsrankitems.h +++ b/libretroshare/src/serialiser/rsrankitems.h @@ -33,8 +33,10 @@ #include "serialiser/rstlvtypes.h" const uint8_t RS_PKT_SUBTYPE_RANK_OLD_LINK = 0x02; /* defunct - don't use! */ -const uint8_t RS_PKT_SUBTYPE_RANK_LINK2 = 0x03; -const uint8_t RS_PKT_SUBTYPE_RANK_PHOTO = 0x04; +const uint8_t RS_PKT_SUBTYPE_RANK_OLD_LINK2 = 0x03; +const uint8_t RS_PKT_SUBTYPE_RANK_LINK3 = 0x04; + +const uint8_t RS_PKT_SUBTYPE_RANK_PHOTO = 0x05; /**************************************************************************/ @@ -54,6 +56,7 @@ virtual std::ostream& print(std::ostream &out, uint16_t indent = 0); uint32_t timestamp; std::wstring title; std::wstring comment; + int32_t score; }; @@ -65,7 +68,7 @@ class RsRankLinkMsg: public RsRankMsg { public: RsRankLinkMsg() - :RsRankMsg(RS_PKT_SUBTYPE_RANK_LINK2) { return; } + :RsRankMsg(RS_PKT_SUBTYPE_RANK_LINK3) { return; } virtual ~RsRankLinkMsg() { return; } virtual void clear(); virtual std::ostream& print(std::ostream &out, uint16_t indent = 0); @@ -75,6 +78,7 @@ virtual std::ostream& print(std::ostream &out, uint16_t indent = 0); uint32_t timestamp; std::wstring title; std::wstring comment; + int32_t score; ***************************/ /* Link specific Fields */ diff --git a/libretroshare/src/services/p3forums.cc b/libretroshare/src/services/p3forums.cc index 96d4c9c20..a582fc578 100644 --- a/libretroshare/src/services/p3forums.cc +++ b/libretroshare/src/services/p3forums.cc @@ -99,25 +99,117 @@ bool p3Forums::getForumList(std::list &forumList) bool p3Forums::getForumThreadList(std::string fId, std::list &msgs) { + std::map::iterator it; + for(it = mForumMsgs.begin(); it != mForumMsgs.end(); it++) + { + if (((it->second).forumId == fId) && ((it->second).parentId == "")) + { + msgs.push_back(it->second); + } + } return true; } -bool p3Forums::getForumThreadMsgList(std::string fId, std::string tId, std::list &msgs) +bool p3Forums::getForumThreadMsgList(std::string fId, std::string pId, std::list &msgs) { + std::map::iterator it; + for(it = mForumMsgs.begin(); it != mForumMsgs.end(); it++) + { + if (((it->second).forumId == fId) && ((it->second).parentId == pId)) + { + msgs.push_back(it->second); + } + } return true; } bool p3Forums::getForumMessage(std::string fId, std::string mId, ForumMsgInfo &msg) { + std::map::iterator it; + it = mForumMsgs.find(mId); + if (it == mForumMsgs.end()) + { + return false; + } + + msg.forumId = (it->second).forumId; + msg.threadId = (it->second).threadId; + msg.parentId = (it->second).parentId; + msg.msgId = (it->second).msgId; + + msg.title = (it->second).title; + msg.msg = (it->second).msg; + msg.ts = (it->second).ts; + + msg.srcId = "SRC"; + msg.ts = (it->second).ts; + return true; } bool p3Forums::ForumMessageSend(ForumMsgInfo &info) { + + createForumMsg(info.forumId, info.parentId, info.title, info.msg); return true; } +std::string p3Forums::createForum(std::wstring forumName, std::wstring forumDesc, uint32_t forumFlags) +{ + time_t now = time(NULL); + + ForumInfo fi; + fi.lastPost = now; + fi.pop = 1; + + fi.forumId = generateRandomServiceId(); + fi.forumName = forumName; + fi.forumDesc = forumDesc; + fi.forumFlags = forumFlags; + fi.forumFlags |= RS_FORUM_ADMIN; + + mForums.push_back(fi); + mForumsChanged = true; + + return fi.forumId; +} + +std::string p3Forums::createForumMsg(std::string fId, std::string pId, + std::wstring title, std::wstring msg) +{ + ThreadInfoSummary tis; + + tis.forumId = fId; + tis.parentId = pId; + + /* find the parent -> copy threadId */ + tis.msgId = generateRandomServiceId(); + + std::map::iterator it; + it = mForumMsgs.find(pId); + + if (it == mForumMsgs.end()) + { + tis.parentId = ""; + tis.threadId = tis.msgId; + } + else + { + tis.threadId = (it->second).threadId; + } + + tis.title = title; + tis.msg = msg; + tis.ts = time(NULL); + + mForumMsgs[tis.msgId] = tis; + mForumsChanged = true; + + return tis.msgId; +} + + /****************************************/ void p3Forums::loadDummyData() diff --git a/libretroshare/src/services/p3forums.h b/libretroshare/src/services/p3forums.h index 230722438..7b6b96515 100644 --- a/libretroshare/src/services/p3forums.h +++ b/libretroshare/src/services/p3forums.h @@ -27,6 +27,7 @@ */ #include "rsiface/rsforums.h" +#include "services/p3service.h" class p3Forums: public RsForums { @@ -40,6 +41,8 @@ virtual ~p3Forums(); virtual bool forumsChanged(std::list &forumIds); +virtual std::string createForum(std::wstring forumName, std::wstring forumDesc, uint32_t forumFlags); + virtual bool getForumList(std::list &forumList); virtual bool getForumThreadList(std::string fId, std::list &msgs); virtual bool getForumThreadMsgList(std::string fId, std::string tId, std::list &msgs); @@ -51,8 +54,13 @@ virtual bool ForumMessageSend(ForumMsgInfo &info); private: +std::string createForumMsg(std::string fid, std::string pid, + std::wstring title, std::wstring msg); + void loadDummyData(); std::list mForums; +std::map mForumMsgs; + bool mForumsChanged; }; diff --git a/libretroshare/src/services/p3ranking.cc b/libretroshare/src/services/p3ranking.cc index 720880596..af62e21e0 100644 --- a/libretroshare/src/services/p3ranking.cc +++ b/libretroshare/src/services/p3ranking.cc @@ -52,6 +52,7 @@ p3Ranking::p3Ranking(p3ConnectMgr *connMgr, uint32_t storePeriod) :CacheSource(type, true, cs, sourcedir), CacheStore(type, true, cs, cft, storedir), + p3Config(CONFIG_TYPE_RANK_LINK), mConnMgr(connMgr), mRepublish(false), mRepublishFriends(false), mRepublishFriendTS(0), mStorePeriod(storePeriod), mUpdated(true) @@ -300,25 +301,68 @@ void p3Ranking::publishMsgs(bool own) } else { - /* iterate through all comments */ - for(cit = it->second.comments.begin(); - cit != it->second.comments.end(); cit++) + /* if we have pushed it out already - don't bother */ + if (it->second.ownTag) { - RsItem *item = cit->second; - /* write to serialiser */ - if (item && (mConnMgr->isFriend(item->PeerId()))) - { #ifdef RANK_DEBUG - std::cerr << "p3Ranking::publishMsgs() Storing Friend Item:"; - std::cerr << std::endl; - item->print(std::cerr, 10); - std::cerr << std::endl; + std::cerr << "p3Ranking::publishMsgs() (Friends) Skipping Own Item"; + std::cerr << std::endl; #endif - stream->SendItem(item); - stream->tick(); /* Tick to write! */ - - } + continue; } + + /* 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; + } + } + } + + + /* now we also add our anon messages to the friends list */ + if (!own) + { + std::list::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! */ } } @@ -389,12 +433,36 @@ void p3Ranking::addRankMsg(RsRankLinkMsg *msg) grp.rid = rid; grp.ownTag = false; - /******** LINK SPECIFIC ****/ +/*************************************************************************/ +/****************************** LINK SPECIFIC ****************************/ +/*************************************************************************/ + grp.link = msg->link; grp.title = msg->title; +/*************************************************************************/ +/****************************** LINK SPECIFIC ****************************/ +/*************************************************************************/ + mData[rid] = grp; it = mData.find(rid); + + + 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; } /* check for old comment */ @@ -624,7 +692,7 @@ float p3Ranking::locked_calcRank(RankGroup &grp) std::cerr << std::endl; #endif - if ((count < 0) || (algScore < 0)) + if ((count <= 0) || (algScore <= 0)) { #ifdef RANK_DEBUG std::cerr << "Final score: 0"; @@ -695,12 +763,14 @@ void p3Ranking::sortAllMsgs() for(it = mData.begin(); it != mData.end(); it++) { (it->second).rank = locked_calcRank(it->second); - if (it->second.rank > 0) + if (it->second.rank < 0) { - mRankings.insert( - std::pair - (it->second.rank, it->first)); + it->second.rank = 0; } + + mRankings.insert( + std::pair + (it->second.rank, it->first)); } } @@ -728,12 +798,17 @@ bool p3Ranking::getRankings(uint32_t first, uint32_t count, std::list::reverse_iterator rit; - for(rit = mRankings.rbegin(); (i < first) && (rit != mRankings.rend()); rit++); + for(rit = mRankings.rbegin(); (i < first) && (rit != mRankings.rend()); rit++, i++); i = 0; - for(; (i < count) && (rit != mRankings.rend()); rit++) + for(; (i < count) && (rit != mRankings.rend()); rit++, i++) { rids.push_back(rit->second); } @@ -754,12 +829,20 @@ bool p3Ranking::getRankDetails(std::string rid, RsRankDetails &details) return false; } +/*************************************************************************/ +/****************************** LINK SPECIFIC ****************************/ +/*************************************************************************/ + details.rid = it->first; details.link = (it->second).link; details.title = (it->second).title; details.rank = (it->second).rank; details.ownTag = (it->second).ownTag; +/*************************************************************************/ +/****************************** LINK SPECIFIC ****************************/ +/*************************************************************************/ + std::map::iterator cit; for(cit = (it->second).comments.begin(); cit != (it->second).comments.end(); cit++) @@ -768,6 +851,7 @@ bool p3Ranking::getRankDetails(std::string rid, RsRankDetails &details) comm.id = (cit->second)->PeerId(); comm.timestamp = (cit->second)->timestamp; comm.comment = (cit->second)->comment; + comm.score = (cit->second)->score; details.comments.push_back(comm); } @@ -821,7 +905,10 @@ bool p3Ranking::updated() } /***** NEW CONTENT *****/ -std::string p3Ranking::newRankMsg(std::wstring link, std::wstring title, std::wstring comment) +/*************************************************************************/ +/****************************** LINK SPECIFIC ****************************/ +/*************************************************************************/ +std::string p3Ranking::newRankMsg(std::wstring link, std::wstring title, std::wstring comment, int32_t score) { /* generate an id */ std::string rid = generateRandomLinkId(); @@ -840,16 +927,22 @@ std::string p3Ranking::newRankMsg(std::wstring link, std::wstring title, std::ws msg->title = title; msg->timestamp = now; msg->comment = comment; + msg->score = score; msg->linktype = RS_LINK_TYPE_WEB; msg->link = link; + addRankMsg(msg); return rid; } -bool p3Ranking::updateComment(std::string rid, std::wstring comment) + +/*************************************************************************/ +/****************************** LINK SPECIFIC ****************************/ +/*************************************************************************/ +bool p3Ranking::updateComment(std::string rid, std::wstring comment, int32_t score) { #ifdef RANK_DEBUG @@ -883,6 +976,7 @@ bool p3Ranking::updateComment(std::string rid, std::wstring comment) msg->timestamp = now; msg->title = (it->second).title; msg->comment = comment; + msg->score = score; msg->linktype = RS_LINK_TYPE_WEB; msg->link = (it->second).link; @@ -900,6 +994,54 @@ bool p3Ranking::updateComment(std::string rid, std::wstring comment) return true; } +/*************************************************************************/ +/****************************** 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; +} + + + pqistreamer *createStreamer(std::string file, std::string src, bool reading) { @@ -953,6 +1095,9 @@ std::string generateRandomLinkId() } +/*************************************************************************/ +/****************************** LINK SPECIFIC ****************************/ +/*************************************************************************/ void p3Ranking::createDummyData() { RsRankLinkMsg *msg = new RsRankLinkMsg(); @@ -966,6 +1111,7 @@ void p3Ranking::createDummyData() msg->timestamp = now - 60 * 60 * 24 * 15; msg->link = L"http://www.retroshare.org"; msg->comment = L"Retroshares Website"; + msg->score = 1; addRankMsg(msg); @@ -977,6 +1123,7 @@ void p3Ranking::createDummyData() msg->timestamp = now - 123; msg->link = L"http://www.lunamutt.org"; msg->comment = L"Lunamutt's Website"; + msg->score = 1; addRankMsg(msg); @@ -990,6 +1137,7 @@ void p3Ranking::createDummyData() 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!"; + msg->score = 1; addRankMsg(msg); @@ -1001,6 +1149,7 @@ void p3Ranking::createDummyData() msg->timestamp = now - 60 * 60 * 7; msg->link = L"http://www.lunamutt.org"; msg->comment += L"A Short Comment"; + msg->score = 1; addRankMsg(msg); @@ -1015,6 +1164,7 @@ void p3Ranking::createDummyData() msg->timestamp = now - 60 * 60; msg->link = L"http://www.lunamutt.com"; msg->comment = L""; + msg->score = 1; addRankMsg(msg); @@ -1026,8 +1176,136 @@ void p3Ranking::createDummyData() msg->timestamp = now - 60 * 60 * 24 * 2; msg->link = L"http://www.lunamutt.com"; msg->comment = L""; + msg->score = 1; addRankMsg(msg); } + +/***************************************************************************/ +/****************************** 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 p3Ranking::saveList(bool &cleanup) +{ + std::list saveData; + + mRankMtx.lock(); /*********************** LOCK *******/ + + cleanup = false; + + std::list::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 load) +{ + std::list::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(*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; + +} + diff --git a/libretroshare/src/services/p3ranking.h b/libretroshare/src/services/p3ranking.h index 39bfa4084..b74787ee9 100644 --- a/libretroshare/src/services/p3ranking.h +++ b/libretroshare/src/services/p3ranking.h @@ -30,6 +30,7 @@ #include "pqi/pqiservice.h" #include "pqi/pqistreamer.h" #include "pqi/p3connmgr.h" +#include "pqi/p3cfgmgr.h" #include "serialiser/rsserial.h" @@ -61,7 +62,7 @@ class RankGroup }; -class p3Ranking: public CacheSource, public CacheStore +class p3Ranking: public CacheSource, public CacheStore, public p3Config { public: @@ -100,8 +101,9 @@ virtual bool getRankings(uint32_t first, uint32_t count, std::list saveList(bool &cleanup); +virtual bool loadList(std::list load); +virtual void saveDone(); + private: void createDummyData(); @@ -143,6 +155,12 @@ void createDummyData(); uint32_t mViewPeriod; uint32_t mSortType; + /* Anonymous Link List */ + std::list mAnon; + }; #endif + + + diff --git a/libretroshare/src/services/p3service.cc b/libretroshare/src/services/p3service.cc index 1d1bfd094..66b05d316 100644 --- a/libretroshare/src/services/p3service.cc +++ b/libretroshare/src/services/p3service.cc @@ -25,6 +25,8 @@ #include "pqi/pqi.h" #include "services/p3service.h" +#include +#include #define SERV_DEBUG 1 @@ -201,4 +203,31 @@ RsRawItem *p3Service::send() } +std::string generateRandomServiceId() +{ + std::ostringstream out; + out << std::hex; +/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ +#ifndef WINDOWS_SYS + /* 4 bytes per random number: 4 x 4 = 16 bytes */ + for(int i = 0; i < 4; i++) + { + out << std::setw(8) << std::setfill('0'); + uint32_t rint = random(); + out << rint; + } +#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 ******************/ + return out.str(); +} + diff --git a/libretroshare/src/services/p3service.h b/libretroshare/src/services/p3service.h index a65cc6f01..bf40d54a0 100644 --- a/libretroshare/src/services/p3service.h +++ b/libretroshare/src/services/p3service.h @@ -48,6 +48,9 @@ * For both Cached and Messages. */ +std::string generateRandomServiceId(); + + class p3Service: public pqiService { protected: