mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-22 15:51:29 -04:00
simplified qblog for blogging only, hopefully test everything out soon
git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@731 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
a52c34a551
commit
d14a1ffb39
6 changed files with 223 additions and 478 deletions
|
@ -24,42 +24,41 @@
|
|||
*/
|
||||
|
||||
#include "serialiser/rsqblogitems.h"
|
||||
#include "services/p3Qblog.h"
|
||||
#include "p3Qblog.h"
|
||||
#include <utility>
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
#include "pqi/pqibin.h"
|
||||
#include "pqi/p3authmgr.h"
|
||||
|
||||
const uint32_t RANK_MAX_FWD_OFFSET = (60 * 60 * 24 * 2); /* 2 Days */
|
||||
|
||||
const uint32_t BLOG_MAX_FWD_OFFSET = (60 * 60 * 24 * 2); /* 2 Days */
|
||||
const uint32_t FRIEND_QBLOG_REPOST_PERIOD = 60; /* every minute for testing */
|
||||
|
||||
/****
|
||||
* #define QBLOG_DEBUG 1
|
||||
****/
|
||||
|
||||
p3Qblog::p3Qblog(p3ConnectMgr *connMgr,
|
||||
#define QBLOG_DEBUG 1
|
||||
|
||||
|
||||
p3Qblog::p3Qblog(p3ConnectMgr *connMgr,
|
||||
uint16_t type, CacheStrapper *cs, CacheTransfer *cft,
|
||||
std::string sourcedir, std::string storedir,
|
||||
std::string sourcedir, std::string storedir,
|
||||
uint32_t storePeriod)
|
||||
:CacheSource(type, true, cs, sourcedir),
|
||||
CacheStore(type, true, cs, cft, storedir),
|
||||
mConnMgr(connMgr), mFilterSwitch(false),
|
||||
:CacheSource(type, true, cs, sourcedir),
|
||||
CacheStore(type, true, cs, cft, storedir),
|
||||
mConnMgr(connMgr),
|
||||
mStorePeriod(storePeriod), mPostsUpdated(false), mProfileUpdated(false)
|
||||
{
|
||||
{
|
||||
{
|
||||
RsStackMutex stack(mBlogMtx);
|
||||
mOwnId = mConnMgr->getOwnId(); // get your own usr Id
|
||||
loadDummy(); // load dummy data
|
||||
|
||||
|
||||
#ifdef QBLOG_DEBUG
|
||||
/*if(!ok)
|
||||
std::cerr << "initialization failed: could not retrieve friend list"; */
|
||||
#endif
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -67,7 +66,7 @@ std::ostream &operator<<(std::ostream& out, const std::wstring wstr)
|
|||
{
|
||||
std::string str(wstr.begin(), wstr.end());
|
||||
out << str;
|
||||
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
|
@ -76,7 +75,7 @@ bool p3Qblog::loadLocalCache(const CacheData &data)
|
|||
std::string filename = data.path + '/' + data.name;
|
||||
std::string hash = data.hash;
|
||||
std::string source = data.pid;
|
||||
|
||||
|
||||
#ifdef QBLOG_DEBUG
|
||||
std::cerr << "p3Ranking::loadLocalCache()";
|
||||
std::cerr << std::endl;
|
||||
|
@ -91,7 +90,7 @@ bool p3Qblog::loadLocalCache(const CacheData &data)
|
|||
#endif
|
||||
|
||||
loadBlogFile(filename, source);
|
||||
|
||||
|
||||
{
|
||||
RsStackMutex stack(mBlogMtx);
|
||||
mPostsUpdated = true; // there is nothing to tick/repost (i.e. comes from cache)
|
||||
|
@ -101,7 +100,7 @@ bool p3Qblog::loadLocalCache(const CacheData &data)
|
|||
{
|
||||
refreshCache(data);
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -129,7 +128,7 @@ int p3Qblog::loadCache(const CacheData &data)
|
|||
|
||||
|
||||
CacheStore::lockData(); /***** LOCK ****/
|
||||
locked_storeCacheEntry(data);
|
||||
locked_storeCacheEntry(data);
|
||||
CacheStore::unlockData(); /***** UNLOCK ****/
|
||||
|
||||
return 1;
|
||||
|
@ -140,37 +139,37 @@ bool p3Qblog::loadBlogFile(std::string filename, std::string src)
|
|||
/* create the serialiser to load info */
|
||||
RsSerialiser *rsSerialiser = new RsSerialiser();
|
||||
rsSerialiser->addSerialType(new RsQblogMsgSerialiser());
|
||||
|
||||
|
||||
uint32_t bioflags = BIN_FLAGS_HASH_DATA | BIN_FLAGS_READABLE;
|
||||
BinInterface *bio = new BinFileInterface(filename.c_str(), bioflags);
|
||||
BinInterface *bio = new BinFileInterface(filename.c_str(), bioflags);
|
||||
pqistreamer *stream = new pqistreamer(rsSerialiser, src, bio, 0);
|
||||
|
||||
|
||||
#ifdef QBLOG_DEBUG
|
||||
|
||||
|
||||
std::cerr << "p3Qblog::loadBlogFile()";
|
||||
std::cerr << std::endl;
|
||||
std::cerr << "\tSource: " << src;
|
||||
std::cerr << std::endl;
|
||||
std::cerr << "\tFilename: " << filename;
|
||||
std::cerr << std::endl;
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* will load file info to these items */
|
||||
RsItem *item;
|
||||
RsQblogMsg *newBlog;
|
||||
|
||||
|
||||
stream->tick(); // tick to read
|
||||
|
||||
|
||||
time_t now = time(NULL);
|
||||
time_t min, max;
|
||||
|
||||
|
||||
{ /********** STACK LOCKED MTX ******/
|
||||
RsStackMutex stack(mBlogMtx);
|
||||
min = now - mStorePeriod;
|
||||
max = now + RANK_MAX_FWD_OFFSET;
|
||||
max = now + BLOG_MAX_FWD_OFFSET;
|
||||
} /********** STACK LOCKED MTX ******/
|
||||
|
||||
|
||||
while(NULL != (item = stream->GetItem()))
|
||||
{
|
||||
#ifdef QBLOG_DEBUG
|
||||
|
@ -179,7 +178,7 @@ bool p3Qblog::loadBlogFile(std::string filename, std::string src)
|
|||
item->print(std::cerr, 10);
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
|
||||
if (NULL == (newBlog = dynamic_cast<RsQblogMsg *>(item)))
|
||||
{
|
||||
#ifdef QBLOG_DEBUG
|
||||
|
@ -189,8 +188,8 @@ bool p3Qblog::loadBlogFile(std::string filename, std::string src)
|
|||
|
||||
delete item;
|
||||
}
|
||||
/* check timestamp */
|
||||
else if (((time_t) newBlog->sendTime < min) ||
|
||||
/* check timestamp: delete if here longer than store period or more than two days */
|
||||
else if (((time_t) newBlog->sendTime < min) ||
|
||||
((time_t) newBlog->sendTime > max))
|
||||
{
|
||||
#ifdef QBLOG_DEBUG
|
||||
|
@ -209,10 +208,10 @@ bool p3Qblog::loadBlogFile(std::string filename, std::string src)
|
|||
|
||||
addBlog(newBlog); // add received blog to list
|
||||
}
|
||||
|
||||
|
||||
stream->tick(); // tick to read
|
||||
}
|
||||
|
||||
|
||||
delete stream; // stream finished with/return resource
|
||||
return true;
|
||||
}
|
||||
|
@ -225,12 +224,12 @@ bool p3Qblog::addBlog(RsQblogMsg *newBlog)
|
|||
newBlog->print(std::cerr, 10); // check whats in the blog item
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
|
||||
{
|
||||
RsStackMutex Stack(mBlogMtx);
|
||||
|
||||
|
||||
mUsrBlogSet[newBlog->PeerId()].insert(std::make_pair(newBlog->sendTime, newBlog->message));
|
||||
|
||||
|
||||
#ifdef QBLOG_DEBUG
|
||||
std::cerr << "p3Qblog::addBlog()";
|
||||
std::cerr << std::endl;
|
||||
|
@ -240,16 +239,15 @@ bool p3Qblog::addBlog(RsQblogMsg *newBlog)
|
|||
std::cerr << std::endl;
|
||||
std::cerr << "\tmUsrBlogSet: blog" << newBlog->message;
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
mPostsUpdated = false; // need to figure how this should work/ wherre this should be placed
|
||||
}
|
||||
|
||||
#endif
|
||||
mPostsUpdated = false; // need to figure how this should work/ where this should be placed
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool p3Qblog::postBlogs(void)
|
||||
{
|
||||
|
||||
#ifdef QBLOG_DEBUG
|
||||
std::cerr << "p3Qblog::postBlogs()";
|
||||
std::cerr << std::endl;
|
||||
|
@ -258,32 +256,32 @@ bool p3Qblog::postBlogs(void)
|
|||
std::string path = CacheSource::getCacheDir();
|
||||
std::ostringstream out;
|
||||
uint16_t subid = 1;
|
||||
|
||||
|
||||
out << "qblogs-" << time(NULL) << ".rsqb"; // create blog file name based on time posted
|
||||
|
||||
|
||||
/* determine filename */
|
||||
std::string tmpname = out.str();
|
||||
std::string fname = path + "/" + tmpname;
|
||||
|
||||
#ifdef RANK_DEBUG
|
||||
std::cerr << "p3Ranking::publishMsgs() Storing to: " << fname;
|
||||
|
||||
#ifdef QBLOG_DEBUG
|
||||
std::cerr << "p3Qblog::postBlogs() Storing to: " << fname;
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
RsSerialiser *rsSerialiser = new RsSerialiser();
|
||||
rsSerialiser->addSerialType(new RsQblogMsgSerialiser());
|
||||
|
||||
|
||||
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);
|
||||
|
||||
{
|
||||
|
||||
{
|
||||
RsStackMutex stack(mBlogMtx); /********** STACK LOCKED MTX ******/
|
||||
|
||||
|
||||
/* iterate through list */
|
||||
std::list<RsQblogMsg*>::iterator it;
|
||||
|
||||
std::list<RsQblogMsg*>::iterator it;
|
||||
|
||||
for(it = mBlogs.begin(); it != mBlogs.end(); it++)
|
||||
{
|
||||
/* only post own blogs */
|
||||
|
@ -291,14 +289,14 @@ bool p3Qblog::postBlogs(void)
|
|||
{
|
||||
/*write to serialiser*/
|
||||
RsItem *item = *it;
|
||||
|
||||
|
||||
#ifdef QBLOG_DEBUG
|
||||
std::cerr << "p3Qblog::postBlogs() Storing Item:";
|
||||
std::cerr << std::endl;
|
||||
item->print(std::cerr, 10);
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
|
||||
stream->SendItem(item);
|
||||
stream->tick(); /* tick to write */
|
||||
}
|
||||
|
@ -308,34 +306,34 @@ bool p3Qblog::postBlogs(void)
|
|||
std::cerr << "p3Ranking::postBlogs() Skipping friend blog items:";
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
stream->tick(); /* Tick for final write! */
|
||||
|
||||
|
||||
/* flag as new info */
|
||||
CacheData data;
|
||||
|
||||
|
||||
{ /********** STACK LOCKED MTX ******/
|
||||
RsStackMutex stack(mBlogMtx);
|
||||
RsStackMutex stack(mBlogMtx);
|
||||
data.pid = mOwnId;
|
||||
} /********** STACK LOCKED MTX ******/
|
||||
|
||||
|
||||
data.cid = CacheId(CacheSource::getCacheType(), subid);
|
||||
|
||||
|
||||
data.path = path;
|
||||
data.name = tmpname;
|
||||
|
||||
|
||||
data.hash = bio->gethash();
|
||||
data.size = bio->bytecount();
|
||||
data.recvd = time(NULL);
|
||||
|
||||
#ifdef RANK_DEBUG
|
||||
|
||||
#ifdef QBLOG_DEBUG
|
||||
std::cerr << "p3Ranking::postBlogs() refreshing Cache";
|
||||
std::cerr << std::endl;
|
||||
std::cerr << "\tCache Path: " << data.path;
|
||||
|
@ -347,137 +345,64 @@ bool p3Qblog::postBlogs(void)
|
|||
std::cerr << "\tCache Size: " << data.size;
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
|
||||
if(data.size > 0) /* don't refresh zero sized caches */
|
||||
{
|
||||
refreshCache(data);
|
||||
}
|
||||
|
||||
|
||||
delete stream;
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
p3Qblog::~p3Qblog()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bool p3Qblog::getFilterSwitch(void)
|
||||
{
|
||||
{
|
||||
RsStackMutex stack(mBlogMtx); // might be pointless
|
||||
return mFilterSwitch;
|
||||
}
|
||||
}
|
||||
|
||||
bool p3Qblog::setFilterSwitch(bool &filterSwitch)
|
||||
{
|
||||
{
|
||||
RsStackMutex stack(mBlogMtx);
|
||||
mFilterSwitch = filterSwitch;
|
||||
|
||||
#ifdef QBLOG_DEBUG
|
||||
std::cerr << "p3Qblog::setFilterSwitch() " << mFilterSwitch;
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool p3Qblog::removeFiltFriend(std::string &usrId)
|
||||
{
|
||||
std::list<std::string>::iterator it;
|
||||
|
||||
{
|
||||
RsStackMutex stack(mBlogMtx);
|
||||
|
||||
/* search through list to remove friend */
|
||||
for(it = mFilterList.begin(); it != mFilterList.end(); it++)
|
||||
{
|
||||
if(it->compare(usrId))
|
||||
{
|
||||
#ifdef QBLOG_DEBUG
|
||||
std::cerr << "p3Qblog::removeFilterFriend() it " << *it;
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
mFilterList.erase(it); // remove friend from list
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef QBLOG_DEBUG
|
||||
std::cerr << "usr could not be found!" << std::endl;
|
||||
#endif
|
||||
|
||||
return false; // could not find friend
|
||||
}
|
||||
|
||||
bool p3Qblog::addToFilter(std::string& usrId)
|
||||
{
|
||||
{
|
||||
RsStackMutex stack(mBlogMtx);
|
||||
std::list<std::string>::iterator it;
|
||||
|
||||
/* search through list in case friend already exist */
|
||||
for(it = mFilterList.begin(); it != mFilterList.end(); it++)
|
||||
{
|
||||
if(it->compare(usrId))
|
||||
{
|
||||
#ifdef QBLOG_DEBUG
|
||||
std::cerr << "usr already in list!" << *it;
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
return false; // user already in list, not added
|
||||
}
|
||||
}
|
||||
|
||||
mFilterList.push_back(usrId);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool p3Qblog::getBlogs(std::map< std::string, std::multimap<long int, std::wstring> > &blogs)
|
||||
{
|
||||
{
|
||||
{
|
||||
RsStackMutex stack(mBlogMtx);
|
||||
|
||||
|
||||
if(mUsrBlogSet.empty()) // return error blogs are empty
|
||||
{
|
||||
std::cerr << "usr blog set empty!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
blogs = mUsrBlogSet;
|
||||
return true;
|
||||
|
||||
#ifdef QBOG_DEBUG
|
||||
|
||||
|
||||
#ifdef QBLOG_DEBUG
|
||||
std::cerr << "p3Qblog::getBlogs() number of blogs: " << mUsrBlogSet.size();
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool p3Qblog::sendBlog(const std::wstring &msg)
|
||||
{
|
||||
time_t blogTimeStamp;
|
||||
|
||||
{
|
||||
|
||||
{
|
||||
RsStackMutex stack(mBlogMtx);
|
||||
mUsrBlogSet[mOwnId].insert(std::make_pair(blogTimeStamp, msg));
|
||||
|
||||
|
||||
RsQblogMsg *blog = new RsQblogMsg();
|
||||
blog->clear();
|
||||
|
||||
|
||||
blog->sendTime = blogTimeStamp;
|
||||
blog->message = msg;
|
||||
|
||||
|
||||
|
||||
#ifdef QBLOG_DEBUG
|
||||
std::cerr << "p3Qblog::sendBlogFile()";
|
||||
std::cerr << std::endl;
|
||||
|
@ -486,45 +411,12 @@ bool p3Qblog::sendBlog(const std::wstring &msg)
|
|||
std::cerr << "\tblogItem->message" << blog->message;
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
|
||||
mBlogs.push_back(blog);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool p3Qblog::getPeerProfile(std::string id, std::list< std::pair<std::wstring, std::wstring> > &entries)
|
||||
{
|
||||
// TODO
|
||||
return true;
|
||||
}
|
||||
|
||||
bool p3Qblog::setProfile(std::pair<std::wstring, std::wstring> entry)
|
||||
{
|
||||
//TODO
|
||||
return true;
|
||||
}
|
||||
|
||||
bool p3Qblog::setFavorites(FileInfo favFile)
|
||||
{
|
||||
//TODO
|
||||
return true;
|
||||
}
|
||||
|
||||
bool p3Qblog::getPeerLatestBlog(std::string id, uint32_t &ts, std::wstring &post)
|
||||
{
|
||||
//TODO
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool p3Qblog::getPeerFavourites(std::string id, std::list<FileInfo> &favs)
|
||||
{
|
||||
//TODO
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void p3Qblog::loadDummy(void)
|
||||
{
|
||||
std::list<std::string> peers;
|
||||
|
@ -535,40 +427,40 @@ void p3Qblog::loadDummy(void)
|
|||
//for(int i = 0; i < 50; i++)
|
||||
// std::cerr << "nothing in peer list!!!" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
srand(60);
|
||||
long int now = time(NULL); // the present time
|
||||
std::multimap<long int, std::wstring> emptySet; // time/blog map
|
||||
|
||||
|
||||
//std::string statusSet[5] = { "great", "rubbish", "ecstatic", "save me", "emo depression"};
|
||||
//std::string songs[5] = { "broken spleen", "niobium", "ewe (a sheep)", "velvet stuff", "chun li kicks"};
|
||||
|
||||
/* the usr dummy usr blogs */
|
||||
/* the usr dummy usr blogs */
|
||||
std::string B1 = "I think we should eat more cheese";
|
||||
std::string B2 = "today was so cool, i got attacked by fifty ninja while buying a loaf so i used my paper bag to suffocate each of them to death at hyper speed";
|
||||
std::string B3 = "Nuthins up";
|
||||
std::string B4 = "stop bothering me";
|
||||
std::string B5 = "I'm really a boring person and having nothin interesting to say";
|
||||
std::string blogs[5] = {B1, B2, B3, B4, B5};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* fill up maps: first usrblogset with empty blogs */
|
||||
|
||||
std::list<std::string>::iterator it;
|
||||
|
||||
|
||||
std::list<std::string>::iterator it;
|
||||
|
||||
mUsrBlogSet.insert(std::make_pair(mOwnId, emptySet));
|
||||
|
||||
|
||||
for(it = peers.begin(); it!=peers.end();it++)
|
||||
{
|
||||
mUsrBlogSet.insert(std::make_pair(*it, emptySet));
|
||||
}
|
||||
|
||||
/* now fill up blog map */
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
for(int i=0; i < 50 ; i++)
|
||||
{
|
||||
std::list<std::string>::iterator it = peers.begin();
|
||||
|
@ -577,7 +469,7 @@ void p3Qblog::loadDummy(void)
|
|||
int b = rand() % 5;
|
||||
cnv_wstr.assign(blogs[b].begin(), blogs[b].end());
|
||||
mUsrBlogSet[mOwnId].insert(std::make_pair(timeStamp, cnv_wstr )); // store a random blog
|
||||
|
||||
|
||||
for(;it!=peers.end(); it++)
|
||||
{
|
||||
timeStamp = now + rand() % 2134223; // a random time for each blog
|
||||
|
@ -586,10 +478,10 @@ void p3Qblog::loadDummy(void)
|
|||
mUsrBlogSet[*it].insert(std::make_pair(timeStamp, cnv_wstr)); // store a random blog
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool p3Qblog::sort(void)
|
||||
|
@ -601,12 +493,12 @@ bool p3Qblog::sort(void)
|
|||
void p3Qblog::tick()
|
||||
{
|
||||
bool postUpdated = false; // so stack mutex is not enabled during postblog call
|
||||
|
||||
|
||||
{
|
||||
RsStackMutex stack(mBlogMtx);
|
||||
postUpdated = mPostsUpdated;
|
||||
}
|
||||
|
||||
|
||||
if(!postUpdated)
|
||||
{
|
||||
if(!postBlogs())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue