Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BRAYNS-634 Refactor JSON-RPC. #1257

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "GraphicsApi.h"
#include "Engine.h"

#include <fmt/format.h>

namespace brayns::experimental
{
GraphicsApi::Loader::~Loader()
Engine::Loader::~Loader()
{
ospShutdown();
}

GraphicsApi::GraphicsApi(std::unique_ptr<Loader> loader):
Engine::Engine(std::unique_ptr<Loader> loader):
_loader(std::move(loader))
{
}

Device GraphicsApi::createDevice(Logger &logger)
Device Engine::createDevice(Logger &logger)
{
auto currentDevice = ospGetCurrentDevice();
if (currentDevice != nullptr)
Expand Down Expand Up @@ -71,7 +71,7 @@ Device GraphicsApi::createDevice(Logger &logger)
return Device(device);
}

GraphicsApi loadGraphicsApi()
Engine loadEngine()
{
auto error = ospLoadModule("cpu");

Expand All @@ -81,8 +81,8 @@ GraphicsApi loadGraphicsApi()
throw std::runtime_error(message);
}

auto loader = std::make_unique<GraphicsApi::Loader>();
auto loader = std::make_unique<Engine::Loader>();

return GraphicsApi(std::move(loader));
return Engine(std::move(loader));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

namespace brayns::experimental
{
class GraphicsApi
class Engine
{
public:
class Loader
Expand All @@ -44,13 +44,13 @@ class GraphicsApi
Loader &operator=(Loader &&other) = delete;
};

explicit GraphicsApi(std::unique_ptr<Loader> loader);
explicit Engine(std::unique_ptr<Loader> loader);

Device createDevice(Logger &logger);

private:
std::unique_ptr<Loader> _loader;
};

GraphicsApi loadGraphicsApi();
Engine loadEngine();
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
94 changes: 94 additions & 0 deletions src/brayns/core/jsonrpc/Errors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* 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 "Errors.h"

namespace brayns::experimental
{
template<>
struct JsonReflector<JsonSchemaError>
{
static JsonValue serialize(const JsonSchemaError &value)
{
auto object = createJsonObject();

auto path = toString(value.path);
object->set("path", path);

auto error = toString(value.error);
object->set("error", error);

return object;
}
};

JsonRpcException::JsonRpcException(int code, const std::string &message, const JsonValue &data):
std::runtime_error(message),
_code(code),
_data(data)
{
}

int JsonRpcException::getCode() const
{
return _code;
}

const JsonValue &JsonRpcException::getData() const
{
return _data;
}

ParseError::ParseError(const std::string &message):
JsonRpcException(-32700, message)
{
}

InvalidRequest::InvalidRequest(const std::string &message):
JsonRpcException(-32600, message)
{
}

InvalidRequest::InvalidRequest(const std::string &message, const std::vector<JsonSchemaError> &errors):
JsonRpcException(-32600, message, serializeToJson(errors))
{
}

MethodNotFound::MethodNotFound(const std::string &method):
JsonRpcException(-32601, fmt::format("Method not found: '{}'", method))
{
}

InvalidParams::InvalidParams(const std::string &message):
JsonRpcException(-32602, message)
{
}

InvalidParams::InvalidParams(const std::string &message, const std::vector<JsonSchemaError> &errors):
JsonRpcException(-32602, message, serializeToJson(errors))
{
}

InternalError::InternalError(const std::string &message):
JsonRpcException(-32603, message)
{
}
} // namespace brayns
74 changes: 74 additions & 0 deletions src/brayns/core/jsonrpc/Errors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* 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 <stdexcept>

#include <brayns/core/jsonv2/Json.h>

namespace brayns::experimental
{
class JsonRpcException : public std::runtime_error
{
public:
explicit JsonRpcException(int code, const std::string &message, const JsonValue &data = {});

int getCode() const;
const JsonValue &getData() const;

private:
int _code = 0;
JsonValue _data;
};

class ParseError : public JsonRpcException
{
public:
explicit ParseError(const std::string &message);
};

class InvalidRequest : public JsonRpcException
{
public:
explicit InvalidRequest(const std::string &message);
explicit InvalidRequest(const std::string &message, const std::vector<JsonSchemaError> &errors);
};

class MethodNotFound : public JsonRpcException
{
public:
explicit MethodNotFound(const std::string &method);
};

class InvalidParams : public JsonRpcException
{
public:
explicit InvalidParams(const std::string &message);
explicit InvalidParams(const std::string &message, const std::vector<JsonSchemaError> &errors);
};

class InternalError : public JsonRpcException
{
public:
explicit InternalError(const std::string &message);
};
}
147 changes: 147 additions & 0 deletions src/brayns/core/jsonrpc/Messages.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/* 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/jsonv2/Json.h>

namespace brayns::experimental
{
using JsonRpcId = std::variant<NullJson, int, std::string>;

struct JsonRpcRequest
{
JsonRpcId id;
std::string method;
JsonValue params;
std::string binary = {};
};

template<>
struct JsonObjectReflector<JsonRpcRequest>
{
static auto reflect()
{
auto builder = JsonObjectInfoBuilder<JsonRpcRequest>();
builder.constant("jsonrpc", "2.0");
builder.field("id", [](auto &object) { return &object.id; }).required(false);
builder.field("method", [](auto &object) { return &object.method; });
builder.field("params", [](auto &object) { return &object.params; }).required(false);
return builder.build();
}
};

struct JsonRpcResponse
{
JsonRpcId id;
JsonValue result;
std::string binary = {};
};

template<>
struct JsonObjectReflector<JsonRpcResponse>
{
static auto reflect()
{
auto builder = JsonObjectInfoBuilder<JsonRpcResponse>();
builder.constant("jsonrpc", "2.0");
builder.field("id", [](auto &object) { return &object.id; });
builder.field("result", [](auto &object) { return &object.result; });
return builder.build();
}
};

struct JsonRpcError
{
int code;
std::string message;
JsonValue data;
};

template<>
struct JsonObjectReflector<JsonRpcError>
{
static auto reflect()
{
auto builder = JsonObjectInfoBuilder<JsonRpcError>();
builder.field("code", [](auto &object) { return &object.code; });
builder.field("message", [](auto &object) { return &object.message; });
builder.field("data", [](auto &object) { return &object.data; }).required(false);
return builder.build();
}
};

struct JsonRpcErrorResponse
{
JsonRpcId id;
JsonRpcError error;
};

template<>
struct JsonObjectReflector<JsonRpcErrorResponse>
{
static auto reflect()
{
auto builder = JsonObjectInfoBuilder<JsonRpcErrorResponse>();
builder.constant("jsonrpc", "2.0");
builder.field("id", [](auto &object) { return &object.id; });
builder.field("error", [](auto &object) { return &object.error; });
return builder.build();
}
};

struct JsonRpcProgress
{
JsonRpcId id;
std::string operation;
float amount;
};

template<>
struct JsonObjectReflector<JsonRpcProgress>
{
static auto reflect()
{
auto builder = JsonObjectInfoBuilder<JsonRpcProgress>();
builder.field("id", [](auto &object) { return &object.id; });
builder.field("operation", [](auto &object) { return &object.operation; });
builder.field("amount", [](auto &object) { return &object.amount; });
return builder.build();
}
};

struct JsonRpcNotification
{
JsonRpcProgress params;
};

template<>
struct JsonObjectReflector<JsonRpcNotification>
{
static auto reflect()
{
auto builder = JsonObjectInfoBuilder<JsonRpcNotification>();
builder.constant("jsonrpc", "2.0");
builder.field("params", [](auto &object) { return &object.params; });
return builder.build();
}
};
}
Loading