Skip to content

Commit

Permalink
Merge pull request #219 Fix shutting down walletd
Browse files Browse the repository at this point in the history
  • Loading branch information
aivve committed May 27, 2023
2 parents 3df16d3 + d83cef6 commit a72eed0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
29 changes: 17 additions & 12 deletions src/JsonRpcServer/JsonRpcServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

namespace CryptoNote {

JsonRpcServer::JsonRpcServer(System::Dispatcher& sys, System::Event& stopEvent, Logging::ILogger& loggerGroup) :
JsonRpcServer::JsonRpcServer(System::Dispatcher* sys, System::Event* stopEvent, Logging::ILogger& loggerGroup) :
m_dispatcher(sys),
stopEvent(stopEvent),
logger(loggerGroup, "JsonRpcServer"),
Expand All @@ -56,16 +56,12 @@ JsonRpcServer::~JsonRpcServer() {

void JsonRpcServer::start(const std::string& bindAddress, uint16_t bindPort, uint16_t bindPortSSL) {
if (m_enable_ssl) {
m_workers.emplace_back(std::unique_ptr<System::RemoteContext<void>>(
new System::RemoteContext<void>(m_dispatcher, std::bind(&JsonRpcServer::listen_ssl, this, bindAddress, bindPortSSL)))
);
m_workers.push_back(std::thread(std::bind(&JsonRpcServer::listen_ssl, this, bindAddress, bindPortSSL)));
}

m_workers.emplace_back(std::unique_ptr<System::RemoteContext<void>>(
new System::RemoteContext<void>(m_dispatcher, std::bind(&JsonRpcServer::listen, this, bindAddress, bindPort)))
);
m_workers.push_back(std::thread(std::bind(&JsonRpcServer::listen, this, bindAddress, bindPort)));

stopEvent.wait();
stopEvent->wait();
}

void JsonRpcServer::stop() {
Expand All @@ -75,10 +71,19 @@ void JsonRpcServer::stop() {

http->stop();

m_dispatcher.remoteSpawn([this]
{
stopEvent.set();
});
if (m_dispatcher != nullptr) {
m_dispatcher->remoteSpawn([&]() {
if (stopEvent != nullptr) {
stopEvent->set();
}
});
}

for (auto& th : m_workers) {
if (th.joinable()) {
th.join();
}
}

m_workers.clear();
}
Expand Down
9 changes: 5 additions & 4 deletions src/JsonRpcServer/JsonRpcServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#pragma once

#include <system_error>
#include <thread>

#include "System/Dispatcher.h"
#include "System/Event.h"
Expand All @@ -46,7 +47,7 @@ namespace CryptoNote {

class JsonRpcServer {
public:
JsonRpcServer(System::Dispatcher& sys, System::Event& stopEvent, Logging::ILogger& loggerGroup);
JsonRpcServer(System::Dispatcher* sys, System::Event* stopEvent, Logging::ILogger& loggerGroup);
JsonRpcServer(const JsonRpcServer&) = delete;

~JsonRpcServer();
Expand Down Expand Up @@ -74,13 +75,13 @@ class JsonRpcServer {
void listen_ssl(const std::string address, const uint16_t port);
bool authenticate(const httplib::Request& request) const;

System::Dispatcher& m_dispatcher;
System::Event& stopEvent;
System::Dispatcher* m_dispatcher;
System::Event* stopEvent;
Logging::LoggerRef logger;
httplib::Server* http;
httplib::SSLServer* https;

std::vector<std::unique_ptr<System::RemoteContext<void>>> m_workers;
std::list<std::thread> m_workers;

std::string m_chain_file;
std::string m_key_file;
Expand Down
2 changes: 1 addition & 1 deletion src/PaymentGate/PaymentServiceJsonRpcServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

namespace PaymentService {

PaymentServiceJsonRpcServer::PaymentServiceJsonRpcServer(System::Dispatcher& sys, System::Event& stopEvent, WalletService& service, Logging::ILogger& loggerGroup)
PaymentServiceJsonRpcServer::PaymentServiceJsonRpcServer(System::Dispatcher* sys, System::Event* stopEvent, WalletService& service, Logging::ILogger& loggerGroup)
: JsonRpcServer(sys, stopEvent, loggerGroup)
, service(service)
, logger(loggerGroup, "PaymentServiceJsonRpcServer")
Expand Down
2 changes: 1 addition & 1 deletion src/PaymentGate/PaymentServiceJsonRpcServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class WalletService;

class PaymentServiceJsonRpcServer : public CryptoNote::JsonRpcServer {
public:
PaymentServiceJsonRpcServer(System::Dispatcher& sys, System::Event& stopEvent, WalletService& service, Logging::ILogger& loggerGroup);
PaymentServiceJsonRpcServer(System::Dispatcher* sys, System::Event* stopEvent, WalletService& service, Logging::ILogger& loggerGroup);
PaymentServiceJsonRpcServer(const PaymentServiceJsonRpcServer&) = delete;

protected:
Expand Down
2 changes: 1 addition & 1 deletion src/PaymentGateService/PaymentGateService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ void PaymentGateService::runWalletService(const CryptoNote::Currency& currency,
}
} else {

PaymentService::PaymentServiceJsonRpcServer rpcServer(*dispatcher, *stopEvent, *service, logger);
PaymentService::PaymentServiceJsonRpcServer rpcServer(dispatcher, stopEvent, *service, logger);

bool rpc_run_ssl = false;
std::string rpc_chain_file = "";
Expand Down

0 comments on commit a72eed0

Please sign in to comment.