Unverified Commit fbdd91ac authored by kladko's avatar kladko

SKALE-4586 Added Thread Pool

parent f575911f
...@@ -31,16 +31,20 @@ using namespace std; ...@@ -31,16 +31,20 @@ using namespace std;
#include <iostream> #include <iostream>
#include <map> #include <map>
#include <memory> #include <memory>
#include <sys/types.h> #include <sys/types.h>
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
#include <string.h> #include <string.h>
#include <vector> #include <vector>
#include <json/value.h>
#include <boost/throw_exception.hpp> #include <boost/throw_exception.hpp>
#include <gmp.h> #include <gmp.h>
#include <thread>
#include <functional>
#include <atomic>
#include <condition_variable>
#include <mutex>
#include "secure_enclave/Verify.h" #include "secure_enclave/Verify.h"
#include "InvalidStateException.h" #include "InvalidStateException.h"
#include "SGXException.h" #include "SGXException.h"
......
...@@ -21,10 +21,9 @@ ...@@ -21,10 +21,9 @@
@date 2021 @date 2021
*/ */
#include "common.h"
#include "Agent.h" #include "Agent.h"
Agent::Agent() : startedWorkers(false) {}; Agent::Agent() : startedWorkers(false) {};
void Agent::waitOnGlobalStartBarrier() { void Agent::waitOnGlobalStartBarrier() {
......
...@@ -23,12 +23,6 @@ ...@@ -23,12 +23,6 @@
#pragma once #pragma once
#include <condition_variable>
#include <mutex>
#include <atomic>
using namespace std;
class Agent { class Agent {
protected: protected:
......
...@@ -21,7 +21,14 @@ ...@@ -21,7 +21,14 @@
@date 2021 @date 2021
*/ */
#include "document.h"
#include "stringbuffer.h"
#include "writer.h"
#include "common.h" #include "common.h"
#include "sgxwallet_common.h"
#include "third_party/spdlog/spdlog.h" #include "third_party/spdlog/spdlog.h"
#include "ZMQServer.h" #include "ZMQServer.h"
#include "WorkerThreadPool.h" #include "WorkerThreadPool.h"
......
...@@ -23,11 +23,6 @@ ...@@ -23,11 +23,6 @@
#pragma once #pragma once
#include <vector>
#include <atomic>
#include <thread>
class Agent; class Agent;
class ZMQServer; class ZMQServer;
......
...@@ -24,9 +24,6 @@ ...@@ -24,9 +24,6 @@
#pragma once #pragma once
#include <memory>
#include <vector>
#include <openssl/pem.h> #include <openssl/pem.h>
#include <openssl/evp.h> #include <openssl/evp.h>
#include <openssl/err.h> #include <openssl/err.h>
......
...@@ -28,13 +28,14 @@ ...@@ -28,13 +28,14 @@
#include "third_party/spdlog/spdlog.h" #include "third_party/spdlog/spdlog.h"
#include "common.h" #include "common.h"
#include "sgxwallet_common.h"
#include "SGXException.h" #include "SGXException.h"
#include "ExitRequestedException.h" #include "ExitRequestedException.h"
#include "ReqMessage.h" #include "ReqMessage.h"
#include "ZMQMessage.h" #include "ZMQMessage.h"
#include "ZMQServer.h" #include "ZMQServer.h"
#include "sgxwallet_common.h"
using namespace std; using namespace std;
...@@ -174,75 +175,109 @@ void ZMQServer::checkForExit() { ...@@ -174,75 +175,109 @@ void ZMQServer::checkForExit() {
} }
} }
void ZMQServer::doOneServerLoop() { void ZMQServer::poll() {
zmq_pollitem_t items[1];
items[0].socket = *socket;
items[0].events = ZMQ_POLLIN;
string replyStr; int pollResult = 0;
Json::Value result; do {
result["status"] = ZMQ_SERVER_ERROR; checkForExit();
result["errorMessage"] = ""; pollResult = zmq_poll(items, 1, 1000);
} while (pollResult == 0);
}
zmq::message_t identity; string ZMQServer::receiveMessage(zmq::message_t& _identity) {
if (!socket->recv(&_identity)) {
checkForExit();
// something terrible happened
spdlog::error("Fatal error: socket->recv(&identity) returned false. Exiting.");
exit(-11);
}
string stringToParse = ""; if (!_identity.more()) {
checkForExit();
// something terrible happened
spdlog::error("Fatal error: zmq_msg_more(identity) returned false. Existing.");
exit(-12);
}
try { zmq::message_t reqMsg;
zmq_pollitem_t items[1]; if (!socket->recv(&reqMsg, 0)) {
items[0].socket = *socket; checkForExit();
items[0].events = ZMQ_POLLIN; // something terrible happened
spdlog::error("Fatal error: socket.recv(&reqMsg, 0) returned false. Exiting");
exit(-13);
}
int pollResult = 0; auto result = string((char *) reqMsg.data(), reqMsg.size());
do { CHECK_STATE(result.front() == '{')
checkForExit(); CHECK_STATE(result.back() == '}')
pollResult = zmq_poll(items, 1, 1000); return result;
} while (pollResult == 0); }
if (!socket->recv(&identity)) { void ZMQServer::sendToClient(Json::Value& _result, zmq::message_t& _identity ) {
checkForExit(); string replyStr;
// something terrible happened try {
spdlog::error("Fatal error: socket->recv(&identity) returned false. Exiting."); Json::FastWriter fastWriter;
exit(-11); fastWriter.omitEndingLineFeed();
}
if (!identity.more()) { replyStr = fastWriter.write(_result);
// something terrible happened
spdlog::error("Fatal error: zmq_msg_more(identity) returned false. Existing.");
exit(-12);
}
zmq::message_t reqMsg; CHECK_STATE(replyStr.size() > 2);
CHECK_STATE(replyStr.front() == '{');
CHECK_STATE(replyStr.back() == '}');
if (!socket->recv(&reqMsg, 0)) { if (!socket->send(_identity, ZMQ_SNDMORE)) {
checkForExit(); exit(-15);
// something terrible happened }
spdlog::error("Fatal error: socket.recv(&reqMsg, 0) returned false. Exiting"); if (!s_send(*socket, replyStr)) {
exit(-13); exit(-16);
} }
} catch (ExitRequestedException) {
throw;
} catch (std::exception &e) {
checkForExit();
spdlog::error("Exception in zmq server worker send :{}", e.what());
exit(-17);
} catch (...) {
checkForExit();
spdlog::error("Unklnown exception in zmq server worker send");
exit(-18);
}
stringToParse = string((char *) reqMsg.data(), reqMsg.size()); }
CHECK_STATE(stringToParse.front() == '{') void ZMQServer::doOneServerLoop() {
CHECK_STATE(stringToParse.back() == '}')
auto parsedMsg = ZMQMessage::parse( Json::Value result;
stringToParse.c_str(), stringToParse.size(), true, checkSignature, checkKeyOwnership); result["status"] = ZMQ_SERVER_ERROR;
zmq::message_t identity;
string msgStr;
if ((dynamic_pointer_cast<BLSSignReqMessage>(parsedMsg)!= nullptr) || try {
dynamic_pointer_cast<ECDSASignReqMessage>(parsedMsg)) {
spdlog::info("FUFUFUFUF");
} else {
spdlog::info("HAHAHA");
}
poll();
msgStr = receiveMessage(identity);
CHECK_STATE2(parsedMsg, ZMQ_COULD_NOT_PARSE); auto msg = ZMQMessage::parse(
msgStr.c_str(), msgStr.size(), true, checkSignature, checkKeyOwnership);
CHECK_STATE2(msg, ZMQ_COULD_NOT_PARSE);
result = parsedMsg->process(); if ((dynamic_pointer_cast<BLSSignReqMessage>(msg)!= nullptr) ||
dynamic_pointer_cast<ECDSASignReqMessage>(msg)) {
spdlog::info("FUFUFUFUF");
} else {
spdlog::info("HAHAHA");
}
result = msg->process();
} catch (ExitRequestedException) { } catch (ExitRequestedException) {
throw; throw;
} catch (std::exception &e) { } catch (std::exception &e) {
...@@ -250,43 +285,17 @@ void ZMQServer::doOneServerLoop() { ...@@ -250,43 +285,17 @@ void ZMQServer::doOneServerLoop() {
result["errorMessage"] = string(e.what()); result["errorMessage"] = string(e.what());
spdlog::error("Exception in zmq server :{}", e.what()); spdlog::error("Exception in zmq server :{}", e.what());
spdlog::error("ID:" + string((char *) identity.data(), identity.size())); spdlog::error("ID:" + string((char *) identity.data(), identity.size()));
spdlog::error("Client request :" + stringToParse); spdlog::error("Client request :" + msgStr);
} catch (...) { } catch (...) {
checkForExit(); checkForExit();
spdlog::error("Error in zmq server "); spdlog::error("Error in zmq server ");
result["errorMessage"] = "Error in zmq server "; result["errorMessage"] = "Error in zmq server ";
spdlog::error("ID:" + string((char *) identity.data(), identity.size())); spdlog::error("ID:" + string((char *) identity.data(), identity.size()));
spdlog::error("Client request :" + stringToParse); spdlog::error("Client request :" + msgStr);
} }
try { sendToClient(result, identity);
Json::FastWriter fastWriter;
fastWriter.omitEndingLineFeed();
replyStr = fastWriter.write(result);
CHECK_STATE(replyStr.size() > 2);
CHECK_STATE(replyStr.front() == '{');
CHECK_STATE(replyStr.back() == '}');
if (!socket->send(identity, ZMQ_SNDMORE)) {
exit(-15);
}
if (!s_send(*socket, replyStr)) {
exit(-16);
}
} catch (ExitRequestedException) {
throw;
} catch (std::exception &e) {
checkForExit();
spdlog::error("Exception in zmq server worker send :{}", e.what());
exit(-17);
} catch (...) {
checkForExit();
spdlog::error("Unklnown exception in zmq server worker send");
exit(-18);
}
} }
void ZMQServer::workerThreadProcessNextMessage() { void ZMQServer::workerThreadProcessNextMessage() {
......
...@@ -25,22 +25,12 @@ ...@@ -25,22 +25,12 @@
#ifndef SGXWALLET_ZMQServer_H #ifndef SGXWALLET_ZMQServer_H
#define SGXWALLET_ZMQServer_H #define SGXWALLET_ZMQServer_H
#include <vector>
#include <thread>
#include <memory>
#include <functional>
#include <atomic>
#include <zmq.hpp> #include <zmq.hpp>
#include "zhelpers.hpp" #include "zhelpers.hpp"
#include "Agent.h" #include "Agent.h"
#include "WorkerThreadPool.h" #include "WorkerThreadPool.h"
using namespace std;
class ZMQServer : public Agent{ class ZMQServer : public Agent{
uint64_t workerThreads; uint64_t workerThreads;
...@@ -85,6 +75,12 @@ public: ...@@ -85,6 +75,12 @@ public:
void checkForExit(); void checkForExit();
void poll();
string receiveMessage(zmq::message_t& _identity);
void sendToClient(Json::Value& _result, zmq::message_t& _identity);
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment