libresapi: added statetoken to identity handler responses

This commit is contained in:
electron128 2016-02-07 11:11:45 +01:00
parent f4f37cc354
commit 116513963f
4 changed files with 58 additions and 10 deletions

View File

@ -226,7 +226,7 @@ class ApiServerMainModules
public: public:
ApiServerMainModules(ResourceRouter& router, StateTokenServer* sts, const RsPlugInInterfaces &ifaces): ApiServerMainModules(ResourceRouter& router, StateTokenServer* sts, const RsPlugInInterfaces &ifaces):
mPeersHandler(sts, ifaces.mNotify, ifaces.mPeers, ifaces.mMsgs), mPeersHandler(sts, ifaces.mNotify, ifaces.mPeers, ifaces.mMsgs),
mIdentityHandler(ifaces.mIdentity), mIdentityHandler(sts, ifaces.mNotify, ifaces.mIdentity),
mForumHandler(ifaces.mGxsForums), mForumHandler(ifaces.mGxsForums),
mServiceControlHandler(ifaces.mServiceControl), mServiceControlHandler(ifaces.mServiceControl),
mFileSearchHandler(sts, ifaces.mNotify, ifaces.mTurtle, ifaces.mFiles), mFileSearchHandler(sts, ifaces.mNotify, ifaces.mTurtle, ifaces.mFiles),

View File

@ -4,6 +4,7 @@ class RsPlugInInterfaces;
namespace resource_api{ namespace resource_api{
// populates the given RsPlugInInterfaces object with pointers from gloabl variables like rsPeers, rsMsgs, rsFiles...
bool getPluginInterfaces(RsPlugInInterfaces& interfaces); bool getPluginInterfaces(RsPlugInInterfaces& interfaces);
} // namespace resource_api } // namespace resource_api

View File

@ -16,17 +16,18 @@ namespace resource_api
class SendIdentitiesListTask: public GxsResponseTask class SendIdentitiesListTask: public GxsResponseTask
{ {
public: public:
SendIdentitiesListTask(RsIdentity* idservice, std::list<RsGxsId> ids): SendIdentitiesListTask(RsIdentity* idservice, std::list<RsGxsId> ids, StateToken state):
GxsResponseTask(idservice, 0) GxsResponseTask(idservice, 0), mStateToken(state)
{ {
for(std::list<RsGxsId>::iterator vit = ids.begin(); vit != ids.end(); ++vit) for(std::list<RsGxsId>::iterator vit = ids.begin(); vit != ids.end(); ++vit)
{ {
requestGxsId(*vit); requestGxsId(*vit);
mIds.push_back(*vit);// convert fro list to vector mIds.push_back(*vit);// convert from list to vector
} }
} }
private: private:
std::vector<RsGxsId> mIds; std::vector<RsGxsId> mIds;
StateToken mStateToken;
protected: protected:
virtual void gxsDoWork(Request &req, Response &resp) virtual void gxsDoWork(Request &req, Response &resp)
{ {
@ -35,6 +36,7 @@ protected:
{ {
streamGxsId(*vit, resp.mDataStream.getStreamToMember()); streamGxsId(*vit, resp.mDataStream.getStreamToMember());
} }
resp.mStateToken = mStateToken;
resp.setOk(); resp.setOk();
done(); done();
} }
@ -93,20 +95,39 @@ protected:
} }
}; };
IdentityHandler::IdentityHandler(RsIdentity *identity): IdentityHandler::IdentityHandler(StateTokenServer *sts, RsNotify *notify, RsIdentity *identity):
mRsIdentity(identity) mStateTokenServer(sts), mNotify(notify), mRsIdentity(identity),
mMtx("IdentityHandler Mtx"), mStateToken(sts->getNewToken())
{ {
mNotify->registerNotifyClient(this);
addResourceHandler("*", this, &IdentityHandler::handleWildcard); addResourceHandler("*", this, &IdentityHandler::handleWildcard);
addResourceHandler("own", this, &IdentityHandler::handleOwn); addResourceHandler("own", this, &IdentityHandler::handleOwn);
addResourceHandler("create_identity", this, &IdentityHandler::handleCreateIdentity); addResourceHandler("create_identity", this, &IdentityHandler::handleCreateIdentity);
} }
IdentityHandler::~IdentityHandler()
{
mNotify->unregisterNotifyClient(this);
}
void IdentityHandler::notifyGxsChange(const RsGxsChanges &changes)
{
RS_STACK_MUTEX(mMtx); // ********** LOCKED **********
// if changes come from identity service, invalidate own state token
if(changes.mService == mRsIdentity->getTokenService())
{
mStateTokenServer->replaceToken(mStateToken);
}
}
void IdentityHandler::handleWildcard(Request &req, Response &resp) void IdentityHandler::handleWildcard(Request &req, Response &resp)
{ {
bool ok = true; bool ok = true;
if(req.isPut()) if(req.isPut())
{ {
#ifdef REMOVE
RsIdentityParameters params; RsIdentityParameters params;
req.mStream << makeKeyValueReference("name", params.nickname); req.mStream << makeKeyValueReference("name", params.nickname);
if(req.mStream.isOK()) if(req.mStream.isOK())
@ -120,9 +141,14 @@ void IdentityHandler::handleWildcard(Request &req, Response &resp)
{ {
ok = false; ok = false;
} }
#endif
} }
else else
{ {
{
RS_STACK_MUTEX(mMtx); // ********** LOCKED **********
resp.mStateToken = mStateToken;
}
RsTokReqOptions opts; RsTokReqOptions opts;
opts.mReqType = GXS_REQUEST_TYPE_GROUP_DATA; opts.mReqType = GXS_REQUEST_TYPE_GROUP_DATA;
uint32_t token; uint32_t token;
@ -179,9 +205,14 @@ void IdentityHandler::handleWildcard(Request &req, Response &resp)
ResponseTask* IdentityHandler::handleOwn(Request &req, Response &resp) ResponseTask* IdentityHandler::handleOwn(Request &req, Response &resp)
{ {
StateToken state;
{
RS_STACK_MUTEX(mMtx); // ********** LOCKED **********
state = mStateToken;
}
std::list<RsGxsId> ids; std::list<RsGxsId> ids;
if(mRsIdentity->getOwnIds(ids)) if(mRsIdentity->getOwnIds(ids))
return new SendIdentitiesListTask(mRsIdentity, ids); return new SendIdentitiesListTask(mRsIdentity, ids, state);
resp.mDataStream.getStreamToMember(); resp.mDataStream.getStreamToMember();
resp.setWarning("identities not loaded yet"); resp.setWarning("identities not loaded yet");
return 0; return 0;

View File

@ -1,20 +1,36 @@
#pragma once #pragma once
#include <retroshare/rsnotify.h>
#include <util/rsthreads.h>
#include "ResourceRouter.h" #include "ResourceRouter.h"
#include "StateTokenServer.h"
class RsIdentity; class RsIdentity;
namespace resource_api namespace resource_api
{ {
class IdentityHandler: public ResourceRouter class IdentityHandler: public ResourceRouter, NotifyClient
{ {
public: public:
IdentityHandler(RsIdentity* identity); IdentityHandler(StateTokenServer* sts, RsNotify* notify, RsIdentity* identity);
virtual ~IdentityHandler();
// from NotifyClient
// note: this may get called from foreign threads
virtual void notifyGxsChange(const RsGxsChanges &changes);
private: private:
RsIdentity* mRsIdentity;
void handleWildcard(Request& req, Response& resp); void handleWildcard(Request& req, Response& resp);
ResponseTask *handleOwn(Request& req, Response& resp); ResponseTask *handleOwn(Request& req, Response& resp);
ResponseTask *handleCreateIdentity(Request& req, Response& resp); ResponseTask *handleCreateIdentity(Request& req, Response& resp);
StateTokenServer* mStateTokenServer;
RsNotify* mNotify;
RsIdentity* mRsIdentity;
RsMutex mMtx;
StateToken mStateToken; // mutex protected
}; };
} // namespace resource_api } // namespace resource_api