RDMnet  HEAD (unstable)
Implementation of ANSI E1.33 (RDMnet)
View other versions:
broker.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2020 ETC Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************
16  * This file is a part of RDMnet. For more information, go to:
17  * https://github.com/ETCLabs/RDMnet
18  *****************************************************************************/
19 
22 
23 #ifndef RDMNET_CPP_BROKER_H_
24 #define RDMNET_CPP_BROKER_H_
25 
26 #include <cstdint>
27 #include <cstring>
28 #include <memory>
29 #include <set>
30 #include <string>
31 #include <utility>
32 #include <vector>
33 #include "etcpal/common.h"
34 #include "etcpal/cpp/error.h"
35 #include "etcpal/cpp/inet.h"
36 #include "etcpal/cpp/log.h"
37 #include "etcpal/cpp/uuid.h"
38 #include "rdm/cpp/uid.h"
39 #include "rdmnet/common.h"
40 #include "rdmnet/defs.h"
41 
42 class BrokerCore;
43 
44 namespace rdmnet
45 {
49 
54 {
57 
58  DnsTxtRecordItem() = default;
59  DnsTxtRecordItem(const char* new_key, const char* new_value);
60  DnsTxtRecordItem(const char* new_key, const uint8_t* new_value, size_t new_value_len);
61  DnsTxtRecordItem(const std::string& new_key, const std::string& new_value);
62  DnsTxtRecordItem(const std::string& new_key, const uint8_t* new_value, size_t new_value_len);
63  DnsTxtRecordItem(const std::string& new_key, const std::vector<uint8_t>& new_value);
64 };
65 
67 inline DnsTxtRecordItem::DnsTxtRecordItem(const char* new_key, const char* new_value)
68 {
69  if (new_key)
70  key = std::string(new_key);
71 
72  if (new_value)
73  value = std::vector<uint8_t>(new_value, &new_value[std::strlen(new_value)]);
74 }
75 
77 inline DnsTxtRecordItem::DnsTxtRecordItem(const char* new_key, const uint8_t* new_value, size_t new_value_len)
78 {
79  if (new_key)
80  key = std::string(new_key);
81 
82  if (new_value)
83  value = std::vector<uint8_t>(new_value, new_value + new_value_len);
84 }
85 
87 inline DnsTxtRecordItem::DnsTxtRecordItem(const std::string& new_key, const std::string& new_value)
88  : key(new_key), value(new_value.begin(), new_value.end())
89 {
90 }
91 
93 inline DnsTxtRecordItem::DnsTxtRecordItem(const std::string& new_key, const uint8_t* new_value, size_t new_value_len)
94  : key(new_key)
95 {
96  if (new_value)
97  value = std::vector<uint8_t>(new_value, new_value + new_value_len);
98 }
99 
101 inline DnsTxtRecordItem::DnsTxtRecordItem(const std::string& new_key, const std::vector<uint8_t>& new_value)
102  : key(new_key), value(new_value)
103 {
104 }
105 
124 class Broker
125 {
126 public:
130  {
137 
139  std::string manufacturer{"Generic Manufacturer"};
141  std::string model{"Generic RDMnet Broker"};
142 
145  };
146 
149  struct Limits
150  {
152  unsigned int connections{0};
154  unsigned int controllers{0};
156  unsigned int controller_messages{500};
158  unsigned int devices{0};
160  unsigned int device_messages{500};
163  unsigned int reject_connections{1000};
164  };
165 
168  struct Settings
169  {
171  rdm::Uid uid;
174 
176  std::string scope{E133_DEFAULT_SCOPE};
178  // bool allow_rdm_scope_change{false}; // (TODO: Not yet implemented)
180  // bool allow_rdm_disable{false}; // (TODO: Not yet implemented)
181 
184  uint16_t listen_port{0};
185 
192 
193  Settings() = default;
194  Settings(const etcpal::Uuid& cid_in, const rdm::Uid& static_uid_in);
195  Settings(const etcpal::Uuid& cid_in, uint16_t rdm_manu_id_in);
196 
198 
199  bool IsValid() const;
200  };
201 
205  {
206  public:
207  virtual ~NotifyHandler() = default;
208 
214  virtual void HandleScopeChanged(const std::string& new_scope) { ETCPAL_UNUSED_ARG(new_scope); }
215  };
216 
217  Broker();
218  virtual ~Broker();
219 
221  Broker(const Broker& other) = delete;
223  Broker& operator=(const Broker& other) = delete;
225  Broker(Broker&& other) = default;
227  Broker& operator=(Broker&& other) = default;
228 
229  etcpal::Error Startup(const Settings& settings, etcpal::Logger* logger = nullptr, NotifyHandler* notify = nullptr);
231  etcpal::Error ChangeScope(const std::string& new_scope, rdmnet_disconnect_reason_t disconnect_reason);
232 
233  const Settings& settings() const;
234 
235 private:
237 };
238 
241 {
242  dns.service_instance_name = "RDMnet Broker Instance " + cid.ToString();
243 }
244 
246 inline Broker::Settings::Settings(const etcpal::Uuid& cid_in, const rdm::Uid& static_uid_in)
247  : cid(cid_in), uid(static_uid_in)
248 {
250 }
251 
253 inline Broker::Settings::Settings(const etcpal::Uuid& cid_in, uint16_t rdm_manu_id_in)
254  : cid(cid_in), uid(rdm::Uid::DynamicUidRequest(rdm_manu_id_in))
255 {
257 }
258 
260 inline bool Broker::Settings::IsValid() const
261 {
262  // clang-format off
263  return (
264  !cid.IsNull() &&
265  (!scope.empty() && scope.length() < (E133_SCOPE_STRING_PADDED_LENGTH - 1)) &&
266  (!dns.manufacturer.empty() && dns.manufacturer.length() < (E133_MANUFACTURER_STRING_PADDED_LENGTH - 1)) &&
267  (!dns.model.empty() && dns.model.length() < (E133_MODEL_STRING_PADDED_LENGTH - 1)) &&
268  (!dns.service_instance_name.empty() && dns.service_instance_name.length() < (E133_SERVICE_NAME_STRING_PADDED_LENGTH - 1)) &&
269  (listen_port == 0 || listen_port >= 1024) &&
270  (uid.manufacturer_id() != 0) &&
271  (uid.IsStatic() || uid.IsDynamicUidRequest())
272  );
273  // clang-format on
274 }
275 
276 }; // namespace rdmnet
277 
278 #endif // RDMNET_CPP_BROKER_H_
std::string ToString() const
A callback interface for notifications from the broker.
Definition: broker.h:205
virtual void HandleScopeChanged(const std::string &new_scope)
The scope of the broker has changed via RDMnet configuration.
Definition: broker.h:214
Defines an instance of RDMnet broker functionality.
Definition: broker.h:125
const Settings & settings() const
Get the current settings the broker is using.
Definition: broker_api.cpp:100
etcpal::Error ChangeScope(const std::string &new_scope, rdmnet_disconnect_reason_t disconnect_reason)
Change the scope on which a broker operates.
Definition: broker_api.cpp:88
Broker & operator=(const Broker &other)=delete
Brokers cannot be copied.
Broker & operator=(Broker &&other)=default
Move an instance of broker functionality.
Broker(Broker &&other)=default
Move an instance of broker functionality.
Broker()
Constructs a broker instance. Broker is not running until Broker::Startup() is called.
Definition: broker_api.cpp:28
void Shutdown(rdmnet_disconnect_reason_t disconnect_reason=kRdmnetDisconnectShutdown)
Shut down all broker functionality and threads.
Definition: broker_api.cpp:66
etcpal::Error Startup(const Settings &settings, etcpal::Logger *logger=nullptr, NotifyHandler *notify=nullptr)
Start all broker functionality and threads.
Definition: broker_api.cpp:52
Broker(const Broker &other)=delete
Brokers cannot be copied.
virtual ~Broker()
Destroys a broker instance. Call Broker::Shutdown() first.
Definition: broker_api.cpp:33
Functions and definitions common to all RDMnet API modules.
rdmnet_disconnect_reason_t
Definition: common.h:85
@ kRdmnetDisconnectShutdown
Definition: common.h:87
A namespace which contains all C++ language definitions in the RDMnet library.
Definition: broker.h:45
T strlen(T... args)
Settings for the Broker's DNS Discovery functionality.
Definition: broker.h:130
std::string model
A string to identify the model of product in which the broker instance is included.
Definition: broker.h:141
std::vector< DnsTxtRecordItem > additional_txt_record_items
Any additional non-standard items to add to the broker's DNS TXT record.
Definition: broker.h:144
std::string service_instance_name
Your unique name for this broker DNS-SD service instance.
Definition: broker.h:136
std::string manufacturer
A string to identify the manufacturer of this broker instance.
Definition: broker.h:139
A set of limits for broker operation.
Definition: broker.h:150
unsigned int connections
The maximum number of client connections supported. 0 means infinite.
Definition: broker.h:152
unsigned int controllers
The maximum number of controllers allowed. 0 means infinite.
Definition: broker.h:154
unsigned int devices
The maximum number of devices allowed. 0 means infinite.
Definition: broker.h:158
unsigned int reject_connections
Definition: broker.h:163
unsigned int controller_messages
The maximum number of queued messages per controller. 0 means infinite.
Definition: broker.h:156
unsigned int device_messages
The maximum number of queued messages per device. 0 means infinite.
Definition: broker.h:160
A group of settings for broker operation.
Definition: broker.h:169
Limits limits
The broker's limits.
Definition: broker.h:173
uint16_t listen_port
Whether the broker should allow the scope to be changed via RDM commands.
Definition: broker.h:184
rdm::Uid uid
The broker's UID.
Definition: broker.h:171
std::string scope
The RDMnet scope on which this broker should operate.
Definition: broker.h:176
std::vector< std::string > listen_interfaces
A list of strings representing the system name of network interfaces to listen on.
Definition: broker.h:191
etcpal::Uuid cid
The broker's CID.
Definition: broker.h:170
DnsAttributes dns
The broker's DNS attributes.
Definition: broker.h:172
bool IsValid() const
Whether this structure contains valid settings for broker operation.
Definition: broker.h:260
void SetDefaultServiceInstanceName()
Generate a DNS service instance name based on the broker's current CID.
Definition: broker.h:240
A key/value pair representing a DNS TXT record item.
Definition: broker.h:54
std::string key
The key is an ASCII-only string.
Definition: broker.h:55
std::vector< uint8_t > value
The value is opaque binary data.
Definition: broker.h:56