Skip to content

Commit

Permalink
Refactor DefaultServer class template
Browse files Browse the repository at this point in the history
Re ECFLOW-1957
  • Loading branch information
marcosbento committed Sep 23, 2024
1 parent fca83b6 commit d4f1b79
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 87 deletions.
3 changes: 0 additions & 3 deletions libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -482,11 +482,9 @@ set(srcs
server/src/ecflow/server/Server.hpp
server/src/ecflow/server/ServerEnvironment.hpp
server/src/ecflow/server/ServerOptions.hpp
server/src/ecflow/server/SslServer.hpp
server/src/ecflow/server/SslTcpServer.hpp
server/src/ecflow/server/TcpBaseServer.hpp
server/src/ecflow/server/TcpServer.hpp
$<$<BOOL:${OPENSSL_FOUND}>:server/src/ecflow/server/SslServer.hpp>
$<$<BOOL:${OPENSSL_FOUND}>:server/src/ecflow/server/SslTcpServer.hpp>
# Server -- Sources
server/src/ecflow/server/BaseServer.cpp
Expand All @@ -498,7 +496,6 @@ set(srcs
server/src/ecflow/server/ServerOptions.cpp
server/src/ecflow/server/TcpBaseServer.cpp
server/src/ecflow/server/TcpServer.cpp
$<$<BOOL:${OPENSSL_FOUND}>:server/src/ecflow/server/SslServer.cpp>
$<$<BOOL:${OPENSSL_FOUND}>:server/src/ecflow/server/SslTcpServer.cpp>

# Service -- Headers
Expand Down
2 changes: 1 addition & 1 deletion libs/base/src/ecflow/base/AbstractServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class AbstractServer {
/// Returns an NON empty string if server is ssl enabled.
/// 0/ "1 : enabled : uses shared ssl certificates";
/// 0/ " : enabled : uses server/port specific ssl certificates";
virtual const std::string& ssl() const { return ecf::Str::EMPTY(); }
virtual std::string ssl() const = 0;

/// returns the current state of the server
/// The following table shows the effect of state, on server behaviour:
Expand Down
2 changes: 2 additions & 0 deletions libs/base/test/MockServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class MockServer : public AbstractServer {
void operator()(void const*) const {}
};

std::string ssl() const override { return ""; }

// Only in server side do we increment state/modify numbers, controlled by: Ecf::set_server(true)
explicit MockServer(Defs* defs) : defs_(defs_ptr(defs, MockServer::null_deleter())) { Ecf::set_server(true); }
explicit MockServer(defs_ptr defs) : defs_(defs) { Ecf::set_server(true); }
Expand Down
2 changes: 2 additions & 0 deletions libs/server/src/ecflow/server/BaseServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class BaseServer : public AbstractServer {
// used in signal, for emergency check point during system session
void sigterm_signal_handler();

friend int run(BaseServer& server);

protected:
/// The io_context used to perform asynchronous operations.
boost::asio::io_context& io_;
Expand Down
5 changes: 0 additions & 5 deletions libs/server/src/ecflow/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,3 @@
*/

#include "ecflow/server/Server.hpp"

Server::Server(boost::asio::io_context& io, ServerEnvironment& serverEnv)
: BaseServer(io, serverEnv),
tcp_server_(this, io, serverEnv) {
}
25 changes: 21 additions & 4 deletions libs/server/src/ecflow/server/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,33 @@
#define ecflow_server_Server_HPP

#include "ecflow/server/BaseServer.hpp"
#include "ecflow/server/ServerEnvironment.hpp"
#include "ecflow/server/SslTcpServer.hpp"
#include "ecflow/server/TcpServer.hpp"

class Server : public BaseServer {
template <typename U>
class DefaultServer : public BaseServer {
public:
/// Constructor opens the acceptor and starts waiting for the first incoming connection.
explicit Server(boost::asio::io_context& io, ServerEnvironment&);
~Server() override = default;
explicit DefaultServer(boost::asio::io_context& io, ServerEnvironment& env)
: BaseServer(io, env),
server_(this, io, env) {}
~DefaultServer() override = default;

std::string ssl() const override {
if constexpr (ECF_OPENSSL == 1) {
return serverEnv_.openssl().ssl();
}
else {
return "";
}
}

private:
TcpServer tcp_server_;
U server_;
};

using Server = DefaultServer<TcpServer>;
using SslServer = DefaultServer<SslTcpServer>;

#endif /* ecflow_server_Server_HPP */
61 changes: 46 additions & 15 deletions libs/server/src/ecflow/server/ServerMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
#include "ecflow/core/Log.hpp"
#include "ecflow/server/Server.hpp"
#include "ecflow/server/ServerEnvironment.hpp"
#ifdef ECF_OPENSSL
#include "ecflow/server/SslServer.hpp"
#endif

using namespace ecf;
using namespace std;
Expand Down Expand Up @@ -49,6 +46,41 @@ int run_server(boost::asio::io_context& io, const ServerEnvironment& server_envi
return 0;
}

namespace {

int run_boost_services(boost::asio::io_context& io, const ServerEnvironment& server_environment) {
for (;;) {
try {
/// Start the server
/// The io_service::run() call will block until all asynchronous operations
/// have finished. While the server is running, there is always at least one
/// asynchronous operation outstanding: the asynchronous accept call waiting
/// for new incoming connections.
io.run();
if (server_environment.debug())
cout << "Normal exit from server\n";
break;
}
catch (std::exception& e) {
// deal with errors from the handlers
std::string msg = "run_server:: ";
msg += e.what();
std::cerr << msg << endl;
ecf::log(Log::ERR, msg);
}
if (server_environment.debug())
cout << "Server EXITING: <------------------------------------------------ port:"
<< server_environment.port() << endl;
}
return 0;
}

} // namespace

int run(BaseServer& server) {
return run_boost_services(server.io_, server.serverEnv_);
}

int main(int argc, char* argv[]) {

try {
Expand All @@ -70,19 +102,18 @@ int main(int argc, char* argv[]) {
<< server_environment.port() << endl;

boost::asio::io_context io;
#ifdef ECF_OPENSSL
if (server_environment.ssl()) {
SslServer theServer(io, server_environment); // This can throw exception, bind address in use.
return run_server(io, server_environment);
}
else {
Server theServer(io, server_environment); // This can throw exception, bind address in use.
return run_server(io, server_environment);

// Launching SSL server
if constexpr (ECF_OPENSSL == 1) {
if (server_environment.ssl()) {
SslServer theServer(io, server_environment); // This throws exception, if bind address in use
return run(theServer);
}
}
#else
Server theServer(io, server_environment); // This can throw exception, bind address in use.
return run_server(io, server_environment);
#endif

// Launching non-SSL server
Server theServer(io, server_environment); // This throws exception, if bind address in use
return run(theServer);
}
catch (ServerEnvironmentException& e) {
// *** deal with server options and environment exceptions
Expand Down
22 changes: 0 additions & 22 deletions libs/server/src/ecflow/server/SslServer.cpp

This file was deleted.

30 changes: 0 additions & 30 deletions libs/server/src/ecflow/server/SslServer.hpp

This file was deleted.

4 changes: 2 additions & 2 deletions libs/server/src/ecflow/server/SslTcpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
#include <iostream>

#include "ecflow/core/Log.hpp"
#include "ecflow/server/BaseServer.hpp"
#include "ecflow/server/ServerEnvironment.hpp"
#include "ecflow/server/SslServer.hpp"

using boost::asio::ip::tcp;
using namespace std;
using namespace ecf;

SslTcpServer::SslTcpServer(SslServer* server, boost::asio::io_context& io, ServerEnvironment& serverEnv)
SslTcpServer::SslTcpServer(BaseServer* server, boost::asio::io_context& io, ServerEnvironment& serverEnv)
: TcpBaseServer(server, io, serverEnv) {
server_->stats().ECF_SSL_ = serverEnv.openssl().info();

Expand Down
3 changes: 1 addition & 2 deletions libs/server/src/ecflow/server/SslTcpServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@

#include "ecflow/base/ssl_connection.hpp"
#include "ecflow/server/TcpBaseServer.hpp"
class SslServer;

class SslTcpServer : public TcpBaseServer {
public:
/// Constructor opens the acceptor and starts waiting for the first incoming connection.
explicit SslTcpServer(SslServer*, boost::asio::io_context& io, ServerEnvironment&);
explicit SslTcpServer(BaseServer*, boost::asio::io_context& io, ServerEnvironment&);
~SslTcpServer() = default;

private:
Expand Down
2 changes: 1 addition & 1 deletion libs/server/src/ecflow/server/TcpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ using boost::asio::ip::tcp;
using namespace std;
using namespace ecf;

TcpServer::TcpServer(Server* server, boost::asio::io_context& io, ServerEnvironment& serverEnv)
TcpServer::TcpServer(BaseServer* server, boost::asio::io_context& io, ServerEnvironment& serverEnv)
: TcpBaseServer(server, io, serverEnv) {
// timer_.stop(); // for timing of commands.

Expand Down
5 changes: 3 additions & 2 deletions libs/server/src/ecflow/server/TcpServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@

#include "ecflow/base/Connection.hpp"
#include "ecflow/server/TcpBaseServer.hpp"
class Server;

class BaseServer;

class TcpServer : public TcpBaseServer {
public:
/// Constructor opens the acceptor and starts waiting for the first incoming connection.
explicit TcpServer(Server*, boost::asio::io_context& io, ServerEnvironment&);
explicit TcpServer(BaseServer*, boost::asio::io_context& io, ServerEnvironment&);
~TcpServer() = default;

private:
Expand Down

0 comments on commit d4f1b79

Please sign in to comment.