2015-06-21 23:42:29 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) FIRST 2015. All Rights Reserved. */
|
|
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#ifndef NT_STORAGE_H_
|
|
|
|
|
#define NT_STORAGE_H_
|
|
|
|
|
|
2015-07-19 16:02:21 -07:00
|
|
|
#include <atomic>
|
2015-06-27 10:22:59 -07:00
|
|
|
#include <cstddef>
|
2015-07-17 07:21:07 -07:00
|
|
|
#include <functional>
|
2015-07-14 23:15:08 -07:00
|
|
|
#include <iosfwd>
|
|
|
|
|
#include <memory>
|
2015-07-18 01:29:51 -07:00
|
|
|
#include <mutex>
|
2015-08-01 16:33:39 -07:00
|
|
|
#include <vector>
|
2015-06-27 10:22:59 -07:00
|
|
|
|
2015-08-13 13:12:15 -07:00
|
|
|
#include "llvm/DenseMap.h"
|
2015-06-21 23:42:29 -07:00
|
|
|
#include "llvm/StringMap.h"
|
2015-07-20 23:36:22 -07:00
|
|
|
#include "atomic_static.h"
|
2015-07-29 23:45:04 -07:00
|
|
|
#include "Message.h"
|
2015-08-02 21:47:01 -07:00
|
|
|
#include "Notifier.h"
|
2015-07-18 01:29:51 -07:00
|
|
|
#include "ntcore_cpp.h"
|
2015-08-13 13:12:15 -07:00
|
|
|
#include "RpcServer.h"
|
2015-07-17 23:41:25 -07:00
|
|
|
#include "SequenceNumber.h"
|
2015-06-21 23:42:29 -07:00
|
|
|
|
2015-07-17 07:21:07 -07:00
|
|
|
namespace nt {
|
2015-06-21 23:42:29 -07:00
|
|
|
|
2015-07-29 23:45:04 -07:00
|
|
|
class NetworkConnection;
|
2015-07-19 16:36:30 -07:00
|
|
|
class StorageTest;
|
|
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
class Storage {
|
2015-07-19 16:36:30 -07:00
|
|
|
friend class StorageTest;
|
2015-06-25 22:57:43 -07:00
|
|
|
public:
|
|
|
|
|
static Storage& GetInstance() {
|
2015-07-20 23:36:22 -07:00
|
|
|
ATOMIC_STATIC(Storage, instance);
|
|
|
|
|
return instance;
|
2015-06-25 22:57:43 -07:00
|
|
|
}
|
2015-07-14 23:15:08 -07:00
|
|
|
~Storage();
|
2015-06-21 23:42:29 -07:00
|
|
|
|
2015-07-31 13:21:19 -07:00
|
|
|
// Accessors required by Dispatcher.
|
2015-07-29 23:45:04 -07:00
|
|
|
typedef std::function<void(std::shared_ptr<Message> msg,
|
|
|
|
|
NetworkConnection* only,
|
|
|
|
|
NetworkConnection* except)> QueueOutgoingFunc;
|
|
|
|
|
void SetOutgoing(QueueOutgoingFunc queue_outgoing, bool server);
|
|
|
|
|
void ClearOutgoing();
|
|
|
|
|
|
|
|
|
|
NT_Type GetEntryType(unsigned int id) const;
|
|
|
|
|
|
2015-08-13 13:12:15 -07:00
|
|
|
void ProcessIncoming(std::shared_ptr<Message> msg, NetworkConnection* conn,
|
|
|
|
|
std::weak_ptr<NetworkConnection> conn_weak);
|
2015-07-31 23:56:06 -07:00
|
|
|
void GetInitialAssignments(NetworkConnection& conn,
|
|
|
|
|
std::vector<std::shared_ptr<Message>>* msgs);
|
|
|
|
|
void ApplyInitialAssignments(NetworkConnection& conn,
|
|
|
|
|
llvm::ArrayRef<std::shared_ptr<Message>> msgs,
|
|
|
|
|
bool new_server,
|
2015-07-31 20:32:52 -07:00
|
|
|
std::vector<std::shared_ptr<Message>>* out_msgs);
|
2015-07-29 23:45:04 -07:00
|
|
|
|
2015-07-26 09:42:14 -07:00
|
|
|
std::mutex& mutex() { return m_mutex; }
|
2015-07-18 01:29:51 -07:00
|
|
|
|
2015-07-20 22:24:47 -07:00
|
|
|
// User functions
|
2015-07-18 01:29:51 -07:00
|
|
|
std::shared_ptr<Value> GetEntryValue(StringRef name) const;
|
|
|
|
|
bool SetEntryValue(StringRef name, std::shared_ptr<Value> value);
|
|
|
|
|
void SetEntryTypeValue(StringRef name, std::shared_ptr<Value> value);
|
|
|
|
|
void SetEntryFlags(StringRef name, unsigned int flags);
|
|
|
|
|
unsigned int GetEntryFlags(StringRef name) const;
|
|
|
|
|
void DeleteEntry(StringRef name);
|
|
|
|
|
void DeleteAllEntries();
|
|
|
|
|
std::vector<EntryInfo> GetEntryInfo(StringRef prefix, unsigned int types);
|
2015-08-02 21:47:01 -07:00
|
|
|
void NotifyEntries(StringRef prefix);
|
2015-07-18 01:29:51 -07:00
|
|
|
|
2015-06-27 10:22:59 -07:00
|
|
|
void SavePersistent(std::ostream& os) const;
|
2015-07-17 07:21:07 -07:00
|
|
|
bool LoadPersistent(
|
|
|
|
|
std::istream& is,
|
|
|
|
|
std::function<void(std::size_t line, const char* msg)> warn);
|
2015-06-27 10:22:59 -07:00
|
|
|
|
2015-08-13 13:12:15 -07:00
|
|
|
// RPC configuration needs to come through here as RPC definitions are
|
|
|
|
|
// actually special Storage value types.
|
|
|
|
|
void CreateRpc(StringRef name, StringRef def, RpcCallback callback);
|
|
|
|
|
void CreatePolledRpc(StringRef name, StringRef def);
|
|
|
|
|
|
|
|
|
|
unsigned int CallRpc(StringRef name, StringRef params);
|
|
|
|
|
bool GetRpcResult(bool blocking, unsigned int call_uid, std::string* result);
|
|
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
private:
|
2015-08-13 13:12:15 -07:00
|
|
|
Storage() : Storage(Notifier::GetInstance(), RpcServer::GetInstance()) {}
|
|
|
|
|
Storage(Notifier& notifier, RpcServer& rpcserver);
|
2015-06-25 22:57:43 -07:00
|
|
|
Storage(const Storage&) = delete;
|
|
|
|
|
Storage& operator=(const Storage&) = delete;
|
2015-06-21 23:42:29 -07:00
|
|
|
|
2015-07-31 13:48:33 -07:00
|
|
|
struct Entry {
|
2015-08-13 13:12:15 -07:00
|
|
|
Entry(llvm::StringRef name_)
|
|
|
|
|
: name(name_), flags(0), id(0xffff), rpc_call_uid(0) {}
|
2015-07-31 13:48:33 -07:00
|
|
|
bool IsPersistent() const { return (flags & NT_PERSISTENT) != 0; }
|
|
|
|
|
|
|
|
|
|
std::string name;
|
|
|
|
|
std::shared_ptr<Value> value;
|
|
|
|
|
unsigned int flags;
|
|
|
|
|
unsigned int id;
|
|
|
|
|
SequenceNumber seq_num;
|
2015-08-13 13:12:15 -07:00
|
|
|
RpcCallback rpc_callback;
|
|
|
|
|
unsigned int rpc_call_uid;
|
2015-07-31 13:48:33 -07:00
|
|
|
};
|
|
|
|
|
|
2015-08-01 11:47:35 -07:00
|
|
|
typedef llvm::StringMap<std::unique_ptr<Entry>> EntriesMap;
|
|
|
|
|
typedef std::vector<Entry*> IdMap;
|
2015-08-13 13:12:15 -07:00
|
|
|
typedef llvm::DenseMap<std::pair<unsigned int, unsigned int>, std::string>
|
|
|
|
|
RpcResultMap;
|
2015-07-19 16:36:30 -07:00
|
|
|
|
2015-07-18 01:29:51 -07:00
|
|
|
mutable std::mutex m_mutex;
|
2015-06-27 10:02:20 -07:00
|
|
|
EntriesMap m_entries;
|
2015-07-29 23:45:04 -07:00
|
|
|
IdMap m_idmap;
|
2015-08-13 13:12:15 -07:00
|
|
|
RpcResultMap m_rpc_results;
|
|
|
|
|
std::atomic_bool m_terminating;
|
|
|
|
|
std::condition_variable m_rpc_results_cond;
|
2015-06-27 10:02:20 -07:00
|
|
|
|
2015-07-29 23:45:04 -07:00
|
|
|
QueueOutgoingFunc m_queue_outgoing;
|
2015-08-13 13:12:15 -07:00
|
|
|
bool m_server = true;
|
2015-08-02 21:47:01 -07:00
|
|
|
Notifier& m_notifier;
|
2015-08-13 13:12:15 -07:00
|
|
|
RpcServer& m_rpc_server;
|
2015-07-29 23:45:04 -07:00
|
|
|
|
2015-07-20 23:36:22 -07:00
|
|
|
ATOMIC_STATIC_DECL(Storage)
|
2015-06-21 23:42:29 -07:00
|
|
|
};
|
|
|
|
|
|
2015-07-17 07:21:07 -07:00
|
|
|
} // namespace nt
|
2015-06-21 23:42:29 -07:00
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
#endif // NT_STORAGE_H_
|