Skip to content

Commit

Permalink
Optimize is12 method handlers (#393)
Browse files Browse the repository at this point in the history
* Enhance control_protocol_state to support user callback on control class method handler

* Update control_protocol_state use in control protocol test

* Rename method_handler to control_protocol_method_handler with @jonathan-r-thorpe suggestion.
  • Loading branch information
lo-simon authored Jun 5, 2024
1 parent 0a29731 commit e2830ab
Show file tree
Hide file tree
Showing 11 changed files with 266 additions and 208 deletions.
2 changes: 1 addition & 1 deletion Development/nmos-cpp-node/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ int main(int argc, char* argv[])
.on_request_authorization_code(nmos::experimental::make_request_authorization_code_handler(gate)); // may be omitted, only required for OAuth client which is using the Authorization Code Flow to obtain the access token
}

nmos::experimental::control_protocol_state control_protocol_state;
nmos::experimental::control_protocol_state control_protocol_state(node_implementation.control_protocol_property_changed);
if (0 <= nmos::fields::control_protocol_ws_port(node_model.settings))
{
node_implementation
Expand Down
14 changes: 7 additions & 7 deletions Development/nmos-cpp-node/node_implementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1008,31 +1008,31 @@ void node_implementation_init(nmos::node_model& model, nmos::experimental::contr
nmos::experimental::make_control_class_property_descriptor(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, slog::base_gate& gate)
auto example_method_with_no_args = [](nmos::resources& resources, const nmos::resource& resource, 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 });
return nmos::details::make_nc_method_result({ 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, slog::base_gate& gate)
auto example_method_with_simple_args = [](nmos::resources& resources, const nmos::resource& resource, 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

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

return nmos::make_control_protocol_message_response(handle, { is_deprecated ? nmos::nc_method_status::method_deprecated : nmos::nc_method_status::ok });
return nmos::details::make_nc_method_result({ 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, slog::base_gate& gate)
auto example_method_with_object_args = [](nmos::resources& resources, const nmos::resource& resource, 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

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

return nmos::make_control_protocol_message_response(handle, { is_deprecated ? nmos::nc_method_status::method_deprecated : nmos::nc_method_status::ok });
return nmos::details::make_nc_method_result({ is_deprecated ? nmos::nc_method_status::method_deprecated : nmos::nc_method_status::ok });
};
// Example control class method descriptors
std::vector<nmos::experimental::method> example_control_method_descriptors =
Expand Down Expand Up @@ -1113,7 +1113,7 @@ void node_implementation_init(nmos::node_model& model, nmos::experimental::contr
// helper function to create Example control instance
auto make_example_control = [&](nmos::nc_oid oid, nmos::nc_oid owner, const utility::string_t& role, const utility::string_t& user_label, const utility::string_t& description, const value& touchpoints,
const value& runtime_property_constraints, // level 2: runtime constraints. See https://specs.amwa.tv/ms-05-02/branches/v1.0.x/docs/Constraints.html
// use of make_nc_property_constraints_string and make_nc_property_constraints_number to create runtime constraints
// use of make_nc_property_constraints_string and make_nc_property_constraints_number to create runtime constraints
example_enum enum_property_,
const utility::string_t& string_property_,
uint64_t number_property_,
Expand Down
18 changes: 5 additions & 13 deletions Development/nmos/control_protocol_handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,15 @@ namespace nmos

namespace experimental
{
// 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_descriptor_handler get_control_protocol_class_descriptor, get_control_protocol_datatype_descriptor_handler get_control_protocol_datatype_descriptor, 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;
// control method handler definition
typedef std::function<web::json::value(nmos::resources& resources, const nmos::resource& resource, const web::json::value& arguments, bool is_deprecated, slog::base_gate& gate)> control_protocol_method_handler;

// method definition (NcMethodDescriptor vs method handler)
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);
}
typedef std::pair<web::json::value, control_protocol_method_handler> method;

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

Expand Down
Loading

0 comments on commit e2830ab

Please sign in to comment.