8 #ifndef NTCORE_STORAGE_H_ 9 #define NTCORE_STORAGE_H_ 21 #include <llvm/DenseMap.h> 22 #include <llvm/SmallSet.h> 23 #include <llvm/StringMap.h> 24 #include <support/condition_variable.h> 25 #include <support/mutex.h> 29 #include "SequenceNumber.h" 30 #include "ntcore_cpp.h" 44 class INetworkConnection;
49 friend class StorageTest;
61 void SetDispatcher(
IDispatcher* dispatcher,
bool server)
override;
62 void ClearDispatcher()
override;
67 NT_Type GetMessageEntryType(
unsigned int id)
const override;
70 std::weak_ptr<INetworkConnection> conn_weak)
override;
71 void GetInitialAssignments(
73 std::vector<std::shared_ptr<Message>>* msgs)
override;
74 void ApplyInitialAssignments(
77 std::vector<std::shared_ptr<Message>>* out_msgs)
override;
82 std::shared_ptr<Value>
GetEntryValue(
unsigned int local_id)
const;
86 std::shared_ptr<Value> value);
89 bool SetEntryValue(
unsigned int local_id, std::shared_ptr<Value> value);
95 void SetEntryFlags(
unsigned int local_id,
unsigned int flags);
108 unsigned int AddListener(
111 unsigned int flags)
const;
112 unsigned int AddListener(
113 unsigned int local_id,
115 unsigned int flags)
const;
117 unsigned int AddPolledListener(
unsigned int poller_uid,
const Twine& prefix,
118 unsigned int flags)
const;
119 unsigned int AddPolledListener(
unsigned int poller_uid,
unsigned int local_id,
120 unsigned int flags)
const;
124 std::vector<unsigned int>
GetEntries(
const Twine& prefix,
unsigned int types);
133 bool periodic)
const override;
135 const Twine& filename,
136 std::function<
void(
size_t line,
const char* msg)> warn)
override;
141 std::function<
void(
size_t line,
const char* msg)> warn);
147 std::function<
void(
size_t line,
const char* msg)> warn);
155 bool GetRpcResult(
unsigned int local_id,
unsigned int call_uid,
156 std::string* result);
157 bool GetRpcResult(
unsigned int local_id,
unsigned int call_uid,
158 std::string* result,
double timeout,
bool* timed_out);
165 bool IsPersistent()
const {
return (flags & NT_PERSISTENT) != 0; }
172 std::shared_ptr<Value> value;
173 unsigned int flags{0};
178 unsigned int id{0xffff};
181 unsigned int local_id{UINT_MAX};
188 bool local_write{
false};
191 unsigned int rpc_uid{UINT_MAX};
195 unsigned int rpc_call_uid{0};
199 typedef std::vector<Entry*> IdMap;
200 typedef std::vector<std::unique_ptr<Entry>> LocalMap;
201 typedef std::pair<unsigned int, unsigned int> RpcIdPair;
205 mutable wpi::mutex m_mutex;
206 EntriesMap m_entries;
209 RpcResultMap m_rpc_results;
210 RpcBlockingCallSet m_rpc_blocking_calls;
212 mutable bool m_persistent_dirty =
false;
215 std::atomic_bool m_terminating;
216 wpi::condition_variable m_rpc_results_cond;
220 bool m_server =
true;
226 void ProcessIncomingEntryAssign(std::shared_ptr<Message> msg,
228 void ProcessIncomingEntryUpdate(std::shared_ptr<Message> msg,
230 void ProcessIncomingFlagsUpdate(std::shared_ptr<Message> msg,
232 void ProcessIncomingEntryDelete(std::shared_ptr<Message> msg,
234 void ProcessIncomingClearEntries(std::shared_ptr<Message> msg,
236 void ProcessIncomingExecuteRpc(std::shared_ptr<Message> msg,
238 std::weak_ptr<INetworkConnection> conn_weak);
239 void ProcessIncomingRpcResponse(std::shared_ptr<Message> msg,
242 bool GetPersistentEntries(
244 std::vector<std::pair<std::string, std::shared_ptr<Value>>>* entries)
247 std::vector<std::pair<std::string, std::shared_ptr<Value>>>*
249 void SetEntryValueImpl(Entry* entry, std::shared_ptr<Value> value,
250 std::unique_lock<wpi::mutex>& lock,
bool local);
251 void SetEntryFlagsImpl(Entry* entry,
unsigned int flags,
252 std::unique_lock<wpi::mutex>& lock,
bool local);
253 void DeleteEntryImpl(Entry* entry, std::unique_lock<wpi::mutex>& lock,
257 template <
typename F>
258 void DeleteAllEntriesImpl(
bool local, F should_delete);
259 void DeleteAllEntriesImpl(
bool local);
260 Entry* GetOrNew(
const Twine& name);
265 #endif // NTCORE_STORAGE_H_ 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:169
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
Definition: SocketError.cpp:17
Definition: IStorage.h:26
void CancelRpcResult(NT_Entry entry, NT_RpcCall call)
Ignore the result of a RPC call.
Definition: ntcore_cpp.cpp:619
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:32
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:21
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: IEntryNotifier.h:16
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:34
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
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
void DeleteEntry(StringRef name)
Delete Entry.
Definition: ntcore_cpp.cpp:183
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:33
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:42
Definition: IEntryNotifier.h:18
std::string GetEntryName(NT_Entry entry)
Gets the name of the specified entry.
Definition: ntcore_cpp.cpp:78