centralized/server.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #ifndef UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_SERVER_CENTRALIZED_SERVER_HPP_INCLUDED
6 #define UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_SERVER_CENTRALIZED_SERVER_HPP_INCLUDED
7 
9 #include <uavcan/debug.hpp>
14 
15 namespace uavcan
16 {
17 namespace dynamic_node_id_server
18 {
19 namespace centralized
20 {
28 class Server : public AbstractServer
29 {
31 
32  /*
33  * Private methods
34  */
35  bool isNodeIDTaken(const NodeID node_id) const
36  {
37  return storage_.isNodeIDOccupied(node_id);
38  }
39 
40  void tryPublishAllocationResult(const NodeID node_id, const UniqueID& unique_id)
41  {
42  const int res = allocation_request_manager_.broadcastAllocationResponse(unique_id, node_id);
43  if (res < 0)
44  {
46  node_.registerInternalFailure("Dynamic allocation response");
47  }
48  }
49 
50  /*
51  * Methods of IAllocationRequestHandler
52  */
54  {
55  return true; // Because there's only one Centralized server in the system
56  }
57 
58  virtual void handleAllocationRequest(const UniqueID& unique_id, const NodeID preferred_node_id)
59  {
60  const NodeID existing_node_id = storage_.getNodeIDForUniqueID(unique_id);
61  if (existing_node_id.isValid())
62  {
63  tryPublishAllocationResult(existing_node_id, unique_id);
64  }
65  else
66  {
67  const NodeID allocated_node_id =
69 
70  if (allocated_node_id.isUnicast())
71  {
72  const int res = storage_.add(allocated_node_id, unique_id);
73  if (res >= 0)
74  {
75  tryPublishAllocationResult(allocated_node_id, unique_id);
76  }
77  else
78  {
80  node_.registerInternalFailure("CentralizedServer storage add");
81  }
82  }
83  else
84  {
85  UAVCAN_TRACE("dynamic_node_id_server::distributed::Server", "Request ignored - no free node ID left");
86  }
87  }
88  }
89 
90  /*
91  * Methods of INodeDiscoveryHandler
92  */
93  virtual bool canDiscoverNewNodes() const
94  {
95  return true; // Because there's only one Centralized server in the system
96  }
97 
98  virtual NodeAwareness checkNodeAwareness(NodeID node_id) const
99  {
101  }
102 
103  virtual void handleNewNodeDiscovery(const UniqueID* unique_id_or_null, NodeID node_id)
104  {
105  if (storage_.isNodeIDOccupied(node_id))
106  {
107  UAVCAN_ASSERT(0); // Such node is already known, the class that called this method should have known that
108  return;
109  }
110 
111  const int res = storage_.add(node_id, (unique_id_or_null == UAVCAN_NULLPTR) ? UniqueID() : *unique_id_or_null);
112  if (res < 0)
113  {
114  tracer_.onEvent(TraceError, res);
115  node_.registerInternalFailure("CentralizedServer storage add");
116  }
117  }
118 
119 public:
121  IStorageBackend& storage,
122  IEventTracer& tracer)
123  : AbstractServer(node, tracer)
124  , storage_(storage)
125  { }
126 
127  int init(const UniqueID& own_unique_id,
129  {
130  /*
131  * Initializing storage first, because the next step requires it to be loaded
132  */
133  int res = storage_.init();
134  if (res < 0)
135  {
136  return res;
137  }
138 
139  /*
140  * Common logic
141  */
142  res = AbstractServer::init(own_unique_id, priority);
143  if (res < 0)
144  {
145  return res;
146  }
147 
148  /*
149  * Making sure that the server is started with the same node ID
150  */
151  {
152  const NodeID stored_own_node_id = storage_.getNodeIDForUniqueID(getOwnUniqueID());
153  if (stored_own_node_id.isValid())
154  {
155  if (stored_own_node_id != node_.getNodeID())
156  {
157  return -ErrInvalidConfiguration;
158  }
159  }
160  else
161  {
163  if (res < 0)
164  {
165  return res;
166  }
167  }
168  }
169 
170  return 0;
171  }
172 
174 };
175 
176 }
177 }
178 }
179 
180 #endif // UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_SERVER_CENTRALIZED_SERVER_HPP_INCLUDED
UAVCAN_NULLPTR
#define UAVCAN_NULLPTR
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:51
uavcan::dynamic_node_id_server::centralized::Server::tryPublishAllocationResult
void tryPublishAllocationResult(const NodeID node_id, const UniqueID &unique_id)
Definition: centralized/server.hpp:40
TraceError
TraceError
Definition: event.hpp:23
uavcan::NodeID::isValid
bool isValid() const
Definition: transfer.hpp:134
debug.hpp
uavcan::dynamic_node_id_server::AbstractServer::allocation_request_manager_
AllocationRequestManager allocation_request_manager_
Definition: abstract_server.hpp:28
uavcan::dynamic_node_id_server::centralized::Server::Server
Server(INode &node, IStorageBackend &storage, IEventTracer &tracer)
Definition: centralized/server.hpp:120
uavcan::NodeID
Definition: transfer.hpp:112
uavcan::dynamic_node_id_server::AllocationRequestManager::broadcastAllocationResponse
int broadcastAllocationResponse(const UniqueID &unique_id, NodeID allocated_node_id)
Definition: allocation_request_manager.hpp:261
uavcan::dynamic_node_id_server::centralized::Server::canPublishFollowupAllocationResponse
virtual bool canPublishFollowupAllocationResponse() const
Definition: centralized/server.hpp:53
uavcan::dynamic_node_id_server::AbstractServer::tracer_
IEventTracer & tracer_
Definition: abstract_server.hpp:27
UAVCAN_TRACE
#define UAVCAN_TRACE(...)
Definition: libuavcan/libuavcan/include/uavcan/debug.hpp:31
uavcan::dynamic_node_id_server::INodeDiscoveryHandler::NodeAwareness
NodeAwareness
Definition: node_discoverer.hpp:40
uavcan::dynamic_node_id_server::centralized::Storage
Definition: storage.hpp:24
uavcan::dynamic_node_id_server::centralized::Storage::init
int init()
Definition: storage.hpp:73
uavcan::dynamic_node_id_server::centralized::Storage::isNodeIDOccupied
bool isNodeIDOccupied(NodeID node_id) const
Definition: storage.hpp:141
uavcan::dynamic_node_id_server::centralized::Server::handleNewNodeDiscovery
virtual void handleNewNodeDiscovery(const UniqueID *unique_id_or_null, NodeID node_id)
Definition: centralized/server.hpp:103
node_id_selector.hpp
uavcan::dynamic_node_id_server::centralized::Server::storage_
Storage storage_
Definition: centralized/server.hpp:30
uavcan::dynamic_node_id_server::AbstractServer::getOwnUniqueID
const UniqueID & getOwnUniqueID() const
Definition: abstract_server.hpp:39
uavcan::INode::registerInternalFailure
virtual void registerInternalFailure(const char *msg)=0
uavcan::uint8_t
std::uint8_t uint8_t
Definition: std.hpp:24
uavcan::TransferPriority::OneHigherThanLowest
static const TransferPriority OneHigherThanLowest
Definition: transfer.hpp:40
uavcan::dynamic_node_id_server::IStorageBackend
Definition: storage_backend.hpp:22
uavcan::dynamic_node_id_server::centralized::Server::handleAllocationRequest
virtual void handleAllocationRequest(const UniqueID &unique_id, const NodeID preferred_node_id)
Definition: centralized/server.hpp:58
uavcan::NodeID::isUnicast
bool isUnicast() const
Definition: transfer.hpp:136
uavcan::dynamic_node_id_server::INodeDiscoveryHandler::NodeAwarenessUnknown
@ NodeAwarenessUnknown
Definition: node_discoverer.hpp:42
uavcan::dynamic_node_id_server::INodeDiscoveryHandler::NodeAwarenessKnownAndCommitted
@ NodeAwarenessKnownAndCommitted
Definition: node_discoverer.hpp:44
uavcan::dynamic_node_id_server::AbstractServer::init
int init(const UniqueID &own_unique_id, const TransferPriority priority)
Definition: abstract_server.hpp:41
uavcan::INode
Definition: abstract_node.hpp:19
uavcan::TransferPriority
Definition: transfer.hpp:28
build_config.hpp
uavcan::dynamic_node_id_server::centralized::Server::checkNodeAwareness
virtual NodeAwareness checkNodeAwareness(NodeID node_id) const
Definition: centralized/server.hpp:98
uavcan::dynamic_node_id_server::centralized::Server::isNodeIDTaken
bool isNodeIDTaken(const NodeID node_id) const
Definition: centralized/server.hpp:35
uavcan::INode::getNodeID
NodeID getNodeID() const
Definition: abstract_node.hpp:39
uavcan::dynamic_node_id_server::NodeIDSelector
Definition: node_id_selector.hpp:19
uavcan::dynamic_node_id_server::IEventTracer::onEvent
virtual void onEvent(TraceCode event_code, int64_t event_argument)=0
uavcan::dynamic_node_id_server::centralized::Storage::getSize
uint8_t getSize() const
Definition: storage.hpp:143
uavcan::dynamic_node_id_server::IEventTracer
Definition: event.hpp:90
uavcan::dynamic_node_id_server::AbstractServer::node_
INode & node_
Definition: abstract_server.hpp:26
storage.hpp
uavcan::dynamic_node_id_server::AbstractServer
Definition: abstract_server.hpp:19
uavcan::dynamic_node_id_server::NodeIDSelector::findFreeNodeID
NodeID findFreeNodeID(const NodeID preferred) const
Definition: node_id_selector.hpp:38
pyuavcan_v0.introspect.node
node
Definition: introspect.py:398
uavcan::dynamic_node_id_server::centralized::Server::init
int init(const UniqueID &own_unique_id, const TransferPriority priority=TransferPriority::OneHigherThanLowest)
Definition: centralized/server.hpp:127
uavcan
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:204
uavcan::dynamic_node_id_server::centralized::Server
Definition: centralized/server.hpp:28
uavcan::dynamic_node_id_server::centralized::Storage::add
int add(const NodeID node_id, const UniqueID &unique_id)
Definition: storage.hpp:86
uavcan::dynamic_node_id_server::centralized::Server::canDiscoverNewNodes
virtual bool canDiscoverNewNodes() const
Definition: centralized/server.hpp:93
storage_marshaller.hpp
uavcan::dynamic_node_id_server::UniqueID
protocol::dynamic_node_id::server::Entry::FieldTypes::unique_id UniqueID
Definition: protocol/dynamic_node_id_server/types.hpp:22
UAVCAN_ASSERT
#define UAVCAN_ASSERT(x)
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:184
abstract_server.hpp
uavcan::dynamic_node_id_server::centralized::Server::getNumAllocations
uint8_t getNumAllocations() const
Definition: centralized/server.hpp:173
uavcan::dynamic_node_id_server::centralized::Storage::getNodeIDForUniqueID
NodeID getNodeIDForUniqueID(const UniqueID &unique_id) const
Definition: storage.hpp:133


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:03