2008-02-05 08:45:04 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* libretroshare/src/serialiser: rsbaseitems.cc
|
|
|
|
*
|
|
|
|
* RetroShare Serialiser.
|
|
|
|
*
|
|
|
|
* Copyright 2007-2008 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".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "serialiser/rsbaseserial.h"
|
|
|
|
#include "serialiser/rsrankitems.h"
|
|
|
|
#include "serialiser/rstlvbase.h"
|
|
|
|
|
|
|
|
#define RSSERIAL_DEBUG 1
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
void RsRankMsg::clear()
|
|
|
|
{
|
|
|
|
rid.clear();
|
|
|
|
timestamp = 0;
|
|
|
|
title.clear();
|
|
|
|
comment.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream &RsRankMsg::print(std::ostream &out, uint16_t indent)
|
|
|
|
{
|
|
|
|
printRsItemBase(out, "RsRankMsg", indent);
|
|
|
|
uint16_t int_Indent = indent + 2;
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "rid: " << rid << std::endl;
|
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "timestamp: " << timestamp << std::endl;
|
|
|
|
|
2008-04-15 12:35:46 -04:00
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
printIndent(out, int_Indent);
|
|
|
|
|
|
|
|
std::string cnv_title(title.begin(), title.end());
|
|
|
|
out << "msg: " << cnv_title << std::endl;
|
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
std::string cnv_comment(comment.begin(), comment.end());
|
|
|
|
out << "comment: " << cnv_comment << std::endl;
|
|
|
|
|
2008-04-15 12:35:46 -04:00
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "score: " << score << std::endl;
|
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
printRsItemEnd(out, "RsRankMsg", indent);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
void RsRankLinkMsg::clear()
|
|
|
|
{
|
|
|
|
rid.clear();
|
2008-03-31 10:06:59 -04:00
|
|
|
pid.clear();
|
2008-02-05 08:45:04 -05:00
|
|
|
timestamp = 0;
|
|
|
|
title.clear();
|
|
|
|
comment.clear();
|
2008-04-15 12:35:46 -04:00
|
|
|
score = 0;
|
2008-02-05 08:45:04 -05:00
|
|
|
linktype = 0;
|
|
|
|
link.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream &RsRankLinkMsg::print(std::ostream &out, uint16_t indent)
|
|
|
|
{
|
|
|
|
printRsItemBase(out, "RsRankLinkMsg", indent);
|
|
|
|
uint16_t int_Indent = indent + 2;
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "rid: " << rid << std::endl;
|
2008-03-31 10:06:59 -04:00
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "pid: " << pid << std::endl;
|
2008-02-05 08:45:04 -05:00
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "timestamp: " << timestamp << std::endl;
|
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
|
|
|
|
std::string cnv_title(title.begin(), title.end());
|
|
|
|
out << "msg: " << cnv_title << std::endl;
|
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
std::string cnv_comment(comment.begin(), comment.end());
|
|
|
|
out << "comment: " << cnv_comment << std::endl;
|
|
|
|
|
2008-04-15 12:35:46 -04:00
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "score: " << score << std::endl;
|
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "linktype: " << linktype << std::endl;
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
std::string cnv_link(link.begin(), link.end());
|
|
|
|
out << "link: " << cnv_link << std::endl;
|
|
|
|
|
|
|
|
printRsItemEnd(out, "RsRankLinkMsg", indent);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t RsRankSerialiser::sizeLink(RsRankLinkMsg *item)
|
|
|
|
{
|
|
|
|
uint32_t s = 8; /* header */
|
|
|
|
s += GetTlvStringSize(item->rid);
|
2008-03-31 10:06:59 -04:00
|
|
|
s += GetTlvStringSize(item->pid);
|
2008-02-05 08:45:04 -05:00
|
|
|
s += 4; /* timestamp */
|
|
|
|
s += GetTlvWideStringSize(item->title);
|
|
|
|
s += GetTlvWideStringSize(item->comment);
|
2008-04-15 12:35:46 -04:00
|
|
|
s += 4; /* score */
|
2008-02-05 08:45:04 -05:00
|
|
|
s += 4; /* linktype */
|
|
|
|
s += GetTlvWideStringSize(item->link);
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* serialise the data to the buffer */
|
|
|
|
bool RsRankSerialiser::serialiseLink(RsRankLinkMsg *item, void *data, uint32_t *pktsize)
|
|
|
|
{
|
|
|
|
uint32_t tlvsize = sizeLink(item);
|
|
|
|
uint32_t offset = 0;
|
|
|
|
|
|
|
|
if (*pktsize < tlvsize)
|
|
|
|
return false; /* not enough space */
|
|
|
|
|
|
|
|
*pktsize = tlvsize;
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
|
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
/* add mandatory parts first */
|
|
|
|
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_GENID, item->rid);
|
2008-03-31 10:06:59 -04:00
|
|
|
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_PEERID, item->pid);
|
2008-02-05 08:45:04 -05:00
|
|
|
|
|
|
|
ok &= setRawUInt32(data, tlvsize, &offset, item->timestamp);
|
|
|
|
|
|
|
|
ok &= SetTlvWideString(data, tlvsize, &offset, TLV_TYPE_WSTR_TITLE, item->title);
|
|
|
|
ok &= SetTlvWideString(data, tlvsize, &offset, TLV_TYPE_WSTR_COMMENT, item->comment);
|
|
|
|
|
2008-04-15 12:35:46 -04:00
|
|
|
ok &= setRawUInt32(data, tlvsize, &offset, *((uint32_t *) &(item->score)));
|
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
ok &= setRawUInt32(data, tlvsize, &offset, item->linktype);
|
|
|
|
|
|
|
|
ok &= SetTlvWideString(data, tlvsize, &offset, TLV_TYPE_WSTR_LINK, item->link);
|
|
|
|
|
|
|
|
if (offset != tlvsize)
|
|
|
|
{
|
|
|
|
ok = false;
|
|
|
|
std::cerr << "RsRankLinkSerialiser::serialiseLink() Size Error! " << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsRankLinkMsg *RsRankSerialiser::deserialiseLink(void *data, uint32_t *pktsize)
|
|
|
|
{
|
|
|
|
/* 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_RANK != getRsItemService(rstype)) ||
|
2008-04-15 12:35:46 -04:00
|
|
|
(RS_PKT_SUBTYPE_RANK_LINK3 != getRsItemSubType(rstype)))
|
2008-02-05 08:45:04 -05:00
|
|
|
{
|
|
|
|
return NULL; /* wrong type */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*pktsize < rssize) /* check size */
|
|
|
|
return NULL; /* not enough data */
|
|
|
|
|
|
|
|
/* set the packet length */
|
|
|
|
*pktsize = rssize;
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
/* ready to load */
|
|
|
|
RsRankLinkMsg *item = new RsRankLinkMsg();
|
|
|
|
item->clear();
|
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
/* get mandatory parts first */
|
|
|
|
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_GENID, item->rid);
|
2008-03-31 10:06:59 -04:00
|
|
|
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_PEERID, item->pid);
|
2008-02-05 08:45:04 -05:00
|
|
|
ok &= getRawUInt32(data, rssize, &offset, &(item->timestamp));
|
|
|
|
ok &= GetTlvWideString(data, rssize, &offset, TLV_TYPE_WSTR_TITLE, item->title);
|
|
|
|
ok &= GetTlvWideString(data, rssize, &offset, TLV_TYPE_WSTR_COMMENT, item->comment);
|
2008-04-15 12:35:46 -04:00
|
|
|
ok &= getRawUInt32(data, rssize, &offset, (uint32_t *) &(item->score));
|
2008-02-05 08:45:04 -05:00
|
|
|
ok &= getRawUInt32(data, rssize, &offset, &(item->linktype));
|
|
|
|
ok &= GetTlvWideString(data, rssize, &offset, TLV_TYPE_WSTR_LINK, item->link);
|
|
|
|
|
|
|
|
if (offset != rssize)
|
|
|
|
{
|
|
|
|
/* error */
|
|
|
|
delete item;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok)
|
|
|
|
{
|
|
|
|
delete item;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t RsRankSerialiser::size(RsItem *item)
|
|
|
|
{
|
|
|
|
return sizeLink((RsRankLinkMsg *) item);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RsRankSerialiser::serialise(RsItem *item, void *data, uint32_t *pktsize)
|
|
|
|
{
|
|
|
|
return serialiseLink((RsRankLinkMsg *) item, data, pktsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
RsItem *RsRankSerialiser::deserialise(void *data, uint32_t *pktsize)
|
|
|
|
{
|
|
|
|
return deserialiseLink(data, pktsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
|