WPILibC++  unspecified
NetworkTableInstance.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2017-2018. 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 <llvm/ArrayRef.h>
18 #include <llvm/StringRef.h>
19 #include <llvm/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 #ifndef NT_NOEXCEPT
27 #ifdef _MSC_VER
28 #if _MSC_VER >= 1900
29 #define NT_NOEXCEPT noexcept
30 #else
31 #define NT_NOEXCEPT throw()
32 #endif
33 #else
34 #define NT_NOEXCEPT noexcept
35 #endif
36 #endif
37 
38 namespace nt {
39 
40 using llvm::ArrayRef;
41 using llvm::StringRef;
42 using llvm::Twine;
43 
62 class NetworkTableInstance final {
63  public:
68  enum NetworkMode {
69  kNetModeNone = NT_NET_MODE_NONE,
70  kNetModeServer = NT_NET_MODE_SERVER,
71  kNetModeClient = NT_NET_MODE_CLIENT,
72  kNetModeStarting = NT_NET_MODE_STARTING,
73  kNetModeFailure = NT_NET_MODE_FAILURE
74  };
75 
79  enum LogLevel {
80  kLogCritical = NT_LOG_CRITICAL,
81  kLogError = NT_LOG_ERROR,
82  kLogWarning = NT_LOG_WARNING,
83  kLogInfo = NT_LOG_INFO,
84  kLogDebug = NT_LOG_DEBUG,
85  kLogDebug1 = NT_LOG_DEBUG1,
86  kLogDebug2 = NT_LOG_DEBUG2,
87  kLogDebug3 = NT_LOG_DEBUG3,
88  kLogDebug4 = NT_LOG_DEBUG4
89  };
90 
94  enum { kDefaultPort = NT_DEFAULT_PORT };
95 
99  NetworkTableInstance() NT_NOEXCEPT;
100 
105  explicit NetworkTableInstance(NT_Inst inst) NT_NOEXCEPT;
106 
111  explicit operator bool() const { return m_handle != 0; }
112 
118 
123  static NetworkTableInstance Create();
124 
129  static void Destroy(NetworkTableInstance inst);
130 
135  NT_Inst GetHandle() const;
136 
142  NetworkTableEntry GetEntry(const Twine& name);
143 
154  std::vector<NetworkTableEntry> GetEntries(const Twine& prefix,
155  unsigned int types);
156 
167  std::vector<EntryInfo> GetEntryInfo(const Twine& prefix,
168  unsigned int types) const;
169 
176  std::shared_ptr<NetworkTable> GetTable(const Twine& key) const;
177 
182  void DeleteAllEntries();
183 
197  NT_EntryListener AddEntryListener(
198  const Twine& prefix,
199  std::function<void(const EntryNotification& event)> callback,
200  unsigned int flags) const;
201 
206  static void RemoveEntryListener(NT_EntryListener entry_listener);
207 
217  bool WaitForEntryListenerQueue(double timeout);
218 
233  NT_ConnectionListener AddConnectionListener(
234  std::function<void(const ConnectionNotification& event)> callback,
235  bool immediate_notify) const;
236 
241  static void RemoveConnectionListener(NT_ConnectionListener conn_listener);
242 
254  bool WaitForConnectionListenerQueue(double timeout);
255 
272  bool WaitForRpcCallQueue(double timeout);
273 
287  void SetNetworkIdentity(const Twine& name);
288 
293  unsigned int GetNetworkMode() const;
294 
304  void StartServer(const Twine& persist_filename = "networktables.ini",
305  const char* listen_address = "",
306  unsigned int port = kDefaultPort);
307 
311  void StopServer();
312 
316  void StartClient();
317 
324  void StartClient(const char* server_name, unsigned int port = kDefaultPort);
325 
332  void StartClient(ArrayRef<std::pair<StringRef, unsigned int>> servers);
333 
341  void StartClient(ArrayRef<StringRef> servers,
342  unsigned int port = kDefaultPort);
343 
351  void StartClientTeam(unsigned int team, unsigned int port = kDefaultPort);
352 
356  void StopClient();
357 
364  void SetServer(const char* server_name, unsigned int port = kDefaultPort);
365 
372  void SetServer(ArrayRef<std::pair<StringRef, unsigned int>> servers);
373 
381  void SetServer(ArrayRef<StringRef> servers, unsigned int port = kDefaultPort);
382 
390  void SetServerTeam(unsigned int team, unsigned int port = kDefaultPort);
391 
399  void StartDSClient(unsigned int port = kDefaultPort);
400 
404  void StopDSClient();
405 
412  void SetUpdateRate(double interval);
413 
420  void Flush() const;
421 
427  std::vector<ConnectionInfo> GetConnections() const;
428 
433  bool IsConnected() const;
434 
449  const char* SavePersistent(const Twine& filename) const;
450 
459  const char* LoadPersistent(
460  const Twine& filename,
461  std::function<void(size_t line, const char* msg)> warn);
462 
470  const char* SaveEntries(const Twine& filename, const Twine& prefix) const;
471 
480  const char* LoadEntries(
481  const Twine& filename, const Twine& prefix,
482  std::function<void(size_t line, const char* msg)> warn);
483 
503  NT_Logger AddLogger(std::function<void(const LogMessage& msg)> func,
504  unsigned int min_level, unsigned int max_level);
505 
510  static void RemoveLogger(NT_Logger logger);
511 
522  bool WaitForLoggerQueue(double timeout);
523 
530  bool operator==(const NetworkTableInstance& other) const {
531  return m_handle == other.m_handle;
532  }
533 
535  bool operator!=(const NetworkTableInstance& other) const {
536  return !(*this == other);
537  }
538 
539  private:
540  /* Native handle */
541  NT_Inst m_handle;
542 };
543 
544 } // namespace nt
545 
546 #include "networktables/NetworkTableInstance.inl"
547 
548 #endif // NTCORE_NETWORKTABLES_NETWORKTABLEINSTANCE_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:530
std::vector< ConnectionInfo > GetConnections() const
Get information on the currently established network connections.
Definition: NetworkTableInstance.inl:140
const char * SaveEntries(const Twine &filename, const Twine &prefix) const
Save table values to a file.
Definition: NetworkTableInstance.inl:160
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.
Definition: NetworkTableInstance.cpp:48
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
std::vector< EntryInfo > GetEntryInfo(const Twine &prefix, unsigned int types) const
Get information about entries starting with the given prefix.
Definition: NetworkTableInstance.inl:44
std::shared_ptr< NetworkTable > GetTable(const Twine &key) const
Gets the table with the specified key.
Definition: NetworkTableInstance.cpp:14
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:236
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:169
static NetworkTableInstance GetDefault()
Get global default instance.
Definition: NetworkTableInstance.inl:18
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
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 * SavePersistent(const Twine &filename) const
Save persistent values to a file.
Definition: NetworkTableInstance.inl:149
NetworkMode
Client/server mode flag values (as returned by GetNetworkMode()).
Definition: NetworkTableInstance.h:68
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:154
NetworkTableEntry GetEntry(const Twine &name)
Gets the entry for a key.
Definition: NetworkTableInstance.inl:32
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:32
NetworkTables Instance.
Definition: NetworkTableInstance.h:62
bool operator!=(const NetworkTableInstance &other) const
Inequality operator.
Definition: NetworkTableInstance.h:535
void SetNetworkIdentity(const Twine &name)
Set the network identity of this node.
Definition: NetworkTableInstance.inl:76
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
void SetServer(const char *server_name, unsigned int port=kDefaultPort)
Sets server address and port for client (without restarting client).
Definition: NetworkTableInstance.inl:111
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:165
Definition: IEntryNotifier.h:16
static NetworkTableInstance Create()
Create an instance.
Definition: NetworkTableInstance.inl:22
LogLevel
Logging levels (as used by SetLogger()).
Definition: NetworkTableInstance.h:79
NT_Inst GetHandle() const
Gets the native handle for the entry.
Definition: NetworkTableInstance.inl:30
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:84
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:210
std::vector< NetworkTableEntry > GetEntries(const Twine &prefix, unsigned int types)
Get entries starting with the given prefix.
Definition: NetworkTableInstance.inl:36
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
static void RemoveLogger(NT_Logger logger)
Remove a logger.
Definition: NetworkTableInstance.inl:177
NT_ConnectionListener AddConnectionListener(std::function< void(const ConnectionNotification &event)> callback, bool immediate_notify) const
Add a connection listener.
Definition: NetworkTableInstance.cpp:55
unsigned int GetNetworkMode() const
Get the current network mode.
Definition: NetworkTableInstance.inl:80
NetworkTables Entry.
Definition: NetworkTableEntry.h:35
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