WPILibC++  2020.3.2
NetworkTableInstance.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef NTCORE_NETWORKTABLES_NETWORKTABLEINSTANCE_H_
9 #define NTCORE_NETWORKTABLES_NETWORKTABLEINSTANCE_H_
10 
11 #include <functional>
12 #include <memory>
13 #include <string>
14 #include <utility>
15 #include <vector>
16 
17 #include <wpi/ArrayRef.h>
18 #include <wpi/StringRef.h>
19 #include <wpi/Twine.h>
20 
21 #include "networktables/NetworkTable.h"
22 #include "networktables/NetworkTableEntry.h"
23 #include "ntcore_c.h"
24 #include "ntcore_cpp.h"
25 
26 namespace nt {
27 
28 using wpi::ArrayRef;
29 using wpi::StringRef;
30 using wpi::Twine;
31 
53 class NetworkTableInstance final {
54  public:
59  enum NetworkMode {
60  kNetModeNone = NT_NET_MODE_NONE,
61  kNetModeServer = NT_NET_MODE_SERVER,
62  kNetModeClient = NT_NET_MODE_CLIENT,
63  kNetModeStarting = NT_NET_MODE_STARTING,
64  kNetModeFailure = NT_NET_MODE_FAILURE,
65  kNetModeLocal = NT_NET_MODE_LOCAL
66  };
67 
71  enum LogLevel {
72  kLogCritical = NT_LOG_CRITICAL,
73  kLogError = NT_LOG_ERROR,
74  kLogWarning = NT_LOG_WARNING,
75  kLogInfo = NT_LOG_INFO,
76  kLogDebug = NT_LOG_DEBUG,
77  kLogDebug1 = NT_LOG_DEBUG1,
78  kLogDebug2 = NT_LOG_DEBUG2,
79  kLogDebug3 = NT_LOG_DEBUG3,
80  kLogDebug4 = NT_LOG_DEBUG4
81  };
82 
86  enum { kDefaultPort = NT_DEFAULT_PORT };
87 
91  NetworkTableInstance() noexcept;
92 
98  explicit NetworkTableInstance(NT_Inst inst) noexcept;
99 
105  explicit operator bool() const { return m_handle != 0; }
106 
113 
119  static NetworkTableInstance Create();
120 
126  static void Destroy(NetworkTableInstance inst);
127 
133  NT_Inst GetHandle() const;
134 
141  NetworkTableEntry GetEntry(const Twine& name);
142 
154  std::vector<NetworkTableEntry> GetEntries(const Twine& prefix,
155  unsigned int types);
156 
168  std::vector<EntryInfo> GetEntryInfo(const Twine& prefix,
169  unsigned int types) const;
170 
177  std::shared_ptr<NetworkTable> GetTable(const Twine& key) const;
178 
183  void DeleteAllEntries();
184 
198  NT_EntryListener AddEntryListener(
199  const Twine& prefix,
200  std::function<void(const EntryNotification& event)> callback,
201  unsigned int flags) const;
202 
208  static void RemoveEntryListener(NT_EntryListener entry_listener);
209 
220  bool WaitForEntryListenerQueue(double timeout);
221 
236  NT_ConnectionListener AddConnectionListener(
237  std::function<void(const ConnectionNotification& event)> callback,
238  bool immediate_notify) const;
239 
245  static void RemoveConnectionListener(NT_ConnectionListener conn_listener);
246 
257  bool WaitForConnectionListenerQueue(double timeout);
258 
276  bool WaitForRpcCallQueue(double timeout);
277 
293  void SetNetworkIdentity(const Twine& name);
294 
300  unsigned int GetNetworkMode() const;
301 
307  void StartLocal();
308 
313  void StopLocal();
314 
324  void StartServer(const Twine& persist_filename = "networktables.ini",
325  const char* listen_address = "",
326  unsigned int port = kDefaultPort);
327 
331  void StopServer();
332 
336  void StartClient();
337 
344  void StartClient(const char* server_name, unsigned int port = kDefaultPort);
345 
352  void StartClient(ArrayRef<std::pair<StringRef, unsigned int>> servers);
353 
361  void StartClient(ArrayRef<StringRef> servers,
362  unsigned int port = kDefaultPort);
363 
371  void StartClientTeam(unsigned int team, unsigned int port = kDefaultPort);
372 
376  void StopClient();
377 
384  void SetServer(const char* server_name, unsigned int port = kDefaultPort);
385 
392  void SetServer(ArrayRef<std::pair<StringRef, unsigned int>> servers);
393 
401  void SetServer(ArrayRef<StringRef> servers, unsigned int port = kDefaultPort);
402 
410  void SetServerTeam(unsigned int team, unsigned int port = kDefaultPort);
411 
419  void StartDSClient(unsigned int port = kDefaultPort);
420 
424  void StopDSClient();
425 
432  void SetUpdateRate(double interval);
433 
440  void Flush() const;
441 
448  std::vector<ConnectionInfo> GetConnections() const;
449 
455  bool IsConnected() const;
456 
472  const char* SavePersistent(const Twine& filename) const;
473 
483  const char* LoadPersistent(
484  const Twine& filename,
485  std::function<void(size_t line, const char* msg)> warn);
486 
495  const char* SaveEntries(const Twine& filename, const Twine& prefix) const;
496 
506  const char* LoadEntries(
507  const Twine& filename, const Twine& prefix,
508  std::function<void(size_t line, const char* msg)> warn);
509 
529  NT_Logger AddLogger(std::function<void(const LogMessage& msg)> func,
530  unsigned int min_level, unsigned int max_level);
531 
537  static void RemoveLogger(NT_Logger logger);
538 
549  bool WaitForLoggerQueue(double timeout);
550 
557  bool operator==(const NetworkTableInstance& other) const {
558  return m_handle == other.m_handle;
559  }
560 
562  bool operator!=(const NetworkTableInstance& other) const {
563  return !(*this == other);
564  }
565 
566  private:
567  /* Native handle */
568  NT_Inst m_handle;
569 };
570 
571 } // namespace nt
572 
573 #include "networktables/NetworkTableInstance.inl"
574 
575 #endif // NTCORE_NETWORKTABLES_NETWORKTABLEINSTANCE_H_
nt::NetworkTableInstance
NetworkTables Instance.
Definition: NetworkTableInstance.h:53
nt::NetworkTableInstance::RemoveEntryListener
static void RemoveEntryListener(NT_EntryListener entry_listener)
Remove an entry listener.
Definition: NetworkTableInstance.inl:53
nt::NetworkTableInstance::WaitForEntryListenerQueue
bool WaitForEntryListenerQueue(double timeout)
Wait for the entry listener queue to be empty.
Definition: NetworkTableInstance.inl:58
nt::NetworkTableInstance::StopLocal
void StopLocal()
Stops local-only operation.
Definition: NetworkTableInstance.inl:86
nt::NetworkTableInstance::StartLocal
void StartLocal()
Starts local-only operation.
Definition: NetworkTableInstance.inl:84
nt::NetworkTableInstance::GetNetworkMode
unsigned int GetNetworkMode() const
Get the current network mode.
Definition: NetworkTableInstance.inl:80
nt::NetworkTableInstance::GetEntryInfo
std::vector< EntryInfo > GetEntryInfo(const Twine &prefix, unsigned int types) const
Get information about entries starting with the given prefix.
Definition: NetworkTableInstance.inl:44
nt::NetworkTableInstance::StopClient
void StopClient()
Stops the client if it is running.
Definition: NetworkTableInstance.inl:113
nt::NetworkTableInstance::Destroy
static void Destroy(NetworkTableInstance inst)
Destroys an instance (note: this has global effect).
Definition: NetworkTableInstance.inl:26
nt::NetworkTableInstance::GetEntry
NetworkTableEntry GetEntry(const Twine &name)
Gets the entry for a key.
Definition: NetworkTableInstance.inl:32
wpi::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:42
nt::NetworkTableInstance::RemoveLogger
static void RemoveLogger(NT_Logger logger)
Remove a logger.
Definition: NetworkTableInstance.inl:181
nt::NetworkTableInstance::LoadPersistent
const char * LoadPersistent(const Twine &filename, std::function< void(size_t line, const char *msg)> warn)
Load persistent values from a file.
Definition: NetworkTableInstance.inl:158
nt::NetworkTableInstance::NetworkTableInstance
NetworkTableInstance() noexcept
Construct invalid instance.
Definition: NetworkTableInstance.inl:13
nt::NetworkTableEntry
NetworkTables Entry.
Definition: NetworkTableEntry.h:39
nt::NetworkTableInstance::operator!=
bool operator!=(const NetworkTableInstance &other) const
Inequality operator.
Definition: NetworkTableInstance.h:562
nt::NetworkTableInstance::Create
static NetworkTableInstance Create()
Create an instance.
Definition: NetworkTableInstance.inl:22
nt::LogMessage
NetworkTables log message.
Definition: ntcore_cpp.h:247
nt::EntryNotification
NetworkTables Entry Notification.
Definition: ntcore_cpp.h:180
nt::NetworkTableInstance::SaveEntries
const char * SaveEntries(const Twine &filename, const Twine &prefix) const
Save table values to a file.
Definition: NetworkTableInstance.inl:164
nt::NetworkTableInstance::AddConnectionListener
NT_ConnectionListener AddConnectionListener(std::function< void(const ConnectionNotification &event)> callback, bool immediate_notify) const
Add a connection listener.
nt::NetworkTableInstance::WaitForRpcCallQueue
bool WaitForRpcCallQueue(double timeout)
Wait for the incoming RPC call queue to be empty.
Definition: NetworkTableInstance.inl:72
nt::NetworkTableInstance::SetUpdateRate
void SetUpdateRate(double interval)
Set the periodic update rate.
Definition: NetworkTableInstance.inl:138
nt::NetworkTableInstance::GetDefault
static NetworkTableInstance GetDefault()
Get global default instance.
Definition: NetworkTableInstance.inl:18
nt::ConnectionNotification
NetworkTables Connection Notification.
Definition: ntcore_cpp.h:221
nt::NetworkTableInstance::StartDSClient
void StartDSClient(unsigned int port=kDefaultPort)
Starts requesting server address from Driver Station.
Definition: NetworkTableInstance.inl:130
nt::NetworkTableInstance::StartClient
void StartClient()
Starts a client.
Definition: NetworkTableInstance.inl:96
nt::NetworkTableInstance::LoadEntries
const char * LoadEntries(const Twine &filename, const Twine &prefix, std::function< void(size_t line, const char *msg)> warn)
Load table values from a file.
Definition: NetworkTableInstance.inl:169
wpi::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
nt::NetworkTableInstance::SetServer
void SetServer(const char *server_name, unsigned int port=kDefaultPort)
Sets server address and port for client (without restarting client).
Definition: NetworkTableInstance.inl:115
nt::NetworkTableInstance::GetTable
std::shared_ptr< NetworkTable > GetTable(const Twine &key) const
Gets the table with the specified key.
nt::NetworkTableInstance::GetHandle
NT_Inst GetHandle() const
Gets the native handle for the entry.
Definition: NetworkTableInstance.inl:30
nt::NetworkTableInstance::StartClientTeam
void StartClientTeam(unsigned int team, unsigned int port=kDefaultPort)
Starts a client using commonly known robot addresses for the specified team.
Definition: NetworkTableInstance.inl:108
nt::NetworkTableInstance::SetNetworkIdentity
void SetNetworkIdentity(const Twine &name)
Set the network identity of this node.
Definition: NetworkTableInstance.inl:76
nt::NetworkTableInstance::IsConnected
bool IsConnected() const
Return whether or not the instance is connected to another node.
Definition: NetworkTableInstance.inl:149
nt::NetworkTableInstance::AddLogger
NT_Logger AddLogger(std::function< void(const LogMessage &msg)> func, unsigned int min_level, unsigned int max_level)
Add logger callback function.
Definition: NetworkTableInstance.inl:175
nt::NetworkTableInstance::SavePersistent
const char * SavePersistent(const Twine &filename) const
Save persistent values to a file.
Definition: NetworkTableInstance.inl:153
nt::NetworkTableInstance::NetworkMode
NetworkMode
Client/server mode flag values (as returned by GetNetworkMode()).
Definition: NetworkTableInstance.h:59
nt::NetworkTableInstance::DeleteAllEntries
void DeleteAllEntries()
Deletes ALL keys in ALL subtables (except persistent values).
Definition: NetworkTableInstance.inl:49
nt::NetworkTableInstance::StopDSClient
void StopDSClient()
Stops requesting server address from Driver Station.
Definition: NetworkTableInstance.inl:134
nt::NetworkTableInstance::Flush
void Flush() const
Flushes all updated values immediately to the network.
Definition: NetworkTableInstance.inl:142
nt::NetworkTableInstance::StopServer
void StopServer()
Stops the server if it is running.
Definition: NetworkTableInstance.inl:94
nt::NetworkTableInstance::LogLevel
LogLevel
Logging levels (as used by SetLogger()).
Definition: NetworkTableInstance.h:71
NT_DEFAULT_PORT
#define NT_DEFAULT_PORT
Default network tables port number.
Definition: ntcore_c.h:49
nt::NetworkTableInstance::operator==
bool operator==(const NetworkTableInstance &other) const
Equality operator.
Definition: NetworkTableInstance.h:557
nt::NetworkTableInstance::WaitForLoggerQueue
bool WaitForLoggerQueue(double timeout)
Wait for the incoming log event queue to be empty.
Definition: NetworkTableInstance.inl:185
nt::NetworkTableInstance::SetServerTeam
void SetServerTeam(unsigned int team, unsigned int port=kDefaultPort)
Sets server addresses and port for client (without restarting client).
Definition: NetworkTableInstance.inl:125
nt::NetworkTableInstance::StartServer
void StartServer(const Twine &persist_filename="networktables.ini", const char *listen_address="", unsigned int port=kDefaultPort)
Starts a server using the specified filename, listening address, and port.
Definition: NetworkTableInstance.inl:88
nt::NetworkTableInstance::AddEntryListener
NT_EntryListener AddEntryListener(const Twine &prefix, std::function< void(const EntryNotification &event)> callback, unsigned int flags) const
Add a listener for all entries starting with a certain prefix.
nt::NetworkTableInstance::GetConnections
std::vector< ConnectionInfo > GetConnections() const
Get information on the currently established network connections.
Definition: NetworkTableInstance.inl:144
nt::NetworkTableInstance::GetEntries
std::vector< NetworkTableEntry > GetEntries(const Twine &prefix, unsigned int types)
Get entries starting with the given prefix.
Definition: NetworkTableInstance.inl:36
wpi::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:85
nt
NetworkTables (ntcore) namespace.
Definition: NetworkTableValue.h:27
nt::NetworkTableInstance::RemoveConnectionListener
static void RemoveConnectionListener(NT_ConnectionListener conn_listener)
Remove a connection listener.
Definition: NetworkTableInstance.inl:62
nt::NetworkTableInstance::WaitForConnectionListenerQueue
bool WaitForConnectionListenerQueue(double timeout)
Wait for the connection listener queue to be empty.
Definition: NetworkTableInstance.inl:67