2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2015-07-12 11:24:34 -07:00
|
|
|
|
2017-08-19 23:08:27 -07:00
|
|
|
#ifndef NTCORE_MESSAGE_H_
|
|
|
|
|
#define NTCORE_MESSAGE_H_
|
2015-07-12 11:24:34 -07:00
|
|
|
|
2015-07-19 23:17:14 -07:00
|
|
|
#include <functional>
|
2015-07-12 11:24:34 -07:00
|
|
|
#include <memory>
|
2015-07-16 01:38:27 -07:00
|
|
|
#include <string>
|
2015-07-12 11:24:34 -07: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
2017-04-23 10:26:17 -07:00
|
|
|
#include "networktables/NetworkTableValue.h"
|
2015-07-12 11:24:34 -07:00
|
|
|
|
2015-07-17 07:21:07 -07:00
|
|
|
namespace nt {
|
2015-07-12 11:24:34 -07:00
|
|
|
|
|
|
|
|
class WireDecoder;
|
|
|
|
|
class WireEncoder;
|
|
|
|
|
|
|
|
|
|
class Message {
|
2015-07-15 21:20:18 -07:00
|
|
|
struct private_init {};
|
|
|
|
|
|
2015-07-12 11:24:34 -07:00
|
|
|
public:
|
|
|
|
|
enum MsgType {
|
|
|
|
|
kUnknown = -1,
|
|
|
|
|
kKeepAlive = 0x00,
|
|
|
|
|
kClientHello = 0x01,
|
|
|
|
|
kProtoUnsup = 0x02,
|
|
|
|
|
kServerHelloDone = 0x03,
|
|
|
|
|
kServerHello = 0x04,
|
|
|
|
|
kClientHelloDone = 0x05,
|
|
|
|
|
kEntryAssign = 0x10,
|
|
|
|
|
kEntryUpdate = 0x11,
|
|
|
|
|
kFlagsUpdate = 0x12,
|
|
|
|
|
kEntryDelete = 0x13,
|
|
|
|
|
kClearEntries = 0x14,
|
|
|
|
|
kExecuteRpc = 0x20,
|
|
|
|
|
kRpcResponse = 0x21
|
|
|
|
|
};
|
2015-07-19 23:17:14 -07:00
|
|
|
typedef std::function<NT_Type(unsigned int id)> GetEntryTypeFunc;
|
2015-07-12 11:24:34 -07:00
|
|
|
|
|
|
|
|
Message() : m_type(kUnknown), m_id(0), m_flags(0), m_seq_num_uid(0) {}
|
2015-07-15 21:20:18 -07:00
|
|
|
Message(MsgType type, const private_init&)
|
|
|
|
|
: m_type(type), m_id(0), m_flags(0), m_seq_num_uid(0) {}
|
2015-07-12 11:24:34 -07:00
|
|
|
|
|
|
|
|
MsgType type() const { return m_type; }
|
|
|
|
|
bool Is(MsgType type) const { return type == m_type; }
|
|
|
|
|
|
2015-07-17 19:41:40 -07:00
|
|
|
// Message data accessors. Callers are responsible for knowing what data is
|
|
|
|
|
// actually provided for a particular message.
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::StringRef str() const { return m_str; }
|
2015-07-17 19:41:40 -07:00
|
|
|
std::shared_ptr<Value> value() const { return m_value; }
|
|
|
|
|
unsigned int id() const { return m_id; }
|
|
|
|
|
unsigned int flags() const { return m_flags; }
|
|
|
|
|
unsigned int seq_num_uid() const { return m_seq_num_uid; }
|
|
|
|
|
|
2015-07-12 11:24:34 -07:00
|
|
|
// Read and write from wire representation
|
|
|
|
|
void Write(WireEncoder& encoder) const;
|
2015-07-15 21:20:18 -07:00
|
|
|
static std::shared_ptr<Message> Read(WireDecoder& decoder,
|
|
|
|
|
GetEntryTypeFunc get_entry_type);
|
2015-07-12 11:24:34 -07:00
|
|
|
|
|
|
|
|
// Create messages without data
|
2015-07-15 21:20:18 -07:00
|
|
|
static std::shared_ptr<Message> KeepAlive() {
|
|
|
|
|
return std::make_shared<Message>(kKeepAlive, private_init());
|
|
|
|
|
}
|
|
|
|
|
static std::shared_ptr<Message> ProtoUnsup() {
|
|
|
|
|
return std::make_shared<Message>(kProtoUnsup, private_init());
|
|
|
|
|
}
|
|
|
|
|
static std::shared_ptr<Message> ServerHelloDone() {
|
|
|
|
|
return std::make_shared<Message>(kServerHelloDone, private_init());
|
|
|
|
|
}
|
|
|
|
|
static std::shared_ptr<Message> ClientHelloDone() {
|
|
|
|
|
return std::make_shared<Message>(kClientHelloDone, private_init());
|
|
|
|
|
}
|
|
|
|
|
static std::shared_ptr<Message> ClearEntries() {
|
|
|
|
|
return std::make_shared<Message>(kClearEntries, private_init());
|
|
|
|
|
}
|
2015-07-12 11:24:34 -07:00
|
|
|
|
|
|
|
|
// Create messages with data
|
2018-04-29 23:33:19 -07:00
|
|
|
static std::shared_ptr<Message> ClientHello(wpi::StringRef self_id);
|
2015-07-15 21:20:18 -07:00
|
|
|
static std::shared_ptr<Message> ServerHello(unsigned int flags,
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::StringRef self_id);
|
|
|
|
|
static std::shared_ptr<Message> EntryAssign(wpi::StringRef name,
|
2015-07-15 21:20:18 -07:00
|
|
|
unsigned int id,
|
|
|
|
|
unsigned int seq_num,
|
|
|
|
|
std::shared_ptr<Value> value,
|
|
|
|
|
unsigned int flags);
|
|
|
|
|
static std::shared_ptr<Message> EntryUpdate(unsigned int id,
|
|
|
|
|
unsigned int seq_num,
|
|
|
|
|
std::shared_ptr<Value> value);
|
|
|
|
|
static std::shared_ptr<Message> FlagsUpdate(unsigned int id,
|
|
|
|
|
unsigned int flags);
|
|
|
|
|
static std::shared_ptr<Message> EntryDelete(unsigned int id);
|
|
|
|
|
static std::shared_ptr<Message> ExecuteRpc(unsigned int id, unsigned int uid,
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::StringRef params);
|
2015-07-15 21:20:18 -07:00
|
|
|
static std::shared_ptr<Message> RpcResponse(unsigned int id, unsigned int uid,
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::StringRef result);
|
2015-07-12 11:24:34 -07:00
|
|
|
|
|
|
|
|
Message(const Message&) = delete;
|
|
|
|
|
Message& operator=(const Message&) = delete;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
MsgType m_type;
|
|
|
|
|
|
|
|
|
|
// Message data. Use varies by message type.
|
2015-07-16 01:38:27 -07:00
|
|
|
std::string m_str;
|
2015-07-12 11:24:34 -07:00
|
|
|
std::shared_ptr<Value> m_value;
|
|
|
|
|
unsigned int m_id; // also used for proto_rev
|
|
|
|
|
unsigned int m_flags;
|
|
|
|
|
unsigned int m_seq_num_uid;
|
|
|
|
|
};
|
|
|
|
|
|
2015-07-17 07:21:07 -07:00
|
|
|
} // namespace nt
|
2015-07-12 11:24:34 -07:00
|
|
|
|
2017-08-19 23:08:27 -07:00
|
|
|
#endif // NTCORE_MESSAGE_H_
|