WPILibC++  unspecified
NetworkTableInstance.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2017. 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 NT_INSTANCE_H_
9 #define NT_INSTANCE_H_
10 
11 #include <functional>
12 #include <memory>
13 #include <string>
14 #include <vector>
15 
16 #include "llvm/ArrayRef.h"
17 #include "llvm/StringRef.h"
18 
19 #include "networktables/NetworkTable.h"
20 #include "networktables/NetworkTableEntry.h"
21 #include "ntcore_c.h"
22 #include "ntcore_cpp.h"
23 
24 #ifndef NT_NOEXCEPT
25 #ifdef _MSC_VER
26 #if _MSC_VER >= 1900
27 #define NT_NOEXCEPT noexcept
28 #else
29 #define NT_NOEXCEPT throw()
30 #endif
31 #else
32 #define NT_NOEXCEPT noexcept
33 #endif
34 #endif
35 
36 namespace nt {
37 
38 using llvm::ArrayRef;
39 using llvm::StringRef;
40 
59 class NetworkTableInstance final {
60  public:
65  enum NetworkMode {
66  kNetModeNone = NT_NET_MODE_NONE,
67  kNetModeServer = NT_NET_MODE_SERVER,
68  kNetModeClient = NT_NET_MODE_CLIENT,
69  kNetModeStarting = NT_NET_MODE_STARTING,
70  kNetModeFailure = NT_NET_MODE_FAILURE
71  };
72 
76  enum LogLevel {
77  kLogCritical = NT_LOG_CRITICAL,
78  kLogError = NT_LOG_ERROR,
79  kLogWarning = NT_LOG_WARNING,
80  kLogInfo = NT_LOG_INFO,
81  kLogDebug = NT_LOG_DEBUG,
82  kLogDebug1 = NT_LOG_DEBUG1,
83  kLogDebug2 = NT_LOG_DEBUG2,
84  kLogDebug3 = NT_LOG_DEBUG3,
85  kLogDebug4 = NT_LOG_DEBUG4
86  };
87 
91  enum { kDefaultPort = NT_DEFAULT_PORT };
92 
96  NetworkTableInstance() NT_NOEXCEPT;
97 
102  explicit NetworkTableInstance(NT_Inst inst) NT_NOEXCEPT;
103 
108  explicit operator bool() const { return m_handle != 0; }
109 
115 
120  static NetworkTableInstance Create();
121 
126  static void Destroy(NetworkTableInstance inst);
127 
132  NT_Inst GetHandle() const;
133 
140 
151  std::vector<NetworkTableEntry> GetEntries(StringRef prefix,
152  unsigned int types);
153 
164  std::vector<EntryInfo> GetEntryInfo(StringRef prefix,
165  unsigned int types) const;
166 
173  std::shared_ptr<NetworkTable> GetTable(StringRef key) const;
174 
179  void DeleteAllEntries();
180 
194  NT_EntryListener AddEntryListener(
195  StringRef prefix,
196  std::function<void(const EntryNotification& event)> callback,
197  unsigned int flags) const;
198 
203  static void RemoveEntryListener(NT_EntryListener entry_listener);
204 
214  bool WaitForEntryListenerQueue(double timeout);
215 
230  NT_ConnectionListener AddConnectionListener(
231  std::function<void(const ConnectionNotification& event)> callback,
232  bool immediate_notify) const;
233 
238  static void RemoveConnectionListener(NT_ConnectionListener conn_listener);
239 
249  bool WaitForConnectionListenerQueue(double timeout);
250 
267  bool WaitForRpcCallQueue(double timeout);
268 
282  void SetNetworkIdentity(StringRef name);
283 
288  unsigned int GetNetworkMode() const;
289 
299  void StartServer(StringRef persist_filename = "networktables.ini",
300  const char* listen_address = "",
301  unsigned int port = kDefaultPort);
302 
306  void StopServer();
307 
311  void StartClient();
312 
319  void StartClient(const char* server_name, unsigned int port = kDefaultPort);
320 
327  void StartClient(ArrayRef<std::pair<StringRef, unsigned int>> servers);
328 
336  void StartClient(ArrayRef<StringRef> servers,
337  unsigned int port = kDefaultPort);
338 
346  void StartClientTeam(unsigned int team, unsigned int port = kDefaultPort);
347 
351  void StopClient();
352 
359  void SetServer(const char* server_name, unsigned int port = kDefaultPort);
360 
367  void SetServer(ArrayRef<std::pair<StringRef, unsigned int>> servers);
368 
376  void SetServer(ArrayRef<StringRef> servers, unsigned int port = kDefaultPort);
377 
385  void SetServerTeam(unsigned int team, unsigned int port = kDefaultPort);
386 
394  void StartDSClient(unsigned int port = kDefaultPort);
395 
399  void StopDSClient();
400 
407  void SetUpdateRate(double interval);
408 
415  void Flush() const;
416 
422  std::vector<ConnectionInfo> GetConnections() const;
423 
428  bool IsConnected() const;
429 
444  const char* SavePersistent(StringRef filename) const;
445 
454  const char* LoadPersistent(
455  StringRef filename,
456  std::function<void(size_t line, const char* msg)> warn);
457 
465  const char* SaveEntries(StringRef filename, StringRef prefix) const;
466 
475  const char* LoadEntries(
476  StringRef filename, StringRef prefix,
477  std::function<void(size_t line, const char* msg)> warn);
478 
498  NT_Logger AddLogger(std::function<void(const LogMessage& msg)> func,
499  unsigned int min_level, unsigned int max_level);
500 
505  static void RemoveLogger(NT_Logger logger);
506 
516  bool WaitForLoggerQueue(double timeout);
517 
524  bool operator==(const NetworkTableInstance& other) const {
525  return m_handle == other.m_handle;
526  }
527 
529  bool operator!=(const NetworkTableInstance& other) const {
530  return !(*this == other);
531  }
532 
533  private:
534  /* Native handle */
535  NT_Inst m_handle;
536 };
537 
538 } // namespace nt
539 
540 #include "networktables/NetworkTableInstance.inl"
541 
542 #endif // NT_INSTANCE_H_
bool WaitForConnectionListenerQueue(double timeout)
Wait for the connection listener queue to be empty.
Definition: NetworkTableInstance.inl:67
bool operator==(const NetworkTableInstance &other) const
Equality operator.
Definition: NetworkTableInstance.h:524
const char * LoadEntries(StringRef filename, StringRef prefix, std::function< void(size_t line, const char *msg)> warn)
Load table values from a file.
Definition: NetworkTableInstance.inl:165
std::vector< ConnectionInfo > GetConnections() const
Get information on the currently established network connections.
Definition: NetworkTableInstance.inl:140
std::vector< EntryInfo > GetEntryInfo(StringRef prefix, unsigned int types) const
Get information about entries starting with the given prefix.
Definition: NetworkTableInstance.inl:44
void SetServerTeam(unsigned int team, unsigned int port=kDefaultPort)
Sets server addresses and port for client (without restarting client).
Definition: NetworkTableInstance.inl:121
void DeleteAllEntries()
Deletes ALL keys in ALL subtables (except persistent values).
Definition: NetworkTableInstance.inl:49
bool WaitForRpcCallQueue(double timeout)
Wait for the incoming RPC call queue to be empty.
Definition: NetworkTableInstance.inl:72
NetworkTables log message.
Definition: ntcore_cpp.h:232
void StopDSClient()
Stops requesting server address from Driver Station.
Definition: NetworkTableInstance.inl:130
bool WaitForEntryListenerQueue(double timeout)
Wait for the entry listener queue to be empty.
Definition: NetworkTableInstance.inl:58
void SetUpdateRate(double interval)
Set the periodic update rate.
Definition: NetworkTableInstance.inl:134
NetworkTables Entry Notification.
Definition: ntcore_cpp.h:165
static NetworkTableInstance GetDefault()
Get global default instance.
Definition: NetworkTableInstance.inl:18
void StartClientTeam(unsigned int team, unsigned int port=kDefaultPort)
Starts a client using commonly known robot addresses for the specified team.
Definition: NetworkTableInstance.inl:104
void StartDSClient(unsigned int port=kDefaultPort)
Starts requesting server address from Driver Station.
Definition: NetworkTableInstance.inl:126
void StopClient()
Stops the client if it is running.
Definition: NetworkTableInstance.inl:109
const char * LoadPersistent(StringRef filename, std::function< void(size_t line, const char *msg)> warn)
Load persistent values from a file.
Definition: NetworkTableInstance.inl:154
NetworkMode
Client/server mode flag values (as returned by GetNetworkMode()).
Definition: NetworkTableInstance.h:65
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:32
NetworkTables Instance.
Definition: NetworkTableInstance.h:59
bool operator!=(const NetworkTableInstance &other) const
Inequality operator.
Definition: NetworkTableInstance.h:529
static void RemoveEntryListener(NT_EntryListener entry_listener)
Remove an entry listener.
Definition: NetworkTableInstance.inl:53
NetworkTableInstance() NT_NOEXCEPT
Construct invalid instance.
Definition: NetworkTableInstance.inl:13
std::vector< NetworkTableEntry > GetEntries(StringRef prefix, unsigned int types)
Get entries starting with the given prefix.
Definition: NetworkTableInstance.inl:36
NetworkTableEntry GetEntry(StringRef name)
Gets the entry for a key.
Definition: NetworkTableInstance.inl:32
const char * SavePersistent(StringRef filename) const
Save persistent values to a file.
Definition: NetworkTableInstance.inl:149
void SetServer(const char *server_name, unsigned int port=kDefaultPort)
Sets server address and port for client (without restarting client).
Definition: NetworkTableInstance.inl:111
Definition: IEntryNotifier.h:15
static NetworkTableInstance Create()
Create an instance.
Definition: NetworkTableInstance.inl:22
LogLevel
Logging levels (as used by SetLogger()).
Definition: NetworkTableInstance.h:76
NT_Inst GetHandle() const
Gets the native handle for the entry.
Definition: NetworkTableInstance.inl:30
NT_EntryListener AddEntryListener(StringRef prefix, std::function< void(const EntryNotification &event)> callback, unsigned int flags) const
Add a listener for all entries starting with a certain prefix.
Definition: NetworkTableInstance.cpp:43
bool IsConnected() const
Return whether or not the instance is connected to another node.
Definition: NetworkTableInstance.inl:145
NetworkTables Connection Notification.
Definition: ntcore_cpp.h:206
std::shared_ptr< NetworkTable > GetTable(StringRef key) const
Gets the table with the specified key.
Definition: NetworkTableInstance.cpp:13
static void RemoveConnectionListener(NT_ConnectionListener conn_listener)
Remove a connection listener.
Definition: NetworkTableInstance.inl:62
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:171
void StartServer(StringRef 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:84
static void RemoveLogger(NT_Logger logger)
Remove a logger.
Definition: NetworkTableInstance.inl:177
const char * SaveEntries(StringRef filename, StringRef prefix) const
Save table values to a file.
Definition: NetworkTableInstance.inl:160
NT_ConnectionListener AddConnectionListener(std::function< void(const ConnectionNotification &event)> callback, bool immediate_notify) const
Add a connection listener.
Definition: NetworkTableInstance.cpp:50
void SetNetworkIdentity(StringRef name)
Set the network identity of this node.
Definition: NetworkTableInstance.inl:76
unsigned int GetNetworkMode() const
Get the current network mode.
Definition: NetworkTableInstance.inl:80
NetworkTables Entry.
Definition: NetworkTableEntry.h:30
static void Destroy(NetworkTableInstance inst)
Destroys an instance (note: this has global effect).
Definition: NetworkTableInstance.inl:26
void StartClient()
Starts a client.
Definition: NetworkTableInstance.inl:92
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:42
void Flush() const
Flushes all updated values immediately to the network.
Definition: NetworkTableInstance.inl:138
void StopServer()
Stops the server if it is running.
Definition: NetworkTableInstance.inl:90
bool WaitForLoggerQueue(double timeout)
Wait for the incoming log event queue to be empty.
Definition: NetworkTableInstance.inl:181