diff --git a/src/jsonapi/jsonapi.cpp b/src/jsonapi/jsonapi.cpp index fd9687d4d..0f298ffc5 100644 --- a/src/jsonapi/jsonapi.cpp +++ b/src/jsonapi/jsonapi.cpp @@ -21,8 +21,16 @@ // Generated at compile time #include "jsonapi-wrappers.h" -JsonApiServer::JsonApiServer(uint16_t port) : mPort(port) +JsonApiServer::JsonApiServer( + uint16_t port, const std::function shutdownCallback) : + mPort(port), mShutdownCallback(shutdownCallback) { + registerHandler("/jsonApiServer/shutdown", + [this](const std::shared_ptr) + { + shutdown(); + }); + // Generated at compile time #include "jsonapi-register.inl" } @@ -32,14 +40,22 @@ void JsonApiServer::run() std::shared_ptr settings(new rb::Settings); settings->set_port(mPort); settings->set_default_header("Connection", "close"); - service.start(settings); + mService.start(settings); } -void JsonApiServer::registerHandler(const std::string& path, const std::function)>& handler) +void JsonApiServer::registerHandler( + const std::string& path, + const std::function)>& handler) { std::shared_ptr resource(new rb::Resource); resource->set_path(path); resource->set_method_handler("GET", handler); resource->set_method_handler("POST", handler); - service.publish(resource); + mService.publish(resource); +} + +void JsonApiServer::shutdown(int exitCode) +{ + mService.stop(); + mShutdownCallback(exitCode); } diff --git a/src/jsonapi/jsonapi.h b/src/jsonapi/jsonapi.h index ce421c102..9c321c22a 100644 --- a/src/jsonapi/jsonapi.h +++ b/src/jsonapi/jsonapi.h @@ -39,12 +39,15 @@ void createChannelHandler(const std::shared_ptr session); */ struct JsonApiServer : RsSingleJobThread { - JsonApiServer(uint16_t port); + JsonApiServer( + uint16_t port, + const std::function shutdownCallback = [](int){} ); /// @see RsSingleJobThread virtual void run(); /** + * @param[in] path Path itno which publish the API call * @param[in] handler function which will be called to handle the requested * path, the function must be declared like: * \code{.cpp} @@ -55,8 +58,21 @@ struct JsonApiServer : RsSingleJobThread const std::string& path, const std::function)>& handler ); + /** + * @brief Shutdown the JSON API server and call shutdownCallback + * @jsonapi{development} + * Beware that this method shout down only the JSON API server instance not + * the whole RetroShare instance, this behaviour can be altered via + * shutdownCallback paramether of @see JsonApiServer::JsonApiServer + * This method is made available also via JSON API with path + * /jsonApiServer/shutdown + * @param exitCode just passed down to the shutdownCallback + */ + void shutdown(int exitCode = 0); + private: uint16_t mPort; - rb::Service service; + rb::Service mService; + const std::function mShutdownCallback; }; diff --git a/src/retroshare/rsinit.h b/src/retroshare/rsinit.h index eb176870a..25dc1b3f5 100644 --- a/src/retroshare/rsinit.h +++ b/src/retroshare/rsinit.h @@ -229,6 +229,12 @@ struct RsLoginHelper bool createLocation( RsLoginHelper::Location& location, const std::string& password, std::string& errorMessage ); + + /** + * @brief Close RetroShare session + * @jsonapi{development} + */ + void closeSession(); }; #endif diff --git a/src/rsserver/rsinit.cc b/src/rsserver/rsinit.cc index 6c064cd3b..d59fa20ca 100644 --- a/src/rsserver/rsinit.cc +++ b/src/rsserver/rsinit.cc @@ -1969,6 +1969,11 @@ bool RsLoginHelper::createLocation( return ret; } +void RsLoginHelper::closeSession() +{ + RsControl::instance()->rsGlobalShutDown(); +} + void RsLoginHelper::Location::serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx )