added unsubscribe protocol, updated GUI

git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-ChatLobby@4738 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2011-12-27 13:47:37 +00:00
parent cc57ab2462
commit 3addb36e4c
12 changed files with 213 additions and 19 deletions

View file

@ -57,7 +57,14 @@ std::ostream& RsChatMsgItem::print(std::ostream &out, uint16_t indent)
printRsItemEnd(out, "RsChatMsgItem", indent);
return out;
}
std::ostream& RsChatLobbyUnsubscribeItem::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsChatLobbyUnsubscribeItem", indent);
printIndent(out, indent);
out << "Lobby id: " << std::hex << lobby_id << std::endl;
printRsItemEnd(out, "RsChatLobbyUnsubscribeItem", indent);
return out;
}
std::ostream& RsChatLobbyConnectChallengeItem::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsChatLobbyConnectChallengeItem", indent);
@ -184,6 +191,7 @@ RsItem *RsChatSerialiser::deserialise(void *data, uint32_t *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) ;
case RS_PKT_SUBTYPE_CHAT_LOBBY_CHALLENGE: return new RsChatLobbyConnectChallengeItem(data,*pktsize) ;
case RS_PKT_SUBTYPE_CHAT_LOBBY_UNSUBSCRIBE: return new RsChatLobbyUnsubscribeItem(data,*pktsize) ;
default:
std::cerr << "Unknown packet type in chat!" << std::endl ;
return NULL ;
@ -200,6 +208,14 @@ uint32_t RsChatMsgItem::serial_size()
return s;
}
uint32_t RsChatLobbyUnsubscribeItem::serial_size()
{
uint32_t s = 8; /* header */
s += 8; // challenge code
return s ;
}
uint32_t RsChatLobbyConnectChallengeItem::serial_size()
{
uint32_t s = 8; /* header */
@ -347,6 +363,32 @@ bool RsChatLobbyMsgItem::serialise(void *data, uint32_t& pktsize)
return ok ;
}
bool RsChatLobbyUnsubscribeItem::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);
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 RsChatLobbyConnectChallengeItem::serialise(void *data, uint32_t& pktsize)
{
uint32_t tlvsize = serial_size() ;
@ -574,7 +616,23 @@ RsChatLobbyMsgItem::RsChatLobbyMsgItem(void *data,uint32_t /*size*/)
if (!ok)
std::cerr << "Unknown error while deserializing." << std::endl ;
}
RsChatLobbyUnsubscribeItem::RsChatLobbyUnsubscribeItem(void *data,uint32_t /*size*/)
: RsChatItem(RS_PKT_SUBTYPE_CHAT_LOBBY_UNSUBSCRIBE)
{
uint32_t rssize = getRsItemSize(data);
bool ok = true ;
std::cerr << "RsChatLobbyUnsubscribeItem: rsitem size is " << rssize << std::endl;
uint32_t offset = 8 ;
/* get mandatory parts first */
ok &= getRawUInt64(data, rssize, &offset, &lobby_id);
if (offset != rssize)
std::cerr << "Size error while deserializing." << std::endl ;
if (!ok)
std::cerr << "Unknown error while deserializing." << std::endl ;
}
RsChatLobbyConnectChallengeItem::RsChatLobbyConnectChallengeItem(void *data,uint32_t /*size*/)
: RsChatItem(RS_PKT_SUBTYPE_CHAT_LOBBY_CHALLENGE)
{

View file

@ -55,6 +55,7 @@ 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 ;
const uint8_t RS_PKT_SUBTYPE_CHAT_LOBBY_CHALLENGE = 0x09 ;
const uint8_t RS_PKT_SUBTYPE_CHAT_LOBBY_UNSUBSCRIBE= 0x0A ;
// for defining tags themselves and msg tags
const uint8_t RS_PKT_SUBTYPE_MSG_TAG_TYPE = 0x03;
@ -129,6 +130,22 @@ class RsChatLobbyMsgItem: public RsChatMsgItem
ChatLobbyNickName nick ;
};
class RsChatLobbyUnsubscribeItem: public RsChatItem
{
public:
RsChatLobbyUnsubscribeItem() :RsChatItem(RS_PKT_SUBTYPE_CHAT_LOBBY_UNSUBSCRIBE) {}
RsChatLobbyUnsubscribeItem(void *data,uint32_t size) ; // deserialization
virtual ~RsChatLobbyUnsubscribeItem() {}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0);
uint64_t lobby_id ;
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
};
class RsChatLobbyConnectChallengeItem: public RsChatItem
{
public:

View file

@ -644,6 +644,15 @@ void p3ChatService::receiveChatQueue()
continue ;
}
RsChatLobbyUnsubscribeItem *cu = dynamic_cast<RsChatLobbyUnsubscribeItem*>(item) ;
if(cu != NULL)
{
handleFriendUnsubscribeLobby(cu) ;
delete item ;
continue ;
}
std::cerr << "Received ChatItem of unhandled type: " << std::endl;
item->print(std::cerr,0) ;
@ -1711,11 +1720,63 @@ ChatLobbyId p3ChatService::createChatLobby(const std::string& lobby_name,const s
return lobby_id ;
}
void p3ChatService::handleFriendUnsubscribeLobby(RsChatLobbyUnsubscribeItem *item)
{
RsStackMutex stack(mChatMtx); /********** STACK LOCKED MTX ******/
std::map<ChatLobbyId,ChatLobbyEntry>::iterator it = _chat_lobbys.find(item->lobby_id) ;
std::cerr << "Received unsubscribed to lobby " << item->lobby_id << ", from friend " << item->PeerId() << std::endl;
if(it == _chat_lobbys.end())
{
std::cerr << "Chat lobby " << item->lobby_id << " does not exist ! Can't unsubscribe!" << std::endl;
return ;
}
for(std::set<std::string>::iterator it2(it->second.participating_friends.begin());it2!=it->second.participating_friends.end();++it2)
if(*it2 == item->PeerId())
{
std::cerr << " removing peer id " << item->PeerId() << " from participant list of lobby " << item->lobby_id << std::endl;
it->second.participating_friends.erase(it2) ;
break ;
}
}
void p3ChatService::unsubscribeChatLobby(const ChatLobbyId& id)
{
RsStackMutex stack(mChatMtx); /********** STACK LOCKED MTX ******/
// send a lobby leaving packet. To be implemented.
std::map<ChatLobbyId,ChatLobbyEntry>::iterator it = _chat_lobbys.find(id) ;
if(it == _chat_lobbys.end())
{
std::cerr << "Chat lobby " << id << " does not exist ! Can't unsubscribe!" << std::endl;
return ;
}
// send a lobby leaving packet to all friends
for(std::set<std::string>::const_iterator it2(it->second.participating_friends.begin());it2!=it->second.participating_friends.end();++it2)
{
RsChatLobbyUnsubscribeItem *item = new RsChatLobbyUnsubscribeItem ;
item->lobby_id = id ;
item->PeerId(*it2) ;
sendItem(item) ;
}
// remove lobby information
_chat_lobbys.erase(it) ;
for(std::map<std::string,ChatLobbyId>::iterator it2(_lobby_ids.begin());it2!=_lobby_ids.end();++it2)
if(it2->second == id)
{
_lobby_ids.erase(it2) ;
break ;
}
// done!
}
bool p3ChatService::setDefaultNickNameForChatLobby(const std::string& nick)
{

View file

@ -219,6 +219,7 @@ class p3ChatService: public p3Service, public p3Config, public pqiMonitor
void checkAndRedirectMsgToLobby(RsChatMsgItem*) ;
void handleConnectionChallenge(RsChatLobbyConnectChallengeItem *item) ;
void sendConnectionChallenge(ChatLobbyId id) ;
void handleFriendUnsubscribeLobby(RsChatLobbyUnsubscribeItem*) ;
void cleanLobbyCaches() ;
static std::string makeVirtualPeerId(ChatLobbyId) ;