From aca9fcb12c3b164e61ccd536f35757566c8f0d8f Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Fri, 8 May 2015 21:00:17 +0300 Subject: [PATCH] Very basic implementation of DynamicNodeIDAllocationServer - not all logic is implemented yet, but it can be used for testing already --- .../dynamic_node_id_allocation_server.hpp | 35 ++++++++++++++++-- .../uc_dynamic_node_id_allocation_server.cpp | 37 +++++++++++++++++++ .../dynamic_node_id_allocation_server.cpp | 2 + 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/libuavcan/include/uavcan/protocol/dynamic_node_id_allocation_server.hpp b/libuavcan/include/uavcan/protocol/dynamic_node_id_allocation_server.hpp index f8bbe2d434..4aa98056aa 100644 --- a/libuavcan/include/uavcan/protocol/dynamic_node_id_allocation_server.hpp +++ b/libuavcan/include/uavcan/protocol/dynamic_node_id_allocation_server.hpp @@ -735,27 +735,56 @@ public: } // namespace dynamic_node_id_server_impl /** - * + * This class implements the top-level allocation logic and server API. */ -class DynamicNodeIDAllocationServer +class DynamicNodeIDAllocationServer : public dynamic_node_id_server_impl::IAllocationRequestHandler + , public dynamic_node_id_server_impl::ILeaderLogCommitHandler { typedef MethodBinder&)> NodeStatusCallback; + typedef MethodBinder&)> + GetNodeInfoResponseCallback; + typedef Map PendingGetNodeInfoAttemptsMap; + enum { MaxGetNodeInfoAttempts = 5 }; + /* * States */ PendingGetNodeInfoAttemptsMap pending_get_node_info_attempts_; + dynamic_node_id_server_impl::RaftCore raft_core_; + dynamic_node_id_server_impl::AllocationRequestManager allocation_request_manager_; /* * Transport */ - Subscriber node_status_sub_; + ServiceClient get_node_info_client_; + INode& getNode() { return get_node_info_client_.getNode(); } + + virtual void handleAllocationRequest(const UniqueID& unique_id, NodeID preferred_node_id); + + virtual void onEntryCommitted(const protocol::dynamic_node_id::server::Entry& entry); + +public: + DynamicNodeIDAllocationServer(INode& node, + IDynamicNodeIDStorageBackend& storage, + IDynamicNodeIDAllocationServerEventTracer& tracer) + : pending_get_node_info_attempts_(node.getAllocator()) + , raft_core_(node, storage, tracer, *this) + , allocation_request_manager_(node, *this) + , node_status_sub_(node) + , get_node_info_client_(node) + { } + + enum { ClusterSizeUnknown = dynamic_node_id_server_impl::ClusterManager::ClusterSizeUnknown }; + + int init(uint8_t cluster_size = ClusterSizeUnknown); }; } diff --git a/libuavcan/src/protocol/uc_dynamic_node_id_allocation_server.cpp b/libuavcan/src/protocol/uc_dynamic_node_id_allocation_server.cpp index b22c382012..588e944ac3 100644 --- a/libuavcan/src/protocol/uc_dynamic_node_id_allocation_server.cpp +++ b/libuavcan/src/protocol/uc_dynamic_node_id_allocation_server.cpp @@ -1723,4 +1723,41 @@ int AllocationRequestManager::broadcastAllocationResponse(const IAllocationReque } // dynamic_node_id_server_impl +/* + * DynamicNodeIDAllocationServer + */ +void DynamicNodeIDAllocationServer::handleAllocationRequest(const UniqueID& unique_id, NodeID preferred_node_id) +{ + // TODO implement proper allocation logic + (void)raft_core_.appendLog(unique_id, preferred_node_id); +} + +void DynamicNodeIDAllocationServer::onEntryCommitted(const protocol::dynamic_node_id::server::Entry& entry) +{ + const int res = allocation_request_manager_.broadcastAllocationResponse(entry.unique_id, entry.node_id); + if (res < 0) + { + getNode().registerInternalFailure("Dynamic allocation final broadcast"); + } +} + +int DynamicNodeIDAllocationServer::init(uint8_t cluster_size) +{ + int res = raft_core_.init(cluster_size); + if (res < 0) + { + return res; + } + + res = allocation_request_manager_.init(); + if (res < 0) + { + return res; + } + + // TODO Initialize the node info transport + + return 0; +} + } diff --git a/libuavcan/test/protocol/dynamic_node_id_allocation_server.cpp b/libuavcan/test/protocol/dynamic_node_id_allocation_server.cpp index 6468d44474..840f4b5adc 100644 --- a/libuavcan/test/protocol/dynamic_node_id_allocation_server.cpp +++ b/libuavcan/test/protocol/dynamic_node_id_allocation_server.cpp @@ -1056,4 +1056,6 @@ TEST(DynamicNodeIDAllocationServer, ObjectSizes) std::cout << "AllocationRequestManager: " << sizeof(uavcan::dynamic_node_id_server_impl::AllocationRequestManager) << std::endl; + + std::cout << "DynamicNodeIDAllocationServer: " << sizeof(uavcan::DynamicNodeIDAllocationServer) << std::endl; }