Merge pull request #593 from G10h4ck/libresapi_distant_chat

Expose libresapi for distant chat
This commit is contained in:
csoler 2016-12-08 09:00:55 +01:00 committed by GitHub
commit 242338d10c
9 changed files with 179 additions and 82 deletions

View File

@ -6,6 +6,8 @@
#include <stdint.h>
#include <ostream>
#include "util/rsdeprecate.h"
namespace resource_api
{
// things to clean up:
@ -183,14 +185,16 @@ class Request
public:
Request(StreamBase& stream) : mStream(stream), mMethod(GET){}
bool isGet(){ return mMethod == GET;}
bool isPut(){ return mMethod == PUT;}
bool isDelete(){ return mMethod == DELETE_AA;}
bool isExec(){ return mMethod == EXEC;}
RS_DEPRECATED bool isGet(){ return mMethod == GET;}
RS_DEPRECATED bool isPut(){ return mMethod == PUT;}
RS_DEPRECATED bool isDelete(){ return mMethod == DELETE_AA;}
RS_DEPRECATED bool isExec(){ return mMethod == EXEC;}
// path is the adress to the resource
// if the path has multiple parts which get handled by different handlers,
// then each handler should pop the top element
/**
* Path is the adress to the resource if the path has multiple parts which
* get handled by different handlers, then each handler should pop the top
* element
*/
std::stack<std::string> mPath;
std::string mFullPath;
bool setPath(const std::string &reqPath)
@ -213,19 +217,16 @@ public:
return true;
}
// parameters should be used to influence the result
// for example include or exclude some information
// question: when to use parameters, and when to use the data field?
// it would be easier to have only one thing...
// UNUSED: was never implemented
//std::vector<std::pair<std::string, std::string> > mParameters;
// contains data for new resources
/// Contains data for new resources
StreamBase& mStream;
// use the is*() methods to query the method type:
enum Method { GET, PUT, DELETE_AA, EXEC};// something is wrong with DELETE, it won't compile with it
Method mMethod;
/**
* @deprecated
* Method and derivated stuff usage is deprecated as it make implementation
* more complex and less readable without advantage
*/
enum Method { GET, PUT, DELETE_AA, EXEC};
RS_DEPRECATED Method mMethod;
};
// new notes on responses

View File

@ -155,6 +155,9 @@ ChatHandler::ChatHandler(StateTokenServer *sts, RsNotify *notify, RsMsgs *msgs,
addResourceHandler("receive_status", this, &ChatHandler::handleReceiveStatus);
addResourceHandler("send_status", this, &ChatHandler::handleSendStatus);
addResourceHandler("unread_msgs", this, &ChatHandler::handleUnreadMsgs);
addResourceHandler("initiate_distant_chat", this, &ChatHandler::handleInitiateDistantChatConnexion);
addResourceHandler("distant_chat_status", this, &ChatHandler::handleDistantChatStatus);
addResourceHandler("close_distant_chat", this, &ChatHandler::handleCloseDistantChatConnexion);
}
ChatHandler::~ChatHandler()
@ -930,7 +933,9 @@ void ChatHandler::handleMessages(Request &req, Response &resp)
std::map<ChatId, std::list<Msg> >::iterator mit = mMsgs.find(id);
if(mit == mMsgs.end())
{
resp.mStateToken = mMsgStateToken; // even set state token, if not found yet, maybe later messages arrive and then the chat id will be found
/* set state token, even if not found yet, maybe later messages arrive
* and then the chat id will be found */
resp.mStateToken = mMsgStateToken;
resp.setFail("chat with id=\""+req.mPath.top()+"\" not found");
return;
}
@ -954,7 +959,6 @@ void ChatHandler::handleSendMessage(Request &req, Response &resp)
resp.setOk();
else
resp.setFail("failed to send message");
}
void ChatHandler::handleMarkChatAsRead(Request &req, Response &resp)
@ -1125,4 +1129,63 @@ void ChatHandler::handleUnreadMsgs(Request &/*req*/, Response &resp)
resp.mStateToken = mUnreadMsgsStateToken;
}
void ChatHandler::handleInitiateDistantChatConnexion(Request& req, Response& resp)
{
std::string own_gxs_hex, remote_gxs_hex;
req.mStream << makeKeyValueReference("own_gxs_hex", own_gxs_hex)
<< makeKeyValueReference("remote_gxs_hex", remote_gxs_hex);
RsGxsId sender_id(own_gxs_hex);
if(sender_id.isNull())
{
resp.setFail("own_gxs_hex is invalid");
return;
}
RsGxsId receiver_id(remote_gxs_hex);
if(receiver_id.isNull())
{
resp.setFail("remote_gxs_hex is invalid");
return;
}
DistantChatPeerId distant_chat_id;
uint32_t error_code;
if(mRsMsgs->initiateDistantChatConnexion(receiver_id, sender_id, distant_chat_id, error_code))
resp.setOk();
else resp.setFail("Failed to initiate distant chat");
ChatId chat_id(distant_chat_id);
resp.mDataStream << makeKeyValue("chat_id", chat_id.toStdString())
<< makeKeyValueReference("error_code", error_code);
}
void ChatHandler::handleDistantChatStatus(Request& req, Response& resp)
{
std::string distant_chat_hex;
req.mStream << makeKeyValueReference("chat_id", distant_chat_hex);
ChatId id(distant_chat_hex);
DistantChatPeerInfo info;
if(mRsMsgs->getDistantChatStatus(id.toDistantChatId(), info)) resp.setOk();
else resp.setFail("Failed to get status for distant chat");
resp.mDataStream << makeKeyValue("own_gxs_hex", info.own_id.toStdString())
<< makeKeyValue("remote_gxs_hex", info.to_id.toStdString())
<< makeKeyValue("chat_id", info.peer_id.toStdString())
<< makeKeyValue("status", info.status);
}
void ChatHandler::handleCloseDistantChatConnexion(Request& req, Response& resp)
{
std::string distant_chat_hex;
req.mStream << makeKeyValueReference("distant_chat_hex", distant_chat_hex);
DistantChatPeerId chat_id(distant_chat_hex);
if (mRsMsgs->closeDistantChatConnexion(chat_id)) resp.setOk();
else resp.setFail("Failed to close distant chat");
}
} // namespace resource_api

View File

@ -11,7 +11,8 @@ class RsIdentity;
namespace resource_api
{
class UnreadMsgNotify{
class UnreadMsgNotify
{
public:
virtual void notifyUnreadMsgCountChanged(const RsPeerId& peer, uint32_t count) = 0;
};
@ -128,6 +129,9 @@ private:
ResponseTask *handleReceiveStatus(Request& req, Response& resp);
void handleSendStatus(Request& req, Response& resp);
void handleUnreadMsgs(Request& req, Response& resp);
void handleInitiateDistantChatConnexion(Request& req, Response& resp);
void handleDistantChatStatus(Request& req, Response& resp);
void handleCloseDistantChatConnexion(Request& req, Response& resp);
void getPlainText(const std::string& in, std::string &out, std::vector<Triple> &links);
// last parameter is only used for lobbies!

View File

@ -275,12 +275,11 @@ bool DistantChatService::initiateDistantChatConnexion(const RsGxsId& to_gxs_id,
bool DistantChatService::getDistantChatStatus(const DistantChatPeerId& tunnel_id, DistantChatPeerInfo& cinfo)
{
RsStackMutex stack(mDistantChatMtx); /********** STACK LOCKED MTX ******/
RS_STACK_MUTEX(mDistantChatMtx);
RsGxsTunnelService::GxsTunnelInfo tinfo;
if(!mGxsTunnels->getTunnelInfo(RsGxsTunnelId(tunnel_id),tinfo))
return false;
if(!mGxsTunnels->getTunnelInfo(RsGxsTunnelId(tunnel_id),tinfo)) return false;
cinfo.to_id = tinfo.destination_gxs_id;
cinfo.own_id = tinfo.source_gxs_id;
@ -288,15 +287,15 @@ bool DistantChatService::getDistantChatStatus(const DistantChatPeerId& tunnel_id
switch(tinfo.tunnel_status)
{
case RsGxsTunnelService::RS_GXS_TUNNEL_STATUS_CAN_TALK : cinfo.status = RS_DISTANT_CHAT_STATUS_CAN_TALK;
break ;
case RsGxsTunnelService::RS_GXS_TUNNEL_STATUS_TUNNEL_DN: cinfo.status = RS_DISTANT_CHAT_STATUS_TUNNEL_DN ;
break ;
case RsGxsTunnelService::RS_GXS_TUNNEL_STATUS_REMOTELY_CLOSED: cinfo.status = RS_DISTANT_CHAT_STATUS_REMOTELY_CLOSED ;
break ;
case RsGxsTunnelService::RS_GXS_TUNNEL_STATUS_CAN_TALK :
cinfo.status = RS_DISTANT_CHAT_STATUS_CAN_TALK; break;
case RsGxsTunnelService::RS_GXS_TUNNEL_STATUS_TUNNEL_DN:
cinfo.status = RS_DISTANT_CHAT_STATUS_TUNNEL_DN; break;
case RsGxsTunnelService::RS_GXS_TUNNEL_STATUS_REMOTELY_CLOSED:
cinfo.status = RS_DISTANT_CHAT_STATUS_REMOTELY_CLOSED; break;
case RsGxsTunnelService::RS_GXS_TUNNEL_STATUS_UNKNOWN:
default:
case RsGxsTunnelService::RS_GXS_TUNNEL_STATUS_UNKNOWN: cinfo.status = RS_DISTANT_CHAT_STATUS_UNKNOWN;
break ;
cinfo.status = RS_DISTANT_CHAT_STATUS_UNKNOWN; break;
}
return true;

View File

@ -293,13 +293,10 @@ void p3ChatService::checkSizeAndSendMessage(RsChatMsgItem *msg)
bool p3ChatService::isOnline(const RsPeerId& pid)
{
// check if the id is a tunnel id or a peer id.
DistantChatPeerInfo dcpinfo;
if(getDistantChatStatus(DistantChatPeerId(pid),dcpinfo))
return dcpinfo.status == RS_DISTANT_CHAT_STATUS_CAN_TALK;
else
return mServiceCtrl->isPeerConnected(getServiceInfo().mServiceType, pid);
else return mServiceCtrl->isPeerConnected(getServiceInfo().mServiceType, pid);
}
bool p3ChatService::sendChat(ChatId destination, std::string msg)
@ -318,8 +315,7 @@ bool p3ChatService::sendChat(ChatId destination, std::string msg)
}
// destination is peer or distant
#ifdef CHAT_DEBUG
std::cerr << "p3ChatService::sendChat()";
std::cerr << std::endl;
std::cerr << "p3ChatService::sendChat()" << std::endl;
#endif
RsPeerId vpid;
@ -344,7 +340,7 @@ bool p3ChatService::sendChat(ChatId destination, std::string msg)
{
/* peer is offline, add to outgoing list */
{
RsStackMutex stack(mChatMtx); /********** STACK LOCKED MTX ******/
RS_STACK_MUTEX(mChatMtx);
privateOutgoingList.push_back(ci);
}

View File

@ -1464,7 +1464,7 @@ RsGxsId p3GxsTunnelService::destinationGxsIdFromHash(const TurtleFileHash& sum)
bool p3GxsTunnelService::getTunnelInfo(const RsGxsTunnelId& tunnel_id,GxsTunnelInfo& info)
{
RsStackMutex stack(mGxsTunnelMtx); /********** STACK LOCKED MTX ******/
RS_STACK_MUTEX(mGxsTunnelMtx);
std::map<RsGxsTunnelId,GxsTunnelPeerInfo>::const_iterator it = _gxs_tunnel_contacts.find(tunnel_id) ;

View File

@ -527,7 +527,8 @@ HEADERS += util/folderiterator.h \
util/rstickevent.h \
util/rsrecogn.h \
util/rsscopetimer.h \
util/stacktrace.h
util/stacktrace.h \
util/rsdeprecate.h
SOURCES += ft/ftchunkmap.cc \
ft/ftcontroller.cc \

View File

@ -79,12 +79,11 @@ ChatId::ChatId(ChatLobbyId id):
lobby_id = id;
}
ChatId::ChatId(std::string str):
lobby_id(0)
ChatId::ChatId(std::string str) : lobby_id(0)
{
type = TYPE_NOT_SET;
if(str.empty())
return;
if(str.empty()) return;
if(str[0] == 'P')
{
type = TYPE_PRIVATE;
@ -93,7 +92,7 @@ ChatId::ChatId(std::string str):
else if(str[0] == 'D')
{
type = TYPE_PRIVATE_DISTANT;
distant_chat_id == DistantChatPeerId(str.substr(1));
distant_chat_id = DistantChatPeerId(str.substr(1));
}
else if(str[0] == 'L')
{

View File

@ -0,0 +1,34 @@
#pragma once
/*
* RetroShare deprecation macros
* Copyright (C) 2016 Gioacchino Mazzurco <gio@eigenlab.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
# define RS_DEPRECATED __attribute__((__deprecated__))
#elif defined(_MSC_VER) && (_MSC_VER >= 1300)
# define RS_DEPRECATED __declspec(deprecated)
#else
# define RS_DEPRECATED
#endif
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
# define RS_DEPRECATED_FOR(f) __attribute__((__deprecated__("Use '" #f "' instead")))
#elif defined(_MSC_FULL_VER) && (_MSC_FULL_VER > 140050320)
# define RS_DEPRECATED_FOR(f) __declspec(deprecated("is deprecated. Use '" #f "' instead"))
#else
# define RS_DEPRECATED_FOR(f) RS_DEPRECATED
#endif