Skip to content

Commit

Permalink
Working.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrien4193 committed Jun 26, 2024
1 parent 93cf5e6 commit c81243e
Show file tree
Hide file tree
Showing 15 changed files with 352 additions and 97 deletions.
100 changes: 20 additions & 80 deletions apps/service.cpp
Original file line number Diff line number Diff line change
@@ -1,99 +1,38 @@
/* Copyright (c) 2015-2024, EPFL/Blue Brain Project
/* Copyright (c) 2015-2024 EPFL/Blue Brain Project
* All rights reserved. Do not distribute without permission.
* Responsible Author: Cyrille Favreau <[email protected]>
*
* Responsible Author: [email protected]
*
* This file is part of Brayns <https://github.com/BlueBrain/Brayns>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 3.0 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include <iostream>

#include <brayns/core/Launcher.h>
#include <brayns/core/Version.h>
#include <brayns/core/cli/CommandLine.h>
#include <brayns/core/utils/Logger.h>

using namespace brayns::experimental;
using brayns::createConsoleLogger;
using brayns::getCopyright;

namespace brayns::experimental
{
struct Settings
{
bool help = false;
bool version = false;
std::string host;
std::uint16_t port = 0;
std::size_t maxClient = 0;
std::size_t maxFrameSize = 0;
bool sslEnabled = false;
// SslSettings ssl = {};
};

template<>
struct ArgvSettingsReflector<Settings>
{
static auto reflect()
{
auto builder = ArgvBuilder<Settings>();
builder.description(getCopyright());
builder.option("help", [](auto &settings) { return &settings.help; })
.description("Display this help message")
.defaultValue(false);
builder.option("version", [](auto &settings) { return &settings.version; })
.description("Display brayns copyright with version")
.defaultValue(false);
builder.option("host", [](auto &settings) { return &settings.host; })
.description("Websocket server hostname, use 0.0.0.0 to allow any host to connect")
.defaultValue("localhost");
builder.option("port", [](auto &settings) { return &settings.port; })
.description("Websocket server port")
.defaultValue(5000);
builder.option("max-client", [](auto &settings) { return &settings.maxClient; })
.description("Maximum number of simultaneously connected clients")
.defaultValue(2);
builder.option("max-frame-size", [](auto &settings) { return &settings.maxFrameSize; })
.description("Maximum frame size the websocket server accepts")
.defaultValue(std::numeric_limits<int>::max());
builder.option("ssl", [](auto &settings) { return &settings.sslEnabled; })
.description("Enable SSL for websocket server, requires a certificate and a private key")
.defaultValue(false);
/*builder.option("private-key-file", [](auto &settings) { return &settings.ssl.privateKeyFile; })
.description("SSL private key used by the websocket server")
.defaultValue("");
builder.option("certificate-file", [](auto &settings) { return &settings.ssl.certificateFile; })
.description("SSL certificate the websocket server will provide to clients")
.defaultValue("");
builder.option("ca-location", [](auto &settings) { return &settings.ssl.caLocation; })
.description("Path to a certificate file to use as certification authority or a CA directory")
.defaultValue("");
builder.option("private-key-passphrase", [](auto &settings) { return &settings.ssl.privateKeyPassphrase; })
.description("Passphrase for the private key if encrypted")
.defaultValue("");*/
return builder.build();
}
};
}

int main(int argc, const char **argv)
{
auto logger = createConsoleLogger("Brayns");

try
{
auto settings = parseArgvAs<Settings>(argc, argv);
auto settings = parseArgvAs<ServiceSettings>(argc, argv);

if (settings.version)
{
Expand All @@ -103,18 +42,19 @@ int main(int argc, const char **argv)

if (settings.help)
{
std::cout << getArgvHelp<Settings>() << '\n';
std::cout << getArgvHelp<ServiceSettings>() << '\n';
return 0;
}

runService(settings);
}
catch (const std::exception &e)
{
logger.fatal("Fatal error: '{}'.", e.what());
return 1;
std::cout << "Fatal error: " << e.what() << ".\n";
}
catch (...)
{
logger.fatal("Unknown fatal error.");
std::cout << "Unknown fatal error.";
return 1;
}

Expand Down
72 changes: 72 additions & 0 deletions src/brayns/core/Launcher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* Copyright (c) 2015-2024 EPFL/Blue Brain Project
* All rights reserved. Do not distribute without permission.
*
* Responsible Author: [email protected]
*
* This file is part of Brayns <https://github.com/BlueBrain/Brayns>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 3.0 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "Launcher.h"

#include <brayns/core/service/Service.h>
#include <brayns/core/utils/Logger.h>

namespace brayns::experimental
{
void runService(const ServiceSettings &settings)
{
auto level = brayns::EnumInfo::getValue<LogLevel>(settings.logLevel);

auto logger = createConsoleLogger("brayns");
logger.setLevel(level);

auto ssl = std::optional<SslSettings>();

if (settings.ssl)
{
ssl = SslSettings{
.privateKeyFile = settings.privateKeyFile,
.certificateFile = settings.certificateFile,
.caLocation = settings.caLocation,
.privateKeyPassphrase = settings.privateKeyPassphrase,
};
}

auto server = WebSocketServerSettings{
.host = settings.host,
.port = settings.port,
.maxThreadCount = settings.maxThreadCount,
.maxQueueSize = settings.maxQueueSize,
.maxFrameSize = settings.maxFrameSize,
.ssl = std::move(ssl),
};

auto endpoints = EndpointRegistry({});

auto tasks = TaskManager();

auto context = std::make_unique<ServiceContext>(ServiceContext{
.logger = std::move(logger),
.server = std::move(server),
.endpoints = std::move(endpoints),
.tasks = std::move(tasks),
});

auto service = Service(std::move(context));

service.run();
}
}
105 changes: 105 additions & 0 deletions src/brayns/core/Launcher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/* Copyright (c) 2015-2024 EPFL/Blue Brain Project
* All rights reserved. Do not distribute without permission.
*
* Responsible Author: [email protected]
*
* This file is part of Brayns <https://github.com/BlueBrain/Brayns>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 3.0 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#pragma once

#include <brayns/core/Version.h>
#include <brayns/core/cli/CommandLine.h>

namespace brayns::experimental
{
struct ServiceSettings
{
bool help;
bool version;
std::string logLevel;
std::string host;
std::uint16_t port;
std::size_t maxThreadCount;
std::size_t maxQueueSize;
std::size_t maxFrameSize;
bool ssl;
std::string privateKeyFile;
std::string certificateFile;
std::string caLocation;
std::string privateKeyPassphrase;
};

template<>
struct ArgvSettingsReflector<ServiceSettings>
{
static auto reflect()
{
auto builder = ArgvBuilder<ServiceSettings>();

builder.description(getCopyright());

builder.option("help", [](auto &settings) { return &settings.help; })
.description("Display this help message and exit")
.defaultValue(false);

builder.option("version", [](auto &settings) { return &settings.version; })
.description("Display brayns copyright with version and exit")
.defaultValue(false);

builder.option("log-level", [](auto &settings) { return &settings.logLevel; })
.description("Log level among [trace, debug, info, warn, error, fatal]")
.defaultValue("info");

builder.option("host", [](auto &settings) { return &settings.host; })
.description("Websocket server hostname, use 0.0.0.0 to allow any host to connect")
.defaultValue("localhost");
builder.option("port", [](auto &settings) { return &settings.port; })
.description("Websocket server port")
.defaultValue(5000);
builder.option("max-thread-count", [](auto &settings) { return &settings.maxThreadCount; })
.description("Maximum number of threads for the websocket server")
.defaultValue(2);
builder.option("max-queue-size", [](auto &settings) { return &settings.maxQueueSize; })
.description("Maximum number of queued connections before they are rejected")
.defaultValue(64);
builder.option("max-frame-size", [](auto &settings) { return &settings.maxFrameSize; })
.description("Maximum frame size the websocket server accepts")
.defaultValue(std::numeric_limits<int>::max());

builder.option("ssl", [](auto &settings) { return &settings.ssl; })
.description("Enable SSL for websocket server, requires a certificate and a private key")
.defaultValue(false);

builder.option("private-key-file", [](auto &settings) { return &settings.privateKeyFile; })
.description("SSL private key used by the websocket server")
.defaultValue("");
builder.option("certificate-file", [](auto &settings) { return &settings.certificateFile; })
.description("SSL certificate the websocket server will provide to clients")
.defaultValue("");
builder.option("ca-location", [](auto &settings) { return &settings.caLocation; })
.description("Path to an additional certification authority (file or directory)")
.defaultValue("");
builder.option("private-key-passphrase", [](auto &settings) { return &settings.privateKeyPassphrase; })
.description("Passphrase for the private key if encrypted")
.defaultValue("");

return builder.build();
}
};

void runService(const ServiceSettings &settings);
}
8 changes: 8 additions & 0 deletions src/brayns/core/api/Task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ const RawTask &getTask(const std::map<TaskId, RawTask> &tasks, TaskId id)

namespace brayns::experimental
{
TaskManager::~TaskManager()
{
for (const auto &[id, task] : _tasks)
{
task.cancel();
}
}

std::vector<TaskInfo> TaskManager::getTasks() const
{
auto infos = std::vector<TaskInfo>();
Expand Down
8 changes: 8 additions & 0 deletions src/brayns/core/api/Task.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ struct TaskInfo
class TaskManager
{
public:
explicit TaskManager() = default;
~TaskManager();

TaskManager(const TaskManager &) = delete;
TaskManager(TaskManager &&) = default;
TaskManager &operator=(const TaskManager &) = delete;
TaskManager &operator=(TaskManager &&) = default;

std::vector<TaskInfo> getTasks() const;
TaskId add(RawTask task);
ProgressInfo getProgress(TaskId id) const;
Expand Down
6 changes: 2 additions & 4 deletions src/brayns/core/jsonrpc/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ JsonRpcRequest parseJsonRpcRequest(const std::string &text)
return deserializeAs<JsonRpcRequest>(json);
}

JsonRpcRequest parseBinaryJsonRpcRequest(std::string binary)
JsonRpcRequest parseBinaryJsonRpcRequest(const std::string &binary)
{
auto data = std::string_view(binary);

Expand All @@ -75,9 +75,7 @@ JsonRpcRequest parseBinaryJsonRpcRequest(std::string binary)

auto request = parseJsonRpcRequest(std::string(text));

binary.erase(0, 4 + textSize);

request.binary = std::move(binary);
request.binary = binary.substr(4 + textSize);

return request;
}
Expand Down
2 changes: 1 addition & 1 deletion src/brayns/core/jsonrpc/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
namespace brayns::experimental
{
JsonRpcRequest parseJsonRpcRequest(const std::string &text);
JsonRpcRequest parseBinaryJsonRpcRequest(std::string binary);
JsonRpcRequest parseBinaryJsonRpcRequest(const std::string &binary);
std::string composeAsText(const JsonRpcResponse &response);
std::string composeAsBinary(const JsonRpcResponse &response);
std::string composeError(const JsonRpcErrorResponse &response);
Expand Down
Loading

0 comments on commit c81243e

Please sign in to comment.