mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-05-23 08:27:34 +08:00
Very basic implementation of DynamicNodeIDAllocationServer - not all logic is implemented yet, but it can be used for testing already
This commit is contained in:
@@ -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<DynamicNodeIDAllocationServer*,
|
||||
void (DynamicNodeIDAllocationServer::*)
|
||||
(const ReceivedDataStructure<protocol::NodeStatus>&)> NodeStatusCallback;
|
||||
|
||||
typedef MethodBinder<DynamicNodeIDAllocationServer*,
|
||||
void (DynamicNodeIDAllocationServer::*)(const ServiceCallResult<protocol::GetNodeInfo>&)>
|
||||
GetNodeInfoResponseCallback;
|
||||
|
||||
typedef Map<NodeID, uint8_t, 10> 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<protocol::NodeStatus, NodeStatusCallback> node_status_sub_;
|
||||
ServiceClient<protocol::GetNodeInfo> 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);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user