Adding Basics of GxsChannel Service into libretroshare.

This includes a generic CommentService, which will be used by other Services.
+ Bugfix, gxs service threads were started before global variables were set.



git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@6186 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2013-03-04 20:26:48 +00:00
parent 99c5633a63
commit 22782e5edd
17 changed files with 2326 additions and 66 deletions

View file

@ -0,0 +1,405 @@
/*
* libretroshare/src/serialiser: rsgxschannelitems.cc
*
* RetroShare C++ Interface.
*
* Copyright 2012-2012 by Robert Fernie
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2.1 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".
*
*/
#include <iostream>
#include "rsgxschannelitems.h"
#include "serialiser/rstlvbase.h"
#include "serialiser/rsbaseserial.h"
#define GXSCHANNEL_DEBUG 1
uint32_t RsGxsChannelSerialiser::size(RsItem *item)
{
RsGxsChannelGroupItem* grp_item = NULL;
RsGxsChannelPostItem* op_item = NULL;
if((grp_item = dynamic_cast<RsGxsChannelGroupItem*>(item)) != NULL)
{
return sizeGxsChannelGroupItem(grp_item);
}
else if((op_item = dynamic_cast<RsGxsChannelPostItem*>(item)) != NULL)
{
return sizeGxsChannelPostItem(op_item);
}
else
{
RsGxsCommentSerialiser::size(item);
}
return 0;
}
bool RsGxsChannelSerialiser::serialise(RsItem *item, void *data, uint32_t *size)
{
RsGxsChannelGroupItem* grp_item = NULL;
RsGxsChannelPostItem* op_item = NULL;
if((grp_item = dynamic_cast<RsGxsChannelGroupItem*>(item)) != NULL)
{
return serialiseGxsChannelGroupItem(grp_item, data, size);
}
else if((op_item = dynamic_cast<RsGxsChannelPostItem*>(item)) != NULL)
{
return serialiseGxsChannelPostItem(op_item, data, size);
}
else
{
return RsGxsCommentSerialiser::serialise(item, data, size);
}
return false;
}
RsItem* RsGxsChannelSerialiser::deserialise(void* data, uint32_t* size)
{
#ifdef GXSCHANNEL_DEBUG
std::cerr << "RsGxsChannelSerialiser::deserialise()" << std::endl;
#endif
/* get the type and size */
uint32_t rstype = getRsItemId(data);
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(RS_SERVICE_GXSV1_TYPE_CHANNELS != getRsItemService(rstype)))
{
return NULL; /* wrong type */
}
switch(getRsItemSubType(rstype))
{
case RS_PKT_SUBTYPE_GXSCHANNEL_GROUP_ITEM:
return deserialiseGxsChannelGroupItem(data, size);
break;
case RS_PKT_SUBTYPE_GXSCHANNEL_POST_ITEM:
return deserialiseGxsChannelPostItem(data, size);
break;
default:
return RsGxsCommentSerialiser::deserialise(data, size);
break;
}
return NULL;
}
/*****************************************************************************************/
/*****************************************************************************************/
/*****************************************************************************************/
void RsGxsChannelGroupItem::clear()
{
mGroup.mDescription.clear();
}
std::ostream& RsGxsChannelGroupItem::print(std::ostream& out, uint16_t indent)
{
printRsItemBase(out, "RsGxsChannelGroupItem", indent);
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << "Description: " << mGroup.mDescription << std::endl;
printRsItemEnd(out ,"RsGxsChannelGroupItem", indent);
return out;
}
uint32_t RsGxsChannelSerialiser::sizeGxsChannelGroupItem(RsGxsChannelGroupItem *item)
{
const RsGxsChannelGroup& group = item->mGroup;
uint32_t s = 8; // header
s += GetTlvStringSize(group.mDescription);
return s;
}
bool RsGxsChannelSerialiser::serialiseGxsChannelGroupItem(RsGxsChannelGroupItem *item, void *data, uint32_t *size)
{
#ifdef GXSCHANNEL_DEBUG
std::cerr << "RsGxsChannelSerialiser::serialiseGxsChannelGroupItem()" << std::endl;
#endif
uint32_t tlvsize = sizeGxsChannelGroupItem(item);
uint32_t offset = 0;
if(*size < tlvsize)
{
#ifdef GXSCHANNEL_DEBUG
std::cerr << "RsGxsChannelSerialiser::serialiseGxsChannelGroupItem() Size too small" << std::endl;
#endif
return false;
}
*size = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
/* GxsChannelGroupItem */
ok &= SetTlvString(data, tlvsize, &offset, 1, item->mGroup.mDescription);
if(offset != tlvsize)
{
#ifdef GXSCHANNEL_DEBUG
std::cerr << "RsGxsChannelSerialiser::serialiseGxsChannelGroupItem() FAIL Size Error! " << std::endl;
#endif
ok = false;
}
#ifdef GXSCHANNEL_DEBUG
if (!ok)
{
std::cerr << "RsGxsChannelSerialiser::serialiseGxsChannelGroupItem() NOK" << std::endl;
}
#endif
return ok;
}
RsGxsChannelGroupItem* RsGxsChannelSerialiser::deserialiseGxsChannelGroupItem(void *data, uint32_t *size)
{
#ifdef GXSCHANNEL_DEBUG
std::cerr << "RsGxsChannelSerialiser::deserialiseGxsChannelGroupItem()" << std::endl;
#endif
/* 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_GXSV1_TYPE_CHANNELS != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_GXSCHANNEL_GROUP_ITEM != getRsItemSubType(rstype)))
{
#ifdef GXSCHANNEL_DEBUG
std::cerr << "RsGxsChannelSerialiser::deserialiseGxsChannelGroupItem() FAIL wrong type" << std::endl;
#endif
return NULL; /* wrong type */
}
if (*size < rssize) /* check size */
{
#ifdef GXSCHANNEL_DEBUG
std::cerr << "RsGxsChannelSerialiser::deserialiseGxsChannelGroupItem() FAIL wrong size" << std::endl;
#endif
return NULL; /* not enough data */
}
/* set the packet length */
*size = rssize;
bool ok = true;
RsGxsChannelGroupItem* item = new RsGxsChannelGroupItem();
/* skip the header */
offset += 8;
ok &= GetTlvString(data, rssize, &offset, 1, item->mGroup.mDescription);
if (offset != rssize)
{
#ifdef GXSCHANNEL_DEBUG
std::cerr << "RsGxsChannelSerialiser::deserialiseGxsChannelGroupItem() FAIL size mismatch" << std::endl;
#endif
/* error */
delete item;
return NULL;
}
if (!ok)
{
#ifdef GXSCHANNEL_DEBUG
std::cerr << "RsGxsChannelSerialiser::deserialiseGxsChannelGroupItem() NOK" << std::endl;
#endif
delete item;
return NULL;
}
return item;
}
/*****************************************************************************************/
/*****************************************************************************************/
/*****************************************************************************************/
void RsGxsChannelPostItem::clear()
{
mMsg.mMsg.clear();
}
std::ostream& RsGxsChannelPostItem::print(std::ostream& out, uint16_t indent)
{
printRsItemBase(out, "RsGxsChannelPostItem", indent);
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << "Msg: " << mMsg.mMsg << std::endl;
printRsItemEnd(out ,"RsGxsChannelPostItem", indent);
return out;
}
uint32_t RsGxsChannelSerialiser::sizeGxsChannelPostItem(RsGxsChannelPostItem *item)
{
const RsGxsChannelPost& msg = item->mMsg;
uint32_t s = 8; // header
s += GetTlvStringSize(msg.mMsg); // mMsg.
return s;
}
bool RsGxsChannelSerialiser::serialiseGxsChannelPostItem(RsGxsChannelPostItem *item, void *data, uint32_t *size)
{
#ifdef GXSCHANNEL_DEBUG
std::cerr << "RsGxsChannelSerialiser::serialiseGxsChannelPostItem()" << std::endl;
#endif
uint32_t tlvsize = sizeGxsChannelPostItem(item);
uint32_t offset = 0;
if(*size < tlvsize)
{
#ifdef GXSCHANNEL_DEBUG
std::cerr << "RsGxsChannelSerialiser::serialiseGxsChannelPostItem()" << std::endl;
#endif
return false;
}
*size = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
/* GxsChannelPostItem */
ok &= SetTlvString(data, tlvsize, &offset, 1, item->mMsg.mMsg);
if(offset != tlvsize)
{
#ifdef GXSCHANNEL_DEBUG
std::cerr << "RsGxsChannelSerialiser::serialiseGxsChannelPostItem() FAIL Size Error! " << std::endl;
#endif
ok = false;
}
#ifdef GXSCHANNEL_DEBUG
if (!ok)
{
std::cerr << "RsGxsChannelSerialiser::serialiseGxsChannelGroupItem() NOK" << std::endl;
}
#endif
return ok;
}
RsGxsChannelPostItem* RsGxsChannelSerialiser::deserialiseGxsChannelPostItem(void *data, uint32_t *size)
{
#ifdef GXSCHANNEL_DEBUG
std::cerr << "RsGxsChannelSerialiser::deserialiseGxsChannelPostItem()" << std::endl;
#endif
/* 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_GXSV1_TYPE_CHANNELS != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_GXSCHANNEL_POST_ITEM != getRsItemSubType(rstype)))
{
#ifdef GXSCHANNEL_DEBUG
std::cerr << "RsGxsChannelSerialiser::deserialiseGxsChannelPostItem() FAIL wrong type" << std::endl;
#endif
return NULL; /* wrong type */
}
if (*size < rssize) /* check size */
{
#ifdef GXSCHANNEL_DEBUG
std::cerr << "RsGxsChannelSerialiser::deserialiseGxsChannelPostItem() FAIL wrong size" << std::endl;
#endif
return NULL; /* not enough data */
}
/* set the packet length */
*size = rssize;
bool ok = true;
RsGxsChannelPostItem* item = new RsGxsChannelPostItem();
/* skip the header */
offset += 8;
ok &= GetTlvString(data, rssize, &offset, 1, item->mMsg.mMsg);
if (offset != rssize)
{
#ifdef GXSCHANNEL_DEBUG
std::cerr << "RsGxsChannelSerialiser::deserialiseGxsChannelPostItem() FAIL size mismatch" << std::endl;
#endif
/* error */
delete item;
return NULL;
}
if (!ok)
{
#ifdef GXSCHANNEL_DEBUG
std::cerr << "RsGxsChannelSerialiser::deserialiseGxsChannelPostItem() NOK" << std::endl;
#endif
delete item;
return NULL;
}
return item;
}
/*****************************************************************************************/
/*****************************************************************************************/
/*****************************************************************************************/

View file

@ -0,0 +1,98 @@
/*
* libretroshare/src/serialiser: rsgxschannelitems.h
*
* RetroShare C++ Interface.
*
* Copyright 2012-2012 by Robert Fernie
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#ifndef RS_GXS_CHANNEL_ITEMS_H
#define RS_GXS_CHANNEL_ITEMS_H
#include <map>
#include "serialiser/rsserviceids.h"
#include "serialiser/rsserial.h"
#include "serialiser/rstlvtypes.h"
#include "serialiser/rsgxscommentitems.h"
#include "rsgxsitems.h"
#include "retroshare/rsgxschannels.h"
const uint8_t RS_PKT_SUBTYPE_GXSCHANNEL_GROUP_ITEM = 0x02;
const uint8_t RS_PKT_SUBTYPE_GXSCHANNEL_POST_ITEM = 0x03;
class RsGxsChannelGroupItem : public RsGxsGrpItem
{
public:
RsGxsChannelGroupItem(): RsGxsGrpItem(RS_SERVICE_GXSV1_TYPE_CHANNELS,
RS_PKT_SUBTYPE_GXSCHANNEL_GROUP_ITEM) { return;}
virtual ~RsGxsChannelGroupItem() { return;}
void clear();
std::ostream &print(std::ostream &out, uint16_t indent = 0);
RsGxsChannelGroup mGroup;
};
class RsGxsChannelPostItem : public RsGxsMsgItem
{
public:
RsGxsChannelPostItem(): RsGxsMsgItem(RS_SERVICE_GXSV1_TYPE_CHANNELS,
RS_PKT_SUBTYPE_GXSCHANNEL_POST_ITEM) {return; }
virtual ~RsGxsChannelPostItem() { return;}
void clear();
std::ostream &print(std::ostream &out, uint16_t indent = 0);
RsGxsChannelPost mMsg;
};
class RsGxsChannelSerialiser : public RsGxsCommentSerialiser
{
public:
RsGxsChannelSerialiser()
:RsGxsCommentSerialiser(RS_SERVICE_GXSV1_TYPE_CHANNELS)
{ return; }
virtual ~RsGxsChannelSerialiser() { return; }
uint32_t size(RsItem *item);
bool serialise (RsItem *item, void *data, uint32_t *size);
RsItem * deserialise(void *data, uint32_t *size);
private:
uint32_t sizeGxsChannelGroupItem(RsGxsChannelGroupItem *item);
bool serialiseGxsChannelGroupItem (RsGxsChannelGroupItem *item, void *data, uint32_t *size);
RsGxsChannelGroupItem * deserialiseGxsChannelGroupItem(void *data, uint32_t *size);
uint32_t sizeGxsChannelPostItem(RsGxsChannelPostItem *item);
bool serialiseGxsChannelPostItem (RsGxsChannelPostItem *item, void *data, uint32_t *size);
RsGxsChannelPostItem * deserialiseGxsChannelPostItem(void *data, uint32_t *size);
};
#endif /* RS_GXS_CHANNEL_ITEMS_H */

View file

@ -0,0 +1,401 @@
/*
* libretroshare/src/serialiser: rsgxscommentitems.cc
*
* RetroShare C++ Interface.
*
* Copyright 2012-2013 by Robert Fernie
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2.1 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".
*
*/
#include <iostream>
#include "rsgxscommentitems.h"
#include "serialiser/rstlvbase.h"
#include "serialiser/rsbaseserial.h"
#define GXSCOMMENT_DEBUG 1
uint32_t RsGxsCommentSerialiser::size(RsItem *item)
{
RsGxsCommentItem* com_item = NULL;
RsGxsVoteItem* vote_item = NULL;
if((com_item = dynamic_cast<RsGxsCommentItem*>(item)) != NULL)
{
return sizeGxsCommentItem(com_item);
}
else if((vote_item = dynamic_cast<RsGxsVoteItem*>(item)) != NULL)
{
return sizeGxsVoteItem(vote_item);
}
std::cerr << "RsGxsCommentSerialiser::size() ERROR invalid item" << std::endl;
return 0;
}
bool RsGxsCommentSerialiser::serialise(RsItem *item, void *data, uint32_t *size)
{
RsGxsCommentItem* com_item = NULL;
RsGxsVoteItem* vote_item = NULL;
if((com_item = dynamic_cast<RsGxsCommentItem*>(item)) != NULL)
{
return serialiseGxsCommentItem(com_item, data, size);
}
else if((vote_item = dynamic_cast<RsGxsVoteItem*>(item)) != NULL)
{
return serialiseGxsVoteItem(vote_item, data, size);
}
std::cerr << "RsGxsCommentSerialiser::serialise() ERROR invalid item" << std::endl;
return false;
}
RsItem* RsGxsCommentSerialiser::deserialise(void* data, uint32_t* size)
{
#ifdef GXSCOMMENT_DEBUG
std::cerr << "RsGxsCommentSerialiser::deserialise()" << std::endl;
#endif
/* get the type and size */
uint32_t rstype = getRsItemId(data);
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(getRsItemService(PacketId()) != getRsItemService(rstype)))
{
return NULL; /* wrong type */
}
switch(getRsItemSubType(rstype))
{
case RS_PKT_SUBTYPE_GXSCOMMENT_COMMENT_ITEM:
return deserialiseGxsCommentItem(data, size);
break;
case RS_PKT_SUBTYPE_GXSCOMMENT_VOTE_ITEM:
return deserialiseGxsVoteItem(data, size);
break;
default:
#ifdef GXSCOMMENT_DEBUG
std::cerr << "RsGxsCommentSerialiser::deserialise(): unknown subtype";
std::cerr << std::endl;
#endif
break;
}
return NULL;
}
/*****************************************************************************************/
/*****************************************************************************************/
/*****************************************************************************************/
void RsGxsCommentItem::clear()
{
mMsg.mComment.clear();
}
std::ostream& RsGxsCommentItem::print(std::ostream& out, uint16_t indent)
{
printRsItemBase(out, "RsGxsCommentItem", indent);
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << "Comment: " << mMsg.mComment << std::endl;
printRsItemEnd(out ,"RsGxsCommentItem", indent);
return out;
}
uint32_t RsGxsCommentSerialiser::sizeGxsCommentItem(RsGxsCommentItem *item)
{
const RsGxsComment& msg = item->mMsg;
uint32_t s = 8; // header
s += GetTlvStringSize(msg.mComment); // mMsg.
return s;
}
bool RsGxsCommentSerialiser::serialiseGxsCommentItem(RsGxsCommentItem *item, void *data, uint32_t *size)
{
#ifdef GXSCOMMENT_DEBUG
std::cerr << "RsGxsCommentSerialiser::serialiseGxsCommentItem()" << std::endl;
#endif
uint32_t tlvsize = sizeGxsCommentItem(item);
uint32_t offset = 0;
if(*size < tlvsize)
{
#ifdef GXSCOMMENT_DEBUG
std::cerr << "RsGxsCommentSerialiser::serialiseGxsCommentItem()" << std::endl;
#endif
return false;
}
*size = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
/* GxsCommentItem */
ok &= SetTlvString(data, tlvsize, &offset, 1, item->mMsg.mComment);
if(offset != tlvsize)
{
#ifdef GXSCOMMENT_DEBUG
std::cerr << "RsGxsCommentSerialiser::serialiseGxsCommentItem() FAIL Size Error! " << std::endl;
#endif
ok = false;
}
#ifdef GXSCOMMENT_DEBUG
if (!ok)
{
std::cerr << "RsGxsCommentSerialiser::serialiseGxsCommentItem() NOK" << std::endl;
}
#endif
return ok;
}
RsGxsCommentItem* RsGxsCommentSerialiser::deserialiseGxsCommentItem(void *data, uint32_t *size)
{
#ifdef GXSCOMMENT_DEBUG
std::cerr << "RsGxsCommentSerialiser::deserialiseGxsCommentItem()" << std::endl;
#endif
/* 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)) ||
(getRsItemService(PacketId()) != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_GXSCOMMENT_COMMENT_ITEM != getRsItemSubType(rstype)))
{
#ifdef GXSCOMMENT_DEBUG
std::cerr << "RsGxsCommentSerialiser::deserialiseGxsCommentItem() FAIL wrong type" << std::endl;
#endif
return NULL; /* wrong type */
}
if (*size < rssize) /* check size */
{
#ifdef GXSCOMMENT_DEBUG
std::cerr << "RsGxsCommentSerialiser::deserialiseGxsCommentItem() FAIL wrong size" << std::endl;
#endif
return NULL; /* not enough data */
}
/* set the packet length */
*size = rssize;
bool ok = true;
RsGxsCommentItem* item = new RsGxsCommentItem(getRsItemService(PacketId()));
/* skip the header */
offset += 8;
ok &= GetTlvString(data, rssize, &offset, 1, item->mMsg.mComment);
if (offset != rssize)
{
#ifdef GXSCOMMENT_DEBUG
std::cerr << "RsGxsCommentSerialiser::deserialiseGxsCommentItem() FAIL size mismatch" << std::endl;
#endif
/* error */
delete item;
return NULL;
}
if (!ok)
{
#ifdef GXSCOMMENT_DEBUG
std::cerr << "RsGxsCommentSerialiser::deserialiseGxsCommentItem() NOK" << std::endl;
#endif
delete item;
return NULL;
}
return item;
}
/*****************************************************************************************/
/*****************************************************************************************/
/*****************************************************************************************/
void RsGxsVoteItem::clear()
{
mMsg.mVoteType = 0;
}
std::ostream& RsGxsVoteItem::print(std::ostream& out, uint16_t indent)
{
printRsItemBase(out, "RsGxsVoteItem", indent);
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << "VoteType: " << mMsg.mVoteType << std::endl;
printRsItemEnd(out ,"RsGxsVoteItem", indent);
return out;
}
uint32_t RsGxsCommentSerialiser::sizeGxsVoteItem(RsGxsVoteItem *item)
{
const RsGxsVote& msg = item->mMsg;
uint32_t s = 8; // header
s += 4; // vote flags.
return s;
}
bool RsGxsCommentSerialiser::serialiseGxsVoteItem(RsGxsVoteItem *item, void *data, uint32_t *size)
{
#ifdef GXSCOMMENT_DEBUG
std::cerr << "RsGxsCommentSerialiser::serialiseGxsVoteItem()" << std::endl;
#endif
uint32_t tlvsize = sizeGxsVoteItem(item);
uint32_t offset = 0;
if(*size < tlvsize)
{
#ifdef GXSCOMMENT_DEBUG
std::cerr << "RsGxsCommentSerialiser::serialiseGxsVoteItem()" << std::endl;
#endif
return false;
}
*size = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
/* GxsVoteItem */
ok &= setRawUInt32(data, tlvsize, &offset, item->mMsg.mVoteType);
if(offset != tlvsize)
{
#ifdef GXSCOMMENT_DEBUG
std::cerr << "RsGxsCommentSerialiser::serialiseGxsVoteItem() FAIL Size Error! " << std::endl;
#endif
ok = false;
}
#ifdef GXSCOMMENT_DEBUG
if (!ok)
{
std::cerr << "RsGxsCommentSerialiser::serialiseGxsVoteItem() NOK" << std::endl;
}
#endif
return ok;
}
RsGxsVoteItem* RsGxsCommentSerialiser::deserialiseGxsVoteItem(void *data, uint32_t *size)
{
#ifdef GXSCOMMENT_DEBUG
std::cerr << "RsGxsCommentSerialiser::deserialiseGxsVoteItem()" << std::endl;
#endif
/* 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)) ||
(getRsItemService(PacketId()) != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_GXSCOMMENT_VOTE_ITEM != getRsItemSubType(rstype)))
{
#ifdef GXSCOMMENT_DEBUG
std::cerr << "RsGxsCommentSerialiser::deserialiseGxsVoteItem() FAIL wrong type" << std::endl;
#endif
return NULL; /* wrong type */
}
if (*size < rssize) /* check size */
{
#ifdef GXSCOMMENT_DEBUG
std::cerr << "RsGxsCommentSerialiser::deserialiseGxsVoteItem() FAIL wrong size" << std::endl;
#endif
return NULL; /* not enough data */
}
/* set the packet length */
*size = rssize;
bool ok = true;
RsGxsVoteItem* item = new RsGxsVoteItem(getRsItemService(PacketId()));
/* skip the header */
offset += 8;
ok &= getRawUInt32(data, rssize, &offset, &(item->mMsg.mVoteType));
if (offset != rssize)
{
#ifdef GXSCOMMENT_DEBUG
std::cerr << "RsGxsCommentSerialiser::deserialiseGxsVoteItem() FAIL size mismatch" << std::endl;
#endif
/* error */
delete item;
return NULL;
}
if (!ok)
{
#ifdef GXSCOMMENT_DEBUG
std::cerr << "RsGxsCommentSerialiser::deserialiseGxsVoteItem() NOK" << std::endl;
#endif
delete item;
return NULL;
}
return item;
}
/*****************************************************************************************/
/*****************************************************************************************/
/*****************************************************************************************/

View file

@ -0,0 +1,97 @@
/*
* libretroshare/src/serialiser: rsgxscommentitems.h
*
* RetroShare C++ Interface.
*
* Copyright 2012-2012 by Robert Fernie
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#ifndef RS_GXS_COMMENT_ITEMS_H
#define RS_GXS_COMMENT_ITEMS_H
#include <map>
#include "serialiser/rsserviceids.h"
#include "serialiser/rsserial.h"
#include "serialiser/rstlvtypes.h"
#include "rsgxsitems.h"
#include "retroshare/rsgxscommon.h"
const uint8_t RS_PKT_SUBTYPE_GXSCOMMENT_COMMENT_ITEM = 0xf1;
const uint8_t RS_PKT_SUBTYPE_GXSCOMMENT_VOTE_ITEM = 0xf2;
class RsGxsCommentItem : public RsGxsMsgItem
{
public:
RsGxsCommentItem(uint8_t service_type): RsGxsMsgItem(service_type,
RS_PKT_SUBTYPE_GXSCOMMENT_COMMENT_ITEM) {return; }
virtual ~RsGxsCommentItem() { return;}
void clear();
std::ostream &print(std::ostream &out, uint16_t indent = 0);
RsGxsComment mMsg;
};
class RsGxsVoteItem : public RsGxsMsgItem
{
public:
RsGxsVoteItem(uint8_t service_type): RsGxsMsgItem(service_type,
RS_PKT_SUBTYPE_GXSCOMMENT_VOTE_ITEM) {return; }
virtual ~RsGxsVoteItem() { return;}
void clear();
std::ostream &print(std::ostream &out, uint16_t indent = 0);
RsGxsVote mMsg;
};
class RsGxsCommentSerialiser : public RsSerialType
{
public:
RsGxsCommentSerialiser(uint16_t service_type)
:RsSerialType(RS_PKT_VERSION_SERVICE, service_type)
{ return; }
virtual ~RsGxsCommentSerialiser() { return; }
uint32_t size(RsItem *item);
bool serialise (RsItem *item, void *data, uint32_t *size);
RsItem * deserialise(void *data, uint32_t *size);
private:
uint32_t sizeGxsCommentItem(RsGxsCommentItem *item);
bool serialiseGxsCommentItem (RsGxsCommentItem *item, void *data, uint32_t *size);
RsGxsCommentItem * deserialiseGxsCommentItem(void *data, uint32_t *size);
uint32_t sizeGxsVoteItem(RsGxsVoteItem *item);
bool serialiseGxsVoteItem (RsGxsVoteItem *item, void *data, uint32_t *size);
RsGxsVoteItem * deserialiseGxsVoteItem(void *data, uint32_t *size);
};
#endif /* RS_GXS_COMMENT_ITEMS_H */

View file

@ -79,9 +79,6 @@ bool RsGxsIdSerialiser::serialise(RsItem *item, void *data, uint32_t *size)
RsItem* RsGxsIdSerialiser::deserialise(void* data, uint32_t* size)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsIdSerialiser::deserialise()" << std::endl;
#endif
/* get the type and size */
uint32_t rstype = getRsItemId(data);
@ -160,9 +157,6 @@ uint32_t RsGxsIdSerialiser::sizeGxsIdGroupItem(RsGxsIdGroupItem *item)
bool RsGxsIdSerialiser::serialiseGxsIdGroupItem(RsGxsIdGroupItem *item, void *data, uint32_t *size)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsIdSerialiser::serialiseGxsIdGroupItem()" << std::endl;
#endif
uint32_t tlvsize = sizeGxsIdGroupItem(item);
uint32_t offset = 0;
@ -208,10 +202,6 @@ bool RsGxsIdSerialiser::serialiseGxsIdGroupItem(RsGxsIdGroupItem *item, void *da
RsGxsIdGroupItem* RsGxsIdSerialiser::deserialiseGxsIdGroupItem(void *data, uint32_t *size)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsIdSerialiser::deserialiseGxsIdGroupItem()" << std::endl;
#endif
/* get the type and size */
uint32_t rstype = getRsItemId(data);
uint32_t rssize = getRsItemSize(data);
@ -313,9 +303,6 @@ uint32_t RsGxsIdSerialiser::sizeGxsIdOpinionItem(RsGxsIdOpinionItem *item)
bool RsGxsIdSerialiser::serialiseGxsIdOpinionItem(RsGxsIdOpinionItem *item, void *data, uint32_t *size)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsIdSerialiser::serialiseGxsIdOpinionItem()" << std::endl;
#endif
uint32_t tlvsize = sizeGxsIdOpinionItem(item);
uint32_t offset = 0;
@ -361,9 +348,6 @@ bool RsGxsIdSerialiser::serialiseGxsIdOpinionItem(RsGxsIdOpinionItem *item, void
RsGxsIdOpinionItem* RsGxsIdSerialiser::deserialiseGxsIdOpinionItem(void *data, uint32_t *size)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsIdSerialiser::deserialiseGxsIdOpinionItem()" << std::endl;
#endif
/* get the type and size */
uint32_t rstype = getRsItemId(data);
uint32_t rssize = getRsItemSize(data);
@ -460,17 +444,13 @@ uint32_t RsGxsIdSerialiser::sizeGxsIdCommentItem(RsGxsIdCommentItem *item)
bool RsGxsIdSerialiser::serialiseGxsIdCommentItem(RsGxsIdCommentItem *item, void *data, uint32_t *size)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsIdSerialiser::serialiseGxsIdCommentItem()" << std::endl;
#endif
uint32_t tlvsize = sizeGxsIdCommentItem(item);
uint32_t offset = 0;
if(*size < tlvsize)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsIdSerialiser::serialiseGxsIdCommentItem()" << std::endl;
std::cerr << "RsGxsIdSerialiser::serialiseGxsIdCommentItem() Not enough space" << std::endl;
#endif
return false;
}
@ -503,14 +483,11 @@ bool RsGxsIdSerialiser::serialiseGxsIdCommentItem(RsGxsIdCommentItem *item, void
#endif
return ok;
}
}
RsGxsIdCommentItem* RsGxsIdSerialiser::deserialiseGxsIdCommentItem(void *data, uint32_t *size)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsIdSerialiser::deserialiseGxsIdCommentItem()" << std::endl;
#endif
/* get the type and size */
uint32_t rstype = getRsItemId(data);
uint32_t rssize = getRsItemSize(data);