core modification for Qblog

*final modifications to qblog network messages: might need to consider 
wstring for profile kvsets tho
*update qblog core to hanldle messages
*updated interface with all methods we will ever need hopefully


git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@614 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
chrisparker126 2008-06-19 21:34:13 +00:00
parent 18112f7116
commit 2a44784999
11 changed files with 489 additions and 378 deletions

View File

@ -47,17 +47,6 @@ extern RsQblog *rsQblog;
RsQblog() { return; }
virtual ~RsQblog() { return; }
/**
* allows user to set his status
* @param status The status of the user
*/
virtual bool setStatus(const std::string &status) = 0;
/**
* returns reference to map of usrs and their status
* @param usrStatus returns map to usr and their status
*/
virtual bool getStatus(std::map<std::string, std::string> &usrStatus) = 0;
/**
* choose whether to filter or not
@ -83,30 +72,29 @@ extern RsQblog *rsQblog;
*/
virtual bool removeFiltFriend(std::string &usrId) = 0;
/**
* get users fav song
* @param usrId the usr whose fav song you want
* @param favSong puts ref for fav song here
*/
virtual bool getProfile(std::map<std::string, std::string> &profile) = 0;
/**
* for now just fav song, TODO: must find way to link to rs profile
*/
virtual bool setProfile(const std::string &favSong) = 0;
/**
* send blog info, will send to a data structure for transmission
* @param msg The msg the usr wants to send
*/
virtual bool sendBlog(const std::string &msg) = 0;
virtual bool sendBlog(const std::wstring &msg) = 0;
/**
* retrieve blog of a usr
* @param blogs contains the blog msgs of usr along with time posted for sorting
*/
virtual bool getBlogs(std::map< std::string, std::multimap<long int, std:: string> > &blogs) = 0;
virtual bool getBlogs(std::map< std::string, std::multimap<long int, std::wstring> > &blogs) = 0;
/**
* set usr profile, send an empty second pair to delete entry
* @param entry profile entry
*/
virtual bool setProfile(std::pair<std::wstring, std::wstring> entry) = 0;
/**
* add fav file, send file info with only name to delete that entry
* @param entry file info entry
*/
virtual bool setFavorites(FileInfo favFile) = 0;
/**
@ -135,7 +123,6 @@ extern RsQblog *rsQblog;
*/
virtual bool getPeerFavourites(std::string id, std::list<FileInfo> &favs) = 0;
};
#endif /*RSQBLOG_H_*/

View File

@ -153,6 +153,12 @@ virtual bool AuthCertificate(std::string id, std::string code) = 0;
virtual bool SignCertificate(std::string id) = 0;
virtual bool TrustCertificate(std::string id, bool trust) = 0;
/**
* saves the usrs cert to file so it can easily be attached in an email
* @return successful or not
*/
virtual bool certToFile(void) = 0;
};
#endif

View File

@ -44,7 +44,7 @@ bool p3Blog::addToFilter(std::string &usrId)
return mQblog->addToFilter(usrId);
}
bool p3Blog::getBlogs(std::map< std::string, std::multimap<long int, std:: string> > &blogs)
bool p3Blog::getBlogs(std::map< std::string, std::multimap<long int, std::wstring> > &blogs)
{
return mQblog->getBlogs(blogs);
}
@ -54,17 +54,7 @@ bool p3Blog::getFilterSwitch(void)
return mQblog->getFilterSwitch();
}
bool p3Blog::getProfile(std::map<std::string, std::string> &profile)
{
return mQblog->getProfile(profile);
}
bool p3Blog::getStatus(std::map<std::string, std::string> &usrStatus)
{
return mQblog->getStatus(usrStatus);
}
bool p3Blog::sendBlog(const std::string &msg)
bool p3Blog::sendBlog(const std::wstring &msg)
{
return mQblog->sendBlog(msg);
}
@ -74,21 +64,23 @@ bool p3Blog::setFilterSwitch(bool &filterSwitch)
return mQblog->setFilterSwitch(filterSwitch);
}
bool p3Blog::setProfile(const std::string &favSong)
{
return mQblog->setProfile(favSong);
}
bool p3Blog::setStatus(const std::string &status)
{
return mQblog->setStatus(status);
}
bool p3Blog::removeFiltFriend(std::string &usrId)
{
return mQblog->removeFiltFriend(usrId);
}
bool p3Blog::setFavorites(FileInfo favFile)
{
//TODO
//return mQblog->setFavorites(favFile);
}
bool p3Blog::setProfile(std::pair<std::wstring, std::wstring> entry)
{
//TODO
//return mQblog->setProfile(entry);
}
bool p3Blog::getPeerLatestBlog(std::string id, uint32_t &ts, std::wstring &post)
{
//return mQblog->getPeerLatestBlog(id, ts, post);

View File

@ -43,17 +43,14 @@ class p3Blog : public RsQblog
p3Blog(p3Qblog* qblog);
virtual ~p3Blog();
virtual bool setStatus(const std::string &status);
virtual bool getStatus(std::map<std::string, std::string> &usrStatus);
virtual bool setFilterSwitch(bool &filterSwitch);
virtual bool getFilterSwitch(void);
virtual bool addToFilter(std::string &usrId);
virtual bool removeFiltFriend(std::string &usrId);
virtual bool getProfile(std::map<std::string, std::string> &profile);
virtual bool setProfile(const std::string &favSong);
virtual bool sendBlog(const std::string &msg);
virtual bool getBlogs(std::map< std::string, std::multimap<long int, std:: string> > &blogs);
virtual bool sendBlog(const std::wstring &msg);
virtual bool getBlogs(std::map< std::string, std::multimap<long int, std::wstring> > &blogs);
virtual bool setFavorites(FileInfo favFile);
virtual bool setProfile(std::pair<std::wstring, std::wstring> entry);
virtual bool getPeerLatestBlog(std::string id, uint32_t &ts, std::wstring &post);
virtual bool getPeerProfile(std::string id, std::list< std::pair<std::wstring, std::wstring> > &entries);
virtual bool getPeerFavourites(std::string id, std::list<FileInfo> &favs);

View File

@ -28,8 +28,10 @@
#include "pqi/p3authmgr.h"
#include <iostream>
#include <fstream>
#include <sstream>
RsPeers *rsPeers = NULL;
/*******
@ -789,6 +791,32 @@ uint32_t RsPeerTranslateTrust(uint32_t trustLvl)
return RS_TRUST_LVL_UNKNOWN;
}
bool p3Peers::certToFile(void)
{
std::string invite = GetRetroshareInvite();
const int SIZE = 400;
char certStore[SIZE]; // enough store to reach certification part of string
std::ofstream cert_file; // to help with counting non cert part of string
std::istringstream certFind(invite); // tie invite to stream
/* find out how long it takes to reach certification part of string */
certFind.get(certStore, SIZE, '=');
invite.erase(0, certFind.gcount()); // delete all characters before certificate part
#ifdef P3PEERS_DEBUG
std::cerr << "p3Peers::certToFile() : certificate" << invite;
#endif
std::string usrId = getPeerName(getOwnId()); // replace with actual textual id
usrId += ".pqi";
cert_file.open(usrId.c_str());
cert_file << invite; //store cert to file
cert_file.close();
return true;
}

View File

@ -75,6 +75,7 @@ virtual std::string SaveCertificateToString(std::string id);
virtual bool AuthCertificate(std::string id, std::string code);
virtual bool SignCertificate(std::string id);
virtual bool TrustCertificate(std::string id, bool trust);
virtual bool certToFile(void);
private:

View File

@ -24,62 +24,56 @@
*
*/
#include <iostream>
#include "serialiser/rsbaseserial.h"
#include "serialiser/rsqblogitems.h"
#include "serialiser/rstlvbase.h"
#include <iostream>
/************************************************************/
RsQblogItem::~RsQblogItem(void)
RsQblogMsg::~RsQblogMsg(void)
{
return;
}
void RsQblogItem::clear()
void RsQblogMsg::clear()
{
blogMsg.first = 0;
blogMsg.second = "";
status = "";
timeStamp = 0;
blogMsg.clear();
}
std::ostream &RsQblogItem::print(std::ostream &out, uint16_t indent)
std::ostream &RsQblogMsg::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsQblogItem", indent);
printRsItemBase(out, "RsQblogMsg", indent);
uint16_t int_Indent = indent + 2;
/* print out the content of the item */
printIndent(out, int_Indent);
out << "blogMsg(time): " << blogMsg.first << std::endl;
out << "blogMsg(time): " << timeStamp << std::endl;
printIndent(out, int_Indent);
out << "blogMsg(message): " << blogMsg.second << std::endl;
std::string cnv_blog(blogMsg.begin(), blogMsg.end());
out << "blogMsg(message): " << cnv_blog << std::endl;
printIndent(out, int_Indent);
out << "status " << status << std::endl;
printIndent(out, int_Indent);
out << "pid " << pid << std::endl;
printRsItemEnd(out, "RsQblogItem", indent);
return out;
}
uint32_t RsQblogSerialiser::sizeItem(RsQblogItem *item)
uint32_t RsQblogMsgSerialiser::sizeItem(RsQblogMsg *item)
{
uint32_t s = 8; // for header size
s += 4; // blog creation time
s += GetTlvStringSize(item->blogMsg.second); // string part of blog
s += GetTlvStringSize(item->status);
s += GetTlvStringSize(item->favSong);
s += GetTlvStringSize(item->pid);
s += GetTlvWideStringSize(item->blogMsg); // size of actual blog
return s;
}
/*******************************************************************************/
bool RsQblogSerialiser::serialiseItem(RsQblogItem* item, void* data, uint32_t *size)
bool RsQblogMsgSerialiser::serialiseItem(RsQblogMsg* item, void* data, uint32_t *size)
{
uint32_t tlvsize = sizeItem(item);
uint32_t offset = 0;
@ -94,24 +88,22 @@ bool RsQblogSerialiser::serialiseItem(RsQblogItem* item, void* data, uint32_t *s
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
#ifdef RSSERIAL_DEBUG
std::cerr << "RsChatSerialiser::serialiseItem() Header: " << ok << std::endl;
std::cerr << "RsChatSerialiser::serialiseItem() Size: " << tlvsize << std::endl;
std::cerr << "RsQblogSerialiser::serialiseItem() Header: " << ok << std::endl;
std::cerr << "RsQblogSerialiser::serialiseItem() Size: " << tlvsize << std::endl;
#endif
/* skip the header */
offset += 8;
/* add mandatory parts first */
ok &= setRawUInt32(data, tlvsize, &offset, item->blogMsg.first);
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_MSG, item->blogMsg.second);
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_MSG, item->status);
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_MSG, item->favSong);
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_MSG, item->favSong);
ok &= setRawUInt32(data, tlvsize, &offset, item->timeStamp);
ok &= SetTlvWideString(data, tlvsize, &offset, TLV_TYPE_STR_MSG, item->blogMsg);
if (offset != tlvsize)
{
ok = false;
#ifdef RSSERIAL_DEBUG
std::cerr << "RsChatSerialiser::serialiseItem() Size Error! " << std::endl;
std::cerr << "RsQblogSerialiser::serialiseItem() Size Error! " << std::endl;
#endif
}
@ -120,7 +112,7 @@ bool RsQblogSerialiser::serialiseItem(RsQblogItem* item, void* data, uint32_t *s
/**************************************************************************/
RsQblogItem* RsQblogSerialiser::deserialiseItem(void * data, uint32_t *size)
RsQblogMsg* RsQblogMsgSerialiser::deserialiseItem(void * data, uint32_t *size)
{
/* get the type and size */
@ -146,18 +138,15 @@ RsQblogItem* RsQblogSerialiser::deserialiseItem(void * data, uint32_t *size)
bool ok = true;
/* ready to load */
RsQblogItem *item = new RsQblogItem();
RsQblogMsg *item = new RsQblogMsg();
item->clear();
/* skip the header */
offset += 8;
/* get mandatory parts first */
ok &= getRawUInt32(data, rssize, &offset, &(item->blogMsg.first));
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_MSG, item->blogMsg.second);
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_MSG, item->status);
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_MSG, item->favSong);
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_MSG, item->pid);
ok &= getRawUInt32(data, rssize, &offset, &(item->timeStamp));
ok &= GetTlvWideString(data, rssize, &offset, TLV_TYPE_STR_MSG, item->blogMsg);
if (offset != rssize)
{
@ -177,21 +166,180 @@ RsQblogItem* RsQblogSerialiser::deserialiseItem(void * data, uint32_t *size)
/*********************************************************************/
bool RsQblogSerialiser::serialise(RsItem *item, void* data, uint32_t* size)
bool RsQblogMsgSerialiser::serialise(RsItem *item, void* data, uint32_t* size)
{
return serialiseItem((RsQblogItem *) item, data, size);
return serialiseItem((RsQblogMsg *) item, data, size);
}
RsItem* RsQblogSerialiser::deserialise(void* data, uint32_t* size)
RsItem* RsQblogMsgSerialiser::deserialise(void* data, uint32_t* size)
{
return deserialiseItem(data, size);
}
uint32_t RsQblogSerialiser::size(RsItem *item)
uint32_t RsQblogMsgSerialiser::size(RsItem *item)
{
return sizeItem((RsQblogItem *) item);
return sizeItem((RsQblogMsg *) item);
}
/********************************************* RsQblogProfile section ***********************************/
/********************************************* RsQblogProfile section ***********************************/
/********************************************* RsQblogProfile section ***********************************/
RsQblogProfile::~RsQblogProfile(void)
{
return;
}
void RsQblogProfile::clear()
{
timeStamp = 0;
openProfile.TlvClear();
favoriteFiles.TlvClear();
}
std::ostream &RsQblogProfile::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsQblogProfile", indent);
uint16_t int_Indent = indent + 2;
out << "RsQblogProfile::print() : timeStamp" << timeStamp;
out << std::endl;
openProfile.print(out, int_Indent);
favoriteFiles.print(out, int_Indent);
favoriteFiles.print(out, int_Indent);
printRsItemEnd(out, "RsQblogProfile", indent);
return out;
}
uint32_t RsQblogProfileSerialiser::sizeItem(RsQblogProfile *item)
{
uint32_t s = 8; // for header size
s += 4; // time stamp
s += item->openProfile.TlvSize();
s += item->favoriteFiles.TlvSize();
return s;
}
/*******************************************************************************/
bool RsQblogProfileSerialiser::serialiseItem(RsQblogProfile* item, void* data, uint32_t *size)
{
uint32_t tlvsize = sizeItem(item);
uint32_t offset = 0;
if (*size < tlvsize)
return false; /* not enough space */
*size = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
#ifdef RSSERIAL_DEBUG
std::cerr << "RsQblogSerialiser::serialiseItem() Header: " << ok << std::endl;
std::cerr << "RsQblogSerialiser::serialiseItem() Size: " << tlvsize << std::endl;
#endif
/* skip the header */
offset += 8;
/* add mandatory part */
ok &= setRawUInt32(data, tlvsize, &offset, item->timeStamp);
ok &= item->openProfile.SetTlv(data, *size, &offset);
ok &= item->favoriteFiles.SetTlv(data, *size, &offset);
if (offset != tlvsize)
{
ok = false;
#ifdef RSSERIAL_DEBUG
std::cerr << "RsQblogSerialiser::serialiseItem() Size Error! " << std::endl;
#endif
}
return ok;
}
/**************************************************************************/
RsQblogProfile* RsQblogProfileSerialiser::deserialiseItem(void * data, uint32_t *size)
{
/* get the type and size */
uint32_t rstype = getRsItemId(data);
uint32_t rssize = getRsItemSize(data);
uint32_t offset = 0;
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(RS_SERVICE_TYPE_QBLOG != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_QBLOG_PROFILE != getRsItemSubType(rstype)))
{
return NULL; /* wrong type */
}
if (*size < rssize) /* check size */
return NULL; /* not enough data */
/* set the packet length */
*size = rssize;
bool ok = true;
/* ready to load */
RsQblogProfile *item = new RsQblogProfile();
item->clear();
/* skip the header */
offset += 8;
/* get mandatory parts first */
RsTlvKeyValueSet* kvSetOpen;
RsTlvFileSet* fSet;
ok &= getRawUInt32(data, rssize, &offset, &(item->timeStamp));
ok &= kvSetOpen->GetTlv(data, *size, &offset);
ok &= fSet->GetTlv(data, *size, &offset);
/* copy over deserialised files */
item->openProfile = *kvSetOpen;
item->favoriteFiles = *fSet;
if (offset != rssize)
{
/* error, improper item */
delete item;
return NULL;
}
if (!ok)
{
delete item;
return NULL;
}
return item;
}
/*********************************************************************/
bool RsQblogProfileSerialiser::serialise(RsItem *item, void* data, uint32_t* size)
{
return serialiseItem((RsQblogProfile *) item, data, size);
}
RsItem* RsQblogProfileSerialiser::deserialise(void* data, uint32_t* size)
{
return deserialiseItem(data, size);
}
uint32_t RsQblogProfileSerialiser::size(RsItem *item)
{
return sizeItem((RsQblogProfile *) item);
}

View File

@ -28,52 +28,121 @@
#include <map>
#include <list>
#include <string>
#include "serialiser/rsserviceids.h"
#include "serialiser/rsserial.h"
#include "serialiser/rstlvtypes.h"
/*! retroshare data sturcture to be serialised */
class RsQblogItem: public RsItem
const uint8_t RS_PKT_SUBTYPE_QBLOG_PROFILE = 0x01;
/*!
* retroshare qblog msg item for storing received and sent blog message
*/
class RsQblogMsg: public RsItem
{
public:
RsQblogItem()
:RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_QBLOG,
RsQblogMsg()
:RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_QBLOG, // add msg id type
RS_PKT_SUBTYPE_DEFAULT)
{ return; }
virtual ~RsQblogItem();
virtual ~RsQblogMsg();
virtual void clear();
/**
* Used for unit test / debugging: i.e. checking serialisation occured fine on both ends
* @param out output stream for printing
* @param indent allows user to choose indentation of output stream
* @return pointer to stream object, to store output to a variety of formats
*/
/// inherited method from RsItem
std::ostream &print(std::ostream &out, uint16_t indent = 0);
/* everything below is serialised */
/// use for time stamp
uint32_t timeStamp;
/// contain blog mesgs and their blog time stamp (client time)
std::pair<uint32_t, std::string> blogMsg;
/// to be serialised: status of a requested user
std::string status;
/// the users favorite song
std::string favSong;
/// peer id TODO: serialise this
std::string pid;
std::wstring blogMsg;
};
/*! to serialise rsQblogItems: method names are self explanatory */
class RsQblogSerialiser : public RsSerialType
/*!
* retroshare qblog profile item for storing received and sent profile info
* designed in an open ended way to accomodate multiple fields
*/
class RsQblogProfile: public RsItem
{
public:
RsQblogProfile()
:RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_QBLOG, // add profile id type
RS_PKT_SUBTYPE_QBLOG_PROFILE)
{ return; }
virtual ~RsQblogProfile();
virtual void clear();
/// inherited method from RsItem
std::ostream &print(std::ostream &out, uint16_t indent = 0);
uint32_t timeStamp;
/// contains various profile information set by user, this and below use an open ended format
RsTlvKeyValueSet openProfile;
/// favoirte files of the user.....
RsTlvFileSet favoriteFiles;
};
/*!
* to serialise rsQblogItems: method names are self explanatory
*/
class RsQblogMsgSerialiser : public RsSerialType
{
public:
RsQblogSerialiser()
RsQblogMsgSerialiser()
:RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_QBLOG)
{ return; }
virtual ~RsQblogSerialiser()
virtual ~RsQblogMsgSerialiser()
{ return; }
/**
* check size of RsItem to be serialised
* @param RsItem RsItem which is going to be serilised
* @return size of the RsItem in bytes
*/
virtual uint32_t size(RsItem *);
/**
* serialise contents of item to data
* @param item RsItem which is going to be serilised
* @param data where contents will be serialised into
* @return size of the RsItem in bytes
*/
virtual bool serialise (RsItem *item, void *data, uint32_t *size);
/**
* serialise contents of item to data
* @param data where contents will be deserialisedout of
* @return size of the RsItem in bytes
*/
virtual RsItem * deserialise(void *data, uint32_t *size);
private:
virtual uint32_t sizeItem(RsQblogMsg *);
virtual bool serialiseItem (RsQblogMsg *item, void *data, uint32_t *size);
virtual RsQblogMsg *deserialiseItem(void *data, uint32_t *size);
};
/*!
* to serialise rsQblogProfile items, method names are self explanatory
*/
class RsQblogProfileSerialiser : public RsSerialType
{
public:
RsQblogProfileSerialiser()
:RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_QBLOG)
{ return; }
virtual ~RsQblogProfileSerialiser()
{ return; }
/**
* check size of RsItem to be serialised
@ -81,17 +150,29 @@ virtual ~RsQblogSerialiser()
* @return size of the RsItem
*/
virtual uint32_t size(RsItem *);
/**
* serialise contents of item to data
* @param item RsItem which is going to be serilised
* @param data where contents will be serialised into
* @return size of the RsItem in bytes
*/
virtual bool serialise (RsItem *item, void *data, uint32_t *size);
/**
* serialise contents of item to data
* @param data where contents will be deserialisedout of
* @return size of the RsItem in bytes
*/
virtual RsItem * deserialise(void *data, uint32_t *size);
private:
virtual uint32_t sizeItem(RsQblogItem *);
virtual bool serialiseItem (RsQblogItem *item, void *data, uint32_t *size);
virtual RsQblogItem *deserialiseItem(void *data, uint32_t *size);
virtual uint32_t sizeItem(RsQblogProfile *);
virtual bool serialiseItem (RsQblogProfile *item, void *data, uint32_t *size);
virtual RsQblogProfile *deserialiseItem(void *data, uint32_t *size);
};
#endif /*RSQBLOGITEM_H_*/

View File

@ -43,8 +43,8 @@ p3Qblog::p3Qblog(p3ConnectMgr *connMgr,
uint32_t storePeriod)
:CacheSource(type, true, cs, sourcedir),
CacheStore(type, true, cs, cft, storedir),
p3Config(CONFIG_TYPE_QBLOG), mConnMgr(connMgr), mFilterSwitch(false),
mStorePeriod(storePeriod), mUpdated(true)
mConnMgr(connMgr), mFilterSwitch(false),
mStorePeriod(storePeriod), mPostsUpdated(false), mProfileUpdated(false)
{
{
RsStackMutex stack(mBlogMtx);
@ -61,6 +61,14 @@ p3Qblog::p3Qblog(p3ConnectMgr *connMgr,
return;
}
std::ostream &operator<<(std::ostream& out, const std::wstring wstr)
{
std::string str(wstr.begin(), wstr.end());
out << str;
return out;
}
bool p3Qblog::loadLocalCache(const CacheData &data)
{
std::string filename = data.path + '/' + data.name;
@ -84,7 +92,7 @@ bool p3Qblog::loadLocalCache(const CacheData &data)
{
RsStackMutex stack(mBlogMtx);
mRepost = false; // there is nothing to tick/repost (i.e. comes from cache)
mPostsUpdated = true; // there is nothing to tick/repost (i.e. comes from cache)
}
if (data.size > 0) /* don't refresh zero sized caches */
@ -129,7 +137,7 @@ bool p3Qblog::loadBlogFile(std::string filename, std::string src)
{
/* create the serialiser to load info */
RsSerialiser *rsSerialiser = new RsSerialiser();
rsSerialiser->addSerialType(new RsQblogSerialiser());
rsSerialiser->addSerialType(new RsQblogMsgSerialiser());
uint32_t bioflags = BIN_FLAGS_HASH_DATA | BIN_FLAGS_READABLE;
BinInterface *bio = new BinFileInterface(filename.c_str(), bioflags);
@ -148,7 +156,7 @@ bool p3Qblog::loadBlogFile(std::string filename, std::string src)
/* will load file info to these items */
RsItem *item;
RsQblogItem *newBlog;
RsQblogMsg *newBlog;
stream->tick(); // tick to read
@ -170,7 +178,7 @@ bool p3Qblog::loadBlogFile(std::string filename, std::string src)
std::cerr << std::endl;
#endif
if (NULL == (newBlog = dynamic_cast<RsQblogItem *>(item)))
if (NULL == (newBlog = dynamic_cast<RsQblogMsg *>(item)))
{
#ifdef QBLOG_DEBUG
std::cerr << "p3Qblog::loadBlogFile() Item not Blog (deleting):";
@ -180,8 +188,8 @@ bool p3Qblog::loadBlogFile(std::string filename, std::string src)
delete item;
}
/* check timestamp */
else if (((time_t) newBlog->blogMsg.first < min) ||
((time_t) newBlog->blogMsg.first > max))
else if (((time_t) newBlog->timeStamp < min) ||
((time_t) newBlog->timeStamp > max))
{
#ifdef QBLOG_DEBUG
std::cerr << "p3Qblog::loadBlogFile() Outside TimeRange (deleting):";
@ -207,7 +215,7 @@ bool p3Qblog::loadBlogFile(std::string filename, std::string src)
return true;
}
bool p3Qblog::addBlog(RsQblogItem *newBlog)
bool p3Qblog::addBlog(RsQblogMsg *newBlog)
{
#ifdef QBLOG_DEBUG
std::cerr << "p3Ranking::addBlog() Item:";
@ -219,26 +227,19 @@ bool p3Qblog::addBlog(RsQblogItem *newBlog)
{
RsStackMutex Stack(mBlogMtx);
mPeerSongSet[newBlog->pid] = newBlog->favSong;
mPeerStatusSet[newBlog->pid] = newBlog->status;
mUsrBlogSet[newBlog->pid].insert(newBlog->blogMsg);
mUsrBlogSet[newBlog->PeerId()].insert(std::make_pair(newBlog->timeStamp, newBlog->blogMsg));
#ifdef QBLOG_DEBUG
std::cerr << "p3Qblog::addBlog()";
std::cerr << std::endl;
std::cerr << "pid" << newBlog->pid;
std::cerr << "\tpeerId" << newBlog->PeerId();
std::cerr << std::endl;
std::cerr << "\tmPeerSongset" << mPeerSongSet[newBlog->pid];
std::cerr << "\tmUsrBlogSet: time" << newBlog->timeStamp;
std::cerr << std::endl;
std::cerr << "\tmPeerStatusSet" << mPeerStatusSet[newBlog->pid];
std::cerr << std::endl;
std::cerr << "\tmUsrBlogSet: time" << newBlog->blogMsg.first;
std::cerr << std::endl;
std::cerr << "\tmUsrBlogSet: blog" << newBlog->blogMsg.second;
std::cerr << "\tmUsrBlogSet: blog" << newBlog->blogMsg;
std::cerr << std::endl;
#endif
mUpdated = true; // might be useless
mRepost = true;
mPostsUpdated = false; // need to figure how this should work/ wherre this should be placed
}
return true;
@ -268,7 +269,7 @@ bool p3Qblog::postBlogs(void)
#endif
RsSerialiser *rsSerialiser = new RsSerialiser();
rsSerialiser->addSerialType(new RsQblogSerialiser());
rsSerialiser->addSerialType(new RsQblogMsgSerialiser());
uint32_t bioflags = BIN_FLAGS_HASH_DATA | BIN_FLAGS_WRITEABLE;
BinInterface *bio = new BinFileInterface(fname.c_str(), bioflags);
@ -279,12 +280,12 @@ bool p3Qblog::postBlogs(void)
RsStackMutex stack(mBlogMtx); /********** STACK LOCKED MTX ******/
/* iterate through list */
std::list<RsQblogItem*>::iterator it;
std::list<RsQblogMsg*>::iterator it;
for(it = mBlogItems.begin(); it != mBlogItems.end(); it++)
for(it = mBlogs.begin(); it != mBlogs.end(); it++)
{
/* only post own blogs */
if(mOwnId == (*it)->pid)
if(mOwnId == (*it)->PeerId())
{
/*write to serialiser*/
RsItem *item = *it;
@ -362,22 +363,6 @@ p3Qblog::~p3Qblog()
return;
}
bool p3Qblog::setStatus(const std::string &status)
{
{
RsStackMutex stack(mBlogMtx);
mPeerStatusSet[mOwnId] = status;
#ifdef QBLOG_DEBUG
std::cerr << "p3Qblog::getStatus() mPeerStatus[mOwnId]" << mPeerStatusSet[mOwnId];
std::cerr << std::endl;
#endif
}
return true;
}
bool p3Qblog::getFilterSwitch(void)
{
{
@ -400,26 +385,6 @@ bool p3Qblog::setFilterSwitch(bool &filterSwitch)
return true;
}
bool p3Qblog::getStatus(std::map<std::string, std::string> &usrStatus)
{
{
RsStackMutex stack(mBlogMtx);
#ifdef QBLOG_DEBUG
if(mPeerStatusSet.empty())
{
std::cerr << "p3Qblog::getStatus() mPeerStatusSet empty! ";
std::cerr << std::endl;
return false;
}
#endif
usrStatus = mPeerStatusSet;
}
return true;
}
bool p3Qblog::removeFiltFriend(std::string &usrId)
{
std::list<std::string>::iterator it;
@ -475,7 +440,7 @@ bool p3Qblog::addToFilter(std::string& usrId)
return true;
}
bool p3Qblog::getBlogs(std::map< std::string, std::multimap<long int, std:: string> > &blogs)
bool p3Qblog::getBlogs(std::map< std::string, std::multimap<long int, std::wstring> > &blogs)
{
{
RsStackMutex stack(mBlogMtx);
@ -496,7 +461,7 @@ bool p3Qblog::getBlogs(std::map< std::string, std::multimap<long int, std:: stri
}
}
bool p3Qblog::sendBlog(const std::string &msg)
bool p3Qblog::sendBlog(const std::wstring &msg)
{
time_t blogTimeStamp;
@ -504,67 +469,64 @@ bool p3Qblog::sendBlog(const std::string &msg)
RsStackMutex stack(mBlogMtx);
mUsrBlogSet[mOwnId].insert(std::make_pair(blogTimeStamp, msg));
RsQblogItem *blogItem = new RsQblogItem();
blogItem->clear();
RsQblogMsg *blog = new RsQblogMsg();
blog->clear();
blog->timeStamp = blogTimeStamp;
blog->blogMsg = msg;
blogItem->pid = mOwnId;
blogItem->blogMsg.first = blogTimeStamp;
blogItem->blogMsg.second = msg;
blogItem->favSong = mPeerSongSet[mOwnId];
blogItem->status = mPeerStatusSet[mOwnId];
#ifdef QBLOG_DEBUG
std::cerr << "p3Qblog::sendBlogFile()";
std::cerr << std::endl;
std::cerr << "\tblogItem->pid" << blogItem->blogMsg.first;
std::cerr << "\tblogItem->timeStamp" << blog->timeStamp;
std::cerr << std::endl;
std::cerr << "\tblogItem->blogMsg.first" << blogItem->blogMsg.first;
std::cerr << std::endl;
std::cerr << "\tblogItem->blogMsg.second" << blogItem->blogMsg.second;
std::cerr << std::endl;
std::cerr << "\tblogItem->favSong" << blogItem->favSong;
std::cerr << std::endl;
std::cerr << "\tblogItem->status" << blogItem->status;
std::cerr << "\tblogItem->blogMsg.second" << blog->blogMsg;
std::cerr << std::endl;
#endif
mBlogItems.push_back(blogItem);
mBlogs.push_back(blog);
}
return true;
}
bool p3Qblog::getProfile(std::map<std::string, std::string> &profile)
bool p3Qblog::getPeerProfile(std::string id, std::list< std::pair<std::wstring, std::wstring> > &entries)
{
{
RsStackMutex stack(mBlogMtx);
/* return error is set empty */
if(mPeerSongSet.empty())
{
std::cerr << "friend song set empty!" << std::endl;
return false;
}
profile = mPeerSongSet;
}
// TODO
return true;
}
bool p3Qblog::setProfile(const std::string &favSong)
bool p3Qblog::setProfile(std::pair<std::wstring, std::wstring> entry)
{
{
RsStackMutex stack(mBlogMtx);
mPeerSongSet[mOwnId] = favSong;
}
//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;
std::wstring cnv_wstr;
mConnMgr->getFriendList(peers); // retrieve peers list from core
if(peers.empty())
{
@ -575,10 +537,10 @@ void p3Qblog::loadDummy(void)
srand(60);
long int now = time(NULL); // the present time
std::multimap<long int, std::string> emptySet; // time/blog map
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"};
//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 */
std::string B1 = "I think we should eat more cheese";
@ -588,34 +550,38 @@ void p3Qblog::loadDummy(void)
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;
mUsrBlogSet.insert(std::make_pair(mOwnId, emptySet));
mPeerSongSet.insert(std::make_pair(mOwnId, songs[rand() % 5]));
mPeerStatusSet.insert(std::make_pair(mOwnId, statusSet[rand() % 5]));
mUsrBlogSet.insert(std::make_pair(mOwnId, emptySet));
for(it = peers.begin(); it!=peers.end();it++)
{
mUsrBlogSet.insert(std::make_pair(*it, emptySet));
mPeerSongSet.insert(std::make_pair(*it, songs[rand() % 5]));
mPeerStatusSet.insert(std::make_pair(*it, statusSet[rand() % 5]));
}
/* now fill up blog map */
for(int i=0; i < 50 ; i++)
{
std::list<std::string>::iterator it = peers.begin();
long int timeStamp;
timeStamp = now + rand() % 2134223;
mUsrBlogSet[mOwnId].insert(std::make_pair(timeStamp, blogs[rand() % 5]));
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
mUsrBlogSet[*it].insert(std::make_pair(timeStamp, blogs[rand() % 5]));
int c = rand() % 5;
cnv_wstr.assign(blogs[c].begin(), blogs[c].end());
mUsrBlogSet[*it].insert(std::make_pair(timeStamp, cnv_wstr)); // store a random blog
}
}
@ -623,94 +589,6 @@ void p3Qblog::loadDummy(void)
}
RsSerialiser* p3Qblog::setupSerialiser()
{
RsSerialiser *rss = new RsSerialiser();
rss->addSerialType(new RsQblogSerialiser()); // add in the types we need!
return rss;
}
std::list<RsItem* > p3Qblog::saveList(bool &cleanup)
{
std::list<RsItem* > saveData;
mBlogMtx.lock(); // LOCK
cleanup = false;
/* create save data */
std::list<RsQblogItem*>::iterator it;
for(it = mBlogItems.begin(); it != mBlogItems.end() ; it++)
saveData.push_back(*it);
/*save data created */
return saveData;
}
void p3Qblog::saveDone()
{
mBlogMtx.unlock(); // UNLOCK
return;
}
bool p3Qblog::loadList(std::list<RsItem* > load)
{
std::list<RsItem*>::iterator it;
RsQblogItem *blog;
#ifdef SERVER_DEBUG
std::cerr << "p3Qblog::loadList() Item Count: " << load.size();
std::cerr << std::endl;
#endif
time_t now = time(NULL);
time_t min, max;
{ RsStackMutex stack(mBlogMtx); /********** 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 != (blog = dynamic_cast<RsQblogItem *>(*it)))
{
/* check date -> if old expire */
if (((time_t) blog->blogMsg.first < min) ||
((time_t) blog->blogMsg.first > max))
{
#ifdef QBLOG_DEBUG
std::cerr << "p3Blog::loadList() Outside TimeRange :";
std::cerr << std::endl;
#endif
/* if outside range -> remove */
delete blog;
continue;
}
#ifdef QBLOG_DEBUG
std::cerr << "p3Ranking::loadList() Anon TimeRange ok";
std::cerr << std::endl;
#endif
/* make a copy to add into standard map */
addBlog(blog);
}
else
{
/* cleanup */
delete (*it);
}
}
return true;
}
bool p3Qblog::sort(void)
{
@ -720,14 +598,14 @@ bool p3Qblog::sort(void)
void p3Qblog::tick()
{
bool repost = false; // so stack mutex is not enabled during postblog call
bool postUpdated = false; // so stack mutex is not enabled during postblog call
{
RsStackMutex stack(mBlogMtx);
repost = mRepost;
postUpdated = mPostsUpdated;
}
if(repost) //
if(!postUpdated)
{
if(!postBlogs())
std::cerr << "p3Qblog::tick():" << "tick failed!";

View File

@ -32,8 +32,6 @@
#include <list>
#include <map>
#include "rsiface/rsQblog.h"
#include "dbase/cachestrapper.h"
#include "pqi/pqiservice.h"
#include "pqi/pqistreamer.h"
@ -41,15 +39,22 @@
#include "pqi/p3cfgmgr.h"
#include "serialiser/rsserial.h"
#include "rsiface/rstypes.h"
class RsQblogItem; /* to populate maps of blogs */
class RsQblogMsg; /* to populate maps of blogs */
class RsQblogProfile; /* to populate profile information */
/*!
* contains definitions of the interface and blog information to be manipulated
* See RsQblog class for virtual methods documentation
*/
class p3Qblog : public CacheSource, public CacheStore, public p3Config
class p3Qblog : public CacheSource, public CacheStore
{
/**
* overloads extractor to make printing wstrings easier
*/
friend std::ostream &operator<<(std::ostream &out, const std::wstring);
public:
p3Qblog(p3ConnectMgr *connMgr,
@ -72,21 +77,32 @@ class RsQblogItem; /* to populate maps of blogs */
public:
virtual bool setStatus(const std::string &status);
virtual bool getStatus(std::map<std::string, std::string> &usrStatus);
virtual bool setFilterSwitch(bool &filterSwitch);
virtual bool getFilterSwitch(void);
virtual bool addToFilter(std::string &usrId);
virtual bool removeFiltFriend(std::string &usrId);
virtual bool getProfile(std::map<std::string, std::string> &profile);
virtual bool setProfile(const std::string &favSong);
virtual bool sendBlog(const std::string &msg);
virtual bool getBlogs(std::map< std::string, std::multimap<long int, std:: string> > &blogs);
virtual bool sendBlog(const std::wstring &msg);
virtual bool getBlogs(std::map< std::string, std::multimap<long int, std::wstring> > &blogs);
virtual bool getPeerLatestBlog(std::string id, uint32_t &ts, std::wstring &post);
virtual bool getPeerProfile(std::string id, std::list< std::pair<std::wstring, std::wstring> > &entries);
virtual bool getPeerFavourites(std::string id, std::list<FileInfo> &favs);
virtual bool setFavorites(FileInfo favFile);
virtual bool setProfile(std::pair<std::wstring, std::wstring> entry);
/*
/**
* to be run by server, update method
*/
void tick();
private:
/********************* begining of private utility methods **************************/
/*
* to load and transform cache source to normal attribute format of a blog message
* @param filename ?? TODO
* @param source ?? TODO
* @param filename
* @param source
*/
bool loadBlogFile(std::string filename, std::string src);
@ -94,40 +110,20 @@ class RsQblogItem; /* to populate maps of blogs */
* add a blog item to maps
* @param newBlog a blog item from a peer or yourself
*/
bool addBlog(RsQblogItem *newBlog);
bool addBlog(RsQblogMsg *newBlog);
/*
* post our blog to our friends, connectivity function
* post our blog to our friends, connectivity method
*/
bool postBlogs(void);
/*
* sort blog maps in time order
* sort usr/blog maps in time order
*/
bool sort(void);
/**
* updates attributes of connected peers
*/
void tick();
// ??
pqistreamer *createStreamer(std::string file, std::string src, bool reading);
protected:
/// p3Config STUFF
virtual RsSerialiser *setupSerialiser();
/// p3Config STUFF
virtual std::list<RsItem *> saveList(bool &cleanup);
/// p3Config STUFF
virtual bool loadList(std::list<RsItem *> load);
/// p3Config STUFF
virtual void saveDone(void);
private:
/************************* end of private methods **************************/
/// handles connection to peers
p3ConnectMgr *mConnMgr;
@ -135,32 +131,28 @@ class RsQblogItem; /* to populate maps of blogs */
RsMutex mBlogMtx;
/// the current usr
std::string mOwnId;
/// contains the list of ids usr only wants to see
std::list<std::string> mFilterList;
/// determines whether filter is activated or not
bool mFilterSwitch;
/// usr and fav song
std::map<std::string, std::string> mPeerSongSet;
/// usr and current status
std::map<std::string, std::string> mPeerStatusSet;
/// contain usr and their blogs
std::map< std::string, std::multimap<long int, std:: string> > mUsrBlogSet;
/// contain usrs and their blogs
std::map< std::string, std::multimap<long int, std::wstring> > mUsrBlogSet;
///fills up above sets
std::list<RsQblogItem*> mBlogItems;
std::list<RsQblogMsg*> mBlogs;
///profile information
std::map<std::string, RsQblogProfile > mProfiles;
///how long to keep posts
uint32_t mStorePeriod;
/// to track updates
bool mUpdated; //possibly useless in light of bottom variable
/// to see whether to update
bool mRepost;
/// to track blog updates
bool mPostsUpdated;
/// to track profile updates
bool mProfileUpdated;
/*
* load dummy data
*/
void loadDummy(void);
};
#endif /*P3QBLOG_H_*/

View File

@ -146,6 +146,7 @@ void createDummyData();
std::string mOwnId;
bool mUpdated;
bool mRepost;
std::map<std::string, RankGroup> mData;
std::multimap<float, std::string> mRankings;