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

Use rockets python client #587

Merged
merged 2 commits into from
Oct 15, 2018
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
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
dist: trusty
dist: xenial
sudo: true
language: python
python:
- 2.7
- 3.5
- 3.6
- 3.7
install:
- cd python
- pip install .
Expand Down
32 changes: 29 additions & 3 deletions brayns/common/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,18 +353,44 @@ enum class DataType
class PropertyMap;
class PropertyObject;

enum class Execution
{
sync,
async
};

/** Description for RPC with no parameter. */
struct RpcDescription
{
std::string methodName;
std::string methodDescription;
Execution type{Execution::sync};
};

/** Description for RPC with one parameter. */
struct RpcParameterDescription
struct RpcParameterDescription : RpcDescription
{
std::string methodName;
std::string methodDescription;
RpcParameterDescription(const std::string& methodName_,
const std::string& methodDescription_,
const Execution type_,
const std::string& paramName_,
const std::string& paramDescription_)
: RpcDescription{methodName_, methodDescription_, type_}
, paramName(paramName_)
, paramDescription(paramDescription_)
{
}

RpcParameterDescription(const std::string& methodName_,
const std::string& methodDescription_,
const std::string& paramName_,
const std::string& paramDescription_)
: RpcDescription{methodName_, methodDescription_, Execution::sync}
, paramName(paramName_)
, paramDescription(paramDescription_)
{
}

std::string paramName;
std::string paramDescription;
};
Expand Down
17 changes: 11 additions & 6 deletions plugins/RocketsPlugin/RocketsPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,8 @@ class RocketsPlugin::Impl
void _handleSchemaRPC()
{
const RpcParameterDescription desc{
METHOD_SCHEMA, "Get the schema of the given endpoint", "endpoint",
METHOD_SCHEMA, "Get the schema of the given endpoint",
Execution::sync, "endpoint",
"name of the endpoint to get its schema"};

_jsonrpcServer->bind(METHOD_SCHEMA, [& schemas = _schemas](
Expand Down Expand Up @@ -1009,7 +1010,8 @@ class RocketsPlugin::Impl
{
using Position = std::array<double, 2>;
const RpcParameterDescription desc{
METHOD_INSPECT, "Inspect the scene at x-y position", "position",
METHOD_INSPECT, "Inspect the scene at x-y position",
Execution::sync, "position",
"x-y position in normalized coordinates"};
_handleRPC<Position, Renderer::PickResult>(
desc, [engine = _engine](const auto& position) {
Expand Down Expand Up @@ -1040,7 +1042,8 @@ class RocketsPlugin::Impl
void _handleSnapshot()
{
const RpcParameterDescription desc{
METHOD_SNAPSHOT, "Make a snapshot of the current view", "settings",
METHOD_SNAPSHOT, "Make a snapshot of the current view",
Execution::async, "settings",
"Snapshot settings for quality and size"};
auto func =
[ engine = _engine,
Expand All @@ -1056,7 +1059,8 @@ class RocketsPlugin::Impl
void _handleStreamTo()
{
const RpcParameterDescription desc{METHOD_STREAM_TO,
"Stream to a displaywall", "param",
"Stream to a displaywall",
Execution::sync, "param",
"Stream parameters"};

_bindEndpoint(METHOD_STREAM_TO, [&](const auto& request) {
Expand All @@ -1081,7 +1085,8 @@ class RocketsPlugin::Impl
METHOD_REQUEST_MODEL_UPLOAD,
"Request upload of blob to trigger adding of model after blob has "
"been received; returns model descriptor on success",
"param", "size, type, name, transformation, etc."};
Execution::async, "param",
"size, type, name, transformation, etc."};

_handleTask<BinaryParam, ModelDescriptorPtr>(
desc,
Expand Down Expand Up @@ -1167,7 +1172,7 @@ class RocketsPlugin::Impl
const RpcParameterDescription desc{
METHOD_ADD_MODEL,
"Add model from remote path; returns model descriptor on success",
"model_param",
Execution::async, "model_param",
"Model parameters including name, path, transformation, etc."};

auto func = [engine = _engine](const auto& modelParam, const auto)
Expand Down
35 changes: 5 additions & 30 deletions plugins/RocketsPlugin/jsonPropertyMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,8 @@ std::string buildJsonRpcSchemaRequestPropertyMaps(
const std::vector<std::pair<std::string, PropertyMap>>& objs)
{
using namespace rapidjson;
Document schema(kObjectType);
auto schema = _buildJsonRpcSchema(desc);
auto& allocator = schema.GetAllocator();
schema.AddMember(StringRef("title"), StringRef(desc.methodName.c_str()),
allocator);
schema.AddMember(StringRef("description"),
StringRef(desc.methodDescription.c_str()), allocator);
schema.AddMember(StringRef("type"), StringRef("method"), allocator);

Value returns(kObjectType);
Value oneOf(kArrayType);
Expand All @@ -295,13 +290,8 @@ std::string buildJsonRpcSchemaRequestPropertyMap(const RpcDescription& desc,
const PropertyMap& obj)
{
using namespace rapidjson;
Document schema(kObjectType);
auto schema = _buildJsonRpcSchema(desc);
auto& allocator = schema.GetAllocator();
schema.AddMember(StringRef("title"), StringRef(desc.methodName.c_str()),
allocator);
schema.AddMember(StringRef("description"),
StringRef(desc.methodDescription.c_str()), allocator);
schema.AddMember(StringRef("type"), StringRef("method"), allocator);

Value returns(kObjectType);
_addPropertyMapSchema(obj, "", allocator, returns);
Expand All @@ -323,13 +313,8 @@ std::string buildJsonRpcSchemaRequestPropertyMap(
const PropertyMap& output)
{
using namespace rapidjson;
Document schema(kObjectType);
auto schema = _buildJsonRpcSchema(desc);
auto& allocator = schema.GetAllocator();
schema.AddMember(StringRef("title"), StringRef(desc.methodName.c_str()),
allocator);
schema.AddMember(StringRef("description"),
StringRef(desc.methodDescription.c_str()), allocator);
schema.AddMember(StringRef("type"), StringRef("method"), allocator);

Value returns(kObjectType);
_addPropertyMapSchema(output, "", allocator, returns);
Expand All @@ -354,13 +339,8 @@ std::string buildJsonRpcSchemaNotifyPropertyMaps(
const std::vector<std::pair<std::string, PropertyMap>>& objs)
{
using namespace rapidjson;
Document schema(kObjectType);
auto schema = _buildJsonRpcSchema(desc);
auto& allocator = schema.GetAllocator();
schema.AddMember(StringRef("title"), StringRef(desc.methodName.c_str()),
allocator);
schema.AddMember(StringRef("description"),
StringRef(desc.methodDescription.c_str()), allocator);
schema.AddMember(StringRef("type"), StringRef("method"), allocator);

bool retVal;
auto retSchema = staticjson::export_json_schema(&retVal);
Expand All @@ -386,13 +366,8 @@ std::string buildJsonRpcSchemaNotifyPropertyMap(
const RpcParameterDescription& desc, const PropertyMap& properties)
{
using namespace rapidjson;
Document schema(kObjectType);
auto schema = _buildJsonRpcSchema(desc);
auto& allocator = schema.GetAllocator();
schema.AddMember(StringRef("title"), StringRef(desc.methodName.c_str()),
allocator);
schema.AddMember(StringRef("description"),
StringRef(desc.methodDescription.c_str()), allocator);
schema.AddMember(StringRef("type"), StringRef("method"), allocator);

bool retVal;
auto retSchema = staticjson::export_json_schema(&retVal);
Expand Down
87 changes: 38 additions & 49 deletions plugins/RocketsPlugin/jsonUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,32 @@ rapidjson::Document getRPCParameterSchema(const std::string& paramName,
const std::string& paramDescription,
T& obj)
{
rapidjson::Document schema = staticjson::export_json_schema(&obj);

using namespace rapidjson;
schema.AddMember(StringRef("name"),
Value(paramName.c_str(), schema.GetAllocator()),
schema.GetAllocator());
auto schema = staticjson::export_json_schema(&obj);
auto& allocator = schema.GetAllocator();

schema.AddMember(StringRef("name"), Value(paramName.c_str(), allocator),
allocator);
schema.AddMember(StringRef("description"),
Value(paramDescription.c_str(), schema.GetAllocator()),
schema.GetAllocator());
Value(paramDescription.c_str(), allocator), allocator);
return schema;
};

rapidjson::Document _buildJsonRpcSchema(const RpcDescription& desc)
{
using namespace rapidjson;
Document schema(kObjectType);
auto& allocator = schema.GetAllocator();
schema.AddMember(StringRef("title"), StringRef(desc.methodName.c_str()),
allocator);
schema.AddMember(StringRef("description"),
StringRef(desc.methodDescription.c_str()), allocator);
schema.AddMember(StringRef("type"), StringRef("method"), allocator);
schema.AddMember(StringRef("async"), desc.type == Execution::async,
allocator);
return schema;
}

/**
* @return JSON schema for RPC with one parameter and a return value, according
* to
Expand All @@ -85,24 +99,18 @@ std::string buildJsonRpcSchemaRequest(const RpcParameterDescription& desc,
P& obj)
{
using namespace rapidjson;
Document schema(kObjectType);
schema.AddMember(StringRef("title"), StringRef(desc.methodName.c_str()),
schema.GetAllocator());
schema.AddMember(StringRef("description"),
StringRef(desc.methodDescription.c_str()),
schema.GetAllocator());
schema.AddMember(StringRef("type"), StringRef("method"),
schema.GetAllocator());
auto schema = _buildJsonRpcSchema(desc);
auto& allocator = schema.GetAllocator();

R retVal;
auto retSchema = staticjson::export_json_schema(&retVal);
schema.AddMember(StringRef("returns"), retSchema, schema.GetAllocator());
schema.AddMember(StringRef("returns"), retSchema, allocator);

Value params(kArrayType);
auto paramSchema =
getRPCParameterSchema<P>(desc.paramName, desc.paramDescription, obj);
params.PushBack(paramSchema, schema.GetAllocator());
schema.AddMember(StringRef("params"), params, schema.GetAllocator());
params.PushBack(paramSchema, allocator);
schema.AddMember(StringRef("params"), params, allocator);

StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
Expand All @@ -127,20 +135,14 @@ std::string buildJsonRpcSchemaRequestReturnOnly(const RpcDescription& desc,
R& retVal)
{
using namespace rapidjson;
Document schema(kObjectType);
schema.AddMember(StringRef("title"), StringRef(desc.methodName.c_str()),
schema.GetAllocator());
schema.AddMember(StringRef("description"),
StringRef(desc.methodDescription.c_str()),
schema.GetAllocator());
schema.AddMember(StringRef("type"), StringRef("method"),
schema.GetAllocator());
auto schema = _buildJsonRpcSchema(desc);
auto& allocator = schema.GetAllocator();

auto retSchema = staticjson::export_json_schema(&retVal);
schema.AddMember(StringRef("returns"), retSchema, schema.GetAllocator());
schema.AddMember(StringRef("returns"), retSchema, allocator);

Value params(kArrayType);
schema.AddMember(StringRef("params"), params, schema.GetAllocator());
schema.AddMember(StringRef("params"), params, allocator);

StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
Expand All @@ -165,20 +167,14 @@ std::string buildJsonRpcSchemaNotify(const RpcParameterDescription& desc,
P& obj)
{
using namespace rapidjson;
Document schema(kObjectType);
schema.AddMember(StringRef("title"), StringRef(desc.methodName.c_str()),
schema.GetAllocator());
schema.AddMember(StringRef("description"),
StringRef(desc.methodDescription.c_str()),
schema.GetAllocator());
schema.AddMember(StringRef("type"), StringRef("method"),
schema.GetAllocator());
auto schema = _buildJsonRpcSchema(desc);
auto& allocator = schema.GetAllocator();

Value params(kArrayType);
auto paramSchema =
getRPCParameterSchema<P>(desc.paramName, desc.paramDescription, obj);
params.PushBack(paramSchema, schema.GetAllocator());
schema.AddMember(StringRef("params"), params, schema.GetAllocator());
params.PushBack(paramSchema, allocator);
schema.AddMember(StringRef("params"), params, allocator);

StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
Expand All @@ -197,20 +193,13 @@ std::string buildJsonRpcSchemaNotify(const RpcParameterDescription& desc)
std::string buildJsonRpcSchemaNotify(const RpcDescription& desc)
{
using namespace rapidjson;
Document schema(kObjectType);
schema.AddMember(StringRef("title"), StringRef(desc.methodName.c_str()),
schema.GetAllocator());
schema.AddMember(StringRef("description"),
StringRef(desc.methodDescription.c_str()),
schema.GetAllocator());
schema.AddMember(StringRef("type"), StringRef("method"),
schema.GetAllocator());
auto schema = _buildJsonRpcSchema(desc);
auto& allocator = schema.GetAllocator();

schema.AddMember(StringRef("returns"), Value(kNullType),
schema.GetAllocator());
schema.AddMember(StringRef("returns"), Value(kNullType), allocator);

Value params(kArrayType);
schema.AddMember(StringRef("params"), params, schema.GetAllocator());
schema.AddMember(StringRef("params"), params, allocator);

StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
Expand Down
2 changes: 1 addition & 1 deletion python/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ run_pydocstyle: $(VENV_INSTALLED)
$(VENV_BIN)/pydocstyle $(LINT_PYFILES) > pydocstyle.txt

run_pylint: $(VENV_INSTALLED)
$(VENV_BIN)/pylint --rcfile=pylintrc --extension-pkg-whitelist=numpy $(LINT_PYFILES) > pylint.txt
$(VENV_BIN)/pylint --rcfile=pylintrc $(LINT_PYFILES) > pylint.txt

run_tests: $(VENV_INSTALLED)
$(VENV_BIN)/nosetests -v --with-coverage --cover-min-percentage=$(MIN_COV) --cover-erase --cover-package brayns
Expand Down
Loading