Few fixes after rebase

This commit is contained in:
Gioacchino Mazzurco 2020-01-06 14:46:36 +01:00
parent df87fe53b1
commit f4f7b8a1ea
No known key found for this signature in database
GPG key ID: A1FBCA3872E87051
4 changed files with 46 additions and 33 deletions

View file

@ -1,7 +1,8 @@
/* /*
* RetroShare JSON API * RetroShare JSON API
* *
* Copyright (C) 2018-2019 Gioacchino Mazzurco <gio@eigenlab.org> * Copyright (C) 2018-2020 Gioacchino Mazzurco <gio@eigenlab.org>
* Copyright (C) 2019-2020 Asociación Civil Altermundi <info@altermundi.net>
* *
* This program is free software: you can redistribute it and/or modify it under * 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 * the terms of the GNU Affero General Public License as published by the
@ -588,19 +589,12 @@ bool JsonApiServer::restart()
return true; return true;
} }
void JsonApiServer::onStopRequested()
{ if(mService->is_up()) mService->stop(); }
bool JsonApiServer::fullstop() bool JsonApiServer::fullstop()
{ {
if(!mService->is_up()) return true; RsThread::fullstop();
mService->stop();
RsThread::ask_for_stop();
while(isRunning())
{
RsDbg() << __PRETTY_FUNCTION__ << " shutting down JSON API service."
<< std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
return true; return true;
} }
@ -610,7 +604,7 @@ void JsonApiServer::setBindingAddress(const std::string& bindAddress)
{ mBindingAddress = bindAddress; } { mBindingAddress = bindAddress; }
std::string JsonApiServer::getBindingAddress() const { return mBindingAddress; } std::string JsonApiServer::getBindingAddress() const { return mBindingAddress; }
void JsonApiServer::runloop() void JsonApiServer::run()
{ {
auto settings = std::make_shared<restbed::Settings>(); auto settings = std::make_shared<restbed::Settings>();
settings->set_port(mListeningPort); settings->set_port(mListeningPort);

View file

@ -1,7 +1,8 @@
/* /*
* RetroShare JSON API * RetroShare JSON API
* *
* Copyright (C) 2018-2019 Gioacchino Mazzurco <gio@eigenlab.org> * Copyright (C) 2018-2020 Gioacchino Mazzurco <gio@eigenlab.org>
* Copyright (C) 2019-2020 Asociación Civil Altermundi <info@altermundi.net>
* *
* This program is free software: you can redistribute it and/or modify it under * 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 * the terms of the GNU Affero General Public License as published by the
@ -139,6 +140,10 @@ public:
const std::function<bool(const std::string&, const std::string&)>& const std::function<bool(const std::string&, const std::string&)>&
callback ); callback );
protected:
/// @see RsThread
void onStopRequested() override;
private: private:
/// @see RsThread /// @see RsThread
void run() override; void run() override;
@ -187,9 +192,6 @@ private:
std::reference_wrapper<const JsonApiResourceProvider>, std::reference_wrapper<const JsonApiResourceProvider>,
std::less<const JsonApiResourceProvider> > mResourceProviders; std::less<const JsonApiResourceProvider> > mResourceProviders;
/// @see RsThread
void runloop() override;
std::shared_ptr<restbed::Service> mService; std::shared_ptr<restbed::Service> mService;
uint16_t mListeningPort; uint16_t mListeningPort;

View file

@ -5,6 +5,7 @@
* * * *
* Copyright (C) 2004-2007 Robert Fernie <retroshare@lunamutt.com> * * Copyright (C) 2004-2007 Robert Fernie <retroshare@lunamutt.com> *
* Copyright (C) 2016-2019 Gioacchino Mazzurco <gio@eigenlab.org> * * Copyright (C) 2016-2019 Gioacchino Mazzurco <gio@eigenlab.org> *
* Copyright (C) 2019-2020 Asociación Civil Altermundi <info@altermundi.net> *
* * * *
* This program is free software: you can redistribute it and/or modify * * This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as * * it under the terms of the GNU Lesser General Public License as *
@ -126,7 +127,17 @@ void RsThread::fullstop()
return; return;
} }
while(!mHasStopped); // Wait for the thread being stopped // Wait for the thread being stopped
auto i = 1;
while(!mHasStopped)
{
std::this_thread::sleep_for(std::chrono::milliseconds(200));
++i;
if(!(i%5))
RsInfo() << __PRETTY_FUNCTION__ << " " << i*0.2 << " seconds passed"
<< " waiting for thread: " << mTid << " " << mFullName
<< " to stop" << std::endl;
}
} }
bool RsThread::start(const std::string& threadName) bool RsThread::start(const std::string& threadName)
@ -140,14 +151,18 @@ bool RsThread::start(const std::string& threadName)
if(pError) if(pError)
{ {
RsErr() << __PRETTY_FUNCTION__ << " pthread_create could not create" RsErr() << __PRETTY_FUNCTION__ << " pthread_create could not create"
<< " a new thread. pError: " << pError << std::endl; << " new thread: " << threadName << " pError: " << pError
<< std::endl;
mHasStopped = true; mHasStopped = true;
print_stacktrace(); print_stacktrace();
return false; return false;
} }
/* Set thread name which is restricted to 16 characters including the /* Store thread full name as PThread is not able to keep it entirely */
* terminating null byte */ mFullName = threadName;
/* Set PThread thread name which is restricted to 16 characters
* including the terminating null byte */
if(pthread_setname_np && !threadName.empty()) if(pthread_setname_np && !threadName.empty())
RS_pthread_setname_np(mTid, threadName.substr(0, 15).c_str()); RS_pthread_setname_np(mTid, threadName.substr(0, 15).c_str());
@ -253,19 +268,13 @@ RsThread::~RsThread()
{ {
if(isRunning()) if(isRunning())
{ {
RsErr() << __PRETTY_FUNCTION__ << " deleting a thread that is still " RsErr() << __PRETTY_FUNCTION__ << " deleting thread: " << mTid << " "
<< mFullName << " that is still "
<< "running! Something seems very wrong here and RetroShare is " << "running! Something seems very wrong here and RetroShare is "
<< "likely to crash because of this." << std::endl; << "likely to crash because of this." << std::endl;
print_stacktrace(); print_stacktrace();
askForStop(); fullstop();
while(isRunning())
{
RsErr() << __PRETTY_FUNCTION__ << "waiting 1s for stop..."
<< std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
} }
} }

View file

@ -4,7 +4,8 @@
* libretroshare: retroshare core library * * libretroshare: retroshare core library *
* * * *
* Copyright (C) 2004-2006 Robert Fernie <retroshare@lunamutt.com> * * Copyright (C) 2004-2006 Robert Fernie <retroshare@lunamutt.com> *
* Copyright (C) 2016-2019 Gioacchino Mazzurco <gio@eigenlab.org> * * Copyright (C) 2016-2020 Gioacchino Mazzurco <gio@eigenlab.org> *
* Copyright (C) 2019-2020 Asociación Civil Altermundi <info@altermundi.net> *
* * * *
* This program is free software: you can redistribute it and/or modify * * This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as * * it under the terms of the GNU Lesser General Public License as *
@ -182,8 +183,9 @@ public:
/** /**
* @brief start the thread and call run() on it. * @brief start the thread and call run() on it.
* @param threadName string containing the name of the thread used for * @param threadName string containing the name of the thread used for
* debugging purposes, it is truncated to 16 characters * debugging purposes, @note inside PThread it is
* including \0 at the end of the string. * truncated to 16 characters including \0 at the end of
* the string.
* @return false on error, true otherwise * @return false on error, true otherwise
*/ */
bool start(const std::string& threadName = ""); bool start(const std::string& threadName = "");
@ -228,6 +230,9 @@ public:
static void async(const std::function<void()>& fn) static void async(const std::function<void()>& fn)
{ std::thread(fn).detach(); } { std::thread(fn).detach(); }
/** @return RsThread full name */
const std::string& threadName() { return mFullName; }
protected: protected:
/** /**
* This method must be implemented by sublasses, will be called once the * This method must be implemented by sublasses, will be called once the
@ -259,6 +264,9 @@ private:
/// Store the id of the corresponding pthread /// Store the id of the corresponding pthread
pthread_t mTid; pthread_t mTid;
/// Store thread full name
std::string mFullName;
}; };
/** /**