moved wiki and wire items to rsitems/

This commit is contained in:
csoler 2017-04-29 21:50:55 +02:00
parent d58c638af8
commit 1159c863a2
7 changed files with 9 additions and 9 deletions

View file

@ -0,0 +1,627 @@
/*
* libretroshare/src/serialiser: rswikiitems.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 "rsitems/rswikiitems.h"
#include "serialiser/rstlvbase.h"
#include "serialization/rstypeserializer.h"
#define GXSID_DEBUG 1
RsItem *RsGxsWikiSerialiser::create_item(uint16_t service, uint8_t item_sub_id) const
{
if(service != RS_SERVICE_GXS_TYPE_WIKI)
return NULL ;
switch(item_sub_id)
{
case RS_PKT_SUBTYPE_WIKI_COLLECTION_ITEM: return new RsGxsWikiCollectionItem();
case RS_PKT_SUBTYPE_WIKI_COMMENT_ITEM: return new RsGxsWikiCommentItem();
case RS_PKT_SUBTYPE_WIKI_SNAPSHOT_ITEM: return new RsGxsWikiSnapshotItem();
default:
return NULL ;
}
}
#ifdef TO_REMOVE
uint32_t RsGxsWikiSerialiser::size(RsItem *item)
{
RsGxsWikiCollectionItem* grp_item = NULL;
RsGxsWikiSnapshotItem* snap_item = NULL;
RsGxsWikiCommentItem* com_item = NULL;
if((grp_item = dynamic_cast<RsGxsWikiCollectionItem*>(item)) != NULL)
{
return sizeGxsWikiCollectionItem(grp_item);
}
else if((snap_item = dynamic_cast<RsGxsWikiSnapshotItem*>(item)) != NULL)
{
return sizeGxsWikiSnapshotItem(snap_item);
}
else if((com_item = dynamic_cast<RsGxsWikiCommentItem*>(item)) != NULL)
{
return sizeGxsWikiCommentItem(com_item);
}
return 0;
}
bool RsGxsWikiSerialiser::serialise(RsItem *item, void *data, uint32_t *size)
{
RsGxsWikiCollectionItem* grp_item = NULL;
RsGxsWikiSnapshotItem* snap_item = NULL;
RsGxsWikiCommentItem* com_item = NULL;
if((grp_item = dynamic_cast<RsGxsWikiCollectionItem*>(item)) != NULL)
{
return serialiseGxsWikiCollectionItem(grp_item, data, size);
}
else if((snap_item = dynamic_cast<RsGxsWikiSnapshotItem*>(item)) != NULL)
{
return serialiseGxsWikiSnapshotItem(snap_item, data, size);
}
else if((com_item = dynamic_cast<RsGxsWikiCommentItem*>(item)) != NULL)
{
return serialiseGxsWikiCommentItem(com_item, data, size);
}
return false;
}
RsItem* RsGxsWikiSerialiser::deserialise(void* data, uint32_t* size)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::deserialise()" << std::endl;
#endif
/* get the type and size */
uint32_t rstype = getRsItemId(data);
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(RS_SERVICE_GXS_TYPE_WIKI != getRsItemService(rstype)))
{
return NULL; /* wrong type */
}
switch(getRsItemSubType(rstype))
{
case RS_PKT_SUBTYPE_WIKI_COLLECTION_ITEM:
return deserialiseGxsWikiCollectionItem(data, size);
break;
case RS_PKT_SUBTYPE_WIKI_SNAPSHOT_ITEM:
return deserialiseGxsWikiSnapshotItem(data, size);
break;
case RS_PKT_SUBTYPE_WIKI_COMMENT_ITEM:
return deserialiseGxsWikiCommentItem(data, size);
break;
default:
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::deserialise(): unknown subtype";
std::cerr << std::endl;
#endif
break;
}
return NULL;
}
/*****************************************************************************************/
/*****************************************************************************************/
/*****************************************************************************************/
#endif
void RsGxsWikiCollectionItem::clear()
{
collection.mDescription.clear();
collection.mCategory.clear();
collection.mHashTags.clear();
}
#ifdef TO_REMOVE
std::ostream& RsGxsWikiCollectionItem::print(std::ostream& out, uint16_t indent)
{
printRsItemBase(out, "RsGxsWikiCollectionItem", indent);
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << "Description: " << collection.mDescription << std::endl;
printIndent(out, int_Indent);
out << "Category: " << collection.mCategory << std::endl;
printIndent(out, int_Indent);
out << "HashTags: " << collection.mHashTags << std::endl;
printRsItemEnd(out ,"RsGxsWikiCollectionItem", indent);
return out;
}
uint32_t RsGxsWikiSerialiser::sizeGxsWikiCollectionItem(RsGxsWikiCollectionItem *item)
{
const RsWikiCollection& collection = item->collection;
uint32_t s = 8; // header
s += GetTlvStringSize(collection.mDescription);
s += GetTlvStringSize(collection.mCategory);
s += GetTlvStringSize(collection.mHashTags);
return s;
}
#endif
void RsGxsWikiCollectionItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx)
{
RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_DESCR ,collection.mDescription,"collection.mDescription") ;
RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_CATEGORY,collection.mCategory ,"collection.mCategory") ;
RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_HASH_TAG,collection.mHashTags ,"collection.mHashTags") ;
}
#ifdef TO_REMOVE
bool RsGxsWikiSerialiser::serialiseGxsWikiCollectionItem(RsGxsWikiCollectionItem *item, void *data, uint32_t *size)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::serialiseGxsWikiCollectionItem()" << std::endl;
#endif
uint32_t tlvsize = sizeGxsWikiCollectionItem(item);
uint32_t offset = 0;
if(*size < tlvsize)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::serialiseGxsWikiCollectionItem()" << std::endl;
#endif
return false;
}
*size = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
/* GxsWikiCollectionItem */
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_DESCR, item->collection.mDescription);
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_CATEGORY, item->collection.mCategory);
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_HASH_TAG, item->collection.mHashTags);
if(offset != tlvsize)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::serialiseGxsWikiCollectionItem() FAIL Size Error! " << std::endl;
#endif
ok = false;
}
#ifdef GXSID_DEBUG
if (!ok)
{
std::cerr << "RsGxsWikiSerialiser::serialiseGxsWikiCollectionItem() NOK" << std::endl;
}
#endif
return ok;
}
RsGxsWikiCollectionItem* RsGxsWikiSerialiser::deserialiseGxsWikiCollectionItem(void *data, uint32_t *size)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::deserialiseGxsWikiCollectionItem()" << 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_GXS_TYPE_WIKI != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_WIKI_COLLECTION_ITEM != getRsItemSubType(rstype)))
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::deserialiseGxsWikiCollectionItem() FAIL wrong type" << std::endl;
#endif
return NULL; /* wrong type */
}
if (*size < rssize) /* check size */
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::deserialiseGxsWikiCollectionItem() FAIL wrong size" << std::endl;
#endif
return NULL; /* not enough data */
}
/* set the packet length */
*size = rssize;
bool ok = true;
RsGxsWikiCollectionItem* item = new RsGxsWikiCollectionItem();
/* skip the header */
offset += 8;
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_DESCR, item->collection.mDescription);
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_CATEGORY, item->collection.mCategory);
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_HASH_TAG, item->collection.mHashTags);
if (offset != rssize)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::deserialiseGxsWikiCollectionItem() FAIL size mismatch" << std::endl;
#endif
/* error */
delete item;
return NULL;
}
if (!ok)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::deserialiseGxsWikiCollectionItem() NOK" << std::endl;
#endif
delete item;
return NULL;
}
return item;
}
/*****************************************************************************************/
/*****************************************************************************************/
/*****************************************************************************************/
#endif
void RsGxsWikiSnapshotItem::clear()
{
snapshot.mPage.clear();
snapshot.mHashTags.clear();
}
#ifdef TO_REMOVE
std::ostream& RsGxsWikiSnapshotItem::print(std::ostream& out, uint16_t indent)
{
printRsItemBase(out, "RsGxsWikiSnapshotItem", indent);
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << "Page: " << snapshot.mPage << std::endl;
printIndent(out, int_Indent);
out << "HashTags: " << snapshot.mHashTags << std::endl;
printRsItemEnd(out ,"RsGxsWikiSnapshotItem", indent);
return out;
}
uint32_t RsGxsWikiSerialiser::sizeGxsWikiSnapshotItem(RsGxsWikiSnapshotItem *item)
{
const RsWikiSnapshot& snapshot = item->snapshot;
uint32_t s = 8; // header
s += GetTlvStringSize(snapshot.mPage);
s += GetTlvStringSize(snapshot.mHashTags);
return s;
}
#endif
void RsGxsWikiSnapshotItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx)
{
RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_WIKI_PAGE,snapshot.mPage,"snapshot.mPage") ;
RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_HASH_TAG ,snapshot.mPage,"snapshot.mHashTags") ;
}
#ifdef TO_REMOVE
bool RsGxsWikiSerialiser::serialiseGxsWikiSnapshotItem(RsGxsWikiSnapshotItem *item, void *data, uint32_t *size)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::serialiseGxsWikiSnapshotItem()" << std::endl;
#endif
uint32_t tlvsize = sizeGxsWikiSnapshotItem(item);
uint32_t offset = 0;
if(*size < tlvsize)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::serialiseGxsWikiSnapshotItem()" << std::endl;
#endif
return false;
}
*size = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
/* GxsWikiSnapshotItem */
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_WIKI_PAGE, item->snapshot.mPage);
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_HASH_TAG, item->snapshot.mHashTags);
if(offset != tlvsize)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::serialiseGxsWikiSnapshotItem() FAIL Size Error! " << std::endl;
#endif
ok = false;
}
#ifdef GXSID_DEBUG
if (!ok)
{
std::cerr << "RsGxsWikiSerialiser::serialiseGxsWikiSnapshotItem() NOK" << std::endl;
}
#endif
return ok;
}
RsGxsWikiSnapshotItem* RsGxsWikiSerialiser::deserialiseGxsWikiSnapshotItem(void *data, uint32_t *size)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::deserialiseGxsWikiSnapshotItem()" << 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_GXS_TYPE_WIKI != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_WIKI_SNAPSHOT_ITEM != getRsItemSubType(rstype)))
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::deserialiseGxsWikiSnapshotItem() FAIL wrong type" << std::endl;
#endif
return NULL; /* wrong type */
}
if (*size < rssize) /* check size */
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::deserialiseGxsWikiSnapshotItem() FAIL wrong size" << std::endl;
#endif
return NULL; /* not enough data */
}
/* set the packet length */
*size = rssize;
bool ok = true;
RsGxsWikiSnapshotItem* item = new RsGxsWikiSnapshotItem();
/* skip the header */
offset += 8;
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_WIKI_PAGE, item->snapshot.mPage);
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_HASH_TAG, item->snapshot.mHashTags);
if (offset != rssize)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::deserialiseGxsWikiSnapshotItem() FAIL size mismatch" << std::endl;
#endif
/* error */
delete item;
return NULL;
}
if (!ok)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::deserialiseGxsWikiSnapshotItem() NOK" << std::endl;
#endif
delete item;
return NULL;
}
return item;
}
/*****************************************************************************************/
/*****************************************************************************************/
/*****************************************************************************************/
#endif
void RsGxsWikiCommentItem::clear()
{
comment.mComment.clear();
}
#ifdef TO_REMOVE
std::ostream& RsGxsWikiCommentItem::print(std::ostream& out, uint16_t indent)
{
printRsItemBase(out, "RsGxsWikiCommentItem", indent);
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << "Comment: " << comment.mComment << std::endl;
printRsItemEnd(out ,"RsGxsWikiCommentItem", indent);
return out;
}
uint32_t RsGxsWikiSerialiser::sizeGxsWikiCommentItem(RsGxsWikiCommentItem *item)
{
const RsWikiComment& comment = item->comment;
uint32_t s = 8; // header
s += GetTlvStringSize(comment.mComment);
return s;
}
#endif
void RsGxsWikiCommentItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx)
{
RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_COMMENT,comment.mComment,"comment.mComment") ;
}
#ifdef TO_REMOVE
bool RsGxsWikiSerialiser::serialiseGxsWikiCommentItem(RsGxsWikiCommentItem *item, void *data, uint32_t *size)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::serialiseGxsWikiCommentItem()" << std::endl;
#endif
uint32_t tlvsize = sizeGxsWikiCommentItem(item);
uint32_t offset = 0;
if(*size < tlvsize)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::serialiseGxsWikiCommentItem()" << std::endl;
#endif
return false;
}
*size = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
/* GxsWikiCommentItem */
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_COMMENT, item->comment.mComment);
if(offset != tlvsize)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::serialiseGxsWikiCommentItem() FAIL Size Error! " << std::endl;
#endif
ok = false;
}
#ifdef GXSID_DEBUG
if (!ok)
{
std::cerr << "RsGxsWikiSerialiser::serialiseGxsWikiCommentItem() NOK" << std::endl;
}
#endif
return ok;
}
RsGxsWikiCommentItem* RsGxsWikiSerialiser::deserialiseGxsWikiCommentItem(void *data, uint32_t *size)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::deserialiseGxsWikiCommentItem()" << 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_GXS_TYPE_WIKI != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_WIKI_COMMENT_ITEM != getRsItemSubType(rstype)))
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::deserialiseGxsWikiCommentItem() FAIL wrong type" << std::endl;
#endif
return NULL; /* wrong type */
}
if (*size < rssize) /* check size */
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::deserialiseGxsWikiCommentItem() FAIL wrong size" << std::endl;
#endif
return NULL; /* not enough data */
}
/* set the packet length */
*size = rssize;
bool ok = true;
RsGxsWikiCommentItem* item = new RsGxsWikiCommentItem();
/* skip the header */
offset += 8;
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_COMMENT, item->comment.mComment);
if (offset != rssize)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::deserialiseGxsWikiCommentItem() FAIL size mismatch" << std::endl;
#endif
/* error */
delete item;
return NULL;
}
if (!ok)
{
#ifdef GXSID_DEBUG
std::cerr << "RsGxsWikiSerialiser::deserialiseGxsWikiCommentItem() NOK" << std::endl;
#endif
delete item;
return NULL;
}
return item;
}
/*****************************************************************************************/
/*****************************************************************************************/
/*****************************************************************************************/
#endif

View file

@ -0,0 +1,90 @@
/*
* libretroshare/src/serialiser: rswikiitems.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.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".
*
*/
#ifndef RS_WIKI_ITEMS_H
#define RS_WIKI_ITEMS_H
#include <map>
#include "rsitems/rsserviceids.h"
#include "rsitems/rsitem.h"
#include "rsitems/rsgxsitems.h"
#include "retroshare/rswiki.h"
const uint8_t RS_PKT_SUBTYPE_WIKI_COLLECTION_ITEM = 0x02;
const uint8_t RS_PKT_SUBTYPE_WIKI_SNAPSHOT_ITEM = 0x03;
const uint8_t RS_PKT_SUBTYPE_WIKI_COMMENT_ITEM = 0x04;
class RsGxsWikiCollectionItem : public RsGxsGrpItem
{
public:
RsGxsWikiCollectionItem(): RsGxsGrpItem(RS_SERVICE_GXS_TYPE_WIKI, RS_PKT_SUBTYPE_WIKI_COLLECTION_ITEM) {}
virtual ~RsGxsWikiCollectionItem() {}
void clear();
virtual void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx);
RsWikiCollection collection;
};
class RsGxsWikiSnapshotItem : public RsGxsMsgItem
{
public:
RsGxsWikiSnapshotItem(): RsGxsMsgItem(RS_SERVICE_GXS_TYPE_WIKI, RS_PKT_SUBTYPE_WIKI_SNAPSHOT_ITEM) {}
virtual ~RsGxsWikiSnapshotItem() {}
void clear();
virtual void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx);
RsWikiSnapshot snapshot;
};
class RsGxsWikiCommentItem : public RsGxsMsgItem
{
public:
RsGxsWikiCommentItem(): RsGxsMsgItem(RS_SERVICE_GXS_TYPE_WIKI, RS_PKT_SUBTYPE_WIKI_COMMENT_ITEM) {}
virtual ~RsGxsWikiCommentItem() {}
void clear();
virtual void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx);
RsWikiComment comment;
};
class RsGxsWikiSerialiser : public RsServiceSerializer
{
public:
RsGxsWikiSerialiser() :RsServiceSerializer(RS_SERVICE_GXS_TYPE_WIKI) {}
virtual ~RsGxsWikiSerialiser() {}
virtual RsItem *create_item(uint16_t /* service */, uint8_t /* item_sub_id */) const;
};
#endif /* RS_WIKI_ITEMS_H */

View file

@ -0,0 +1,443 @@
/*
* libretroshare/src/serialiser: rswikiitems.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 "rswireitems.h"
#include "serialization/rstypeserializer.h"
#define WIRE_DEBUG 1
RsItem *RsGxsWireSerialiser::create_item(uint16_t service,uint8_t item_subtype) const
{
if(service != RS_SERVICE_GXS_TYPE_WIRE)
return NULL ;
switch(item_subtype)
{
case RS_PKT_SUBTYPE_WIRE_GROUP_ITEM: return new RsGxsWireGroupItem();
case RS_PKT_SUBTYPE_WIRE_PULSE_ITEM: return new RsGxsWirePulseItem();
default:
return NULL ;
}
}
#ifdef TO_REMOVE
uint32_t RsGxsWireSerialiser::size(RsItem *item)
{
RsGxsWireGroupItem* grp_item = NULL;
RsGxsWirePulseItem* snap_item = NULL;
if((grp_item = dynamic_cast<RsGxsWireGroupItem*>(item)) != NULL)
{
return sizeGxsWireGroupItem(grp_item);
}
else if((snap_item = dynamic_cast<RsGxsWirePulseItem*>(item)) != NULL)
{
return sizeGxsWirePulseItem(snap_item);
}
return 0;
}
bool RsGxsWireSerialiser::serialise(RsItem *item, void *data, uint32_t *size)
{
RsGxsWireGroupItem* grp_item = NULL;
RsGxsWirePulseItem* snap_item = NULL;
if((grp_item = dynamic_cast<RsGxsWireGroupItem*>(item)) != NULL)
{
return serialiseGxsWireGroupItem(grp_item, data, size);
}
else if((snap_item = dynamic_cast<RsGxsWirePulseItem*>(item)) != NULL)
{
return serialiseGxsWirePulseItem(snap_item, data, size);
}
return false;
}
RsItem* RsGxsWireSerialiser::deserialise(void* data, uint32_t* size)
{
#ifdef WIRE_DEBUG
std::cerr << "RsGxsWireSerialiser::deserialise()" << std::endl;
#endif
/* get the type and size */
uint32_t rstype = getRsItemId(data);
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(RS_SERVICE_GXS_TYPE_WIRE != getRsItemService(rstype)))
{
return NULL; /* wrong type */
}
switch(getRsItemSubType(rstype))
{
case RS_PKT_SUBTYPE_WIRE_GROUP_ITEM:
return deserialiseGxsWireGroupItem(data, size);
break;
case RS_PKT_SUBTYPE_WIRE_PULSE_ITEM:
return deserialiseGxsWirePulseItem(data, size);
break;
default:
#ifdef WIRE_DEBUG
std::cerr << "RsGxsWireSerialiser::deserialise(): unknown subtype";
std::cerr << std::endl;
#endif
break;
}
return NULL;
}
/*****************************************************************************************/
/*****************************************************************************************/
/*****************************************************************************************/
#endif
void RsGxsWireGroupItem::clear()
{
group.mDescription.clear();
}
#ifdef TO_REMOVE
std::ostream& RsGxsWireGroupItem::print(std::ostream& out, uint16_t indent)
{
printRsItemBase(out, "RsGxsWireGroupItem", indent);
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << "Description: " << group.mDescription << std::endl;
printRsItemEnd(out ,"RsGxsWireGroupItem", indent);
return out;
}
uint32_t RsGxsWireSerialiser::sizeGxsWireGroupItem(RsGxsWireGroupItem *item)
{
const RsWireGroup& group = item->group;
uint32_t s = 8; // header
s += GetTlvStringSize(group.mDescription);
return s;
}
#endif
void RsGxsWireGroupItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx)
{
RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_DESCR,group.mDescription,"group.mDescription") ;
}
#ifdef TO_REMOVE
bool RsGxsWireSerialiser::serialiseGxsWireGroupItem(RsGxsWireGroupItem *item, void *data, uint32_t *size)
{
#ifdef WIRE_DEBUG
std::cerr << "RsGxsWireSerialiser::serialiseGxsWireGroupItem()" << std::endl;
#endif
uint32_t tlvsize = sizeGxsWireGroupItem(item);
uint32_t offset = 0;
if(*size < tlvsize)
{
#ifdef WIRE_DEBUG
std::cerr << "RsGxsWireSerialiser::serialiseGxsWireGroupItem()" << std::endl;
#endif
return false;
}
*size = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
/* GxsWireGroupItem */
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_DESCR, item->group.mDescription);
if(offset != tlvsize)
{
#ifdef WIRE_DEBUG
std::cerr << "RsGxsWireSerialiser::serialiseGxsWireGroupItem() FAIL Size Error! " << std::endl;
#endif
ok = false;
}
#ifdef WIRE_DEBUG
if (!ok)
{
std::cerr << "RsGxsWireSerialiser::serialiseGxsWireGroupItem() NOK" << std::endl;
}
#endif
return ok;
}
RsGxsWireGroupItem* RsGxsWireSerialiser::deserialiseGxsWireGroupItem(void *data, uint32_t *size)
{
#ifdef WIRE_DEBUG
std::cerr << "RsGxsWireSerialiser::deserialiseGxsWireGroupItem()" << 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_GXS_TYPE_WIRE != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_WIRE_GROUP_ITEM != getRsItemSubType(rstype)))
{
#ifdef WIRE_DEBUG
std::cerr << "RsGxsWireSerialiser::deserialiseGxsWireGroupItem() FAIL wrong type" << std::endl;
#endif
return NULL; /* wrong type */
}
if (*size < rssize) /* check size */
{
#ifdef WIRE_DEBUG
std::cerr << "RsGxsWireSerialiser::deserialiseGxsWireGroupItem() FAIL wrong size" << std::endl;
#endif
return NULL; /* not enough data */
}
/* set the packet length */
*size = rssize;
bool ok = true;
RsGxsWireGroupItem* item = new RsGxsWireGroupItem();
/* skip the header */
offset += 8;
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_DESCR, item->group.mDescription);
if (offset != rssize)
{
#ifdef WIRE_DEBUG
std::cerr << "RsGxsWireSerialiser::deserialiseGxsWireGroupItem() FAIL size mismatch" << std::endl;
#endif
/* error */
delete item;
return NULL;
}
if (!ok)
{
#ifdef WIRE_DEBUG
std::cerr << "RsGxsWireSerialiser::deserialiseGxsWireGroupItem() NOK" << std::endl;
#endif
delete item;
return NULL;
}
return item;
}
/*****************************************************************************************/
/*****************************************************************************************/
/*****************************************************************************************/
void RsGxsWirePulseItem::clear()
{
pulse.mPulseText.clear();
pulse.mHashTags.clear();
}
std::ostream& RsGxsWirePulseItem::print(std::ostream& out, uint16_t indent)
{
printRsItemBase(out, "RsGxsWirePulseItem", indent);
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << "Page: " << pulse.mPulseText << std::endl;
printIndent(out, int_Indent);
out << "HashTags: " << pulse.mHashTags << std::endl;
printRsItemEnd(out ,"RsGxsWirePulseItem", indent);
return out;
}
uint32_t RsGxsWireSerialiser::sizeGxsWirePulseItem(RsGxsWirePulseItem *item)
{
const RsWirePulse& pulse = item->pulse;
uint32_t s = 8; // header
s += GetTlvStringSize(pulse.mPulseText);
s += GetTlvStringSize(pulse.mHashTags);
return s;
}
#endif
void RsGxsWirePulseItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx)
{
RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_MSG,pulse.mPulseText,"pulse.mPulseText") ;
RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_HASH_TAG,pulse.mHashTags,"pulse.mHashTags") ;
}
#ifdef TO_REMOVE
bool RsGxsWireSerialiser::serialiseGxsWirePulseItem(RsGxsWirePulseItem *item, void *data, uint32_t *size)
{
#ifdef WIRE_DEBUG
std::cerr << "RsGxsWireSerialiser::serialiseGxsWirePulseItem()" << std::endl;
#endif
uint32_t tlvsize = sizeGxsWirePulseItem(item);
uint32_t offset = 0;
if(*size < tlvsize)
{
#ifdef WIRE_DEBUG
std::cerr << "RsGxsWireSerialiser::serialiseGxsWirePulseItem()" << std::endl;
#endif
return false;
}
*size = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
/* GxsWirePulseItem */
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_MSG, item->pulse.mPulseText);
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_HASH_TAG, item->pulse.mHashTags);
if(offset != tlvsize)
{
#ifdef WIRE_DEBUG
std::cerr << "RsGxsWireSerialiser::serialiseGxsWirePulseItem() FAIL Size Error! " << std::endl;
#endif
ok = false;
}
#ifdef WIRE_DEBUG
if (!ok)
{
std::cerr << "RsGxsWireSerialiser::serialiseGxsWirePulseItem() NOK" << std::endl;
}
#endif
return ok;
}
RsGxsWirePulseItem* RsGxsWireSerialiser::deserialiseGxsWirePulseItem(void *data, uint32_t *size)
{
#ifdef WIRE_DEBUG
std::cerr << "RsGxsWireSerialiser::deserialiseGxsWirePulseItem()" << 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_GXS_TYPE_WIRE != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_WIRE_PULSE_ITEM != getRsItemSubType(rstype)))
{
#ifdef WIRE_DEBUG
std::cerr << "RsGxsWireSerialiser::deserialiseGxsWirePulseItem() FAIL wrong type" << std::endl;
#endif
return NULL; /* wrong type */
}
if (*size < rssize) /* check size */
{
#ifdef WIRE_DEBUG
std::cerr << "RsGxsWireSerialiser::deserialiseGxsWirePulseItem() FAIL wrong size" << std::endl;
#endif
return NULL; /* not enough data */
}
/* set the packet length */
*size = rssize;
bool ok = true;
RsGxsWirePulseItem* item = new RsGxsWirePulseItem();
/* skip the header */
offset += 8;
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_MSG, item->pulse.mPulseText);
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_HASH_TAG, item->pulse.mHashTags);
if (offset != rssize)
{
#ifdef WIRE_DEBUG
std::cerr << "RsGxsWireSerialiser::deserialiseGxsWirePulseItem() FAIL size mismatch" << std::endl;
#endif
/* error */
delete item;
return NULL;
}
if (!ok)
{
#ifdef WIRE_DEBUG
std::cerr << "RsGxsWireSerialiser::deserialiseGxsWirePulseItem() NOK" << std::endl;
#endif
delete item;
return NULL;
}
return item;
}
/*****************************************************************************************/
/*****************************************************************************************/
/*****************************************************************************************/
#endif

View file

@ -0,0 +1,79 @@
/*
* libretroshare/src/serialiser: rswireitems.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.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".
*
*/
#ifndef RS_WIRE_ITEMS_H
#define RS_WIRE_ITEMS_H
#include <map>
#include "rsitems/rsserviceids.h"
#include "serialiser/rsserial.h"
//#include "serialiser/rstlvtypes.h"
#include "rsgxsitems.h"
#include "retroshare/rswire.h"
const uint8_t RS_PKT_SUBTYPE_WIRE_GROUP_ITEM = 0x02;
const uint8_t RS_PKT_SUBTYPE_WIRE_PULSE_ITEM = 0x03;
class RsGxsWireGroupItem : public RsGxsGrpItem
{
public:
RsGxsWireGroupItem(): RsGxsGrpItem(RS_SERVICE_GXS_TYPE_WIRE, RS_PKT_SUBTYPE_WIRE_GROUP_ITEM) {}
virtual ~RsGxsWireGroupItem() {}
void clear();
virtual void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx);
RsWireGroup group;
};
class RsGxsWirePulseItem : public RsGxsMsgItem
{
public:
RsGxsWirePulseItem(): RsGxsMsgItem(RS_SERVICE_GXS_TYPE_WIRE, RS_PKT_SUBTYPE_WIRE_PULSE_ITEM) {}
virtual ~RsGxsWirePulseItem() {}
void clear();
virtual void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx);
RsWirePulse pulse;
};
class RsGxsWireSerialiser : public RsServiceSerializer
{
public:
RsGxsWireSerialiser() :RsServiceSerializer(RS_SERVICE_GXS_TYPE_WIRE) {}
virtual ~RsGxsWireSerialiser() {}
virtual RsItem *create_item(uint16_t service,uint8_t item_subtype) const ;
};
#endif /* RS_WIKI_ITEMS_H */