From ab2b952432afd73a6d6c6ea7502ab11ce54028f2 Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Fri, 19 Feb 2016 11:25:50 +0300 Subject: [PATCH] Refactored the dynamic node ID allocation server: added a new class AbstractServer, which is inherited by CentralizedServer and DistributedServer. This change allowed to move the data and logic that is common to both types of servers to a single location. In the next step this will be used to add more complex common logic. --- .../abstract_server.hpp | 68 +++++++++++++++++++ .../centralized/server.hpp | 47 ++++--------- .../distributed/server.hpp | 51 ++++---------- 3 files changed, 96 insertions(+), 70 deletions(-) create mode 100644 libuavcan/include/uavcan/protocol/dynamic_node_id_server/abstract_server.hpp diff --git a/libuavcan/include/uavcan/protocol/dynamic_node_id_server/abstract_server.hpp b/libuavcan/include/uavcan/protocol/dynamic_node_id_server/abstract_server.hpp new file mode 100644 index 0000000000..c465f42070 --- /dev/null +++ b/libuavcan/include/uavcan/protocol/dynamic_node_id_server/abstract_server.hpp @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2015 Pavel Kirienko + */ + +#ifndef UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_SERVER_SERVER_HPP_INCLUDED +#define UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_SERVER_SERVER_HPP_INCLUDED + +#include +#include +#include +#include +#include + +namespace uavcan +{ +namespace dynamic_node_id_server +{ + +class AbstractServer : protected IAllocationRequestHandler + , protected INodeDiscoveryHandler +{ + UniqueID own_unique_id_; + +protected: + INode& node_; + IEventTracer& tracer_; + AllocationRequestManager allocation_request_manager_; + NodeDiscoverer node_discoverer_; + + AbstractServer(INode& node, + IEventTracer& tracer) : + node_(node), + tracer_(tracer), + allocation_request_manager_(node, tracer, *this), + node_discoverer_(node, tracer, *this) + { } + + const UniqueID& getOwnUniqueID() const { return own_unique_id_; } + + int init(const UniqueID& own_unique_id, const TransferPriority priority) + { + int res = 0; + + own_unique_id_ = own_unique_id; + + res = allocation_request_manager_.init(priority); + if (res < 0) + { + return res; + } + + res = node_discoverer_.init(priority); + if (res < 0) + { + return res; + } + + return 0; + } + +public: + const NodeDiscoverer& getNodeDiscoverer() const { return node_discoverer_; } +}; + +} +} + +#endif // Include guard diff --git a/libuavcan/include/uavcan/protocol/dynamic_node_id_server/centralized/server.hpp b/libuavcan/include/uavcan/protocol/dynamic_node_id_server/centralized/server.hpp index 31daf63c68..abbddcd8c3 100644 --- a/libuavcan/include/uavcan/protocol/dynamic_node_id_server/centralized/server.hpp +++ b/libuavcan/include/uavcan/protocol/dynamic_node_id_server/centralized/server.hpp @@ -7,8 +7,7 @@ #include #include -#include -#include +#include #include #include #include @@ -26,15 +25,8 @@ namespace centralized * * This version is suitable only for simple non-critical systems. */ -class Server : IAllocationRequestHandler - , INodeDiscoveryHandler +class Server : public AbstractServer { - UniqueID own_unique_id_; - - INode& node_; - IEventTracer& tracer_; - AllocationRequestManager allocation_request_manager_; - NodeDiscoverer node_discoverer_; Storage storage_; /* @@ -128,10 +120,7 @@ public: Server(INode& node, IStorageBackend& storage, IEventTracer& tracer) - : node_(node) - , tracer_(tracer) - , allocation_request_manager_(node, tracer, *this) - , node_discoverer_(node, tracer, *this) + : AbstractServer(node, tracer) , storage_(storage) { } @@ -147,13 +136,20 @@ public: return res; } + /* + * Common logic + */ + res = AbstractServer::init(own_unique_id, priority); + if (res < 0) + { + return res; + } + /* * Making sure that the server is started with the same node ID */ - own_unique_id_ = own_unique_id; - { - const NodeID stored_own_node_id = storage_.getNodeIDForUniqueID(own_unique_id_); + const NodeID stored_own_node_id = storage_.getNodeIDForUniqueID(getOwnUniqueID()); if (stored_own_node_id.isValid()) { if (stored_own_node_id != node_.getNodeID()) @@ -163,7 +159,7 @@ public: } else { - res = storage_.add(node_.getNodeID(), own_unique_id_); + res = storage_.add(node_.getNodeID(), getOwnUniqueID()); if (res < 0) { return res; @@ -171,21 +167,6 @@ public: } } - /* - * Misc - */ - res = allocation_request_manager_.init(priority); - if (res < 0) - { - return res; - } - - res = node_discoverer_.init(priority); - if (res < 0) - { - return res; - } - return 0; } diff --git a/libuavcan/include/uavcan/protocol/dynamic_node_id_server/distributed/server.hpp b/libuavcan/include/uavcan/protocol/dynamic_node_id_server/distributed/server.hpp index e36fb323c4..cd72825e7d 100644 --- a/libuavcan/include/uavcan/protocol/dynamic_node_id_server/distributed/server.hpp +++ b/libuavcan/include/uavcan/protocol/dynamic_node_id_server/distributed/server.hpp @@ -9,9 +9,8 @@ #include #include #include -#include +#include #include -#include #include namespace uavcan @@ -23,8 +22,7 @@ namespace distributed /** * This class implements the top-level allocation logic and server API. */ -class UAVCAN_EXPORT Server : IAllocationRequestHandler - , INodeDiscoveryHandler +class UAVCAN_EXPORT Server : public AbstractServer , IRaftLeaderMonitor { struct UniqueIDLogPredicate @@ -55,19 +53,10 @@ class UAVCAN_EXPORT Server : IAllocationRequestHandler } }; - /* - * Constants - */ - UniqueID own_unique_id_; - /* * States */ - INode& node_; - IEventTracer& tracer_; RaftCore raft_core_; - AllocationRequestManager allocation_request_manager_; - NodeDiscoverer node_discoverer_; /* * Methods of IAllocationRequestHandler @@ -196,7 +185,7 @@ class UAVCAN_EXPORT Server : IAllocationRequestHandler if (!result.isConstructed()) { - raft_core_.appendLog(own_unique_id_, node_.getNodeID()); + raft_core_.appendLog(getOwnUniqueID(), node_.getNodeID()); } } @@ -239,11 +228,8 @@ public: Server(INode& node, IStorageBackend& storage, IEventTracer& tracer) - : node_(node) - , tracer_(tracer) + : AbstractServer(node, tracer) , raft_core_(node, storage, tracer, *this) - , allocation_request_manager_(node, tracer, *this) - , node_discoverer_(node, tracer, *this) { } int init(const UniqueID& own_unique_id, @@ -259,37 +245,29 @@ public: return res; } + /* + * Common logic + */ + res = AbstractServer::init(own_unique_id, priority); + if (res < 0) + { + return res; + } + /* * Making sure that the server is started with the same node ID */ - own_unique_id_ = own_unique_id; - const LazyConstructor own_log_entry = raft_core_.traverseLogFromEndUntil(NodeIDLogPredicate(node_.getNodeID())); if (own_log_entry.isConstructed()) { - if (own_log_entry->entry.unique_id != own_unique_id_) + if (own_log_entry->entry.unique_id != getOwnUniqueID()) { return -ErrInvalidConfiguration; } } - /* - * Misc - */ - res = allocation_request_manager_.init(priority); - if (res < 0) - { - return res; - } - - res = node_discoverer_.init(priority); - if (res < 0) - { - return res; - } - return 0; } @@ -299,7 +277,6 @@ public: * These accessors are needed for debugging, visualization and testing. */ const RaftCore& getRaftCore() const { return raft_core_; } - const NodeDiscoverer& getNodeDiscoverer() const { return node_discoverer_; } }; /**