Skip to content

Commit cf83fae

Browse files
committed
Refactoring.
1 parent b70330e commit cf83fae

File tree

16 files changed

+479
-608
lines changed

16 files changed

+479
-608
lines changed

apps/service.cpp

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,43 +19,11 @@
1919
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2020
*/
2121

22-
#include <iostream>
23-
2422
#include <brayns/core/Launcher.h>
25-
#include <brayns/core/Version.h>
26-
#include <brayns/core/cli/CommandLine.h>
2723

2824
using namespace brayns;
2925

3026
int main(int argc, const char **argv)
3127
{
32-
try
33-
{
34-
auto settings = parseArgvAs<ServiceSettings>(argc, argv);
35-
36-
if (settings.version)
37-
{
38-
std::cout << getCopyright() << '\n';
39-
return 0;
40-
}
41-
42-
if (settings.help)
43-
{
44-
std::cout << getArgvHelp<ServiceSettings>() << '\n';
45-
return 0;
46-
}
47-
48-
runService(settings);
49-
}
50-
catch (const std::exception &e)
51-
{
52-
std::cout << "Fatal error: " << e.what() << ".\n";
53-
}
54-
catch (...)
55-
{
56-
std::cout << "Unknown fatal error.";
57-
return 1;
58-
}
59-
60-
return 0;
28+
return runService(argc, argv);
6129
}

src/brayns/core/Launcher.cpp

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,18 @@
2121

2222
#include "Launcher.h"
2323

24+
#include <iostream>
25+
2426
#include <brayns/core/service/Service.h>
2527
#include <brayns/core/utils/Logger.h>
2628

27-
namespace brayns
29+
namespace
2830
{
29-
void runService(const ServiceSettings &settings)
31+
using namespace brayns;
32+
33+
Service createService(const ServiceSettings &settings, Logger &logger)
3034
{
3135
auto level = getEnumValue<LogLevel>(settings.logLevel);
32-
33-
auto logger = createConsoleLogger("brayns");
3436
logger.setLevel(level);
3537

3638
auto ssl = std::optional<SslSettings>();
@@ -54,19 +56,55 @@ void runService(const ServiceSettings &settings)
5456
.ssl = std::move(ssl),
5557
};
5658

57-
auto endpoints = EndpointRegistry({});
58-
59-
auto tasks = TaskManager();
59+
auto api = Api({});
6060

6161
auto context = std::make_unique<ServiceContext>(ServiceContext{
62-
.logger = std::move(logger),
62+
.logger = logger,
6363
.server = std::move(server),
64-
.endpoints = std::move(endpoints),
65-
.tasks = std::move(tasks),
64+
.api = std::move(api),
6665
});
6766

68-
auto service = Service(std::move(context));
67+
return Service(std::move(context));
68+
}
69+
}
70+
71+
namespace brayns
72+
{
73+
int runService(int argc, const char **argv)
74+
{
75+
auto logger = createConsoleLogger("brayns");
76+
77+
try
78+
{
79+
auto settings = parseArgvAs<ServiceSettings>(argc, argv);
80+
81+
if (settings.help)
82+
{
83+
std::cout << getArgvHelp<ServiceSettings>() << '\n';
84+
return 0;
85+
}
86+
87+
if (settings.version)
88+
{
89+
std::cout << getCopyright() << '\n';
90+
return 0;
91+
}
92+
93+
auto service = createService(settings, logger);
6994

70-
service.run();
95+
service.run();
96+
97+
return 0;
98+
}
99+
catch (const std::exception &e)
100+
{
101+
logger.fatal("Cannot start service: {}", e.what());
102+
return 1;
103+
}
104+
catch (...)
105+
{
106+
logger.fatal("Cannot start service for unknown reason");
107+
return 1;
108+
}
71109
}
72110
}

src/brayns/core/Launcher.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,5 @@ struct ArgvSettingsReflector<ServiceSettings>
101101
}
102102
};
103103

104-
void runService(const ServiceSettings &settings);
104+
int runService(int argc, const char **argv);
105105
}

src/brayns/core/api/Task.cpp renamed to src/brayns/core/api/Api.cpp

Lines changed: 85 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <cassert>
2525

2626
#include <fmt/format.h>
27+
#include "Api.h"
2728

2829
namespace
2930
{
@@ -35,61 +36,125 @@ const RawTask &getTask(const std::map<TaskId, RawTask> &tasks, TaskId id)
3536

3637
if (i == tasks.end())
3738
{
38-
throw InvalidParams(fmt::format("Invalid task ID: {}", id));
39+
throw InvalidParams(fmt::format("Task ID not found: {}", id));
3940
}
4041

4142
return i->second;
4243
}
44+
45+
TaskId addTask(RawTask task, std::map<TaskId, RawTask> &tasks, IdGenerator<TaskId> ids)
46+
{
47+
auto id = ids.next();
48+
assert(!tasks.contains(id));
49+
50+
try
51+
{
52+
tasks[id] = std::move(task);
53+
}
54+
catch (...)
55+
{
56+
ids.recycle(id);
57+
throw;
58+
}
59+
60+
return id;
61+
}
4362
}
4463

4564
namespace brayns
4665
{
47-
TaskManager::~TaskManager()
66+
Api::Api(std::map<std::string, Endpoint> endpoints):
67+
_endpoints(std::move(endpoints))
68+
{
69+
}
70+
71+
Api::~Api()
4872
{
4973
for (const auto &[id, task] : _tasks)
5074
{
5175
task.cancel();
5276
}
5377
}
5478

55-
std::vector<TaskInfo> TaskManager::getTasks() const
79+
std::vector<std::string> Api::getMethods() const
5680
{
57-
auto infos = std::vector<TaskInfo>();
58-
infos.reserve(_tasks.size());
81+
auto result = std::vector<std::string>();
82+
result.reserve(_endpoints.size());
5983

60-
for (const auto &[id, task] : _tasks)
84+
for (const auto &[method, endpoint] : _endpoints)
6185
{
62-
infos.push_back({id, task.getProgress()});
86+
result.push_back(method);
6387
}
6488

65-
return infos;
89+
return result;
6690
}
6791

68-
TaskId TaskManager::add(RawTask task)
92+
const EndpointSchema &Api::getSchema(const std::string &method) const
6993
{
70-
auto id = _ids.next();
71-
assert(!_tasks.contains(id));
94+
auto i = _endpoints.find(method);
7295

73-
try
96+
if (i == _endpoints.end())
7497
{
75-
_tasks[id] = std::move(task);
98+
throw InvalidParams(fmt::format("Invalid endpoint method: '{}'", method));
7699
}
77-
catch (...)
100+
101+
return i->second.schema;
102+
}
103+
104+
RawResult Api::execute(const std::string &method, RawParams params)
105+
{
106+
auto i = _endpoints.find(method);
107+
108+
if (i == _endpoints.end())
78109
{
79-
_ids.recycle(id);
80-
throw;
110+
throw MethodNotFound(method);
81111
}
82112

83-
return id;
113+
const auto &endpoint = i->second;
114+
115+
auto errors = validate(params.json, endpoint.schema.params);
116+
117+
if (!errors.empty())
118+
{
119+
throw InvalidParams("Invalid params schema", errors);
120+
}
121+
122+
if (endpoint.schema.async)
123+
{
124+
const auto &launcher = std::get<TaskLauncher>(endpoint.handler);
125+
126+
auto task = launcher(std::move(params));
127+
128+
auto id = addTask(std::move(task), _tasks, _ids);
129+
130+
return {serializeToJson(TaskResult{id})};
131+
}
132+
133+
const auto &runner = std::get<TaskRunner>(endpoint.handler);
134+
135+
return runner(std::move(params));
136+
}
137+
138+
std::vector<TaskInfo> Api::getTasks() const
139+
{
140+
auto infos = std::vector<TaskInfo>();
141+
infos.reserve(_tasks.size());
142+
143+
for (const auto &[id, task] : _tasks)
144+
{
145+
infos.push_back({id, task.getProgress()});
146+
}
147+
148+
return infos;
84149
}
85150

86-
ProgressInfo TaskManager::getProgress(TaskId id) const
151+
ProgressInfo Api::getTaskProgress(TaskId id) const
87152
{
88153
const auto &task = getTask(_tasks, id);
89154
return task.getProgress();
90155
}
91156

92-
RawResult TaskManager::wait(TaskId id)
157+
RawResult Api::waitForTaskResult(TaskId id)
93158
{
94159
const auto &task = getTask(_tasks, id);
95160
auto result = task.wait();
@@ -98,7 +163,7 @@ RawResult TaskManager::wait(TaskId id)
98163
return result;
99164
}
100165

101-
void TaskManager::cancel(TaskId id)
166+
void Api::cancelTask(TaskId id)
102167
{
103168
const auto &task = getTask(_tasks, id);
104169
task.cancel();

src/brayns/core/service/RequestHandler.h renamed to src/brayns/core/api/Api.h

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,37 @@
2121

2222
#pragma once
2323

24-
#include <brayns/core/api/Endpoint.h>
25-
#include <brayns/core/utils/Logger.h>
26-
#include <brayns/core/websocket/WebSocketHandler.h>
24+
#include <map>
25+
26+
#include <brayns/core/utils/IdGenerator.h>
27+
28+
#include "Endpoint.h"
29+
#include "Task.h"
2730

2831
namespace brayns
2932
{
30-
class RequestHandler
33+
class Api
3134
{
3235
public:
33-
explicit RequestHandler(const EndpointRegistry &endpoints, TaskManager &tasks, Logger &logger);
36+
explicit Api(std::map<std::string, Endpoint> endpoints);
37+
~Api();
38+
39+
Api(const Api &) = delete;
40+
Api(Api &&) = default;
41+
Api &operator=(const Api &) = delete;
42+
Api &operator=(Api &&) = default;
3443

35-
void handle(const RawRequest &request);
44+
std::vector<std::string> getMethods() const;
45+
const EndpointSchema &getSchema(const std::string &method) const;
46+
RawResult execute(const std::string &method, RawParams params);
47+
std::vector<TaskInfo> getTasks() const;
48+
ProgressInfo getTaskProgress(TaskId id) const;
49+
RawResult waitForTaskResult(TaskId id);
50+
void cancelTask(TaskId id);
3651

3752
private:
38-
const EndpointRegistry *_endpoints;
39-
TaskManager *_tasks;
40-
Logger *_logger;
53+
std::map<std::string, Endpoint> _endpoints;
54+
std::map<TaskId, RawTask> _tasks;
55+
IdGenerator<TaskId> _ids;
4156
};
4257
}

0 commit comments

Comments
 (0)