Skip to content

Commit ac3a7fc

Browse files
committed
engine: Replace direct use of oak types with ServerRegistrar interface
This makes replacing the implementation easier and speeds up builds.
1 parent d8c826a commit ac3a7fc

File tree

4 files changed

+78
-30
lines changed

4 files changed

+78
-30
lines changed

engine/src/registrar.hpp

+8-17
Original file line numberDiff line numberDiff line change
@@ -27,45 +27,37 @@
2727
#include <cloe/registrar.hpp> // for cloe::Registrar
2828

2929
#include "coordinator.hpp" // for Coordinator
30-
#include "server.hpp" // for Server, oak::ProxyRegistrar
30+
#include "server.hpp" // for Server, ServerRegistrar
3131

3232
namespace engine {
3333

3434
class Registrar : public cloe::Registrar {
3535
public:
36-
Registrar(Server& s, std::shared_ptr<Coordinator> c)
37-
: static_registrar_(s.static_registrar())
38-
, api_registrar_(s.api_registrar())
36+
Registrar(std::unique_ptr<ServerRegistrar> r, std::shared_ptr<Coordinator> c)
37+
: server_registrar_(std::move(r))
3938
, coordinator_(std::move(c)) {}
4039

4140
Registrar(const Registrar& ar,
4241
const std::string& trigger_prefix,
4342
const std::string& static_prefix,
4443
const std::string& api_prefix)
45-
: static_registrar_(ar.static_registrar_)
46-
, api_registrar_(ar.api_registrar_)
47-
, coordinator_(ar.coordinator_) {
44+
: coordinator_(ar.coordinator_) {
4845
if (trigger_prefix.empty()) {
4946
trigger_prefix_ = ar.trigger_prefix_;
5047
} else {
5148
trigger_prefix_ = ar.trigger_prefix_ + trigger_prefix;
5249
}
53-
if (!static_prefix.empty()) {
54-
static_registrar_ = ar.static_registrar_.with_prefix(static_prefix);
55-
}
56-
if (!api_prefix.empty()) {
57-
api_registrar_ = ar.api_registrar_.with_prefix(api_prefix);
58-
}
50+
server_registrar_ = ar.server_registrar_->with_prefix(static_prefix, api_prefix);
5951
}
6052

6153
void register_static_handler(const std::string& endpoint, cloe::Handler h) override {
62-
static_registrar_.register_handler(endpoint, h);
54+
server_registrar_->register_static_handler(endpoint, h);
6355
}
6456

6557
void register_api_handler(const std::string& endpoint,
6658
cloe::HandlerType t,
6759
cloe::Handler h) override {
68-
api_registrar_.register_handler(endpoint, t, h);
60+
server_registrar_->register_api_handler(endpoint, t, h);
6961
}
7062

7163
std::unique_ptr<cloe::Registrar> clone() const {
@@ -113,8 +105,7 @@ class Registrar : public cloe::Registrar {
113105
}
114106

115107
private:
116-
oak::Registrar static_registrar_;
117-
oak::ProxyRegistrar<cloe::HandlerType> api_registrar_;
108+
std::unique_ptr<ServerRegistrar> server_registrar_;
118109
std::shared_ptr<Coordinator> coordinator_;
119110
std::string trigger_prefix_;
120111
};

engine/src/server.cpp

+45-4
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,53 @@
2222

2323
#include "server.hpp"
2424

25-
#include <memory> // for unique_ptr<>
25+
#include <memory> // for unique_ptr<>, make_unique
2626
#include <utility> // for make_pair
2727

28-
#include <cloe/registrar.hpp> // for HandlerType
28+
#include <cloe/registrar.hpp> // for HandlerType
2929
#include <cloe/utility/output_serializer_json.hpp> // for JsonFileSerializer
3030

3131
#include <oak/server.hpp> // for Server, StaticRegistrar, ...
3232

3333
namespace engine {
3434

35+
class ServerRegistrarImpl : public ServerRegistrar {
36+
public:
37+
ServerRegistrarImpl(
38+
oak::Registrar static_reg, oak::ProxyRegistrar<cloe::HandlerType> api_reg)
39+
: static_registrar_(static_reg), api_registrar_(api_reg) {}
40+
41+
std::unique_ptr<ServerRegistrar> clone() const override {
42+
return std::make_unique<ServerRegistrarImpl>(static_registrar_, api_registrar_);
43+
}
44+
45+
std::unique_ptr<ServerRegistrar> with_prefix(const std::string& static_prefix,
46+
const std::string& api_prefix) const override {
47+
auto static_reg = static_registrar_;
48+
if (!static_prefix.empty()) {
49+
static_reg = static_registrar_.with_prefix(static_prefix);
50+
}
51+
auto api_reg = api_registrar_;
52+
if (!api_prefix.empty()) {
53+
api_reg = api_registrar_.with_prefix(api_prefix);
54+
}
55+
return std::make_unique<ServerRegistrarImpl>(static_reg, api_reg);
56+
}
57+
58+
void register_static_handler(const std::string& endpoint, cloe::Handler h) override {
59+
static_registrar_.register_handler(endpoint, h);
60+
}
61+
62+
void register_api_handler(const std::string& endpoint, cloe::HandlerType t,
63+
cloe::Handler h) override {
64+
api_registrar_.register_handler(endpoint, t, h);
65+
}
66+
67+
private:
68+
oak::Registrar static_registrar_;
69+
oak::ProxyRegistrar<cloe::HandlerType> api_registrar_;
70+
};
71+
3572
class ServerImpl : public Server {
3673
public:
3774
ServerImpl(const cloe::ServerConf& config) : Server(config) {
@@ -98,9 +135,13 @@ class ServerImpl : public Server {
98135
[this](const cloe::Request&, cloe::Response& r) { r.write(this->server_.endpoints()); });
99136
}
100137

101-
oak::Registrar static_registrar() override { return static_registrar_.with("", nullptr); }
138+
std::unique_ptr<ServerRegistrar> server_registrar() override {
139+
return std::make_unique<ServerRegistrarImpl>(static_registrar(), api_registrar());
140+
}
141+
142+
oak::Registrar static_registrar() { return static_registrar_.with("", nullptr); }
102143

103-
oak::ProxyRegistrar<cloe::HandlerType> api_registrar() override {
144+
oak::ProxyRegistrar<cloe::HandlerType> api_registrar() {
104145
return oak::ProxyRegistrar<cloe::HandlerType>({
105146
std::make_pair(cloe::HandlerType::STATIC, &static_api_registrar_),
106147
std::make_pair(cloe::HandlerType::DYNAMIC, &locked_api_registrar_),

engine/src/server.hpp

+24-8
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,33 @@
2525
#include <memory> // for unique_ptr<>
2626

2727
#include <cloe/registrar.hpp> // for Registrar
28-
#include <oak/registrar.hpp> // for oak::Registrar, oak::ProxyRegistrar
2928

3029
#include "stack.hpp" // for ServerConf
3130
#include "utility/defer.hpp" // for Defer
3231

3332
namespace engine {
3433

34+
/**
35+
* Server registrar interface.
36+
*
37+
* This lets you register static and API endpoints with the server.
38+
* Get a new one from Server::server_registrar().
39+
*/
40+
class ServerRegistrar {
41+
public:
42+
virtual ~ServerRegistrar() = default;
43+
44+
virtual std::unique_ptr<ServerRegistrar> clone() const = 0;
45+
46+
virtual std::unique_ptr<ServerRegistrar> with_prefix(const std::string& static_prefix,
47+
const std::string& api_prefix) const = 0;
48+
49+
virtual void register_static_handler(const std::string& endpoint, cloe::Handler h) = 0;
50+
51+
virtual void register_api_handler(const std::string& endpoint, cloe::HandlerType t,
52+
cloe::Handler h) = 0;
53+
};
54+
3555
/**
3656
* Server interface to make altering the implementation easier.
3757
*
@@ -81,14 +101,10 @@ class Server {
81101
virtual void enroll(cloe::Registrar& r) = 0;
82102

83103
/**
84-
* Return the static content registrar.
85-
*/
86-
virtual oak::Registrar static_registrar() = 0;
87-
88-
/**
89-
* Return the API registrar.
104+
* Return a new ServerRegistrar that lets you register static content and
105+
* API endpoints with the web server.
90106
*/
91-
virtual oak::ProxyRegistrar<cloe::HandlerType> api_registrar() = 0;
107+
virtual std::unique_ptr<ServerRegistrar> server_registrar() = 0;
92108

93109
/**
94110
* Refresh and/or start streaming api data to a file.

engine/src/simulation.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ SimulationResult Simulation::run() {
11611161
SimulationContext ctx;
11621162
ctx.server = make_server(config_.server);
11631163
ctx.coordinator.reset(new Coordinator{});
1164-
ctx.registrar.reset(new Registrar{*ctx.server, ctx.coordinator});
1164+
ctx.registrar.reset(new Registrar{ctx.server->server_registrar(), ctx.coordinator});
11651165
ctx.commander.reset(new CommandExecuter{logger()});
11661166
ctx.sync = SimulationSync(config_.simulation.model_step_width);
11671167
ctx.config = config_;

0 commit comments

Comments
 (0)