mirror of
https://github.com/RetroShare/RetroShare.git
synced 2024-12-25 23:49:35 -05:00
added serialisation code for lobby msgs, and test code for the new RsItems
git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-ChatLobby@4692 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
cd30487898
commit
723a8ca7f0
@ -57,7 +57,38 @@ std::ostream& RsChatMsgItem::print(std::ostream &out, uint16_t indent)
|
||||
printRsItemEnd(out, "RsChatMsgItem", indent);
|
||||
return out;
|
||||
}
|
||||
std::ostream& RsChatLobbyMsgItem::print(std::ostream &out, uint16_t indent)
|
||||
{
|
||||
RsChatMsgItem::print(out,indent) ;
|
||||
|
||||
printIndent(out, indent);
|
||||
out << "Lobby ID: " << std::hex << lobby_id << std::endl;
|
||||
printIndent(out, indent);
|
||||
out << "Msg ID: " << std::hex << msg_id << std::dec << std::endl;
|
||||
printIndent(out, indent);
|
||||
out << "Nick: " << nick << std::dec << std::endl;
|
||||
|
||||
printRsItemEnd(out, "RsChatLobbyMsgItem", indent);
|
||||
return out;
|
||||
}
|
||||
|
||||
std::ostream& RsChatLobbyInviteItem::print(std::ostream &out, uint16_t indent)
|
||||
{
|
||||
printRsItemBase(out, "RsChatLobbyInviteItem", indent);
|
||||
uint16_t int_Indent = indent + 2;
|
||||
|
||||
printIndent(out, int_Indent);
|
||||
out << "peerId: " << PeerId() << std::endl;
|
||||
|
||||
printIndent(out, int_Indent);
|
||||
out << "lobby id: " << std::hex << lobby_id << std::dec << std::endl;
|
||||
|
||||
printIndent(out, int_Indent);
|
||||
out << "lobby name: " << lobby_name << std::endl;
|
||||
|
||||
printRsItemEnd(out, "RsChatLobbyInviteItem", indent);
|
||||
return out;
|
||||
}
|
||||
std::ostream& RsPrivateChatMsgConfigItem::print(std::ostream &out, uint16_t indent)
|
||||
{
|
||||
printRsItemBase(out, "RsPrivateChatMsgConfigItem", indent);
|
||||
@ -137,10 +168,12 @@ RsItem *RsChatSerialiser::deserialise(void *data, uint32_t *pktsize)
|
||||
|
||||
switch(getRsItemSubType(rstype))
|
||||
{
|
||||
case RS_PKT_SUBTYPE_DEFAULT: return new RsChatMsgItem(data,*pktsize) ;
|
||||
case RS_PKT_SUBTYPE_DEFAULT: return new RsChatMsgItem(data,*pktsize) ;
|
||||
case RS_PKT_SUBTYPE_PRIVATECHATMSG_CONFIG: return new RsPrivateChatMsgConfigItem(data,*pktsize) ;
|
||||
case RS_PKT_SUBTYPE_CHAT_STATUS: return new RsChatStatusItem(data,*pktsize) ;
|
||||
case RS_PKT_SUBTYPE_CHAT_AVATAR: return new RsChatAvatarItem(data,*pktsize) ;
|
||||
case RS_PKT_SUBTYPE_CHAT_STATUS: return new RsChatStatusItem(data,*pktsize) ;
|
||||
case RS_PKT_SUBTYPE_CHAT_AVATAR: return new RsChatAvatarItem(data,*pktsize) ;
|
||||
case RS_PKT_SUBTYPE_CHAT_LOBBY_MSG: return new RsChatLobbyMsgItem(data,*pktsize) ;
|
||||
case RS_PKT_SUBTYPE_CHAT_LOBBY_INVITE: return new RsChatLobbyInviteItem(data,*pktsize) ;
|
||||
default:
|
||||
std::cerr << "Unknown packet type in chat!" << std::endl ;
|
||||
return NULL ;
|
||||
@ -157,6 +190,23 @@ uint32_t RsChatMsgItem::serial_size()
|
||||
return s;
|
||||
}
|
||||
|
||||
uint32_t RsChatLobbyMsgItem::serial_size()
|
||||
{
|
||||
uint32_t s = RsChatMsgItem::serial_size() ; // parent
|
||||
s += 8; // lobby_id
|
||||
s += 8; // msg_id
|
||||
s += GetTlvStringSize(nick) ; // nick
|
||||
|
||||
return s;
|
||||
}
|
||||
uint32_t RsChatLobbyInviteItem::serial_size()
|
||||
{
|
||||
uint32_t s = 8; /* header */
|
||||
s += 8; // lobby_id
|
||||
s += GetTlvStringSize(lobby_name) ; // lobby name
|
||||
|
||||
return s;
|
||||
}
|
||||
uint32_t RsPrivateChatMsgConfigItem::serial_size()
|
||||
{
|
||||
uint32_t s = 8; /* header */
|
||||
@ -201,7 +251,7 @@ RsChatAvatarItem::~RsChatAvatarItem()
|
||||
/* serialise the data to the buffer */
|
||||
bool RsChatMsgItem::serialise(void *data, uint32_t& pktsize)
|
||||
{
|
||||
uint32_t tlvsize = serial_size() ;
|
||||
uint32_t tlvsize = RsChatMsgItem::serial_size() ;
|
||||
uint32_t offset = 0;
|
||||
|
||||
if (pktsize < tlvsize)
|
||||
@ -228,7 +278,7 @@ bool RsChatMsgItem::serialise(void *data, uint32_t& pktsize)
|
||||
#ifdef CHAT_DEBUG
|
||||
std::cerr << "Serialized the following message:" << std::endl;
|
||||
std::cerr << "========== BEGIN MESSAGE =========" << std::endl;
|
||||
for(int i=0;i<message.length();++i)
|
||||
for(uint32_t i=0;i<message.length();++i)
|
||||
std::cerr << (char)message[i] ;
|
||||
std::cerr << std::endl;
|
||||
std::cerr << "=========== END MESSAGE ==========" << std::endl;
|
||||
@ -244,12 +294,77 @@ bool RsChatMsgItem::serialise(void *data, uint32_t& pktsize)
|
||||
#ifdef CHAT_DEBUG
|
||||
std::cerr << "computed size: " << 256*((unsigned char*)data)[6]+((unsigned char*)data)[7] << std::endl ;
|
||||
#endif
|
||||
return ok ;
|
||||
}
|
||||
|
||||
std::cerr << "Serialization result: " ;
|
||||
for(int i=0;i<20;++i)
|
||||
std::cerr << (int)((uint8_t*)data)[i] << " " ;
|
||||
std::cerr << std::endl ;
|
||||
return ok;
|
||||
/* serialise the data to the buffer */
|
||||
bool RsChatLobbyMsgItem::serialise(void *data, uint32_t& pktsize)
|
||||
{
|
||||
uint32_t tlvsize = serial_size() ;
|
||||
|
||||
if (pktsize < tlvsize)
|
||||
return false; /* not enough space */
|
||||
|
||||
bool ok = true;
|
||||
ok &= RsChatMsgItem::serialise(data,pktsize) ; // first, serialize parent
|
||||
uint32_t offset = pktsize;
|
||||
ok &= setRsItemHeader(data, tlvsize, PacketId(), tlvsize); // correct header!
|
||||
|
||||
pktsize = tlvsize;
|
||||
|
||||
/* add mandatory parts first */
|
||||
ok &= setRawUInt64(data, tlvsize, &offset, lobby_id);
|
||||
ok &= setRawUInt64(data, tlvsize, &offset, msg_id);
|
||||
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_NAME, nick);
|
||||
|
||||
#ifdef CHAT_DEBUG
|
||||
std::cerr << "Serialized the following message:" << std::endl;
|
||||
std::cerr << "========== BEGIN MESSAGE =========" << std::endl;
|
||||
for(uint32_t i=0;i<message.length();++i)
|
||||
std::cerr << (char)message[i] ;
|
||||
std::cerr << std::endl;
|
||||
std::cerr << "=========== END MESSAGE ==========" << std::endl;
|
||||
#endif
|
||||
|
||||
if (offset != tlvsize)
|
||||
{
|
||||
ok = false;
|
||||
#ifdef CHAT_DEBUG
|
||||
std::cerr << "RsChatSerialiser::serialiseItem() Size Error! " << std::endl;
|
||||
#endif
|
||||
}
|
||||
#ifdef CHAT_DEBUG
|
||||
std::cerr << "computed size: " << 256*((unsigned char*)data)[6]+((unsigned char*)data)[7] << std::endl ;
|
||||
#endif
|
||||
return ok ;
|
||||
}
|
||||
|
||||
bool RsChatLobbyInviteItem::serialise(void *data, uint32_t& pktsize)
|
||||
{
|
||||
uint32_t tlvsize = serial_size() ;
|
||||
|
||||
if (pktsize < tlvsize)
|
||||
return false; /* not enough space */
|
||||
|
||||
bool ok = true ;
|
||||
ok &= setRsItemHeader(data, tlvsize, PacketId(), tlvsize); // correct header!
|
||||
uint32_t offset = 8 ;
|
||||
|
||||
ok &= setRawUInt64(data, tlvsize, &offset, lobby_id);
|
||||
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_NAME, lobby_name);
|
||||
|
||||
if (offset != tlvsize)
|
||||
{
|
||||
ok = false;
|
||||
#ifdef CHAT_DEBUG
|
||||
std::cerr << "RsChatSerialiser::serialiseItem() Size Error! " << std::endl;
|
||||
#endif
|
||||
}
|
||||
#ifdef CHAT_DEBUG
|
||||
std::cerr << "computed size: " << 256*((unsigned char*)data)[6]+((unsigned char*)data)[7] << std::endl ;
|
||||
#endif
|
||||
pktsize = tlvsize ;
|
||||
return ok ;
|
||||
}
|
||||
|
||||
bool RsPrivateChatMsgConfigItem::serialise(void *data, uint32_t& pktsize)
|
||||
@ -377,8 +492,8 @@ bool RsChatAvatarItem::serialise(void *data, uint32_t& pktsize)
|
||||
|
||||
return ok;
|
||||
}
|
||||
RsChatMsgItem::RsChatMsgItem(void *data,uint32_t /*size*/)
|
||||
: RsChatItem(RS_PKT_SUBTYPE_DEFAULT)
|
||||
RsChatMsgItem::RsChatMsgItem(void *data,uint32_t /*size*/,uint8_t subtype)
|
||||
: RsChatItem(subtype)
|
||||
{
|
||||
uint32_t offset = 8; // skip the header
|
||||
uint32_t rssize = getRsItemSize(data);
|
||||
@ -403,6 +518,50 @@ RsChatMsgItem::RsChatMsgItem(void *data,uint32_t /*size*/)
|
||||
std::cerr << "Unknown error while deserializing." << std::endl ;
|
||||
}
|
||||
|
||||
RsChatLobbyMsgItem::RsChatLobbyMsgItem(void *data,uint32_t /*size*/)
|
||||
: RsChatMsgItem(data,0,RS_PKT_SUBTYPE_CHAT_LOBBY_MSG)
|
||||
{
|
||||
uint32_t rssize = getRsItemSize(data);
|
||||
bool ok = true ;
|
||||
|
||||
uint32_t offset = RsChatMsgItem::serial_size() ;
|
||||
|
||||
/* get mandatory parts first */
|
||||
ok &= getRawUInt64(data, rssize, &offset, &lobby_id);
|
||||
ok &= getRawUInt64(data, rssize, &offset, &msg_id);
|
||||
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_NAME, nick);
|
||||
|
||||
#ifdef CHAT_DEBUG
|
||||
std::cerr << "Building new chat msg item." << std::endl ;
|
||||
#endif
|
||||
if (offset != rssize)
|
||||
std::cerr << "Size error while deserializing." << std::endl ;
|
||||
if (!ok)
|
||||
std::cerr << "Unknown error while deserializing." << std::endl ;
|
||||
}
|
||||
|
||||
RsChatLobbyInviteItem::RsChatLobbyInviteItem(void *data,uint32_t /*size*/)
|
||||
: RsChatItem(RS_PKT_SUBTYPE_CHAT_LOBBY_INVITE)
|
||||
{
|
||||
uint32_t rssize = getRsItemSize(data);
|
||||
bool ok = true ;
|
||||
|
||||
std::cerr << "RsChatLobbyInviteItem: rsitem size is " << rssize << std::endl;
|
||||
uint32_t offset = 8 ;
|
||||
|
||||
/* get mandatory parts first */
|
||||
ok &= getRawUInt64(data, rssize, &offset, &lobby_id);
|
||||
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_NAME, lobby_name);
|
||||
|
||||
#ifdef CHAT_DEBUG
|
||||
std::cerr << "Building new chat msg item." << std::endl ;
|
||||
#endif
|
||||
if (offset != rssize)
|
||||
std::cerr << "Size error while deserializing." << std::endl ;
|
||||
if (!ok)
|
||||
std::cerr << "Unknown error while deserializing." << std::endl ;
|
||||
}
|
||||
|
||||
RsPrivateChatMsgConfigItem::RsPrivateChatMsgConfigItem(void *data,uint32_t /*size*/)
|
||||
: RsChatItem(RS_PKT_SUBTYPE_PRIVATECHATMSG_CONFIG)
|
||||
{
|
||||
|
@ -46,18 +46,20 @@ const uint32_t RS_CHAT_FLAG_CUSTOM_STATE_AVAILABLE = 0x0080;
|
||||
const uint32_t RS_CHAT_FLAG_PARTIAL_MESSAGE = 0x0100;
|
||||
const uint32_t RS_CHAT_FLAG_LOBBY = 0x0200;
|
||||
|
||||
const uint32_t RS_CHATMSG_CONFIGFLAG_INCOMING = 0x0001;
|
||||
const uint32_t RS_CHATMSG_CONFIGFLAG_INCOMING = 0x0001;
|
||||
|
||||
const uint8_t RS_PKT_SUBTYPE_CHAT_AVATAR = 0x03 ;
|
||||
const uint8_t RS_PKT_SUBTYPE_CHAT_STATUS = 0x04 ;
|
||||
const uint8_t RS_PKT_SUBTYPE_PRIVATECHATMSG_CONFIG = 0x05 ;
|
||||
const uint8_t RS_PKT_SUBTYPE_CHAT_LOBBY_MSG = 0x06 ;
|
||||
const uint8_t RS_PKT_SUBTYPE_CHAT_LOBBY_INVITE = 0x07 ;
|
||||
const uint8_t RS_PKT_SUBTYPE_CHAT_LOBBY_ACCEPT = 0x08 ;
|
||||
|
||||
// for defining tags themselves and msg tags
|
||||
const uint8_t RS_PKT_SUBTYPE_MSG_TAG_TYPE = 0x03;
|
||||
const uint8_t RS_PKT_SUBTYPE_MSG_TAGS = 0x04;
|
||||
const uint8_t RS_PKT_SUBTYPE_MSG_SRC_TAG = 0x05;
|
||||
const uint8_t RS_PKT_SUBTYPE_MSG_PARENT_TAG = 0x06;
|
||||
const uint8_t RS_PKT_SUBTYPE_MSG_TAG_TYPE = 0x03;
|
||||
const uint8_t RS_PKT_SUBTYPE_MSG_TAGS = 0x04;
|
||||
const uint8_t RS_PKT_SUBTYPE_MSG_SRC_TAG = 0x05;
|
||||
const uint8_t RS_PKT_SUBTYPE_MSG_PARENT_TAG = 0x06;
|
||||
|
||||
typedef uint64_t ChatLobbyId ;
|
||||
typedef uint64_t ChatLobbyMsgId ;
|
||||
@ -89,7 +91,7 @@ class RsChatMsgItem: public RsChatItem
|
||||
RsChatMsgItem() :RsChatItem(RS_PKT_SUBTYPE_DEFAULT) {}
|
||||
RsChatMsgItem(uint8_t subtype) :RsChatItem(subtype) {}
|
||||
|
||||
RsChatMsgItem(void *data,uint32_t size) ; // deserialization
|
||||
RsChatMsgItem(void *data,uint32_t size,uint8_t subtype = RS_PKT_SUBTYPE_DEFAULT) ; // deserialization
|
||||
|
||||
virtual ~RsChatMsgItem() {}
|
||||
virtual void clear() {}
|
||||
@ -110,17 +112,34 @@ class RsChatLobbyMsgItem: public RsChatMsgItem
|
||||
public:
|
||||
RsChatLobbyMsgItem() :RsChatMsgItem(RS_PKT_SUBTYPE_CHAT_LOBBY_MSG) {}
|
||||
|
||||
RsChatLobbyMsgItem(void *data,uint32_t size) {} // deserialization /// TODO!!!
|
||||
RsChatLobbyMsgItem(void *data,uint32_t size) ; // deserialization /// TODO!!!
|
||||
|
||||
virtual ~RsChatLobbyMsgItem() {}
|
||||
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0);
|
||||
|
||||
virtual bool serialise(void *data,uint32_t& size) ; // Isn't it better that items can serialize themselves ?
|
||||
virtual uint32_t serial_size() ; // deserialise is handled using a constructor
|
||||
|
||||
ChatLobbyId lobby_id ;
|
||||
ChatLobbyMsgId msg_id ;
|
||||
ChatLobbyNickName nick ;
|
||||
};
|
||||
|
||||
class RsChatLobbyInviteItem: public RsChatItem
|
||||
{
|
||||
public:
|
||||
RsChatLobbyInviteItem() :RsChatItem(RS_PKT_SUBTYPE_CHAT_LOBBY_INVITE) {}
|
||||
RsChatLobbyInviteItem(void *data,uint32_t size) ; // deserialization
|
||||
|
||||
virtual ~RsChatLobbyInviteItem() {}
|
||||
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0);
|
||||
|
||||
ChatLobbyId lobby_id ;
|
||||
std::string lobby_name ;
|
||||
|
||||
/// TODO !!!
|
||||
virtual bool serialise(void *data,uint32_t& size) { return true ; } // Isn't it better that items can serialize themselves ?
|
||||
virtual uint32_t serial_size() { return 0;} // deserialise is handled using a constructor
|
||||
virtual bool serialise(void *data,uint32_t& size) ; // Isn't it better that items can serialize themselves ?
|
||||
virtual uint32_t serial_size() ; // deserialise is handled using a constructor
|
||||
};
|
||||
|
||||
/*!
|
||||
|
@ -25,6 +25,7 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "util/rsrandom.h"
|
||||
#include "serialiser/rsmsgitems.h"
|
||||
#include "serialiser/rstlvutil.h"
|
||||
#include "util/utest.h"
|
||||
@ -41,6 +42,24 @@ RsSerialType* init_item(RsChatMsgItem& cmi)
|
||||
|
||||
return new RsChatSerialiser();
|
||||
}
|
||||
RsSerialType* init_item(RsChatLobbyMsgItem& cmi)
|
||||
{
|
||||
RsSerialType *serial = init_item( *dynamic_cast<RsChatMsgItem*>(&cmi)) ;
|
||||
|
||||
cmi.msg_id = RSRandom::random_u64() ;
|
||||
cmi.lobby_id = RSRandom::random_u64() ;
|
||||
cmi.nick = "My nickname" ;
|
||||
|
||||
return serial ;
|
||||
}
|
||||
|
||||
RsSerialType* init_item(RsChatLobbyInviteItem& cmi)
|
||||
{
|
||||
cmi.lobby_id = RSRandom::random_u64() ;
|
||||
cmi.lobby_name = "Name of the lobby" ;
|
||||
|
||||
return new RsChatSerialiser();
|
||||
}
|
||||
|
||||
RsSerialType* init_item(RsPrivateChatMsgConfigItem& pcmi)
|
||||
{
|
||||
@ -162,8 +181,25 @@ bool operator ==(const RsChatStatusItem& csiLeft, const RsChatStatusItem& csiRig
|
||||
|
||||
return true;
|
||||
}
|
||||
bool operator ==(const RsChatLobbyMsgItem& csiLeft, const RsChatLobbyMsgItem& csiRight)
|
||||
{
|
||||
if(! ( (RsChatMsgItem&)csiLeft == (RsChatMsgItem&)csiRight))
|
||||
return false ;
|
||||
|
||||
if(csiLeft.lobby_id != csiRight.lobby_id) return false ;
|
||||
if(csiLeft.msg_id != csiRight.msg_id) return false ;
|
||||
if(csiLeft.nick != csiRight.nick) return false ;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool operator ==(const RsChatLobbyInviteItem& csiLeft, const RsChatLobbyInviteItem& csiRight)
|
||||
{
|
||||
if(csiLeft.lobby_id != csiRight.lobby_id) return false ;
|
||||
if(csiLeft.lobby_name != csiRight.lobby_name) return false ;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool operator ==(const RsChatAvatarItem& caiLeft, const RsChatAvatarItem& caiRight)
|
||||
{
|
||||
@ -241,7 +277,8 @@ bool operator ==(const RsMsgParentId& msLeft, const RsMsgParentId& msRight)
|
||||
int main()
|
||||
{
|
||||
test_RsItem<RsChatMsgItem >(); REPORT("Serialise/Deserialise RsChatMsgItem");
|
||||
test_RsItem<RsChatMsgItem >(); REPORT("Serialise/Deserialise RsPrivateChatMsgConfigItem");
|
||||
test_RsItem<RsChatLobbyMsgItem >(); REPORT("Serialise/Deserialise RsChatLobbyMsgItem");
|
||||
test_RsItem<RsChatLobbyInviteItem >(); REPORT("Serialise/Deserialise RsChatLobbyInviteItem");
|
||||
test_RsItem<RsChatStatusItem >(); REPORT("Serialise/Deserialise RsChatStatusItem");
|
||||
test_RsItem<RsChatAvatarItem >(); REPORT("Serialise/Deserialise RsChatAvatarItem");
|
||||
test_RsItem<RsMsgItem >(); REPORT("Serialise/Deserialise RsMsgItem");
|
||||
|
Loading…
Reference in New Issue
Block a user