Skip to content

Commit

Permalink
Add non-standard class method with the necessary parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
lo-simon committed Jan 15, 2024
1 parent 3911889 commit 2fbd8e1
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 47 deletions.
26 changes: 13 additions & 13 deletions Development/nmos-cpp-node/node_implementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1008,15 +1008,15 @@ void node_implementation_init(nmos::node_model& model, nmos::experimental::contr
nmos::experimental::make_control_class_property(U("Example object sequence property"), { 3, 14 }, object_sequence, U("ExampleDataType"), false, false, true)
};

auto example_method_with_no_args = [](nmos::resources& resources, const nmos::resource& resource, int32_t handle, const web::json::value& arguments, bool is_deprecated, nmos::get_control_protocol_class_handler get_control_protocol_class, nmos::get_control_protocol_datatype_handler get_control_protocol_datatype, nmos::control_protocol_property_changed_handler property_changed, slog::base_gate& gate)
auto example_method_with_no_args = [](nmos::resources& resources, const nmos::resource& resource, int32_t handle, const web::json::value& arguments, bool is_deprecated, slog::base_gate& gate)
{
// note, model mutex is already locked by the outer function, so access to control_protocol_resources is OK...

slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "Executing the example method with no arguments";

return nmos::make_control_protocol_message_response(handle, { is_deprecated ? nmos::nc_method_status::method_deprecated : nmos::nc_method_status::ok });
};
auto example_method_with_simple_args = [](nmos::resources& resources, const nmos::resource& resource, int32_t handle, const web::json::value& arguments, bool is_deprecated, nmos::get_control_protocol_class_handler get_control_protocol_class, nmos::get_control_protocol_datatype_handler get_control_protocol_datatype, nmos::control_protocol_property_changed_handler property_changed, slog::base_gate& gate)
auto example_method_with_simple_args = [](nmos::resources& resources, const nmos::resource& resource, int32_t handle, const web::json::value& arguments, bool is_deprecated, slog::base_gate& gate)
{
// note, model mutex is already locked by the outer function, so access to control_protocol_resources is OK...
// and the method parameters constriants has already been validated by the outer function
Expand All @@ -1025,7 +1025,7 @@ void node_implementation_init(nmos::node_model& model, nmos::experimental::contr

return nmos::make_control_protocol_message_response(handle, { is_deprecated ? nmos::nc_method_status::method_deprecated : nmos::nc_method_status::ok });
};
auto example_method_with_object_args = [](nmos::resources& resources, const nmos::resource& resource, int32_t handle, const web::json::value& arguments, bool is_deprecated, nmos::get_control_protocol_class_handler get_control_protocol_class, nmos::get_control_protocol_datatype_handler get_control_protocol_datatype, nmos::control_protocol_property_changed_handler property_changed, slog::base_gate& gate)
auto example_method_with_object_args = [](nmos::resources& resources, const nmos::resource& resource, int32_t handle, const web::json::value& arguments, bool is_deprecated, slog::base_gate& gate)
{
// note, model mutex is already locked by the outer function, so access to control_protocol_resources is OK...
// and the method parameters constriants has already been validated by the outer function
Expand All @@ -1037,22 +1037,22 @@ void node_implementation_init(nmos::node_model& model, nmos::experimental::contr
// Example control class methods
std::vector<nmos::experimental::method> example_control_methods =
{
{ nmos::experimental::make_control_class_method(U("Example method with no arguments"), { 3, 1 }, U("MethodNoArgs"), U("NcMethodResult"), {}, false), example_method_with_no_args },
{ nmos::experimental::make_control_class_method(U("Example deprecated method with no arguments"), { 3, 2 }, U("MethodNoArgs"), U("NcMethodResult"), {}, true), example_method_with_no_args },
{ nmos::experimental::make_control_class_method(U("Example method with no arguments"), { 3, 1 }, U("MethodNoArgs"), U("NcMethodResult"), {}, false, example_method_with_no_args) },
{ nmos::experimental::make_control_class_method(U("Example deprecated method with no arguments"), { 3, 2 }, U("MethodNoArgs"), U("NcMethodResult"), {}, true, example_method_with_no_args) },
{ nmos::experimental::make_control_class_method(U("Example method with simple arguments"), { 3, 3 }, U("MethodSimpleArgs"), U("NcMethodResult"),
{
nmos::details::make_nc_parameter_descriptor(U("Enum example argument"), enum_arg, U("ExampleEnum"), false, false, value::null()),
nmos::details::make_nc_parameter_descriptor(U("String example argument"), string_arg, U("NcString"), false, false, make_string_example_argument_constraints()), // e.g. include method property constraints
nmos::details::make_nc_parameter_descriptor(U("Number example argument"), number_arg, U("NcUint64"), false, false, make_number_example_argument_constraints()), // e.g. include method property constraints
nmos::details::make_nc_parameter_descriptor(U("Boolean example argument"), boolean_arg, U("NcBoolean"), false, false, value::null())
{
nmos::experimental::make_control_class_method_parameter(U("Enum example argument"), enum_arg, U("ExampleEnum")),
nmos::experimental::make_control_class_method_parameter(U("String example argument"), string_arg, U("NcString"), false, false, make_string_example_argument_constraints()), // e.g. include method property constraints
nmos::experimental::make_control_class_method_parameter(U("Number example argument"), number_arg, U("NcUint64"), false, false, make_number_example_argument_constraints()), // e.g. include method property constraints
nmos::experimental::make_control_class_method_parameter(U("Boolean example argument"), boolean_arg, U("NcBoolean"))
},
false), example_method_with_simple_args
false, example_method_with_simple_args)
},
{ nmos::experimental::make_control_class_method(U("Example method with object argument"), { 3, 4 }, U("MethodObjectArg"), U("NcMethodResult"),
{
nmos::details::make_nc_parameter_descriptor(U("Object example argument"), obj_arg, U("ExampleDataType"), false, false, value::null())
nmos::experimental::make_control_class_method_parameter(U("Object example argument"), obj_arg, U("ExampleDataType"))
},
false), example_method_with_object_args
false, example_method_with_object_args)
}
};

Expand Down
2 changes: 1 addition & 1 deletion Development/nmos/control_protocol_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace nmos
auto& methods = control_class.methods;
auto method_found = std::find_if(methods.begin(), methods.end(), [&method_id](const experimental::method& method)
{
return method_id == details::parse_nc_method_id(nmos::fields::nc::id(method.first));
return method_id == details::parse_nc_method_id(nmos::fields::nc::id(std::get<0>(method)));
});
if (methods.end() != method_found)
{
Expand Down
20 changes: 16 additions & 4 deletions Development/nmos/control_protocol_handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define NMOS_CONTROL_PROTOCOL_HANDLERS_H

#include <functional>
#include <map>
#include "nmos/control_protocol_typedefs.h"
#include "nmos/resources.h"

Expand Down Expand Up @@ -39,11 +38,24 @@ namespace nmos

namespace experimental
{
// method handler definition
typedef std::function<web::json::value(nmos::resources& resources, const nmos::resource& resource, int32_t handle, const web::json::value& arguments, bool is_deprecated, get_control_protocol_class_handler get_control_protocol_class, get_control_protocol_datatype_handler get_control_protocol_datatype, control_protocol_property_changed_handler property_changed, slog::base_gate& gate)> method_handler;
// standard method handler definition
typedef std::function<web::json::value(nmos::resources& resources, const nmos::resource& resource, int32_t handle, const web::json::value& arguments, bool is_deprecated, get_control_protocol_class_handler get_control_protocol_class, get_control_protocol_datatype_handler get_control_protocol_datatype, control_protocol_property_changed_handler property_changed, slog::base_gate& gate)> standard_method_handler;

// non-standard method handler definition
typedef std::function<web::json::value(nmos::resources& resources, const nmos::resource& resource, int32_t handle, const web::json::value& arguments, bool is_deprecated, slog::base_gate& gate)> non_standard_method_handler;

// method definition (NcMethodDescriptor vs method handler)
typedef std::pair<web::json::value, nmos::experimental::method_handler> method;
typedef std::tuple<web::json::value, standard_method_handler, non_standard_method_handler> method;

inline method make_control_class_standard_method(const web::json::value& nc_method_descriptor, standard_method_handler method_handler)
{
return std::make_tuple(nc_method_descriptor, method_handler, nullptr);
}

inline method make_control_class_non_standard_method(const web::json::value& nc_method_descriptor, non_standard_method_handler method_handler)
{
return std::make_tuple(nc_method_descriptor, nullptr, method_handler);
}
}

// callback to retrieve a specific method
Expand Down
4 changes: 2 additions & 2 deletions Development/nmos/control_protocol_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ namespace nmos
auto& fixed_role = control_class.fixed_role;
auto properties = control_class.properties;
auto methods = value::array();
for (const auto& method : control_class.methods) { web::json::push_back(methods, method.first); }
for (const auto& method : control_class.methods) { web::json::push_back(methods, std::get<0>(method)); }
auto events = control_class.events;

if (include_inherited)
Expand All @@ -551,7 +551,7 @@ namespace nmos
const auto& inherited_control_class = get_control_protocol_class(inherited_class_id);
{
for (const auto& property : inherited_control_class.properties.as_array()) { web::json::push_back(properties, property); }
for (const auto& method : inherited_control_class.methods) { web::json::push_back(methods, method.first); }
for (const auto& method : inherited_control_class.methods) { web::json::push_back(methods, std::get<0>(method)); }
for (const auto& event : inherited_control_class.events.as_array()) { web::json::push_back(events, event); }
}
inherited_class_id.pop_back();
Expand Down
2 changes: 1 addition & 1 deletion Development/nmos/control_protocol_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ namespace nmos
auto& ctl_class = control_class.second;

auto methods = value::array();
for (const auto& method : ctl_class.methods) { web::json::push_back(methods, method.first); }
for (const auto& method : ctl_class.methods) { web::json::push_back(methods, std::get<0>(method)); }

const auto class_description = ctl_class.fixed_role.is_null()
? make_nc_class_descriptor(ctl_class.description, ctl_class.class_id, ctl_class.name, ctl_class.properties, methods, ctl_class.events)
Expand Down
31 changes: 21 additions & 10 deletions Development/nmos/control_protocol_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,26 @@ namespace nmos
return nmos::details::make_nc_parameter_descriptor(description, name, type_name, is_nullable, is_sequence, constraints);
}

// create control class method
web::json::value make_control_class_method(const utility::string_t& description, const nc_method_id& id, const nc_name& name, const utility::string_t& result_datatype, const std::vector<web::json::value>& parameters_, bool is_deprecated)
namespace details
{
using web::json::value;
web::json::value make_control_class_method(const utility::string_t& description, const nc_method_id& id, const nc_name& name, const utility::string_t& result_datatype, const std::vector<web::json::value>& parameters_, bool is_deprecated)
{
using web::json::value;

value parameters = value::array();
for (const auto& parameter : parameters_) { web::json::push_back(parameters, parameter); }
value parameters = value::array();
for (const auto& parameter : parameters_) { web::json::push_back(parameters, parameter); }

return nmos::details::make_nc_method_descriptor(description, id, name, result_datatype, parameters, is_deprecated);
return nmos::details::make_nc_method_descriptor(description, id, name, result_datatype, parameters, is_deprecated);
}
}
// create control class method
method make_control_class_method(const utility::string_t& description, const nc_method_id& id, const nc_name& name, const utility::string_t& result_datatype, const std::vector<web::json::value>& parameters, bool is_deprecated, standard_method_handler method_handler)
{
return make_control_class_standard_method(details::make_control_class_method(description, id, name, result_datatype, parameters, is_deprecated), method_handler);
}
method make_control_class_method(const utility::string_t& description, const nc_method_id& id, const nc_name& name, const utility::string_t& result_datatype, const std::vector<web::json::value>& parameters, bool is_deprecated, non_standard_method_handler method_handler)
{
return make_control_class_non_standard_method(details::make_control_class_method(description, id, name, result_datatype, parameters, is_deprecated), method_handler);
}

// create control class event
Expand All @@ -91,16 +102,16 @@ namespace nmos
return std::vector<web::json::value>{};
};

auto to_methods_vector = [](const web::json::value& nc_method_descriptors, const std::map<nmos::nc_method_id, method_handler>& method_handlers)
auto to_methods_vector = [](const web::json::value& nc_method_descriptors, const std::map<nmos::nc_method_id, const standard_method_handler>& method_handlers)
{
// NcMethodDescriptor vs method_handler
std::vector<nmos::experimental::method> methods;
// NcMethodDescriptor vs method
std::vector<method> methods;

if (!nc_method_descriptors.is_null())
{
for (const auto& nc_method_descriptor : nc_method_descriptors.as_array())
{
methods.push_back({ nc_method_descriptor, method_handlers.at(nmos::details::parse_nc_method_id(nmos::fields::nc::id(nc_method_descriptor))) });
methods.push_back(make_control_class_standard_method(nc_method_descriptor, method_handlers.at(nmos::details::parse_nc_method_id(nmos::fields::nc::id(nc_method_descriptor)))));
}
}
return methods;
Expand Down
27 changes: 14 additions & 13 deletions Development/nmos/control_protocol_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace nmos
web::json::value fixed_role;

web::json::value properties = web::json::value::array(); // array of NcPropertyDescriptor
std::vector<method> methods; // vector of NcMethodDescriptor and method_handler
std::vector<method> methods; // vector of NcMethodDescriptor, standard_method_handler and non_standard_method_handler
web::json::value events = web::json::value::array(); // array of NcEventDescriptor

control_class()
Expand Down Expand Up @@ -68,30 +68,31 @@ namespace nmos
// insert datatype, false if datatype already presented
bool insert(const experimental::datatype& datatype);
// erase datatype of the given datatype name, false if the required datatype not found
bool erase(const utility::string_t& name);
bool erase(const utility::string_t& datatype_name);
};

// helper functions to create non-standard control class
//

// create control class with fixed role
control_class make_control_class(const utility::string_t& description, const nc_class_id& class_id, const nc_name& name, const utility::string_t& fixed_role, const std::vector<web::json::value>& properties = {}, const std::vector<method>& methods = {}, const std::vector<web::json::value>& events = {});
// create control class with no fixed role
control_class make_control_class(const utility::string_t& description, const nc_class_id& class_id, const nc_name& name, const std::vector<web::json::value>& properties = {}, const std::vector<method>& methods = {}, const std::vector<web::json::value>& events = {});

// create control class property
web::json::value make_control_class_property(const utility::string_t& description, const nc_property_id& id, const nc_name& name, const utility::string_t& type_name,
bool is_read_only = false, bool is_nullable = false, bool is_sequence = false, bool is_deprecated = false, const web::json::value& constraints = web::json::value::null());

// create control class method parameter
web::json::value make_control_class_method_parameter(const utility::string_t& description, const nc_name& name, const utility::string_t& type_name,
bool is_nullable = false, bool is_sequence = false, const web::json::value& constraints = web::json::value::null());
// create control class method
web::json::value make_control_class_method(const utility::string_t& description, const nc_method_id& id, const nc_name& name, const utility::string_t& result_datatype,
const std::vector<web::json::value>& parameters = {}, bool is_deprecated = false);
method make_control_class_method(const utility::string_t& description, const nc_method_id& id, const nc_name& name, const utility::string_t& result_datatype,
const std::vector<web::json::value>& parameters, bool is_deprecated, non_standard_method_handler method_handler);

// create control class event
web::json::value make_control_class_event(const utility::string_t& description, const nc_event_id& id, const nc_name& name, const utility::string_t& event_datatype,
bool is_deprecated = false);

// create control class property
web::json::value make_control_class_property(const utility::string_t& description, const nc_property_id& id, const nc_name& name, const utility::string_t& type_name,
bool is_read_only = false, bool is_nullable = false, bool is_sequence = false, bool is_deprecated = false, const web::json::value& constraints = web::json::value::null());

// create control class with fixed role
control_class make_control_class(const utility::string_t& description, const nc_class_id& class_id, const nc_name& name, const utility::string_t& fixed_role, const std::vector<web::json::value>& properties = {}, const std::vector<std::pair<web::json::value, nmos::experimental::method_handler>>& methods = {}, const std::vector<web::json::value>& events = {});
// create control class with no fixed role
control_class make_control_class(const utility::string_t& description, const nc_class_id& class_id, const nc_name& name, const std::vector<web::json::value>& properties = {}, const std::vector<std::pair<web::json::value, nmos::experimental::method_handler>>& methods = {}, const std::vector<web::json::value>& events = {});
}
}

Expand Down
Loading

0 comments on commit 2fbd8e1

Please sign in to comment.