8 #ifndef NTCORE_STORAGE_H_ 9 #define NTCORE_STORAGE_H_ 21 #include <wpi/DenseMap.h> 22 #include <wpi/SmallSet.h> 23 #include <wpi/StringMap.h> 24 #include <wpi/condition_variable.h> 25 #include <wpi/mutex.h> 29 #include "SequenceNumber.h" 30 #include "ntcore_cpp.h" 41 class INetworkConnection;
46 friend class StorageTest;
58 void SetDispatcher(
IDispatcher* dispatcher,
bool server)
override;
59 void ClearDispatcher()
override;
64 NT_Type GetMessageEntryType(
unsigned int id)
const override;
67 std::weak_ptr<INetworkConnection> conn_weak)
override;
68 void GetInitialAssignments(
70 std::vector<std::shared_ptr<Message>>* msgs)
override;
71 void ApplyInitialAssignments(
74 std::vector<std::shared_ptr<Message>>* out_msgs)
override;
79 std::shared_ptr<Value>
GetEntryValue(
unsigned int local_id)
const;
83 std::shared_ptr<Value> value);
86 bool SetEntryValue(
unsigned int local_id, std::shared_ptr<Value> value);
92 void SetEntryFlags(
unsigned int local_id,
unsigned int flags);
105 unsigned int AddListener(
108 unsigned int flags)
const;
109 unsigned int AddListener(
110 unsigned int local_id,
112 unsigned int flags)
const;
114 unsigned int AddPolledListener(
unsigned int poller_uid,
const Twine& prefix,
115 unsigned int flags)
const;
116 unsigned int AddPolledListener(
unsigned int poller_uid,
unsigned int local_id,
117 unsigned int flags)
const;
121 std::vector<unsigned int>
GetEntries(
const Twine& prefix,
unsigned int types);
130 bool periodic)
const override;
132 const Twine& filename,
133 std::function<
void(
size_t line,
const char* msg)> warn)
override;
138 std::function<
void(
size_t line,
const char* msg)> warn);
144 std::function<
void(
size_t line,
const char* msg)> warn);
152 bool GetRpcResult(
unsigned int local_id,
unsigned int call_uid,
153 std::string* result);
154 bool GetRpcResult(
unsigned int local_id,
unsigned int call_uid,
155 std::string* result,
double timeout,
bool* timed_out);
162 bool IsPersistent()
const {
return (flags & NT_PERSISTENT) != 0; }
169 std::shared_ptr<Value> value;
170 unsigned int flags{0};
175 unsigned int id{0xffff};
178 unsigned int local_id{UINT_MAX};
185 bool local_write{
false};
188 unsigned int rpc_uid{UINT_MAX};
192 unsigned int rpc_call_uid{0};
196 typedef std::vector<Entry*> IdMap;
197 typedef std::vector<std::unique_ptr<Entry>> LocalMap;
198 typedef std::pair<unsigned int, unsigned int> RpcIdPair;
202 mutable wpi::mutex m_mutex;
203 EntriesMap m_entries;
206 RpcResultMap m_rpc_results;
207 RpcBlockingCallSet m_rpc_blocking_calls;
209 mutable bool m_persistent_dirty =
false;
212 std::atomic_bool m_terminating;
213 wpi::condition_variable m_rpc_results_cond;
217 bool m_server =
true;
223 void ProcessIncomingEntryAssign(std::shared_ptr<Message> msg,
225 void ProcessIncomingEntryUpdate(std::shared_ptr<Message> msg,
227 void ProcessIncomingFlagsUpdate(std::shared_ptr<Message> msg,
229 void ProcessIncomingEntryDelete(std::shared_ptr<Message> msg,
231 void ProcessIncomingClearEntries(std::shared_ptr<Message> msg,
233 void ProcessIncomingExecuteRpc(std::shared_ptr<Message> msg,
235 std::weak_ptr<INetworkConnection> conn_weak);
236 void ProcessIncomingRpcResponse(std::shared_ptr<Message> msg,
239 bool GetPersistentEntries(
241 std::vector<std::pair<std::string, std::shared_ptr<Value>>>* entries)
244 std::vector<std::pair<std::string, std::shared_ptr<Value>>>*
246 void SetEntryValueImpl(Entry* entry, std::shared_ptr<Value> value,
247 std::unique_lock<wpi::mutex>& lock,
bool local);
248 void SetEntryFlagsImpl(Entry* entry,
unsigned int flags,
249 std::unique_lock<wpi::mutex>& lock,
bool local);
250 void DeleteEntryImpl(Entry* entry, std::unique_lock<wpi::mutex>& lock,
254 template <
typename F>
255 void DeleteAllEntriesImpl(
bool local, F should_delete);
256 void DeleteAllEntriesImpl(
bool local);
257 Entry* GetOrNew(
const Twine& name);
262 #endif // NTCORE_STORAGE_H_ This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
const char * SaveEntries(NT_Inst inst, const Twine &filename, const Twine &prefix)
Save table values to a file.
Definition: ntcore_cpp.cpp:941
void SetEntryFlags(StringRef name, unsigned int flags)
Set Entry Flags.
Definition: ntcore_cpp.cpp:157
bool SetEntryValue(StringRef name, std::shared_ptr< Value > value)
Set Entry Value.
Definition: ntcore_cpp.cpp:131
unsigned int GetEntryFlags(StringRef name)
Get Entry Flags.
Definition: ntcore_cpp.cpp:170
bool SetDefaultEntryValue(StringRef name, std::shared_ptr< Value > value)
Set Default Entry Value Returns copy of current entry value if it exists.
Definition: ntcore_cpp.cpp:118
NetworkTables Entry Notification.
Definition: ntcore_cpp.h:171
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:41
namespace to hold default to_json function
Definition: json_binary_writer.cpp:39
Definition: IStorage.h:26
void CancelRpcResult(NT_Entry entry, NT_RpcCall call)
Ignore the result of a RPC call.
Definition: ntcore_cpp.cpp:619
NT_Entry GetEntry(NT_Inst inst, const Twine &name)
Get Entry Handle.
Definition: ntcore_cpp.cpp:56
const char * LoadEntries(NT_Inst inst, const Twine &filename, const Twine &prefix, std::function< void(size_t line, const char *msg)> warn)
Load table values from a file.
Definition: ntcore_cpp.cpp:949
NT_RpcCall CallRpc(NT_Entry entry, StringRef params)
Call a RPC function.
Definition: ntcore_cpp.cpp:577
void SetEntryTypeValue(StringRef name, std::shared_ptr< Value > value)
Set Entry Type and Value.
Definition: ntcore_cpp.cpp:144
const char * LoadPersistent(StringRef filename, std::function< void(size_t line, const char *msg)> warn)
Load persistent values from a file.
Definition: ntcore_cpp.cpp:926
void CreateRpc(NT_Entry entry, StringRef def, std::function< void(const RpcAnswer &answer)> callback)
Create a callback-based RPC entry point.
Definition: ntcore_cpp.cpp:478
Definition: raw_istream.h:26
std::vector< NT_Entry > GetEntries(NT_Inst inst, const Twine &prefix, unsigned int types)
Get Entry Handles.
Definition: ntcore_cpp.cpp:66
Definition: SequenceNumber.h:14
Definition: IStorage.h:21
NT_Type GetEntryType(NT_Entry entry)
Gets the type for the specified entry, or unassigned if non existent.
Definition: ntcore_cpp.cpp:87
NetworkTables Entry Information.
Definition: ntcore_cpp.h:35
std::vector< EntryInfo > GetEntryInfo(StringRef prefix, unsigned int types)
Get Entry Information.
Definition: ntcore_cpp.cpp:208
const char * SavePersistent(StringRef filename)
Save persistent values to a file.
Definition: ntcore_cpp.cpp:915
Definition: IDispatcher.h:21
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void DeleteAllEntries()
Delete All Entries.
Definition: ntcore_cpp.cpp:196
std::shared_ptr< Value > GetEntryValue(StringRef name)
Get Entry Value.
Definition: ntcore_cpp.cpp:105
Definition: IRpcServer.h:18
uint64_t GetEntryLastChange(NT_Entry entry)
Gets the last time the entry was changed.
Definition: ntcore_cpp.cpp:96
Definition: INetworkConnection.h:18
bool GetRpcResult(NT_Entry entry, NT_RpcCall call, std::string *result)
Get the result (return value) of a RPC call.
Definition: ntcore_cpp.cpp:589
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
void DeleteEntry(StringRef name)
Delete Entry.
Definition: ntcore_cpp.cpp:183
Definition: IEntryNotifier.h:18
std::string GetEntryName(NT_Entry entry)
Gets the name of the specified entry.
Definition: ntcore_cpp.cpp:78