RDMnet  0.3.0
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 
53 {
56 
57  DnsTxtRecordItem() = default;
58  DnsTxtRecordItem(const char* new_key, const char* new_value);
59  DnsTxtRecordItem(const char* new_key, const uint8_t* new_value, size_t new_value_len);
60  DnsTxtRecordItem(const std::string& new_key, const std::string& new_value);
61  DnsTxtRecordItem(const std::string& new_key, const uint8_t* new_value, size_t new_value_len);
62  DnsTxtRecordItem(const std::string& new_key, const std::vector<uint8_t>& new_value);
63 };
64 
66 inline DnsTxtRecordItem::DnsTxtRecordItem(const char* new_key, const char* new_value)
67  : key(new_key), value(new_value, &new_value[std::strlen(new_value)])
68 {
69 }
70 
72 inline DnsTxtRecordItem::DnsTxtRecordItem(const char* new_key, const uint8_t* new_value, size_t new_value_len)
73  : key(new_key), value(new_value, new_value + new_value_len)
74 {
75 }
76 
78 inline DnsTxtRecordItem::DnsTxtRecordItem(const std::string& new_key, const std::string& new_value)
79  : key(new_key), value(new_value.begin(), new_value.end())
80 {
81 }
82 
84 inline DnsTxtRecordItem::DnsTxtRecordItem(const std::string& new_key, const uint8_t* new_value, size_t new_value_len)
85  : key(new_key), value(new_value, new_value + new_value_len)
86 {
87 }
88 
90 inline DnsTxtRecordItem::DnsTxtRecordItem(const std::string& new_key, const std::vector<uint8_t>& new_value)
91  : key(new_key), value(new_value)
92 {
93 }
94 
113 class Broker
114 {
115 public:
119  {
126 
128  std::string manufacturer{"Generic Manufacturer"};
130  std::string model{"Generic RDMnet Broker"};
131 
134  };
135 
137  struct Limits
138  {
140  unsigned int connections{0};
142  unsigned int controllers{0};
144  unsigned int controller_messages{500};
146  unsigned int devices{0};
148  unsigned int device_messages{500};
151  unsigned int reject_connections{1000};
152  };
153 
156  struct Settings
157  {
159  rdm::Uid uid;
162 
164  std::string scope{E133_DEFAULT_SCOPE};
168  bool allow_rdm_disable{true};
169 
172  uint16_t listen_port{0};
173 
180 
181  Settings() = default;
182  Settings(const etcpal::Uuid& cid_in, const rdm::Uid& static_uid_in);
183  Settings(const etcpal::Uuid& cid_in, uint16_t rdm_manu_id_in);
184 
186 
187  bool IsValid() const;
188  };
189 
193  {
194  public:
195  virtual ~NotifyHandler() = default;
196 
202  virtual void HandleScopeChanged(const std::string& new_scope) { ETCPAL_UNUSED_ARG(new_scope); }
203  };
204 
205  Broker();
206  virtual ~Broker();
207 
209  Broker(const Broker& other) = delete;
211  Broker& operator=(const Broker& other) = delete;
213  Broker(Broker&& other) = default;
215  Broker& operator=(Broker&& other) = default;
216 
217  etcpal::Error Startup(const Settings& settings, etcpal::Logger* logger = nullptr, NotifyHandler* notify = nullptr);
219  etcpal::Error ChangeScope(const std::string& new_scope, rdmnet_disconnect_reason_t disconnect_reason);
220 
221  const Settings& settings() const;
222 
223 private:
225 };
226 
229 {
230  dns.service_instance_name = "RDMnet Broker Instance " + cid.ToString();
231 }
232 
234 inline Broker::Settings::Settings(const etcpal::Uuid& cid_in, const rdm::Uid& static_uid_in)
235  : cid(cid_in), uid(static_uid_in)
236 {
238 }
239 
241 inline Broker::Settings::Settings(const etcpal::Uuid& cid_in, uint16_t rdm_manu_id_in)
242  : cid(cid_in), uid(rdm::Uid::DynamicUidRequest(rdm_manu_id_in))
243 {
245 }
246 
248 inline bool Broker::Settings::IsValid() const
249 {
250  // clang-format off
251  return (
252  !cid.IsNull() &&
253  (!scope.empty() && scope.length() < (E133_SCOPE_STRING_PADDED_LENGTH - 1)) &&
254  (!dns.manufacturer.empty() && dns.manufacturer.length() < (E133_MANUFACTURER_STRING_PADDED_LENGTH - 1)) &&
255  (!dns.model.empty() && dns.model.length() < (E133_MODEL_STRING_PADDED_LENGTH - 1)) &&
256  (!dns.service_instance_name.empty() && dns.service_instance_name.length() < (E133_SERVICE_NAME_STRING_PADDED_LENGTH - 1)) &&
257  (listen_port == 0 || listen_port >= 1024) &&
258  (uid.manufacturer_id() != 0) &&
259  (uid.IsStatic() || uid.IsDynamicUidRequest())
260  );
261  // clang-format on
262 }
263 
264 }; // namespace rdmnet
265 
267 
268 #endif // RDMNET_CPP_BROKER_H_
std::string ToString() const
A callback interface for notifications from the broker.
Definition: broker.h:193
virtual void HandleScopeChanged(const std::string &new_scope)
The scope of the broker has changed via RDMnet configuration.
Definition: broker.h:202
Defines an instance of RDMnet broker functionality.
Definition: broker.h:114
virtual ~Broker()
Destroys a broker instance. Call Broker::Shutdown() first.
Definition: broker_api.cpp:33
Broker & operator=(Broker &&other)=default
Move an instance of broker functionality.
void Shutdown(rdmnet_disconnect_reason_t disconnect_reason=kRdmnetDisconnectShutdown)
Shut down all broker functionality and threads.
Definition: broker_api.cpp:63
Broker & operator=(const Broker &other)=delete
Brokers cannot be copied.
Broker(Broker &&other)=default
Move an instance of broker functionality.
const Settings & settings() const
Get the current settings the broker is using.
Definition: broker_api.cpp:91
Broker()
Constructs a broker instance. Broker is not running until Broker::Startup() is called.
Definition: broker_api.cpp:28
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.
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:82
Functions and definitions common to all RDMnet API modules.
rdmnet_disconnect_reason_t
Disconnect reason defines for the BrokerDisconnectMsg.
Definition: common.h:85
@ kRdmnetDisconnectShutdown
The remote component is shutting down.
Definition: common.h:87
A namespace which contains all C++ language definitions in the RDMnet library.
Definition: broker.h:45
Settings for the Broker's DNS Discovery functionality.
Definition: broker.h:119
std::string service_instance_name
Your unique name for this broker DNS-SD service instance.
Definition: broker.h:125
std::string manufacturer
A string to identify the manufacturer of this broker instance.
Definition: broker.h:128
std::string model
A string to identify the model of product in which the broker instance is included.
Definition: broker.h:130
std::vector< DnsTxtRecordItem > additional_txt_record_items
Any additional non-standard items to add to the broker's DNS TXT record.
Definition: broker.h:133
A set of limits for broker operation.
Definition: broker.h:138
unsigned int connections
The maximum number of client connections supported. 0 means infinite.
Definition: broker.h:140
unsigned int controller_messages
The maximum number of queued messages per controller. 0 means infinite.
Definition: broker.h:144
unsigned int reject_connections
If you reach the number of max connections, this number of tcp-level connections are still supported ...
Definition: broker.h:151
unsigned int devices
The maximum number of devices allowed. 0 means infinite.
Definition: broker.h:146
unsigned int controllers
The maximum number of controllers allowed. 0 means infinite.
Definition: broker.h:142
unsigned int device_messages
The maximum number of queued messages per device. 0 means infinite.
Definition: broker.h:148
A group of settings for broker operation.
Definition: broker.h:157
Limits limits
The broker's limits.
Definition: broker.h:161
uint16_t listen_port
The port on which this broker should listen for incoming connections (and advertise via DNS).
Definition: broker.h:172
std::vector< std::string > listen_interfaces
A list of strings representing the system name of network interfaces to listen on.
Definition: broker.h:179
void SetDefaultServiceInstanceName()
Generate a DNS service instance name based on the broker's current CID.
Definition: broker.h:228
etcpal::Uuid cid
The broker's CID.
Definition: broker.h:158
bool allow_rdm_scope_change
Whether the broker should allow the scope to be changed via RDM commands.
Definition: broker.h:166
std::string scope
The RDMnet scope on which this broker should operate.
Definition: broker.h:164
bool IsValid() const
Whether this structure contains valid settings for broker operation.
Definition: broker.h:248
rdm::Uid uid
The broker's UID.
Definition: broker.h:159
bool allow_rdm_disable
Whether the broker should allow being disabled and enabled via the BROKER_STATUS RDM command.
Definition: broker.h:168
DnsAttributes dns
The broker's DNS attributes.
Definition: broker.h:160
A key/value pair representing a DNS TXT record item.
Definition: broker.h:53
std::vector< uint8_t > value
The value is opaque binary data.
Definition: broker.h:55
std::string key
The key is an ASCII-only string.
Definition: broker.h:54