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 <fstream>
|
|
|
|
|
|
|
|
|
|
#include "Dispatcher.h"
|
2015-07-31 22:41:26 -07:00
|
|
|
#include "Log.h"
|
2015-07-17 07:21:07 -07:00
|
|
|
#include "Storage.h"
|
|
|
|
|
|
|
|
|
|
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-07-17 07:21:07 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveEntryListener(unsigned int entry_listener_uid) {}
|
|
|
|
|
|
|
|
|
|
unsigned int AddConnectionListener(ConnectionListenerCallback callback) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveConnectionListener(unsigned int conn_listener_uid) {}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Remote Procedure Call Functions
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
unsigned int CreateRpc(StringRef name, const RpcDefinition& def,
|
|
|
|
|
RpcCallback callback) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DeleteRpc(unsigned int rpc_uid) {}
|
|
|
|
|
|
|
|
|
|
unsigned int CallRpc(StringRef name,
|
|
|
|
|
ArrayRef<std::shared_ptr<Value>> params) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<Value>> GetRpcResult(unsigned int result_uid) {
|
|
|
|
|
return std::vector<std::shared_ptr<Value>>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 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) {
|
|
|
|
|
Dispatcher& dispatcher = Dispatcher::GetInstance();
|
|
|
|
|
dispatcher.StartServer(listen_address, port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-07-17 07:21:07 -07:00
|
|
|
const Storage& storage = Storage::GetInstance();
|
2015-07-18 00:39:08 -07:00
|
|
|
|
|
|
|
|
std::string fn = filename;
|
|
|
|
|
std::string tmp = filename;
|
|
|
|
|
tmp += ".tmp";
|
|
|
|
|
std::string bak = filename;
|
|
|
|
|
bak += ".bak";
|
|
|
|
|
|
|
|
|
|
// start by writing to temporary file
|
|
|
|
|
std::ofstream os(tmp);
|
2015-07-17 07:21:07 -07:00
|
|
|
if (!os) return "could not open file";
|
|
|
|
|
storage.SavePersistent(os);
|
2015-07-18 00:39:08 -07:00
|
|
|
os.flush();
|
|
|
|
|
if (!os) {
|
|
|
|
|
os.close();
|
|
|
|
|
std::remove(tmp.c_str());
|
|
|
|
|
return "error saving file";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// safely move to real file
|
|
|
|
|
std::remove(bak.c_str());
|
|
|
|
|
if (std::rename(fn.c_str(), bak.c_str()) != 0)
|
|
|
|
|
return "could not rename real file to backup";
|
|
|
|
|
if (std::rename(tmp.c_str(), fn.c_str()) != 0) {
|
|
|
|
|
std::rename(bak.c_str(), fn.c_str()); // attempt to restore backup
|
|
|
|
|
return "could not rename temp file to real file";
|
|
|
|
|
}
|
2015-07-17 07:21:07 -07:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
Storage& storage = Storage::GetInstance();
|
|
|
|
|
std::ifstream is(filename);
|
|
|
|
|
if (!is) return "could not open file";
|
|
|
|
|
if (!storage.LoadPersistent(is, warn)) return "error reading file";
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
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
|