WPILibC++  unspecified
Message.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2015-2018. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef NTCORE_MESSAGE_H_
9 #define NTCORE_MESSAGE_H_
10 
11 #include <functional>
12 #include <memory>
13 #include <string>
14 
15 #include "networktables/NetworkTableValue.h"
16 
17 namespace nt {
18 
19 class WireDecoder;
20 class WireEncoder;
21 
22 class Message {
23  struct private_init {};
24 
25  public:
26  enum MsgType {
27  kUnknown = -1,
28  kKeepAlive = 0x00,
29  kClientHello = 0x01,
30  kProtoUnsup = 0x02,
31  kServerHelloDone = 0x03,
32  kServerHello = 0x04,
33  kClientHelloDone = 0x05,
34  kEntryAssign = 0x10,
35  kEntryUpdate = 0x11,
36  kFlagsUpdate = 0x12,
37  kEntryDelete = 0x13,
38  kClearEntries = 0x14,
39  kExecuteRpc = 0x20,
40  kRpcResponse = 0x21
41  };
42  typedef std::function<NT_Type(unsigned int id)> GetEntryTypeFunc;
43 
44  Message() : m_type(kUnknown), m_id(0), m_flags(0), m_seq_num_uid(0) {}
45  Message(MsgType type, const private_init&)
46  : m_type(type), m_id(0), m_flags(0), m_seq_num_uid(0) {}
47 
48  MsgType type() const { return m_type; }
49  bool Is(MsgType type) const { return type == m_type; }
50 
51  // Message data accessors. Callers are responsible for knowing what data is
52  // actually provided for a particular message.
53  llvm::StringRef str() const { return m_str; }
54  std::shared_ptr<Value> value() const { return m_value; }
55  unsigned int id() const { return m_id; }
56  unsigned int flags() const { return m_flags; }
57  unsigned int seq_num_uid() const { return m_seq_num_uid; }
58 
59  // Read and write from wire representation
60  void Write(WireEncoder& encoder) const;
61  static std::shared_ptr<Message> Read(WireDecoder& decoder,
62  GetEntryTypeFunc get_entry_type);
63 
64  // Create messages without data
65  static std::shared_ptr<Message> KeepAlive() {
66  return std::make_shared<Message>(kKeepAlive, private_init());
67  }
68  static std::shared_ptr<Message> ProtoUnsup() {
69  return std::make_shared<Message>(kProtoUnsup, private_init());
70  }
71  static std::shared_ptr<Message> ServerHelloDone() {
72  return std::make_shared<Message>(kServerHelloDone, private_init());
73  }
74  static std::shared_ptr<Message> ClientHelloDone() {
75  return std::make_shared<Message>(kClientHelloDone, private_init());
76  }
77  static std::shared_ptr<Message> ClearEntries() {
78  return std::make_shared<Message>(kClearEntries, private_init());
79  }
80 
81  // Create messages with data
82  static std::shared_ptr<Message> ClientHello(llvm::StringRef self_id);
83  static std::shared_ptr<Message> ServerHello(unsigned int flags,
84  llvm::StringRef self_id);
85  static std::shared_ptr<Message> EntryAssign(llvm::StringRef name,
86  unsigned int id,
87  unsigned int seq_num,
88  std::shared_ptr<Value> value,
89  unsigned int flags);
90  static std::shared_ptr<Message> EntryUpdate(unsigned int id,
91  unsigned int seq_num,
92  std::shared_ptr<Value> value);
93  static std::shared_ptr<Message> FlagsUpdate(unsigned int id,
94  unsigned int flags);
95  static std::shared_ptr<Message> EntryDelete(unsigned int id);
96  static std::shared_ptr<Message> ExecuteRpc(unsigned int id, unsigned int uid,
97  llvm::StringRef params);
98  static std::shared_ptr<Message> RpcResponse(unsigned int id, unsigned int uid,
99  llvm::StringRef result);
100 
101  Message(const Message&) = delete;
102  Message& operator=(const Message&) = delete;
103 
104  private:
105  MsgType m_type;
106 
107  // Message data. Use varies by message type.
108  std::string m_str;
109  std::shared_ptr<Value> m_value;
110  unsigned int m_id; // also used for proto_rev
111  unsigned int m_flags;
112  unsigned int m_seq_num_uid;
113 };
114 
115 } // namespace nt
116 
117 #endif // NTCORE_MESSAGE_H_
Definition: Message.h:22
Definition: WireEncoder.h:28
Definition: IEntryNotifier.h:16
Definition: WireDecoder.h:32
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:42