Skip to content

Commit

Permalink
Add readonly on Get propertry support.
Browse files Browse the repository at this point in the history
Add callback to retrieve control classes from control_protocol_state
  • Loading branch information
lo-simon committed Aug 8, 2023
1 parent f42bb05 commit bb6db52
Show file tree
Hide file tree
Showing 14 changed files with 1,747 additions and 1,302 deletions.
6 changes: 6 additions & 0 deletions Development/cmake/NmosCppLibraries.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,10 @@ set(NMOS_CPP_NMOS_SOURCES
nmos/connection_api.cpp
nmos/connection_events_activation.cpp
nmos/connection_resources.cpp
nmos/control_protocol_handlers.cpp
nmos/control_protocol_resource.cpp
nmos/control_protocol_resources.cpp
nmos/control_protocol_state.cpp
nmos/control_protocol_ws_api.cpp
nmos/did_sdid.cpp
nmos/events_api.cpp
Expand Down Expand Up @@ -906,7 +909,10 @@ set(NMOS_CPP_NMOS_HEADERS
nmos/connection_api.h
nmos/connection_events_activation.h
nmos/connection_resources.h
nmos/control_protocol_handlers.h
nmos/control_protocol_resource.h
nmos/control_protocol_resources.h
nmos/control_protocol_state.h
nmos/control_protocol_ws_api.h
nmos/device_type.h
nmos/did_sdid.h
Expand Down
5 changes: 5 additions & 0 deletions Development/nmos-cpp-node/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "nmos/server.h"
#include "node_implementation.h"

#include "nmos/control_protocol_state.h"

int main(int argc, char* argv[])
{
// Construct our data models including mutexes to protect them
Expand Down Expand Up @@ -107,6 +109,9 @@ int main(int argc, char* argv[])
}
#endif

nmos::experimental::control_protocol_state control_protocol_state;
node_implementation.on_get_control_classes(nmos::make_get_control_protocol_classes_handler(control_protocol_state, gate));

// Set up the node server

auto node_server = nmos::experimental::make_node_server(node_model, node_implementation, log_model, gate);
Expand Down
64 changes: 64 additions & 0 deletions Development/nmos/control_protocol_handlers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "nmos/control_protocol_handlers.h"

#include "cpprest/basic_utils.h"
#include "nmos/control_protocol_state.h"
#include "nmos/slog.h"

namespace nmos
{
get_control_protocol_classes_handler make_get_control_protocol_classes_handler(nmos::experimental::control_protocol_state& control_protocol_state, slog::base_gate& gate)
{
return [&]()
{
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "Retrieve all control classes from cache";

auto lock = control_protocol_state.read_lock();

return control_protocol_state.control_classes;
};
}

get_control_protocol_class_handler make_get_control_protocol_class_handler(nmos::experimental::control_protocol_state& control_protocol_state, slog::base_gate& gate)
{
return [&](const details::nc_class_id& class_id)
{
using web::json::value;

slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "Retrieve control class from cache";

auto lock = control_protocol_state.read_lock();

auto class_id_data = details::make_nc_class_id(class_id);

auto& control_classes = control_protocol_state.control_classes;
auto found = control_classes.find(class_id_data);
if (control_classes.end() != found)
{
return found->second;
}

return experimental::control_class{ value::array(), value::array(), value::array() };
};
}

add_control_protocol_class_handler make_add_control_protocol_class_handler(nmos::experimental::control_protocol_state& control_protocol_state, slog::base_gate& gate)
{
return [&](const details::nc_class_id& class_id, const experimental::control_class& control_class)
{
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "Add control class to cache";

auto lock = control_protocol_state.write_lock();

auto class_id_data = details::make_nc_class_id(class_id);

auto& control_classes = control_protocol_state.control_classes;
if (control_classes.end() == control_classes.find(class_id_data))
{
return false;
}

control_classes[class_id_data] = control_class;
return true;
};
}
}
44 changes: 44 additions & 0 deletions Development/nmos/control_protocol_handlers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef NMOS_CONTROL_PROTOCOL_HANDLERS_H
#define NMOS_CONTROL_PROTOCOL_HANDLERS_H

#include <cstdint>
#include <functional>
#include "nmos/control_protocol_resource.h"
#include "nmos/control_protocol_state.h"

namespace slog
{
class base_gate;
}

namespace nmos
{
namespace experimental
{
struct control_class;
struct control_protocol_state;
}

// callback to retrieve all control protocol classes
// this callback should not throw exceptions
typedef std::function<experimental::control_classes()> get_control_protocol_classes_handler;

// callback to retrieve a specific control protocol class
// this callback should not throw exceptions
typedef std::function<experimental::control_class(const details::nc_class_id& class_id)> get_control_protocol_class_handler;

// callback to add user control protocol class
// this callback should not throw exceptions
typedef std::function<bool(const details::nc_class_id& class_id, const experimental::control_class& control_class)> add_control_protocol_class_handler;

// construct callback to retrieve all control protocol classes
get_control_protocol_classes_handler make_get_control_protocol_classes_handler(nmos::experimental::control_protocol_state& control_protocol_state, slog::base_gate& gate);

// construct callback to retrieve control protocol class
get_control_protocol_class_handler make_get_control_protocol_class_handler(nmos::experimental::control_protocol_state& control_protocol_state, slog::base_gate& gate);

// construct callback to add control protocol class
add_control_protocol_class_handler make_add_control_protocol_class_handler(nmos::experimental::control_protocol_state& control_protocol_state, slog::base_gate& gate);
}

#endif
Loading

0 comments on commit bb6db52

Please sign in to comment.