mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
Implement independent instances.
Previously, most of the classes were implemented as singletons so only one
instance was possible.
This change adds an instance handle-based API. In Java, this API is located
in a different package than the old API (edu.wpi.first.networktables).
Backwards compatibility with ITable and the old NetworkTable API is largely
maintained, but a handful of classes have moved to the new package in Java
(ConnectionInfo and PersistentException), and the old JNI has been completed
replaced.
Also:
- Move SetTeam implementation to Dispatcher.
- Consistently pass time through Java and C++ Value API.
- Rename nt_Value.h to NetworkTableValue.h for consistency with Java.
- Improve documentation
- Make C++ and Java APIs more consistent
- Document RPC functions and support RPC in Java.
- Add polling features for entry and connection listeners and use them to
move callback threads to Java level.
- Remove thread start and stop hooks (as polling is available).
- Make Notifiers, RpcServer, Dispatcher, and Storage mockable.
- Set NOTIFY_NEW on immediate entry notifications.
- Make GetTable("/") and GetTable("") equivalent.
- Generate local notification for flags update when loading persistent file.
And many unit test updates/changes:
- Use InitGoogleMock instead of InitGoogleTest in test main.
- Move test printers to TestPrinter.h/cpp.
- Provide printers for StringRef, EntryNotifier, and Handle.
- StorageTest: Check notifications.
- Add entry notifier unit tests.
- Storage: Add test for incoming entry assignment.
- Update connection listener tests.
- Add entry listener unit tests.
Fixes #11, #140, #189, #190, #192, #193, #221
This commit is contained in:
@@ -1,38 +1,50 @@
|
||||
#include <chrono>
|
||||
#include <climits>
|
||||
#include <cstdio>
|
||||
#include <thread>
|
||||
#include <iostream>
|
||||
|
||||
#include "support/json.h"
|
||||
|
||||
#include "ntcore.h"
|
||||
|
||||
std::string callback1(nt::StringRef name, nt::StringRef params_str,
|
||||
const nt::ConnectionInfo& conn_info) {
|
||||
auto params = nt::UnpackRpcValues(params_str, NT_DOUBLE);
|
||||
if (params.empty()) {
|
||||
std::fputs("empty params?\n", stderr);
|
||||
return "";
|
||||
void callback1(const nt::RpcAnswer& answer) {
|
||||
wpi::json params;
|
||||
try {
|
||||
params = wpi::json::from_cbor(answer.params);
|
||||
} catch (wpi::json::parse_error err) {
|
||||
std::fputs("could not decode params?\n", stderr);
|
||||
return;
|
||||
}
|
||||
return nt::PackRpcValues(nt::Value::MakeDouble(params[0]->GetDouble() + 1.2));
|
||||
if (!params.is_number()) {
|
||||
std::fputs("did not get number\n", stderr);
|
||||
return;
|
||||
}
|
||||
double val = params.get<double>();
|
||||
answer.PostResponse(wpi::json::to_cbor(val + 1.2));
|
||||
}
|
||||
|
||||
int main() {
|
||||
nt::RpcDefinition def;
|
||||
def.version = 1;
|
||||
def.name = "myfunc1";
|
||||
def.params.emplace_back("param1", nt::Value::MakeDouble(0.0));
|
||||
def.results.emplace_back("result1", NT_DOUBLE);
|
||||
nt::CreateRpc("func1", nt::PackRpcDefinition(def), callback1);
|
||||
auto inst = nt::GetDefaultInstance();
|
||||
nt::StartServer(inst, "rpc_speed.ini", "", 10000);
|
||||
auto entry = nt::GetEntry(inst, "func1");
|
||||
nt::CreateRpc(entry, nt::StringRef("", 1), callback1);
|
||||
std::string call1_result_str;
|
||||
|
||||
auto start2 = std::chrono::high_resolution_clock::now();
|
||||
auto start = nt::Now();
|
||||
for (int i=0; i<10000; ++i) {
|
||||
unsigned int call1_uid =
|
||||
nt::CallRpc("func1", nt::PackRpcValues(nt::Value::MakeDouble(i)));
|
||||
nt::GetRpcResult(true, call1_uid, &call1_result_str);
|
||||
auto call1_result = nt::UnpackRpcValues(call1_result_str, NT_DOUBLE);
|
||||
if (call1_result.empty()) {
|
||||
std::fputs("empty result?\n", stderr);
|
||||
unsigned int call1_uid = nt::CallRpc(entry, wpi::json::to_cbor(i));
|
||||
nt::GetRpcResult(entry, call1_uid, &call1_result_str);
|
||||
wpi::json call1_result;
|
||||
try {
|
||||
call1_result = wpi::json::from_cbor(call1_result_str);
|
||||
} catch (wpi::json::parse_error err) {
|
||||
std::fputs("could not decode result?\n", stderr);
|
||||
return 1;
|
||||
}
|
||||
if (!call1_result.is_number()) {
|
||||
std::fputs("result is not number?\n", stderr);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user