improved the logic in distant chat. Now all traffic is referred to by the GXS id of the contact instead of virtual peer id which would change with tunnel route. Work still needed to fix up the GUI

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@7687 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2014-11-16 22:46:18 +00:00
parent b4ee877c64
commit 8480d46eb5
9 changed files with 135 additions and 137 deletions

View File

@ -83,7 +83,7 @@ bool DistantChatService::handleRecvItem(RsChatItem *item)
bool DistantChatService::handleOutgoingItem(RsChatItem *item)
{
for(std::map<TurtleFileHash,DistantChatPeerInfo>::const_iterator it=_distant_chat_peers.begin();it!=_distant_chat_peers.end();++it)
if( it->second.virtual_peer_id == item->PeerId())
if( it->second.gxs_id == RsGxsId(item->PeerId()))
{
#ifdef CHAT_DEBUG
std::cerr << "p3ChatService::sendPrivateChatItem(): sending to " << item->PeerId() << ": interpreted as a distant chat virtual peer id." << std::endl;
@ -98,7 +98,7 @@ bool DistantChatService::handleOutgoingItem(RsChatItem *item)
void DistantChatService::handleRecvChatStatusItem(RsChatStatusItem *cs)
{
if(cs->flags & RS_CHAT_FLAG_CLOSING_DISTANT_CONNECTION)
markDistantChatAsClosed(cs->PeerId()) ;
markDistantChatAsClosed(RsGxsId(cs->PeerId())) ;
}
bool DistantChatService::getHashFromVirtualPeerId(const TurtleVirtualPeerId& vpid,TurtleFileHash& hash)
@ -317,6 +317,7 @@ void DistantChatService::receiveTurtleData( RsTurtleGenericTunnelItem *gitem,con
#endif
uint8_t aes_key[DISTANT_CHAT_AES_KEY_SIZE] ;
RsGxsId gxs_id ;
{
RsStackMutex stack(mDistantChatMtx); /********** STACK LOCKED MTX ******/
@ -330,6 +331,7 @@ void DistantChatService::receiveTurtleData( RsTurtleGenericTunnelItem *gitem,con
it->second.last_contact = time(NULL) ;
memcpy(aes_key,it->second.aes_key,DISTANT_CHAT_AES_KEY_SIZE) ;
it->second.status = RS_DISTANT_CHAT_STATUS_CAN_TALK ;
gxs_id = it->second.gxs_id ;
}
// Call the AES crypto module
@ -389,7 +391,18 @@ void DistantChatService::receiveTurtleData( RsTurtleGenericTunnelItem *gitem,con
// Setup the virtual peer to be the origin, and pass it on.
//
citem->PeerId(virtualPeerIdFromHash(hash)) ;
#ifdef DEBUG_DISTANT_CHAT
std::cerr << "(II) Setting peer id to " << decrypted_size << std::endl;
#endif
// DH key items are sent even before we know who we speak to, so the virtual peer id is used in this
// case only.
if(dynamic_cast<RsChatDHPublicKeyItem*>(citem) != NULL)
citem->PeerId(virtual_peer_id) ;
else
citem->PeerId(RsPeerId(gxs_id)) ;
//RsServer::notify()->notifyPeerStatusChanged(hash,RS_STATUS_ONLINE) ;
handleIncomingItem(citem) ; // Treats the item, and deletes it
@ -525,9 +538,9 @@ void DistantChatService::handleRecvDHPublicKey(RsChatDHPublicKeyItem *item)
RsChatStatusItem *cs = new RsChatStatusItem ;
cs->status_string = "Tunnel is secured with PFS session. You can talk!" ;
cs->status_string = "Tunnel is secured with PFS session. ACK received. You can talk!" ;
cs->flags = RS_CHAT_FLAG_PRIVATE | RS_CHAT_FLAG_ACK_DISTANT_CONNECTION;
cs->PeerId(vpid);
cs->PeerId(RsPeerId(senders_id));
pendingDistantChatItems.push_back(cs) ;
@ -597,7 +610,7 @@ bool DistantChatService::locked_sendDHPublicKey(const DistantChatPeerInfo& pinfo
dhitem->signature = signature ;
dhitem->gxs_key = signature_key_public ;
dhitem->PeerId(pinfo.virtual_peer_id) ;
dhitem->PeerId(RsPeerId(pinfo.gxs_id)) ;
#ifdef DEBUG_DISTANT_CHAT
std::cerr << " Pushing DH session key item to pending distant messages..." << std::endl;
@ -668,15 +681,8 @@ void DistantChatService::sendTurtleData(RsChatItem *item)
uint8_t aes_key[DISTANT_CHAT_AES_KEY_SIZE] ;
TurtleVirtualPeerId virtual_peer_id = item->PeerId();
RsFileHash hash ;
if(!getHashFromVirtualPeerId(item->PeerId(),hash))
{
std::cerr << "Cannot get hash from virtual peer id " << item->PeerId() << ". Dropping the chat message." << std::endl;
delete[] buff ;
return ;
}
TurtleFileHash hash = hashFromGxsId(RsGxsId(item->PeerId())) ;
TurtleVirtualPeerId virtual_peer_id ;
{
RsStackMutex stack(mDistantChatMtx); /********** STACK LOCKED MTX ******/
@ -684,12 +690,13 @@ void DistantChatService::sendTurtleData(RsChatItem *item)
if(it == _distant_chat_peers.end())
{
std::cerr << "(EE) item is not going into a registered tunnel. Weird. peer id = " << item->PeerId() << std::endl;
std::cerr << "(EE) No distant chat info for peer id = " << item->PeerId() << ". Dropping the item!" << std::endl;
delete[] buff ;
return ;
}
it->second.last_contact = time(NULL) ;
memcpy(aes_key,it->second.aes_key,DISTANT_CHAT_AES_KEY_SIZE) ;
virtual_peer_id = it->second.virtual_peer_id ;
}
#ifdef DEBUG_DISTANT_CHAT
std::cerr << "DistantChatService::sendTurtleData(): tunnel found. Encrypting data." << std::endl;
@ -750,7 +757,6 @@ void DistantChatService::sendTurtleData(RsChatItem *item)
bool DistantChatService::initiateDistantChatConnexion(const RsGxsId& gxs_id,uint32_t& error_code)
{
//pid = DistantChatPeerId(gxs_id) ; // the two ids have the same size. Still, I prefer to have 2 different types.
TurtleFileHash hash = hashFromGxsId(gxs_id) ;
// should be a parameter.
@ -799,43 +805,42 @@ void DistantChatService::startClientDistantChatConnection(const RsFileHash& hash
mTurtle->monitorTunnels(hash,this) ;
}
DistantChatPeerId DistantChatService::virtualPeerIdFromHash(const TurtleFileHash& hash)
{
RsStackMutex stack(mDistantChatMtx); /********** STACK LOCKED MTX ******/
std::map<TurtleFileHash,DistantChatPeerInfo>::const_iterator it = _distant_chat_peers.find(hash) ;
if(it == _distant_chat_peers.end())
{
std::cerr << " (EE) Cannot find hash " << hash << " in distant peers list. Virtual peer id cannot be returned." << std::endl;
return DistantChatPeerId() ;
}
else
return it->second.virtual_peer_id ;
}
// DistantChatPeerId DistantChatService::virtualPeerIdFromHash(const TurtleFileHash& hash)
// {
// RsStackMutex stack(mDistantChatMtx); /********** STACK LOCKED MTX ******/
//
// std::map<TurtleFileHash,DistantChatPeerInfo>::const_iterator it = _distant_chat_peers.find(hash) ;
//
// if(it == _distant_chat_peers.end())
// {
// std::cerr << " (EE) Cannot find hash " << hash << " in distant peers list. Virtual peer id cannot be returned." << std::endl;
// return DistantChatPeerId() ;
// }
// else
// return it->second.virtual_peer_id ;
// }
TurtleFileHash DistantChatService::hashFromGxsId(const RsGxsId& gid)
{
if(DistantChatPeerId::SIZE_IN_BYTES > Sha1CheckSum::SIZE_IN_BYTES)
if(RsGxsId::SIZE_IN_BYTES > Sha1CheckSum::SIZE_IN_BYTES)
std::cerr << __PRETTY_FUNCTION__ << ": Serious inconsistency error." << std::endl;
assert(Sha1CheckSum::SIZE_IN_BYTES >= DistantChatPeerId::SIZE_IN_BYTES) ;
assert(Sha1CheckSum::SIZE_IN_BYTES >= RsGxsId::SIZE_IN_BYTES) ;
unsigned char tmp[Sha1CheckSum::SIZE_IN_BYTES] ;
memset(tmp,0,Sha1CheckSum::SIZE_IN_BYTES) ;
memcpy(tmp,gid.toByteArray(),DistantChatPeerId::SIZE_IN_BYTES) ;
memcpy(tmp,gid.toByteArray(),RsGxsId::SIZE_IN_BYTES) ;
return Sha1CheckSum(tmp);
}
bool DistantChatService::getDistantChatStatus(const DistantChatPeerId& pid,RsGxsId& gxs_id,uint32_t& status)
bool DistantChatService::getDistantChatStatus(const RsGxsId& gxs_id,uint32_t& status)
{
RsStackMutex stack(mDistantChatMtx); /********** STACK LOCKED MTX ******/
for(std::map<TurtleFileHash,DistantChatPeerInfo>::const_iterator it = _distant_chat_peers.begin() ; it != _distant_chat_peers.end();++it)
if(it->second.virtual_peer_id == pid)
if(it->second.gxs_id == gxs_id)
{
status = it->second.status ;
gxs_id = it->second.gxs_id ;
return true ;
}
@ -843,7 +848,7 @@ bool DistantChatService::getDistantChatStatus(const DistantChatPeerId& pid,RsGxs
return false ;
}
bool DistantChatService::closeDistantChatConnexion(const DistantChatPeerId& pid)
bool DistantChatService::closeDistantChatConnexion(const RsGxsId& pid)
{
// two cases:
// - client needs to stop asking for tunnels => remove the hash from the list of tunnelled files
@ -858,7 +863,7 @@ bool DistantChatService::closeDistantChatConnexion(const DistantChatPeerId& pid)
RsStackMutex stack(mDistantChatMtx); /********** STACK LOCKED MTX ******/
for(std::map<TurtleFileHash,DistantChatPeerInfo>::const_iterator it = _distant_chat_peers.begin() ; it != _distant_chat_peers.end();++it)
if(it->second.virtual_peer_id == pid)
if(it->second.gxs_id == pid)
{
if(it->second.direction == RsTurtleGenericTunnelItem::DIRECTION_SERVER)
is_client = true ;
@ -869,7 +874,7 @@ bool DistantChatService::closeDistantChatConnexion(const DistantChatPeerId& pid)
if(virtual_peer_id.isNull())
{
std::cerr << "Cannot close chat associated to virtual peer id " << pid << ": not found." << std::endl;
std::cerr << "Cannot close chat associated to GXS Id " << pid << ": not found." << std::endl;
return false ;
}
}
@ -882,9 +887,9 @@ bool DistantChatService::closeDistantChatConnexion(const DistantChatPeerId& pid)
cs->status_string = "" ;
cs->flags = RS_CHAT_FLAG_PRIVATE | RS_CHAT_FLAG_CLOSING_DISTANT_CONNECTION;
cs->PeerId(virtual_peer_id);
cs->PeerId(RsPeerId(pid));
sendTurtleData(cs) ; // that needs to be done off-mutex!
sendTurtleData(cs) ; // that needs to be done off-mutex and before we close the tunnel.
RsStackMutex stack(mDistantChatMtx); /********** STACK LOCKED MTX ******/
std::map<RsFileHash,DistantChatPeerInfo>::iterator it = _distant_chat_peers.find(hash) ;
@ -908,12 +913,12 @@ bool DistantChatService::closeDistantChatConnexion(const DistantChatPeerId& pid)
return true ;
}
void DistantChatService::markDistantChatAsClosed(const TurtleVirtualPeerId& vpid)
void DistantChatService::markDistantChatAsClosed(const RsGxsId& pid)
{
RsStackMutex stack(mDistantChatMtx); /********** STACK LOCKED MTX ******/
for(std::map<TurtleFileHash,DistantChatPeerInfo>::iterator it = _distant_chat_peers.begin();it!=_distant_chat_peers.end();++it)
if(it->second.virtual_peer_id == vpid)
if(it->second.gxs_id == pid)
it->second.status = RS_DISTANT_CHAT_STATUS_REMOTELY_CLOSED ;
}

View File

@ -51,8 +51,8 @@ class DistantChatService: public RsTurtleClientService
// Om success, stores the invite in the map above, so that we can respond to tunnel requests.
//
bool initiateDistantChatConnexion(const RsGxsId& gxs_id,uint32_t& error_code) ;
bool closeDistantChatConnexion(const DistantChatPeerId& pid) ;
virtual bool getDistantChatStatus(const DistantChatPeerId& hash,RsGxsId& gxs_id,uint32_t& status) ;
bool closeDistantChatConnexion(const RsGxsId& pid) ;
virtual bool getDistantChatStatus(const RsGxsId &gxs_id,uint32_t &status) ;
// derived in p3ChatService
virtual void handleIncomingItem(RsItem *) = 0;
@ -95,7 +95,7 @@ class DistantChatService: public RsTurtleClientService
virtual void receiveTurtleData(RsTurtleGenericTunnelItem *item,const RsFileHash& hash,const RsPeerId& virtual_peer_id,RsTurtleGenericTunnelItem::Direction direction) ;
void addVirtualPeer(const TurtleFileHash&, const TurtleVirtualPeerId&,RsTurtleGenericTunnelItem::Direction dir) ;
void removeVirtualPeer(const TurtleFileHash&, const TurtleVirtualPeerId&) ;
void markDistantChatAsClosed(const TurtleVirtualPeerId& vpid) ;
void markDistantChatAsClosed(const RsGxsId &vpid) ;
void startClientDistantChatConnection(const RsFileHash& hash,const RsGxsId& to_gxs_id,const RsGxsId& from_gxs_id) ;
bool getHashFromVirtualPeerId(const TurtleVirtualPeerId& pid,RsFileHash& hash) ;
TurtleFileHash hashFromGxsId(const RsGxsId& pid) ;

View File

@ -272,15 +272,13 @@ void p3ChatService::checkSizeAndSendMessage(RsChatMsgItem *msg)
}
bool p3ChatService::isOnline(const DistantChatPeerId& pid)
bool p3ChatService::isOnline(const RsPeerId& pid)
{
// check if the id is a tunnel id or a peer id.
uint32_t status ;
RsGxsId gxs_id ;
std::string hash ;
if(getDistantChatStatus(pid,gxs_id,status))
if(getDistantChatStatus(RsGxsId(pid),status))
return status == RS_DISTANT_CHAT_STATUS_CAN_TALK ;
else
return mServiceCtrl->isPeerConnected(getServiceInfo().mServiceType, pid);

View File

@ -426,8 +426,8 @@ virtual ChatLobbyId createChatLobby(const std::string& lobby_name,const std::str
/****************************************/
virtual bool initiateDistantChatConnexion(const RsGxsId& pid,uint32_t& error_code) = 0;
virtual bool getDistantChatStatus(const DistantChatPeerId& pid,RsGxsId& gxs_id,uint32_t& status) = 0;
virtual bool closeDistantChatConnexion(const DistantChatPeerId& pid) = 0;
virtual bool getDistantChatStatus(const RsGxsId& gxs_id,uint32_t& status) = 0;
virtual bool closeDistantChatConnexion(const RsGxsId& pid) = 0;
};

View File

@ -368,11 +368,11 @@ bool p3Msgs::initiateDistantChatConnexion(const RsGxsId& gxs_id,uint32_t& error_
{
return mChatSrv->initiateDistantChatConnexion(gxs_id,error_code) ;
}
bool p3Msgs::getDistantChatStatus(const DistantChatPeerId& pid,RsGxsId& gxs_id,uint32_t& status)
bool p3Msgs::getDistantChatStatus(const RsGxsId &gxs_id,uint32_t &status)
{
return mChatSrv->getDistantChatStatus(pid,gxs_id,status) ;
return mChatSrv->getDistantChatStatus(gxs_id,status) ;
}
bool p3Msgs::closeDistantChatConnexion(const DistantChatPeerId& pid)
bool p3Msgs::closeDistantChatConnexion(const RsGxsId& pid)
{
return mChatSrv->closeDistantChatConnexion(pid) ;
}

View File

@ -197,8 +197,8 @@ class p3Msgs: public RsMsgs
virtual ChatLobbyId createChatLobby(const std::string& lobby_name,const std::string& lobby_topic,const std::list<RsPeerId>& invited_friends,uint32_t privacy_type) ;
virtual bool initiateDistantChatConnexion(const RsGxsId& gxs_id,uint32_t& error_code) ;
virtual bool getDistantChatStatus(const DistantChatPeerId& pid,RsGxsId& gxs_id,uint32_t& status) ;
virtual bool closeDistantChatConnexion(const DistantChatPeerId& pid) ;
virtual bool getDistantChatStatus(const RsGxsId& gxs_id,uint32_t& status) ;
virtual bool closeDistantChatConnexion(const RsGxsId &pid) ;
private:

View File

@ -98,9 +98,8 @@ void ChatDialog::init(const RsPeerId &peerId, const QString &title)
}
uint32_t distant_peer_status ;
RsGxsId distant_chat_gxs_id ;
if(rsMsgs->getDistantChatStatus(peerId,distant_chat_gxs_id,distant_peer_status))
if(rsMsgs->getDistantChatStatus(RsGxsId(peerId),distant_peer_status))
chatflags = RS_CHAT_OPEN | RS_CHAT_FOCUS; // use own flags
if (chatflags & RS_CHAT_OPEN) {
@ -119,7 +118,7 @@ void ChatDialog::init(const RsPeerId &peerId, const QString &title)
cd = new PopupDistantChatDialog();
chatDialogs[peerId] = cd;
QString peer_name = cd->getPeerName(peerId) ;
cd->init(peerId, tr("Talking to ")+peer_name+" (GXS id="+QString::fromStdString(distant_chat_gxs_id.toStdString())+")") ;
cd->init(peerId, tr("Talking to ")+peer_name+" (GXS id="+QString::fromStdString(peerId.toStdString())+")") ;
} else {
RsPeerDetails sslDetails;
@ -210,10 +209,9 @@ void ChatDialog::init(const RsPeerId &peerId, const QString &title)
return;
}
RsGxsId distant_chat_gxs_id ;
uint32_t distant_peer_status ;
if(rsMsgs->getDistantChatStatus(peerId,distant_chat_gxs_id,distant_peer_status))
if(rsMsgs->getDistantChatStatus(RsGxsId(peerId),distant_peer_status))
{
getChat(peerId, forceFocus ? RS_CHAT_OPEN | RS_CHAT_FOCUS : RS_CHAT_OPEN ); // use own flags
return ;

View File

@ -58,9 +58,9 @@ PopupDistantChatDialog::PopupDistantChatDialog(QWidget *parent, Qt::WindowFlags
updateDisplay() ;
}
void PopupDistantChatDialog::init(const DistantChatPeerId& pid,const QString & title)
void PopupDistantChatDialog::init(const RsPeerId &pid,const QString & title)
{
_pid = pid ;
_pid = RsGxsId(pid) ;
PopupChatDialog::init(pid,title) ;
}
@ -77,8 +77,7 @@ void PopupDistantChatDialog::updateDisplay()
//
uint32_t status= RS_DISTANT_CHAT_STATUS_UNKNOWN;
RsGxsId gxs_id ;
rsMsgs->getDistantChatStatus(_pid,gxs_id,status) ;
rsMsgs->getDistantChatStatus(_pid,status) ;
switch(status)
{
@ -119,8 +118,7 @@ void PopupDistantChatDialog::closeEvent(QCloseEvent *e)
//std::cerr << "Closing window => closing distant chat for hash " << _pid << std::endl;
uint32_t status= RS_DISTANT_CHAT_STATUS_UNKNOWN;
RsGxsId gxs_id ;
rsMsgs->getDistantChatStatus(_pid,gxs_id,status) ;
rsMsgs->getDistantChatStatus(_pid,status) ;
if(status != RS_DISTANT_CHAT_STATUS_REMOTELY_CLOSED)
{
@ -140,21 +138,20 @@ void PopupDistantChatDialog::closeEvent(QCloseEvent *e)
PopupChatDialog::closeEvent(e) ;
}
QString PopupDistantChatDialog::getPeerName(const DistantChatPeerId &id) const
QString PopupDistantChatDialog::getPeerName(const RsGxsId& id) const
{
uint32_t status ;
RsGxsId gxs_id ;
if(rsMsgs->getDistantChatStatus(id,gxs_id,status))
if(rsMsgs->getDistantChatStatus(id,status))
{
RsIdentityDetails details ;
if(rsIdentity->getIdDetails(gxs_id,details))
if(rsIdentity->getIdDetails(RsGxsId(id),details))
return QString::fromUtf8( details.mNickname.c_str() ) ;
else
return QString::fromStdString(gxs_id.toStdString()) ;
return QString::fromStdString(id.toStdString()) ;
}
else
return ChatDialog::getPeerName(id) ;
return ChatDialog::getPeerName(RsPeerId(id)) ;
}

View File

@ -37,17 +37,17 @@ class PopupDistantChatDialog: public PopupChatDialog
/** Default destructor */
virtual ~PopupDistantChatDialog();
virtual void init(const DistantChatPeerId &pid, const QString &title);
virtual void init(const RsPeerId &pid, const QString &title);
virtual void closeEvent(QCloseEvent *e) ;
virtual QString getPeerName(const DistantChatPeerId& id) const ;
virtual QString getPeerName(const RsGxsId &id) const ;
protected slots:
void updateDisplay() ; // overloads RsAutoUpdatePage
private:
QTimer *_update_timer ;
DistantChatPeerId _pid ;
RsGxsId _pid ;
QLabel *_status_label ;
friend class ChatDialog;