2015-07-17 07:21:07 -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. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "ntcore.h"
|
|
|
|
|
|
|
|
|
|
#include <cassert>
|
2015-07-18 00:39:08 -07:00
|
|
|
#include <cstdio>
|
2015-07-17 07:21:07 -07:00
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
|
|
#include "Dispatcher.h"
|
2015-07-31 22:41:26 -07:00
|
|
|
#include "Log.h"
|
2015-08-02 21:47:01 -07:00
|
|
|
#include "Notifier.h"
|
2015-08-13 13:12:15 -07:00
|
|
|
#include "RpcServer.h"
|
2015-07-17 07:21:07 -07:00
|
|
|
#include "Storage.h"
|
2015-08-13 13:12:15 -07:00
|
|
|
#include "WireDecoder.h"
|
|
|
|
|
#include "WireEncoder.h"
|
2015-07-17 07:21:07 -07:00
|
|
|
|
|
|
|
|
namespace nt {
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Table Functions
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<Value> GetEntryValue(StringRef name) {
|
2015-07-18 01:29:51 -07:00
|
|
|
return Storage::GetInstance().GetEntryValue(name);
|
2015-07-17 07:21:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SetEntryValue(StringRef name, std::shared_ptr<Value> value) {
|
2015-07-18 01:29:51 -07:00
|
|
|
return Storage::GetInstance().SetEntryValue(name, value);
|
2015-07-17 07:21:07 -07:00
|
|
|
}
|
|
|
|
|
|
2015-07-18 01:29:51 -07:00
|
|
|
void SetEntryTypeValue(StringRef name, std::shared_ptr<Value> value) {
|
|
|
|
|
Storage::GetInstance().SetEntryTypeValue(name, value);
|
|
|
|
|
}
|
2015-07-17 07:21:07 -07:00
|
|
|
|
2015-07-18 01:29:51 -07:00
|
|
|
void SetEntryFlags(StringRef name, unsigned int flags) {
|
|
|
|
|
Storage::GetInstance().SetEntryFlags(name, flags);
|
|
|
|
|
}
|
2015-07-17 07:21:07 -07:00
|
|
|
|
|
|
|
|
unsigned int GetEntryFlags(StringRef name) {
|
2015-07-18 01:29:51 -07:00
|
|
|
return Storage::GetInstance().GetEntryFlags(name);
|
2015-07-17 07:21:07 -07:00
|
|
|
}
|
|
|
|
|
|
2015-07-18 01:29:51 -07:00
|
|
|
void DeleteEntry(StringRef name) {
|
|
|
|
|
Storage::GetInstance().DeleteEntry(name);
|
|
|
|
|
}
|
2015-07-17 07:21:07 -07:00
|
|
|
|
2015-07-18 01:29:51 -07:00
|
|
|
void DeleteAllEntries() {
|
|
|
|
|
Storage::GetInstance().DeleteAllEntries();
|
|
|
|
|
}
|
2015-07-17 07:21:07 -07:00
|
|
|
|
|
|
|
|
std::vector<EntryInfo> GetEntryInfo(StringRef prefix, unsigned int types) {
|
2015-07-18 01:29:51 -07:00
|
|
|
return Storage::GetInstance().GetEntryInfo(prefix, types);
|
2015-07-17 07:21:07 -07:00
|
|
|
}
|
|
|
|
|
|
2015-07-17 22:40:00 -07:00
|
|
|
void Flush() {
|
2015-07-19 16:09:59 -07:00
|
|
|
Dispatcher::GetInstance().Flush();
|
2015-07-17 22:40:00 -07:00
|
|
|
}
|
2015-07-17 07:21:07 -07:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Callback Creation Functions
|
|
|
|
|
*/
|
|
|
|
|
|
2015-07-23 01:02:53 -07:00
|
|
|
unsigned int AddEntryListener(StringRef prefix, EntryListenerCallback callback,
|
|
|
|
|
bool immediate_notify) {
|
2015-08-02 21:47:01 -07:00
|
|
|
Notifier& notifier = Notifier::GetInstance();
|
|
|
|
|
unsigned int uid = notifier.AddEntryListener(prefix, callback);
|
|
|
|
|
notifier.Start();
|
|
|
|
|
if (immediate_notify) Storage::GetInstance().NotifyEntries(prefix);
|
|
|
|
|
return uid;
|
2015-07-17 07:21:07 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-02 21:47:01 -07:00
|
|
|
void RemoveEntryListener(unsigned int entry_listener_uid) {
|
|
|
|
|
Notifier::GetInstance().RemoveEntryListener(entry_listener_uid);
|
|
|
|
|
}
|
2015-07-17 07:21:07 -07:00
|
|
|
|
|
|
|
|
unsigned int AddConnectionListener(ConnectionListenerCallback callback) {
|
2015-08-02 21:47:01 -07:00
|
|
|
Notifier& notifier = Notifier::GetInstance();
|
|
|
|
|
unsigned int uid = notifier.AddConnectionListener(callback);
|
|
|
|
|
Notifier::GetInstance().Start();
|
|
|
|
|
return uid;
|
2015-07-17 07:21:07 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-02 21:47:01 -07:00
|
|
|
void RemoveConnectionListener(unsigned int conn_listener_uid) {
|
|
|
|
|
Notifier::GetInstance().RemoveConnectionListener(conn_listener_uid);
|
|
|
|
|
}
|
2015-07-17 07:21:07 -07:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Remote Procedure Call Functions
|
|
|
|
|
*/
|
|
|
|
|
|
2015-08-13 13:12:15 -07:00
|
|
|
void CreateRpc(StringRef name, StringRef def, RpcCallback callback) {
|
|
|
|
|
Storage::GetInstance().CreateRpc(name, def, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreatePolledRpc(StringRef name, StringRef def) {
|
|
|
|
|
Storage::GetInstance().CreatePolledRpc(name, def);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PollRpc(bool blocking, RpcCallInfo* call_info) {
|
|
|
|
|
return RpcServer::GetInstance().PollRpc(blocking, call_info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PostRpcResponse(unsigned int rpc_id, unsigned int call_uid,
|
|
|
|
|
StringRef result) {
|
|
|
|
|
RpcServer::GetInstance().PostRpcResponse(rpc_id, call_uid, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned int CallRpc(StringRef name, StringRef params) {
|
|
|
|
|
return Storage::GetInstance().CallRpc(name, params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GetRpcResult(bool blocking, unsigned int call_uid, std::string* result) {
|
|
|
|
|
return Storage::GetInstance().GetRpcResult(blocking, call_uid, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string PackRpcDefinition(const RpcDefinition& def) {
|
|
|
|
|
WireEncoder enc(0x0300);
|
|
|
|
|
enc.Write8(def.version);
|
|
|
|
|
enc.WriteString(def.name);
|
|
|
|
|
|
|
|
|
|
// parameters
|
|
|
|
|
unsigned int params_size = def.params.size();
|
|
|
|
|
if (params_size > 0xff) params_size = 0xff;
|
|
|
|
|
enc.Write8(params_size);
|
|
|
|
|
for (std::size_t i = 0; i < params_size; ++i) {
|
|
|
|
|
enc.WriteType(def.params[i].def_value->type());
|
|
|
|
|
enc.WriteString(def.params[i].name);
|
|
|
|
|
enc.WriteValue(*def.params[i].def_value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// results
|
|
|
|
|
unsigned int results_size = def.results.size();
|
|
|
|
|
if (results_size > 0xff) results_size = 0xff;
|
|
|
|
|
enc.Write8(results_size);
|
|
|
|
|
for (std::size_t i = 0; i < results_size; ++i) {
|
|
|
|
|
enc.WriteType(def.results[i].type);
|
|
|
|
|
enc.WriteString(def.results[i].name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return enc.ToStringRef();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool UnpackRpcDefinition(StringRef packed, RpcDefinition* def) {
|
|
|
|
|
raw_mem_istream is(packed.data(), packed.size());
|
|
|
|
|
WireDecoder dec(is, 0x0300);
|
|
|
|
|
if (!dec.Read8(&def->version)) return false;
|
|
|
|
|
if (!dec.ReadString(&def->name)) return false;
|
|
|
|
|
|
|
|
|
|
// parameters
|
|
|
|
|
unsigned int params_size;
|
|
|
|
|
if (!dec.Read8(¶ms_size)) return false;
|
|
|
|
|
def->params.resize(0);
|
|
|
|
|
def->params.reserve(params_size);
|
|
|
|
|
for (std::size_t i = 0; i < params_size; ++i) {
|
|
|
|
|
RpcParamDef pdef;
|
|
|
|
|
NT_Type type;
|
|
|
|
|
if (!dec.ReadType(&type)) return false;
|
|
|
|
|
if (!dec.ReadString(&pdef.name)) return false;
|
|
|
|
|
pdef.def_value = dec.ReadValue(type);
|
|
|
|
|
if (!pdef.def_value) return false;
|
|
|
|
|
def->params.emplace_back(std::move(pdef));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// results
|
|
|
|
|
unsigned int results_size;
|
|
|
|
|
if (!dec.Read8(&results_size)) return false;
|
|
|
|
|
def->results.resize(0);
|
|
|
|
|
def->results.reserve(results_size);
|
|
|
|
|
for (std::size_t i = 0; i < results_size; ++i) {
|
|
|
|
|
RpcResultDef rdef;
|
|
|
|
|
if (!dec.ReadType(&rdef.type)) return false;
|
|
|
|
|
if (!dec.ReadString(&rdef.name)) return false;
|
|
|
|
|
def->results.emplace_back(std::move(rdef));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string PackRpcValues(ArrayRef<std::shared_ptr<Value>> values) {
|
|
|
|
|
WireEncoder enc(0x0300);
|
|
|
|
|
for (auto& value : values) enc.WriteValue(*value);
|
|
|
|
|
return enc.ToStringRef();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<Value>> UnpackRpcValues(StringRef packed,
|
|
|
|
|
ArrayRef<NT_Type> types) {
|
|
|
|
|
raw_mem_istream is(packed.data(), packed.size());
|
|
|
|
|
WireDecoder dec(is, 0x0300);
|
|
|
|
|
std::vector<std::shared_ptr<Value>> vec;
|
|
|
|
|
for (auto type : types) {
|
|
|
|
|
auto item = dec.ReadValue(type);
|
|
|
|
|
if (!item) return std::vector<std::shared_ptr<Value>>();
|
|
|
|
|
vec.emplace_back(std::move(item));
|
|
|
|
|
}
|
|
|
|
|
return vec;
|
2015-07-17 07:21:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Client/Server Functions
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
void SetNetworkIdentity(StringRef name) {
|
2015-07-19 16:09:59 -07:00
|
|
|
Dispatcher::GetInstance().SetIdentity(name);
|
2015-07-17 07:21:07 -07:00
|
|
|
}
|
|
|
|
|
|
2015-07-19 16:11:56 -07:00
|
|
|
void StartServer(StringRef persist_filename, const char *listen_address,
|
2015-07-17 07:21:07 -07:00
|
|
|
unsigned int port) {
|
2015-08-19 19:09:25 -07:00
|
|
|
Dispatcher::GetInstance().StartServer(persist_filename, listen_address, port);
|
2015-07-17 07:21:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StopServer() {
|
2015-07-19 16:09:59 -07:00
|
|
|
Dispatcher::GetInstance().Stop();
|
2015-07-17 07:21:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StartClient(const char *server_name, unsigned int port) {
|
2015-07-19 16:09:59 -07:00
|
|
|
Dispatcher::GetInstance().StartClient(server_name, port);
|
2015-07-17 07:21:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StopClient() {
|
2015-07-19 16:09:59 -07:00
|
|
|
Dispatcher::GetInstance().Stop();
|
2015-07-17 07:21:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SetUpdateRate(double interval) {
|
2015-07-19 16:09:59 -07:00
|
|
|
Dispatcher::GetInstance().SetUpdateRate(interval);
|
2015-07-17 07:21:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<ConnectionInfo> GetConnections() {
|
2015-08-02 10:47:05 -07:00
|
|
|
return Dispatcher::GetInstance().GetConnections();
|
2015-07-17 07:21:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Persistent Functions
|
|
|
|
|
*/
|
|
|
|
|
|
2015-07-18 00:39:08 -07:00
|
|
|
const char* SavePersistent(StringRef filename) {
|
2015-08-19 19:09:25 -07:00
|
|
|
return Storage::GetInstance().SavePersistent(filename, false);
|
2015-07-17 07:21:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char* LoadPersistent(
|
2015-07-18 00:39:08 -07:00
|
|
|
StringRef filename,
|
2015-07-17 07:21:07 -07:00
|
|
|
std::function<void(size_t line, const char* msg)> warn) {
|
2015-08-19 19:09:25 -07:00
|
|
|
return Storage::GetInstance().LoadPersistent(filename, warn);
|
2015-07-17 07:21:07 -07:00
|
|
|
}
|
|
|
|
|
|
2015-07-31 22:41:26 -07:00
|
|
|
void SetLogger(LogFunc func, unsigned int min_level) {
|
|
|
|
|
Logger& logger = Logger::GetInstance();
|
|
|
|
|
logger.SetLogger(func);
|
|
|
|
|
logger.set_min_level(min_level);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-17 07:21:07 -07:00
|
|
|
} // namespace nt
|