photov2 items serialisation/deserialisation

git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-gxs-b1@5364 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
chrisparker126 2012-07-31 21:27:34 +00:00
parent e6c6ed9b91
commit 3f5c96ab8a
3 changed files with 475 additions and 18 deletions

View File

@ -118,22 +118,22 @@ class RsGxsGrpItem : public RsItem
public:
RsGxsGrpItem() : RsItem(0) { return; }
RsGxsGrpItem(uint16_t service, uint8_t subtype)
: RsItem(RS_PKT_VERSION_SERVICE, service, subtype) { return; }
virtual ~RsGxsGrpItem(){}
RsGroupMetaData meta;
};
class RsGxsMsgItem : public RsItem
{
public:
RsGxsMsgItem() : RsItem(0) { return; }
RsGxsMsgItem(uint16_t service, uint8_t subtype)
: RsItem(RS_PKT_VERSION_SERVICE, service, subtype) { return; }
virtual ~RsGxsMsgItem(){}
RsMsgMetaData meta;
};

View File

@ -1,45 +1,491 @@
/*
* rsphotov2items.cc
* libretroshare/src/retroshare: rsphoto.h
*
* RetroShare C++ 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".
*
* Created on: 22 Jul 2012
* Author: crispy
*/
#include "rsphotov2items.h"
#define GXS_PHOTO_SERIAL_DEBUG
uint32_t RsGxsPhotoSerialiser::size(RsItem* item)
{
uint32_t RsGxsPhotoSerialiser::size(RsItem* item) {
RsGxsPhotoPhotoItem* ppItem = NULL;
RsGxsPhotoAlbumItem* paItem = NULL;
if((ppItem = dynamic_cast<RsGxsPhotoPhotoItem*>(item)) != NULL)
{
return sizeGxsPhotoPhotoItem(ppItem);
}
else if((paItem = dynamic_cast<RsGxsPhotoAlbumItem*>(item)) != NULL)
{
return sizeGxsPhotoAlbumItem(paItem);
}
else
{
#ifdef GXS_PHOTO_SERIAL_DEBUG
#endif
return NULL;
}
}
bool RsGxsPhotoSerialiser::serialise(RsItem* item, void* data, uint32_t* size) {
bool RsGxsPhotoSerialiser::serialise(RsItem* item, void* data, uint32_t* size)
{
RsGxsPhotoPhotoItem* ppItem = NULL;
RsGxsPhotoAlbumItem* paItem = NULL;
if((ppItem = dynamic_cast<RsGxsPhotoPhotoItem*>(item)) != NULL)
{
return serialiseGxsPhotoPhotoItem(ppItem, data, size);
}
else if((paItem = dynamic_cast<RsGxsPhotoAlbumItem*>(item)) != NULL)
{
return serialiseGxsPhotoAlbumItem(paItem, data, size);
}
else
{
#ifdef GXS_PHOTO_SERIAL_DEBUG
#endif
return false;
}
RsItem* RsGxsPhotoSerialiser::deserialise(void* data, uint32_t* size) {
}
uint32_t RsGxsPhotoSerialiser::sizeGxsPhotoAlbumItem(RsGxsPhotoAlbumItem* item) {
RsItem* RsGxsPhotoSerialiser::deserialise(void* data, uint32_t* size)
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsGxsPhotoSerialiser::deserialise()" << std::endl;
#endif
/* get the type and size */
uint32_t rstype = getRsItemId(data);
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(RS_SERVICE_TYPE_PHOTO != getRsItemService(rstype)))
{
return NULL; /* wrong type */
}
switch(getRsItemSubType(rstype))
{
case RS_PKT_SUBTYPE_NXS_SYNC_GRP:
return deserialiseGxsPhotoPhotoItem(data, size);
case RS_PKT_SUBTYPE_NXS_SYNC_GRP_ITEM:
return deserialiseGxsPhotoAlbumItem(data, size);
default:
{
#ifdef GXS_PHOTO_SERIAL_DEBUG
std::cerr << "RsGxsPhotoSerialiser::deserialise(): subtype could not be dealt with"
<< std::endl;
#endif
break;
}
}
return NULL;
}
uint32_t RsGxsPhotoSerialiser::sizeGxsPhotoAlbumItem(RsGxsPhotoAlbumItem* item)
{
const RsPhotoAlbum& album = item->album;
uint32_t s = 8; // header
s += GetTlvStringSize(album.mCaption);
s += GetTlvStringSize(album.mCategory);
s += GetTlvStringSize(album.mDescription);
s += GetTlvStringSize(album.mHashTags);
s += GetTlvStringSize(album.mOther);
s += GetTlvStringSize(album.mPhotoPath);
s += GetTlvStringSize(album.mPhotographer);
s += GetTlvStringSize(album.mWhen);
s += GetTlvStringSize(album.mWhere);
RsTlvBinaryData b(item->PacketService()); // TODO, need something more persisitent
b.setBinData(album.mThumbnail.data, album.mThumbnail.size);
s += GetTlvStringSize(album.mThumbnail.type);
s += b.TlvSize();
return s;
}
bool RsGxsPhotoSerialiser::serialiseGxsPhotoAlbumItem(RsGxsPhotoAlbumItem* item, void* data,
uint32_t* size) {
uint32_t* size)
{
#ifdef GXS_PHOTO_SERIAL_DEBUG
std::cerr << "RsGxsPhotoSerialiser::serialiseGxsPhotoAlbumItem()" << std::endl;
#endif
uint32_t tlvsize = sizeGxsPhotoAlbumItem(item);
uint32_t offset = 0;
if(*size < tlvsize){
#ifdef GXS_PHOTO_SERIAL_DEBUG
std::cerr << "RsGxsPhotoSerialiser::serialiseGxsPhotoAlbumItem()" << std::endl;
#endif
return false;
}
*size = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
/* GxsPhotoAlbumItem */
ok &= SetTlvString(data, tlvsize, &offset, 1, item->album.mCaption);
ok &= SetTlvString(data, tlvsize, &offset, 1, item->album.mCategory);
ok &= SetTlvString(data, tlvsize, &offset, 1, item->album.mDescription);
ok &= SetTlvString(data, tlvsize, &offset, 1, item->album.mHashTags);
ok &= SetTlvString(data, tlvsize, &offset, 1, item->album.mOther);
ok &= SetTlvString(data, tlvsize, &offset, 1, item->album.mPhotoPath);
ok &= SetTlvString(data, tlvsize, &offset, 1, item->album.mPhotographer);
ok &= SetTlvString(data, tlvsize, &offset, 1, item->album.mWhen);
ok &= SetTlvString(data, tlvsize, &offset, 1, item->album.mWhere);
ok &= SetTlvString(data, tlvsize, &offset, 1, item->album.mThumbnail.type);
RsTlvBinaryData b(item->PacketService()); // TODO, need something more persisitent
b.setBinData(item->album.mThumbnail.data, item->album.mThumbnail.size);
b.SetTlv(item->album.mThumbnail.data, tlvsize, &offset);
if(offset != tlvsize)
{
#ifdef GXS_PHOTO_SERIAL_DEBUG
std::cerr << "RsGxsPhotoSerialiser::serialiseGxsPhotoAlbumItem() FAIL Size Error! " << std::endl;
#endif
ok = false;
}
#ifdef GXS_PHOTO_SERIAL_DEBUG
if (!ok)
{
std::cerr << "RsGxsPhotoSerialiser::serialiseGxsPhotoAlbumItem() NOK" << std::endl;
}
#endif
return ok;
}
RsGxsPhotoAlbumItem* RsGxsPhotoSerialiser::deserialiseGxsPhotoAlbumItem(void* data,
uint32_t* size) {
uint32_t* size)
{
#ifdef GXS_PHOTO_SERIAL_DEBUG
std::cerr << "RsGxsPhotoSerialiser::deserialiseGxsPhotoAlbumItem()" << 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_TYPE_PHOTO != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_PHOTO_ITEM != getRsItemSubType(rstype)))
{
#ifdef GXS_PHOTO_SERIAL_DEBUG
std::cerr << "RsGxsPhotoSerialiser::deserialiseGxsPhotoAlbumItem() FAIL wrong type" << std::endl;
#endif
return NULL; /* wrong type */
}
uint32_t RsGxsPhotoSerialiser::sizeGxsPhotoPhotoItem(RsGxsPhotoPhotoItem* item) {
if (*size < rssize) /* check size */
{
#ifdef GXS_PHOTO_SERIAL_DEBUG
std::cerr << "RsGxsPhotoSerialiser::deserialiseGxsPhotoAlbumItem() FAIL wrong size" << std::endl;
#endif
return NULL; /* not enough data */
}
/* set the packet length */
*size = rssize;
bool ok = true;
RsGxsPhotoAlbumItem* item = new RsGxsPhotoAlbumItem();
/* skip the header */
offset += 8;
ok &= GetTlvString(data, rssize, &offset, 1, item->album.mCaption);
ok &= GetTlvString(data, rssize, &offset, 1, item->album.mCategory);
ok &= GetTlvString(data, rssize, &offset, 1, item->album.mDescription);
ok &= GetTlvString(data, rssize, &offset, 1, item->album.mHashTags);
ok &= GetTlvString(data, rssize, &offset, 1, item->album.mOther);
ok &= GetTlvString(data, rssize, &offset, 1, item->album.mPhotoPath);
ok &= GetTlvString(data, rssize, &offset, 1, item->album.mPhotographer);
ok &= GetTlvString(data, rssize, &offset, 1, item->album.mWhen);
ok &= GetTlvString(data, rssize, &offset, 1, item->album.mWhere);
ok &= GetTlvString(data, rssize, &offset, 1, item->album.mThumbnail.type);
RsTlvBinaryData b(RS_SERVICE_TYPE_PHOTO); // TODO, need something more persisitent
ok &= b.GetTlv(data, rssize, &offset);
item->album.mThumbnail.data = (uint8_t*)b.bin_data;
item->album.mThumbnail.size = b.bin_len;
b.TlvShallowClear();
if (offset != rssize)
{
#ifdef GXS_PHOTO_SERIAL_DEBUG
std::cerr << "RsGxsPhotoSerialiser::deserialiseGxsPhotoAlbumItem() FAIL size mismatch" << std::endl;
#endif
/* error */
delete item;
return NULL;
}
if (!ok)
{
#ifdef GXS_PHOTO_SERIAL_DEBUG
std::cerr << "RsGxsPhotoSerialiser::deserialiseGxsPhotoAlbumItem() NOK" << std::endl;
#endif
delete item;
return NULL;
}
return item;
}
uint32_t RsGxsPhotoSerialiser::sizeGxsPhotoPhotoItem(RsGxsPhotoPhotoItem* item)
{
const RsPhotoPhoto& photo = item->photo;
uint32_t s = 8; // header size
s += GetTlvStringSize(photo.mCaption);
s += GetTlvStringSize(photo.mCategory);
s += GetTlvStringSize(photo.mDescription);
s += GetTlvStringSize(photo.mHashTags);
s += GetTlvStringSize(photo.mOther);
s += GetTlvStringSize(photo.mPhotographer);
s += GetTlvStringSize(photo.mWhen);
s += GetTlvStringSize(photo.mWhere);
RsTlvBinaryData b(item->PacketService()); // TODO, need something more persisitent
b.setBinData(photo.mThumbnail.data, photo.mThumbnail.size);
s += GetTlvStringSize(photo.mThumbnail.type);
s += b.TlvSize();
return s;
}
bool RsGxsPhotoSerialiser::serialiseGxsPhotoPhotoItem(RsGxsPhotoPhotoItem* item, void* data,
uint32_t* size) {
uint32_t* size)
{
#ifdef GXS_PHOTO_SERIAL_DEBUG
std::cerr << "RsGxsPhotoSerialiser::serialiseGxsPhotoPhotoItem()" << std::endl;
#endif
uint32_t tlvsize = sizeGxsPhotoPhotoItem(item);
uint32_t offset = 0;
if(*size < tlvsize){
#ifdef GXS_PHOTO_SERIAL_DEBUG
std::cerr << "RsGxsPhotoSerialiser::serialiseGxsPhotoPhotoItem()" << std::endl;
#endif
return false;
}
*size = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
/* skip the header */
offset += 8;
/* GxsPhotoAlbumItem */
ok &= SetTlvString(data, tlvsize, &offset, 1, item->photo.mCaption);
ok &= SetTlvString(data, tlvsize, &offset, 1, item->photo.mCategory);
ok &= SetTlvString(data, tlvsize, &offset, 1, item->photo.mDescription);
ok &= SetTlvString(data, tlvsize, &offset, 1, item->photo.mHashTags);
ok &= SetTlvString(data, tlvsize, &offset, 1, item->photo.mOther);
ok &= SetTlvString(data, tlvsize, &offset, 1, item->photo.mPhotographer);
ok &= SetTlvString(data, tlvsize, &offset, 1, item->photo.mWhen);
ok &= SetTlvString(data, tlvsize, &offset, 1, item->photo.mWhere);
ok &= SetTlvString(data, tlvsize, &offset, 1, item->photo.mThumbnail.type);
RsTlvBinaryData b(RS_SERVICE_TYPE_PHOTO); // TODO, need something more persisitent
b.setBinData(item->photo.mThumbnail.data, item->photo.mThumbnail.size);
b.SetTlv(item->photo.mThumbnail.data, tlvsize, &offset);
if(offset != tlvsize)
{
#ifdef GXS_PHOTO_SERIAL_DEBUG
std::cerr << "RsGxsPhotoSerialiser::serialiseGxsPhotoPhotoItem() FAIL Size Error! " << std::endl;
#endif
ok = false;
}
#ifdef GXS_PHOTO_SERIAL_DEBUG
if (!ok)
{
std::cerr << "RsGxsPhotoSerialiser::serialiseGxsPhotoPhotoItem() NOK" << std::endl;
}
#endif
return ok;
}
RsGxsPhotoPhotoItem* RsGxsPhotoSerialiser::deserialiseGxsPhotoPhotoItem(void* data,
uint32_t* size) {
uint32_t* size)
{
#ifdef GXS_PHOTO_SERIAL_DEBUG
std::cerr << "RsGxsPhotoSerialiser::deserialiseGxsPhotoPhotoItem()" << 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_TYPE_PHOTO != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_PHOTO_SHOW_ITEM != getRsItemSubType(rstype)))
{
#ifdef GXS_PHOTO_SERIAL_DEBUG
std::cerr << "RsGxsPhotoSerialiser::deserialiseGxsPhotoPhotoItem() FAIL wrong type" << std::endl;
#endif
return NULL; /* wrong type */
}
if (*size < rssize) /* check size */
{
#ifdef GXS_PHOTO_SERIAL_DEBUG
std::cerr << "RsGxsPhotoSerialiser::deserialiseGxsPhotoPhotoItem() FAIL wrong size" << std::endl;
#endif
return NULL; /* not enough data */
}
/* set the packet length */
*size = rssize;
bool ok = true;
RsGxsPhotoPhotoItem* item = new RsGxsPhotoPhotoItem();
/* skip the header */
offset += 8;
ok &= GetTlvString(data, rssize, &offset, 1, item->photo.mCaption);
ok &= GetTlvString(data, rssize, &offset, 1, item->photo.mCategory);
ok &= GetTlvString(data, rssize, &offset, 1, item->photo.mDescription);
ok &= GetTlvString(data, rssize, &offset, 1, item->photo.mHashTags);
ok &= GetTlvString(data, rssize, &offset, 1, item->photo.mOther);
ok &= GetTlvString(data, rssize, &offset, 1, item->photo.mPhotographer);
ok &= GetTlvString(data, rssize, &offset, 1, item->photo.mWhen);
ok &= GetTlvString(data, rssize, &offset, 1, item->photo.mWhere);
ok &= GetTlvString(data, rssize, &offset, 1, item->photo.mThumbnail.type);
RsTlvBinaryData b(RS_SERVICE_TYPE_PHOTO); // TODO, need something more persisitent
ok &= b.GetTlv(data, rssize, &offset);
item->photo.mThumbnail.data = (uint8_t*)(b.bin_data);
item->photo.mThumbnail.size = b.bin_len;
b.TlvShallowClear();
if (offset != rssize)
{
#ifdef GXS_PHOTO_SERIAL_DEBUG
std::cerr << "RsGxsPhotoSerialiser::deserialiseGxsPhotoPhotoItem() FAIL size mismatch" << std::endl;
#endif
/* error */
delete item;
return NULL;
}
if (!ok)
{
#ifdef GXS_PHOTO_SERIAL_DEBUG
std::cerr << "RsGxsPhotoSerialiser::deserialiseGxsPhotoPhotoItem() NOK" << std::endl;
#endif
delete item;
return NULL;
}
return item;
}
void RsGxsPhotoAlbumItem::clear()
{
album.mCaption.clear();
album.mCategory.clear();
album.mDescription.clear();
album.mHashTags.clear();
album.mOther.clear();
album.mPhotoPath.clear();
album.mPhotographer.clear();
album.mWhen.clear();
album.mWhere.clear();
album.mThumbnail.deleteImage();
}
std::ostream& RsGxsPhotoAlbumItem::print(std::ostream& out, uint16_t indent)
{
printRsItemBase(out, "RsGxsPhotoAlbumItem", indent);
uint16_t int_Indent = indent + 2;
printRsItemEnd(out ,"RsGxsPhotoAlbumItem", indent);
return out;
}
void RsGxsPhotoPhotoItem::clear()
{
photo.mCaption.clear();
photo.mCategory.clear();
photo.mDescription.clear();
photo.mHashTags.clear();
photo.mOther.clear();
photo.mPhotographer.clear();
photo.mWhen.clear();
photo.mWhere.clear();
photo.mThumbnail.deleteImage();
}
std::ostream& RsGxsPhotoPhotoItem::print(std::ostream& out, uint16_t indent)
{
printRsItemBase(out, "RsGxsPhotoPhotoItem", indent);
uint16_t int_Indent = indent + 2;
printRsItemEnd(out ,"RsGxsPhotoPhotoItem", indent);
return out;
}

View File

@ -33,6 +33,7 @@
#include "serialiser/rstlvtypes.h"
#include "rsgxsitems.h"
#include "rsphotoitems.h"
#include "retroshare/rsphotoV2.h"
@ -41,7 +42,13 @@ class RsGxsPhotoAlbumItem : public RsGxsGrpItem
public:
RsGxsPhotoAlbumItem() {}
RsGxsPhotoAlbumItem(): RsGxsGrpItem(RS_SERVICE_TYPE_PHOTO,
RS_PKT_SUBTYPE_PHOTO_ITEM) { return;}
virtual void clear();
std::ostream &print(std::ostream &out, uint16_t indent = 0);
RsPhotoAlbum album;
};
@ -49,7 +56,11 @@ class RsGxsPhotoPhotoItem : public RsGxsMsgItem
{
public:
RsGxsPhotoPhotoItem() {}
RsGxsPhotoPhotoItem(): RsGxsMsgItem(RS_SERVICE_TYPE_PHOTO,
RS_PKT_SUBTYPE_PHOTO_SHOW_ITEM) {return; }
virtual void clear();
std::ostream &print(std::ostream &out, uint16_t indent = 0);
RsPhotoPhoto photo;
};