2012-08-06 17:00:38 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* libretroshare/src/gxs: rsgenexchange.cc
|
|
|
|
*
|
|
|
|
* RetroShare Gxs exchange interface.
|
|
|
|
*
|
|
|
|
* Copyright 2012-2012 by Christopher Evi-Parker, 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".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-05-21 18:07:43 -04:00
|
|
|
#include "rsgenexchange.h"
|
|
|
|
|
2012-07-05 17:26:14 -04:00
|
|
|
RsGenExchange::RsGenExchange(RsGeneralDataService *gds,
|
2012-07-26 17:48:54 -04:00
|
|
|
RsNetworkExchangeService *ns, RsSerialType *serviceSerialiser, uint16_t servType)
|
|
|
|
: mGenMtx("GenExchange"), mDataStore(gds), mNetService(ns), mSerialiser(serviceSerialiser), mServType(servType)
|
2012-05-21 18:07:43 -04:00
|
|
|
{
|
2012-07-05 17:26:14 -04:00
|
|
|
|
2012-07-18 17:35:41 -04:00
|
|
|
mDataAccess = new RsGxsDataAccess(gds);
|
2012-07-05 17:26:14 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
RsGenExchange::~RsGenExchange()
|
|
|
|
{
|
2012-07-26 17:48:54 -04:00
|
|
|
// need to destruct in a certain order (prob a bad thing!)
|
2012-07-05 17:26:14 -04:00
|
|
|
delete mNetService;
|
|
|
|
|
|
|
|
delete mDataAccess;
|
|
|
|
mDataAccess = NULL;
|
|
|
|
|
|
|
|
delete mDataStore;
|
|
|
|
mDataStore = NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RsGenExchange::tick()
|
|
|
|
{
|
2012-07-18 17:35:41 -04:00
|
|
|
mDataAccess->processRequests();
|
2012-07-26 17:48:54 -04:00
|
|
|
|
|
|
|
publishGrps();
|
|
|
|
|
|
|
|
publishMsgs();
|
|
|
|
|
2012-08-06 17:00:38 -04:00
|
|
|
notifyChanges(mNotifications);
|
|
|
|
mNotifications.clear();
|
|
|
|
|
2012-07-05 17:26:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-22 17:38:15 -04:00
|
|
|
bool RsGenExchange::getGroupList(const uint32_t &token, std::list<RsGxsGroupId> &groupIds)
|
2012-07-05 17:26:14 -04:00
|
|
|
{
|
2012-07-22 17:38:15 -04:00
|
|
|
return mDataAccess->getGroupList(token, groupIds);
|
2012-07-26 17:48:54 -04:00
|
|
|
|
2012-07-05 17:26:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RsGenExchange::getMsgList(const uint32_t &token,
|
2012-07-22 17:38:15 -04:00
|
|
|
GxsMsgIdResult &msgIds)
|
2012-07-05 17:26:14 -04:00
|
|
|
{
|
2012-07-22 17:38:15 -04:00
|
|
|
return mDataAccess->getMsgList(token, msgIds);
|
2012-07-05 17:26:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RsGenExchange::getGroupMeta(const uint32_t &token, std::list<RsGroupMetaData> &groupInfo)
|
|
|
|
{
|
2012-07-22 17:38:15 -04:00
|
|
|
std::list<RsGxsGrpMetaData*> metaL;
|
|
|
|
bool ok = mDataAccess->getGroupSummary(token, metaL);
|
|
|
|
|
2012-07-26 17:48:54 -04:00
|
|
|
std::list<RsGxsGrpMetaData*>::iterator lit = metaL.begin();
|
2012-07-28 16:01:30 -04:00
|
|
|
RsGroupMetaData m;
|
2012-07-26 17:48:54 -04:00
|
|
|
for(; lit != metaL.end(); lit++)
|
|
|
|
{
|
2012-07-28 16:01:30 -04:00
|
|
|
RsGxsGrpMetaData& gMeta = *(*lit);
|
|
|
|
m = gMeta;
|
2012-07-26 17:48:54 -04:00
|
|
|
groupInfo.push_back(m);
|
2012-07-28 16:01:30 -04:00
|
|
|
delete (*lit);
|
2012-07-26 17:48:54 -04:00
|
|
|
}
|
|
|
|
|
2012-07-22 17:38:15 -04:00
|
|
|
return ok;
|
2012-07-05 17:26:14 -04:00
|
|
|
}
|
|
|
|
|
2012-07-22 17:38:15 -04:00
|
|
|
|
2012-07-05 17:26:14 -04:00
|
|
|
bool RsGenExchange::getMsgMeta(const uint32_t &token,
|
2012-07-22 17:38:15 -04:00
|
|
|
GxsMsgMetaMap &msgInfo)
|
2012-07-05 17:26:14 -04:00
|
|
|
{
|
2012-07-22 17:38:15 -04:00
|
|
|
std::list<RsGxsMsgMetaData*> metaL;
|
|
|
|
GxsMsgMetaResult result;
|
|
|
|
bool ok = mDataAccess->getMsgSummary(token, result);
|
|
|
|
|
|
|
|
GxsMsgMetaResult::iterator mit = result.begin();
|
|
|
|
|
|
|
|
for(; mit != result.end(); mit++)
|
|
|
|
{
|
|
|
|
std::vector<RsGxsMsgMetaData*>& metaV = mit->second;
|
|
|
|
|
2012-08-06 17:00:38 -04:00
|
|
|
msgInfo[mit->first] = std::vector<RsMsgMetaData>();
|
|
|
|
std::vector<RsMsgMetaData>& msgInfoV = msgInfo[mit->first];
|
2012-07-05 17:26:14 -04:00
|
|
|
|
2012-08-06 17:00:38 -04:00
|
|
|
std::vector<RsGxsMsgMetaData*>::iterator vit = metaV.begin();
|
|
|
|
RsMsgMetaData meta;
|
2012-07-22 17:38:15 -04:00
|
|
|
for(; vit != metaV.end(); vit++)
|
|
|
|
{
|
2012-08-06 17:00:38 -04:00
|
|
|
RsGxsMsgMetaData& m = *(*vit);
|
|
|
|
meta = m;
|
|
|
|
msgInfoV.push_back(meta);
|
2012-07-22 17:38:15 -04:00
|
|
|
delete *vit;
|
|
|
|
}
|
2012-08-06 17:00:38 -04:00
|
|
|
metaV.clear();
|
2012-07-22 17:38:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
2012-07-05 17:26:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RsGenExchange::getGroupData(const uint32_t &token, std::vector<RsGxsGrpItem *> grpItem)
|
|
|
|
{
|
2012-07-22 17:38:15 -04:00
|
|
|
|
|
|
|
std::list<RsNxsGrp*> nxsGrps;
|
|
|
|
bool ok = mDataAccess->getGroupData(token, nxsGrps);
|
|
|
|
|
|
|
|
std::list<RsNxsGrp*>::iterator lit = nxsGrps.begin();
|
|
|
|
|
|
|
|
if(ok)
|
|
|
|
{
|
|
|
|
for(; lit != nxsGrps.end(); lit++)
|
|
|
|
{
|
2012-07-26 17:48:54 -04:00
|
|
|
RsTlvBinaryData& data = (*lit)->grp;
|
2012-07-22 17:38:15 -04:00
|
|
|
RsItem* item = mSerialiser->deserialise(data.bin_data, &data.bin_len);
|
2012-08-06 17:00:38 -04:00
|
|
|
|
|
|
|
if(item != NULL){
|
|
|
|
RsGxsGrpItem* gItem = dynamic_cast<RsGxsGrpItem*>(item);
|
|
|
|
gItem->meta = *((*lit)->metaData);
|
|
|
|
grpItem.push_back(gItem);
|
|
|
|
delete *lit;
|
|
|
|
}
|
2012-07-22 17:38:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ok;
|
2012-07-05 17:26:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RsGenExchange::getMsgData(const uint32_t &token,
|
2012-07-22 17:38:15 -04:00
|
|
|
GxsMsgDataMap &msgItems)
|
2012-07-05 17:26:14 -04:00
|
|
|
{
|
2012-07-22 17:38:15 -04:00
|
|
|
NxsMsgDataResult msgResult;
|
|
|
|
bool ok = mDataAccess->getMsgData(token, msgResult);
|
|
|
|
NxsMsgDataResult::iterator mit = msgResult.begin();
|
|
|
|
|
|
|
|
if(ok)
|
|
|
|
{
|
|
|
|
for(; mit != msgResult.end(); mit++)
|
|
|
|
{
|
|
|
|
std::vector<RsGxsMsgItem*> gxsMsgItems;
|
2012-07-26 17:48:54 -04:00
|
|
|
const RsGxsGroupId& grpId = mit->first;
|
2012-07-22 17:38:15 -04:00
|
|
|
std::vector<RsNxsMsg*>& nxsMsgsV = mit->second;
|
|
|
|
std::vector<RsNxsMsg*>::iterator vit
|
|
|
|
= nxsMsgsV.begin();
|
|
|
|
for(; vit != nxsMsgsV.end(); vit++)
|
|
|
|
{
|
|
|
|
RsNxsMsg*& msg = *vit;
|
|
|
|
|
|
|
|
RsItem* item = mSerialiser->deserialise(msg->msg.bin_data,
|
|
|
|
&msg->msg.bin_len);
|
|
|
|
RsGxsMsgItem* mItem = dynamic_cast<RsGxsMsgItem*>(item);
|
2012-08-06 17:00:38 -04:00
|
|
|
mItem->meta = *((*vit)->metaData); // get meta info from nxs msg
|
2012-07-22 17:38:15 -04:00
|
|
|
gxsMsgItems.push_back(mItem);
|
|
|
|
delete msg;
|
|
|
|
}
|
|
|
|
msgItems[grpId] = gxsMsgItems;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ok;
|
2012-07-05 17:26:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-28 16:01:30 -04:00
|
|
|
RsTokenServiceV2* RsGenExchange::getTokenService()
|
2012-07-05 17:26:14 -04:00
|
|
|
{
|
|
|
|
return mDataAccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RsGenExchange::notifyNewGroups(std::vector<RsNxsGrp *> &groups)
|
|
|
|
{
|
|
|
|
std::vector<RsNxsGrp*>::iterator vit = groups.begin();
|
|
|
|
|
|
|
|
// store these for tick() to pick them up
|
|
|
|
for(; vit != groups.end(); vit++)
|
|
|
|
mReceivedGrps.push_back(*vit);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-08-06 17:00:38 -04:00
|
|
|
void RsGenExchange::notifyNewMessages(std::vector<RsNxsMsg *>& messages)
|
2012-07-05 17:26:14 -04:00
|
|
|
{
|
|
|
|
std::vector<RsNxsMsg*>::iterator vit = messages.begin();
|
|
|
|
|
|
|
|
// store these for tick() to pick them up
|
|
|
|
for(; vit != messages.end(); vit++)
|
|
|
|
mReceivedMsgs.push_back(*vit);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool RsGenExchange::publishGroup(RsGxsGrpItem *grpItem)
|
|
|
|
{
|
|
|
|
|
2012-07-26 17:48:54 -04:00
|
|
|
RsStackMutex stack(mGenMtx);
|
|
|
|
mGrpsToPublish.push_back(grpItem);
|
|
|
|
|
|
|
|
return true;
|
2012-07-05 17:26:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RsGenExchange::publishMsg(RsGxsMsgItem *msgItem)
|
|
|
|
{
|
2012-07-26 17:48:54 -04:00
|
|
|
|
|
|
|
RsStackMutex stack(mGenMtx);
|
|
|
|
|
|
|
|
mMsgsToPublish.push_back(msgItem);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RsGenExchange::publishMsgs()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mGenMtx);
|
|
|
|
|
|
|
|
std::vector<RsGxsMsgItem*>::iterator vit = mMsgsToPublish.begin();
|
|
|
|
|
|
|
|
for(; vit != mMsgsToPublish.end(); )
|
|
|
|
{
|
|
|
|
|
|
|
|
RsNxsMsg* msg = new RsNxsMsg(mServType);
|
|
|
|
RsGxsMsgItem* msgItem = *vit;
|
|
|
|
uint32_t size = mSerialiser->size(msgItem);
|
|
|
|
char mData[size];
|
|
|
|
bool ok = mSerialiser->serialise(msgItem, mData, &size);
|
|
|
|
|
|
|
|
if(ok)
|
|
|
|
{
|
|
|
|
msg->metaData = new RsGxsMsgMetaData();
|
2012-08-06 17:00:38 -04:00
|
|
|
*(msg->metaData) = msgItem->meta;
|
2012-07-26 17:48:54 -04:00
|
|
|
ok = mDataAccess->addMsgData(msg);
|
|
|
|
|
|
|
|
if(ok)
|
|
|
|
{
|
|
|
|
RsGxsMsgChange* mc = new RsGxsMsgChange();
|
|
|
|
mNotifications.push_back(mc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-06 17:00:38 -04:00
|
|
|
// if addition failed then delete nxs message
|
2012-07-26 17:48:54 -04:00
|
|
|
if(!ok)
|
|
|
|
{
|
|
|
|
#ifdef GEN_EXCH_DEBUG
|
2012-08-06 17:00:38 -04:00
|
|
|
std::cerr << "RsGenExchange::publishMsgs() failed to serialise msg " << std::endl;
|
2012-07-26 17:48:54 -04:00
|
|
|
#endif
|
|
|
|
delete msg;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-08-06 17:00:38 -04:00
|
|
|
delete msgItem; // delete msg item as we're done with it
|
2012-07-26 17:48:54 -04:00
|
|
|
}
|
2012-08-06 17:00:38 -04:00
|
|
|
|
|
|
|
// clear msg list as we're done publishing them and entries
|
|
|
|
// are invalid
|
|
|
|
mMsgsToPublish.clear();
|
2012-07-26 17:48:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void RsGenExchange::publishGrps()
|
|
|
|
{
|
|
|
|
|
|
|
|
RsStackMutex stack(mGenMtx);
|
|
|
|
|
|
|
|
std::vector<RsGxsGrpItem*>::iterator vit = mGrpsToPublish.begin();
|
|
|
|
|
|
|
|
for(; vit != mGrpsToPublish.end();)
|
|
|
|
{
|
|
|
|
|
|
|
|
RsNxsGrp* grp = new RsNxsGrp(mServType);
|
|
|
|
RsGxsGrpItem* grpItem = *vit;
|
|
|
|
uint32_t size = mSerialiser->size(grpItem);
|
|
|
|
|
|
|
|
char gData[size];
|
|
|
|
bool ok = mSerialiser->serialise(grpItem, gData, &size);
|
|
|
|
|
|
|
|
if(ok)
|
|
|
|
{
|
|
|
|
grp->metaData = new RsGxsGrpMetaData();
|
2012-08-06 17:00:38 -04:00
|
|
|
*(grp->metaData) = grpItem->meta;
|
2012-07-26 17:48:54 -04:00
|
|
|
ok = mDataAccess->addGroupData(grp);
|
2012-07-28 16:01:30 -04:00
|
|
|
RsGxsGroupChange* gc = new RsGxsGroupChange();
|
2012-07-26 17:48:54 -04:00
|
|
|
mNotifications.push_back(gc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!ok)
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef GEN_EXCH_DEBUG
|
|
|
|
std::cerr << "RsGenExchange::publishGrps() failed to publish grp " << std::endl;
|
|
|
|
#endif
|
|
|
|
delete grp;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete grpItem;
|
|
|
|
|
|
|
|
vit = mGrpsToPublish.erase(vit);
|
|
|
|
}
|
|
|
|
|
2012-08-06 17:00:38 -04:00
|
|
|
// clear grp list as we're done publishing them and entries
|
|
|
|
// are invalid
|
|
|
|
mGrpsToPublish.clear();
|
|
|
|
}
|
|
|
|
void RsGenExchange::processRecvdData()
|
|
|
|
{
|
2012-05-21 18:07:43 -04:00
|
|
|
}
|
2012-07-26 17:48:54 -04:00
|
|
|
|
|
|
|
|
2012-08-06 17:00:38 -04:00
|
|
|
void RsGenExchange::processRecvdMessages()
|
|
|
|
{
|
|
|
|
}
|
2012-07-26 17:48:54 -04:00
|
|
|
|
|
|
|
|
2012-08-06 17:00:38 -04:00
|
|
|
void RsGenExchange::processRecvdGroups()
|
|
|
|
{
|
|
|
|
}
|
2012-07-26 17:48:54 -04:00
|
|
|
|