Unverified Commit e4afc8fa authored by kladko's avatar kladko

q

parent 5c3e16c7
......@@ -66,7 +66,7 @@ bin_PROGRAMS = sgxwallet testw cert_util
## have to be explicitly listed.
## have to be explicitly listed
COMMON_SRC = InvalidStateException.cpp Exception.cpp InvalidArgumentException.cpp Log.cpp \
COMMON_SRC = ZMQServer.cpp ServerWorker.cpp InvalidStateException.cpp Exception.cpp InvalidArgumentException.cpp Log.cpp \
SGXWalletServer.cpp SGXRegistrationServer.cpp CSRManagerServer.cpp BLSCrypto.cpp \
ECDSACrypto.cpp \
DKGCrypto.cpp ServerInit.cpp BLSPrivateKeyShareSGX.cpp LevelDB.cpp ServerDataChecker.cpp SEKManager.cpp \
......@@ -113,8 +113,8 @@ nodist_testw_SOURCES=${nodist_sgxwallet_SOURCES}
EXTRA_testw_DEPENDENCIES=${EXTRA_sgxwallet_DEPENDENCIES}
testw_LDADD= ${sgxwallet_LDADD}
cert_util_SOURCES= InvalidStateException.cpp Exception.cpp InvalidArgumentException.cpp Log.cpp cert_util.cpp stubclient.cpp LevelDB.cpp SGXRegistrationServer.cpp CSRManagerServer.cpp \
ServerTask.cpp ServerWorker.cpp
cert_util_SOURCES= InvalidStateException.cpp Exception.cpp InvalidArgumentException.cpp Log.cpp cert_util.cpp stubclient.cpp LevelDB.cpp SGXRegistrationServer.cpp CSRManagerServer.cpp
cert_util_LDADD=-LlibBLS/deps/deps_inst/x86_or_x64/lib -Lleveldb/build -LlibBLS/build \
-LlibBLS/build/libff/libff \
-Llibzmq/build/lib/ \
......
......@@ -57,6 +57,7 @@
#include "BLSCrypto.h"
#include "ServerInit.h"
#include "SGXException.h"
#include "ZMQServer.h"
#include "SGXWalletServer.hpp"
uint32_t enclaveLogLevel = 0;
......@@ -106,6 +107,9 @@ void systemHealthCheck() {
}
}
static ZMQServer* zmqServer = nullptr;
atomic<bool> exiting(false);
void initUserSpace() {
libff::inhibit_profiling_counters = true;
......@@ -118,6 +122,17 @@ void initUserSpace() {
systemHealthCheck();
#endif
zmqServer = new ZMQServer();
static std::thread serverThread(std::bind(&ZMQServer::run, zmqServer));
}
void exitZMQServer() {
auto doExit = !exiting.exchange(true);
if (doExit) {
delete zmqServer;
zmqServer = nullptr;
}
}
......@@ -180,7 +195,6 @@ uint64_t initEnclave() {
void initAll(uint32_t _logLevel, bool _checkCert, bool _autoSign) {
static atomic<bool> sgxServerInited(false);
......
......@@ -38,6 +38,8 @@ EXTERNC void initUserSpace();
EXTERNC uint64_t initEnclave();
EXTERNC void exitZMQServer();
#endif //SGXWALLET_SERVERINIT_H
......@@ -6,31 +6,27 @@
ServerWorker::ServerWorker(zmq::context_t &ctx, int sock_type) : ctx_(ctx),
worker_(ctx_, sock_type){};
worker_(ctx_, sock_type) {};
void ServerWorker::work() {
worker_.connect("inproc://backend");
try {
while (true) {
zmq::message_t identity;
zmq::message_t msg;
zmq::message_t copied_id;
zmq::message_t copied_msg;
worker_.recv(&identity);
worker_.recv(&msg);
int replies = within(5);
for (int reply = 0; reply < replies; ++reply) {
s_sleep(within(1000) + 1);
copied_id.copy(&identity);
copied_msg.copy(&msg);
worker_.send(copied_id, ZMQ_SNDMORE);
worker_.send(copied_msg);
}
}
catch (std::exception &e) {
spdlog::info("Exiting zmq server worker:{}", e.what());
return;
} catch (...) {
spdlog::error("Error in zmq server worker");
return;
}
catch (std::exception &e) {}
}
......@@ -13,6 +13,8 @@
#include <zmq.hpp>
#include "zhelpers.hpp"
#include "third_party/spdlog/spdlog.h"
class ServerWorker {
......
1.64.2
\ No newline at end of file
1.65.1
\ No newline at end of file
......@@ -16,49 +16,77 @@
You should have received a copy of the GNU Affero General Public License
along with sgxwallet. If not, see <https://www.gnu.org/licenses/>.
@file ServerTask.cpp
@file ZMQServer.cpp
@author Stan Kladko
@date 2019
*/
#include "third_party/spdlog/spdlog.h"
#include "ServerWorker.h"
#include "ServerTask.h"
#include "ZMQServer.h"
#include "sgxwallet_common.h"
using namespace std;
ServerTask::ServerTask()
: ctx_(1),
ZMQServer::ZMQServer()
: isExitRequested(false), ctx_(1),
frontend_(ctx_, ZMQ_ROUTER),
backend_(ctx_, ZMQ_DEALER) {}
void ServerTask::run() {
frontend_.bind("tcp://*:" + to_string(BASE_PORT + 4)) ;
backend_.bind("inproc://backend");
void ZMQServer::run() {
std::vector < ServerWorker * > worker;
std::vector < std::thread * > worker_thread;
for (int i = 0; i < kMaxThread; ++i) {
worker.push_back(new ServerWorker(ctx_, ZMQ_DEALER));
auto port = BASE_PORT + 4;
worker_thread.push_back(new std::thread(std::bind(&ServerWorker::work, worker[i])));
worker_thread[i]->detach();
spdlog::info("Starting zmq server ...");
try {
frontend_.bind("tcp://*:" + to_string(BASE_PORT + 4));
} catch (...) {
spdlog::error("Server task could not bind to port:{}", port);
exit(-100);
}
spdlog::info("Bound port ...");
try {
zmq::proxy(static_cast<void *>(frontend_),
static_cast<void *>(backend_),
nullptr);
backend_.bind("inproc://backend");
} catch (exception &e) {
spdlog::error("Could not bind to zmq backend: {}", e.what());
exit(-101);
}
catch (std::exception &e) {}
static std::vector<ServerWorker *> worker;
static std::vector<std::thread *> worker_thread;
spdlog::info("Creating {} zmq server workers ...", kMaxThread);
try {
for (int i = 0; i < kMaxThread; ++i) {
delete worker[i];
delete worker_thread[i];
worker.push_back(new ServerWorker(ctx_, ZMQ_DEALER));
worker_thread.push_back(new std::thread(std::bind(&ServerWorker::work, worker[i])));
}
} catch (std::exception &e) {
spdlog::error("Could not create zmq server workers:{} ", e.what());
exit(-102);
}
try {
zmq::proxy(static_cast<void *>(frontend_), static_cast<void *>(backend_), nullptr);
} catch (exception& _e) {
spdlog::info("Exiting zmq server {}", _e.what());
return;
} catch (...) {
spdlog::info("Exiting zmq server");
return;
}
}
......@@ -16,29 +16,34 @@
You should have received a copy of the GNU Affero General Public License
along with sgxwallet. If not, see <https://www.gnu.org/licenses/>.
@file ServerTask.h
@file ZMQServer.h
@author Stan Kladko
@date 2020
*/
#ifndef SGXWALLET_SERVERTASK_H
#define SGXWALLET_SERVERTASK_H
#ifndef SGXWALLET_ZMQServer_H
#define SGXWALLET_ZMQServer_H
#include <vector>
#include <thread>
#include <memory>
#include <functional>
#include <atomic>
#include <zmq.hpp>
#include "zhelpers.hpp"
using namespace std;
class ServerTask {
class ZMQServer {
public:
ServerTask();
ZMQServer();
atomic<bool> isExitRequested;
enum {
kMaxThread = 5
......@@ -52,4 +57,4 @@ private:
zmq::socket_t backend_;
};
#endif //SGXWALLET_SERVERTASK_H
#endif //SGXWALLET_ZMQServer_H
......@@ -34,10 +34,13 @@
#include "TestUtils.h"
#include "ZMQServer.h"
#include "testw.h"
#include "sgxwall.h"
#include "sgxwallet.h"
void SGXWallet::usage() {
cerr << "usage: sgxwallet\n";
exit(-21);
......@@ -201,6 +204,8 @@ int main(int argc, char *argv[]) {
cerr << "Successfully completed generating test keys into sgx_data" << endl;
}
while (true) {
sleep(10);
}
......
......@@ -89,6 +89,7 @@ public:
}
~TestFixtureHTTPS() {
exitZMQServer();
TestUtils::destroyEnclave();
}
};
......@@ -101,6 +102,7 @@ public:
}
~TestFixtureNoResetFromBackup() {
exitZMQServer();
TestUtils::destroyEnclave();
}
};
......@@ -114,6 +116,7 @@ public:
}
~TestFixtureNoReset() {
exitZMQServer();
TestUtils::destroyEnclave();
}
};
......
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