Skip to content

Commit d776be7

Browse files
authored
BRAYNS-645 Basic service. (#1270)
1 parent 4308323 commit d776be7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2783
-652
lines changed

apps/service.cpp

Lines changed: 26 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,60 @@
1-
/* Copyright (c) 2015-2024, EPFL/Blue Brain Project
1+
/* Copyright (c) 2015-2024 EPFL/Blue Brain Project
22
* All rights reserved. Do not distribute without permission.
3-
* Responsible Author: Cyrille Favreau <[email protected]>
3+
*
4+
* Responsible Author: [email protected]
45
*
56
* This file is part of Brayns <https://github.com/BlueBrain/Brayns>
67
*
7-
* This program is free software: you can redistribute it and/or modify
8-
* it under the terms of the GNU General Public License as published by
9-
* the Free Software Foundation, either version 3 of the License, or
10-
* (at your option) any later version.
8+
* This library is free software; you can redistribute it and/or modify it under
9+
* the terms of the GNU Lesser General Public License version 3.0 as published
10+
* by the Free Software Foundation.
1111
*
12-
* This program is distributed in the hope that it will be useful,
13-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15-
* GNU General Public License for more details.
12+
* This library is distributed in the hope that it will be useful, but WITHOUT
13+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14+
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15+
* details.
1616
*
17-
* You should have received a copy of the GNU General Public License
18-
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with this library; if not, write to the Free Software Foundation, Inc.,
19+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1920
*/
2021

2122
#include <iostream>
2223

23-
#include <brayns/core/Brayns.h>
24-
#include <brayns/core/CommandLine.h>
25-
#include <brayns/core/utils/Log.h>
26-
#include <brayns/core/utils/Timer.h>
27-
28-
#ifdef BRAYNS_ENABLE_CIRCUITS
29-
#include <brayns/circuits/CircuitExplorerPlugin.h>
30-
#endif
24+
#include <brayns/core/Launcher.h>
25+
#include <brayns/core/Version.h>
26+
#include <brayns/core/cli/CommandLine.h>
3127

32-
#ifdef BRAYNS_ENABLE_ATLAS
33-
#include <brayns/atlas/AtlasExplorerPlugin.h>
34-
#endif
28+
using namespace brayns::experimental;
29+
using brayns::getCopyright;
3530

3631
int main(int argc, const char **argv)
3732
{
3833
try
3934
{
40-
auto commandLine = brayns::CommandLine(argc, argv);
35+
auto settings = parseArgvAs<ServiceSettings>(argc, argv);
4136

42-
if (commandLine.hasVersion())
37+
if (settings.version)
4338
{
44-
auto version = commandLine.getVersion();
45-
std::cout << version << '\n';
39+
std::cout << getCopyright() << '\n';
4640
return 0;
4741
}
4842

49-
if (commandLine.hasHelp())
43+
if (settings.help)
5044
{
51-
auto help = commandLine.getHelp();
52-
std::cout << help << '\n';
45+
std::cout << getArgvHelp<ServiceSettings>() << '\n';
5346
return 0;
5447
}
5548

56-
auto instance = brayns::Brayns(argc, argv);
57-
58-
#ifdef BRAYNS_ENABLE_CIRCUITS
59-
brayns::loadCircuitExplorer(instance);
60-
#endif
61-
62-
#ifdef BRAYNS_ENABLE_ATLAS
63-
brayns::loadAtlasExplorer(instance);
64-
#endif
65-
66-
brayns::Log::info("Starting Brayns service.");
67-
68-
auto timer = brayns::Timer();
69-
70-
instance.runAsService();
71-
72-
brayns::Log::info("Service was running for {} seconds.", timer.seconds());
49+
runService(settings);
7350
}
7451
catch (const std::exception &e)
7552
{
76-
brayns::Log::fatal("Fatal error: '{}'.", e.what());
77-
return 1;
53+
std::cout << "Fatal error: " << e.what() << ".\n";
7854
}
7955
catch (...)
8056
{
81-
brayns::Log::fatal("Unknown fatal error.");
57+
std::cout << "Unknown fatal error.";
8258
return 1;
8359
}
8460

src/brayns/core/Brayns.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class LoggingStartup
5252

5353
static void _logVersion()
5454
{
55-
auto copyright = brayns::Version::getCopyright();
55+
auto copyright = brayns::getCopyright();
5656
brayns::Log::info("{}.", copyright);
5757
}
5858

src/brayns/core/CommandLine.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class HelpFormatter
6666
static std::string format(const std::vector<brayns::ArgvProperty> &properties)
6767
{
6868
std::ostringstream stream;
69-
stream << brayns::Version::getCopyright() << '.';
69+
stream << brayns::getCopyright() << '.';
7070
stream << "\n\nBrayns can be configured using command line options with keyword arguments (--name value).";
7171
stream << "\n\nExample: braynsService --uri 0.0.0.0:5000 --window-size '1920 1080'";
7272
stream << "\n\nThe supported options are the following:";
@@ -157,7 +157,7 @@ std::string CommandLine::getHelp()
157157

158158
std::string CommandLine::getVersion()
159159
{
160-
return Version::getCopyright();
160+
return getCopyright();
161161
}
162162

163163
CommandLine::CommandLine(int argc, const char **argv):

src/brayns/core/Launcher.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* Copyright (c) 2015-2024 EPFL/Blue Brain Project
2+
* All rights reserved. Do not distribute without permission.
3+
*
4+
* Responsible Author: [email protected]
5+
*
6+
* This file is part of Brayns <https://github.com/BlueBrain/Brayns>
7+
*
8+
* This library is free software; you can redistribute it and/or modify it under
9+
* the terms of the GNU Lesser General Public License version 3.0 as published
10+
* by the Free Software Foundation.
11+
*
12+
* This library is distributed in the hope that it will be useful, but WITHOUT
13+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14+
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15+
* details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with this library; if not, write to the Free Software Foundation, Inc.,
19+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*/
21+
22+
#include "Launcher.h"
23+
24+
#include <brayns/core/service/Service.h>
25+
#include <brayns/core/utils/Logger.h>
26+
27+
namespace brayns::experimental
28+
{
29+
void runService(const ServiceSettings &settings)
30+
{
31+
auto level = brayns::EnumInfo::getValue<LogLevel>(settings.logLevel);
32+
33+
auto logger = createConsoleLogger("brayns");
34+
logger.setLevel(level);
35+
36+
auto ssl = std::optional<SslSettings>();
37+
38+
if (settings.ssl)
39+
{
40+
ssl = SslSettings{
41+
.privateKeyFile = settings.privateKeyFile,
42+
.certificateFile = settings.certificateFile,
43+
.caLocation = settings.caLocation,
44+
.privateKeyPassphrase = settings.privateKeyPassphrase,
45+
};
46+
}
47+
48+
auto server = WebSocketServerSettings{
49+
.host = settings.host,
50+
.port = settings.port,
51+
.maxThreadCount = settings.maxThreadCount,
52+
.maxQueueSize = settings.maxQueueSize,
53+
.maxFrameSize = settings.maxFrameSize,
54+
.ssl = std::move(ssl),
55+
};
56+
57+
auto endpoints = EndpointRegistry({});
58+
59+
auto tasks = TaskManager();
60+
61+
auto context = std::make_unique<ServiceContext>(ServiceContext{
62+
.logger = std::move(logger),
63+
.server = std::move(server),
64+
.endpoints = std::move(endpoints),
65+
.tasks = std::move(tasks),
66+
});
67+
68+
auto service = Service(std::move(context));
69+
70+
service.run();
71+
}
72+
}

src/brayns/core/Launcher.h

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/* Copyright (c) 2015-2024 EPFL/Blue Brain Project
2+
* All rights reserved. Do not distribute without permission.
3+
*
4+
* Responsible Author: [email protected]
5+
*
6+
* This file is part of Brayns <https://github.com/BlueBrain/Brayns>
7+
*
8+
* This library is free software; you can redistribute it and/or modify it under
9+
* the terms of the GNU Lesser General Public License version 3.0 as published
10+
* by the Free Software Foundation.
11+
*
12+
* This library is distributed in the hope that it will be useful, but WITHOUT
13+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14+
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15+
* details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with this library; if not, write to the Free Software Foundation, Inc.,
19+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*/
21+
22+
#pragma once
23+
24+
#include <brayns/core/Version.h>
25+
#include <brayns/core/cli/CommandLine.h>
26+
27+
namespace brayns::experimental
28+
{
29+
struct ServiceSettings
30+
{
31+
bool help;
32+
bool version;
33+
std::string logLevel;
34+
std::string host;
35+
std::uint16_t port;
36+
std::size_t maxThreadCount;
37+
std::size_t maxQueueSize;
38+
std::size_t maxFrameSize;
39+
bool ssl;
40+
std::string privateKeyFile;
41+
std::string certificateFile;
42+
std::string caLocation;
43+
std::string privateKeyPassphrase;
44+
};
45+
46+
template<>
47+
struct ArgvSettingsReflector<ServiceSettings>
48+
{
49+
static auto reflect()
50+
{
51+
auto builder = ArgvBuilder<ServiceSettings>();
52+
53+
builder.description(getCopyright());
54+
55+
builder.option("help", [](auto &settings) { return &settings.help; })
56+
.description("Display this help message and exit")
57+
.defaultValue(false);
58+
59+
builder.option("version", [](auto &settings) { return &settings.version; })
60+
.description("Display brayns copyright with version and exit")
61+
.defaultValue(false);
62+
63+
builder.option("log-level", [](auto &settings) { return &settings.logLevel; })
64+
.description("Log level among [trace, debug, info, warn, error, fatal]")
65+
.defaultValue("info");
66+
67+
builder.option("host", [](auto &settings) { return &settings.host; })
68+
.description("Websocket server hostname, use 0.0.0.0 to allow any host to connect")
69+
.defaultValue("localhost");
70+
builder.option("port", [](auto &settings) { return &settings.port; })
71+
.description("Websocket server port")
72+
.defaultValue(5000);
73+
builder.option("max-thread-count", [](auto &settings) { return &settings.maxThreadCount; })
74+
.description("Maximum number of threads for the websocket server")
75+
.defaultValue(2);
76+
builder.option("max-queue-size", [](auto &settings) { return &settings.maxQueueSize; })
77+
.description("Maximum number of queued connections before they are rejected")
78+
.defaultValue(64);
79+
builder.option("max-frame-size", [](auto &settings) { return &settings.maxFrameSize; })
80+
.description("Maximum frame size the websocket server accepts")
81+
.defaultValue(std::numeric_limits<int>::max());
82+
83+
builder.option("ssl", [](auto &settings) { return &settings.ssl; })
84+
.description("Enable SSL for websocket server, requires a certificate and a private key")
85+
.defaultValue(false);
86+
87+
builder.option("private-key-file", [](auto &settings) { return &settings.privateKeyFile; })
88+
.description("SSL private key used by the websocket server")
89+
.defaultValue("");
90+
builder.option("certificate-file", [](auto &settings) { return &settings.certificateFile; })
91+
.description("SSL certificate the websocket server will provide to clients")
92+
.defaultValue("");
93+
builder.option("ca-location", [](auto &settings) { return &settings.caLocation; })
94+
.description("Path to an additional certification authority (file or directory)")
95+
.defaultValue("");
96+
builder.option("private-key-passphrase", [](auto &settings) { return &settings.privateKeyPassphrase; })
97+
.description("Passphrase for the private key if encrypted")
98+
.defaultValue("");
99+
100+
return builder.build();
101+
}
102+
};
103+
104+
void runService(const ServiceSettings &settings);
105+
}

src/brayns/core/Version.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,24 @@
2525

2626
namespace brayns
2727
{
28-
std::string Version::getTag()
28+
std::string getVersionTag()
2929
{
3030
constexpr auto major = BRAYNS_VERSION_MAJOR;
3131
constexpr auto minor = BRAYNS_VERSION_MINOR;
3232
constexpr auto patch = BRAYNS_VERSION_PATCH;
3333
constexpr auto preRelease = BRAYNS_VERSION_PRE_RELEASE;
34+
3435
if (preRelease == 0)
3536
{
3637
return fmt::format("{}.{}.{}", major, minor, patch);
3738
}
39+
3840
return fmt::format("{}.{}.{}-{}", major, minor, patch, preRelease);
3941
}
4042

41-
std::string Version::getCopyright()
43+
std::string getCopyright()
4244
{
43-
auto tag = getTag();
45+
auto tag = getVersionTag();
4446
return fmt::format("Brayns version {} Copyright (c) 2015-2024, EPFL/Blue Brain Project", tag);
4547
}
4648
}

src/brayns/core/Version.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@
2323

2424
namespace brayns
2525
{
26-
class Version
27-
{
28-
public:
29-
static std::string getTag();
30-
static std::string getCopyright();
31-
};
26+
std::string getVersionTag();
27+
std::string getCopyright();
3228
}

0 commit comments

Comments
 (0)