Brian Silverman | f7bd1c2 | 2015-12-24 16:07:11 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2015. 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_CPP_H_ |
| 9 | #define NTCORE_CPP_H_ |
| 10 | |
| 11 | #include <cassert> |
| 12 | #include <functional> |
| 13 | #include <memory> |
| 14 | #include <string> |
| 15 | #include <vector> |
| 16 | |
| 17 | #include "llvm/ArrayRef.h" |
| 18 | #include "llvm/StringRef.h" |
| 19 | |
| 20 | #include "nt_Value.h" |
| 21 | |
| 22 | namespace nt { |
| 23 | |
| 24 | using llvm::ArrayRef; |
| 25 | using llvm::StringRef; |
| 26 | |
| 27 | /** NetworkTables Entry Information */ |
| 28 | struct EntryInfo { |
| 29 | /** Entry name */ |
| 30 | std::string name; |
| 31 | |
| 32 | /** Entry type */ |
| 33 | NT_Type type; |
| 34 | |
| 35 | /** Entry flags */ |
| 36 | unsigned int flags; |
| 37 | |
| 38 | /** Timestamp of last change to entry (type or value). */ |
| 39 | unsigned long long last_change; |
| 40 | }; |
| 41 | |
| 42 | /** NetworkTables Connection Information */ |
| 43 | struct ConnectionInfo { |
| 44 | std::string remote_id; |
| 45 | std::string remote_name; |
| 46 | unsigned int remote_port; |
| 47 | unsigned long long last_update; |
| 48 | unsigned int protocol_version; |
| 49 | }; |
| 50 | |
| 51 | /** NetworkTables RPC Parameter Definition */ |
| 52 | struct RpcParamDef { |
| 53 | RpcParamDef() = default; |
| 54 | RpcParamDef(StringRef name_, std::shared_ptr<Value> def_value_) |
| 55 | : name(name_), def_value(def_value_) {} |
| 56 | |
| 57 | std::string name; |
| 58 | std::shared_ptr<Value> def_value; |
| 59 | }; |
| 60 | |
| 61 | /** NetworkTables RPC Result Definition */ |
| 62 | struct RpcResultDef { |
| 63 | RpcResultDef() = default; |
| 64 | RpcResultDef(StringRef name_, NT_Type type_) : name(name_), type(type_) {} |
| 65 | |
| 66 | std::string name; |
| 67 | NT_Type type; |
| 68 | }; |
| 69 | |
| 70 | /** NetworkTables RPC Definition */ |
| 71 | struct RpcDefinition { |
| 72 | unsigned int version; |
| 73 | std::string name; |
| 74 | std::vector<RpcParamDef> params; |
| 75 | std::vector<RpcResultDef> results; |
| 76 | }; |
| 77 | |
| 78 | /** NetworkTables RPC Call Data */ |
| 79 | struct RpcCallInfo { |
| 80 | unsigned int rpc_id; |
| 81 | unsigned int call_uid; |
| 82 | std::string name; |
| 83 | std::string params; |
| 84 | }; |
| 85 | |
| 86 | /* |
| 87 | * Table Functions |
| 88 | */ |
| 89 | |
| 90 | /** Get Entry Value. |
| 91 | * Returns copy of current entry value. |
| 92 | * Note that one of the type options is "unassigned". |
| 93 | * |
| 94 | * @param name entry name (UTF-8 string) |
| 95 | * @return entry value |
| 96 | */ |
| 97 | std::shared_ptr<Value> GetEntryValue(StringRef name); |
| 98 | |
| 99 | /** Set Entry Value. |
| 100 | * Sets new entry value. If type of new value differs from the type of the |
| 101 | * currently stored entry, returns error and does not update value. |
| 102 | * |
| 103 | * @param name entry name (UTF-8 string) |
| 104 | * @param value new entry value |
| 105 | * @return False on error (type mismatch), True on success |
| 106 | */ |
| 107 | bool SetEntryValue(StringRef name, std::shared_ptr<Value> value); |
| 108 | |
| 109 | /** Set Entry Type and Value. |
| 110 | * Sets new entry value. If type of new value differs from the type of the |
| 111 | * currently stored entry, the currently stored entry type is overridden |
| 112 | * (generally this will generate an Entry Assignment message). |
| 113 | * |
| 114 | * This is NOT the preferred method to update a value; generally |
| 115 | * SetEntryValue() should be used instead, with appropriate error handling. |
| 116 | * |
| 117 | * @param name entry name (UTF-8 string) |
| 118 | * @param value new entry value |
| 119 | */ |
| 120 | void SetEntryTypeValue(StringRef name, std::shared_ptr<Value> value); |
| 121 | |
| 122 | /** Set Entry Flags. |
| 123 | */ |
| 124 | void SetEntryFlags(StringRef name, unsigned int flags); |
| 125 | |
| 126 | /** Get Entry Flags. |
| 127 | */ |
| 128 | unsigned int GetEntryFlags(StringRef name); |
| 129 | |
| 130 | /** Delete Entry. |
| 131 | * Deletes an entry. This is a new feature in version 3.0 of the protocol, |
| 132 | * so this may not have an effect if any other node in the network is not |
| 133 | * version 3.0 or newer. |
| 134 | * |
| 135 | * Note: GetConnections() can be used to determine the protocol version |
| 136 | * of direct remote connection(s), but this is not sufficient to determine |
| 137 | * if all nodes in the network are version 3.0 or newer. |
| 138 | * |
| 139 | * @param name entry name (UTF-8 string) |
| 140 | */ |
| 141 | void DeleteEntry(StringRef name); |
| 142 | |
| 143 | /** Delete All Entries. |
| 144 | * Deletes ALL table entries. This is a new feature in version 3.0 of the |
| 145 | * so this may not have an effect if any other node in the network is not |
| 146 | * version 3.0 or newer. |
| 147 | * |
| 148 | * Note: GetConnections() can be used to determine the protocol version |
| 149 | * of direct remote connection(s), but this is not sufficient to determine |
| 150 | * if all nodes in the network are version 3.0 or newer. |
| 151 | */ |
| 152 | void DeleteAllEntries(); |
| 153 | |
| 154 | /** Get Entry Information. |
| 155 | * Returns an array of entry information (name, entry type, |
| 156 | * and timestamp of last change to type/value). The results are optionally |
| 157 | * filtered by string prefix and entry type to only return a subset of all |
| 158 | * entries. |
| 159 | * |
| 160 | * @param prefix entry name required prefix; only entries whose name |
| 161 | * starts with this string are returned |
| 162 | * @param types bitmask of NT_Type values; 0 is treated specially |
| 163 | * as a "don't care" |
| 164 | * @return Array of entry information. |
| 165 | */ |
| 166 | std::vector<EntryInfo> GetEntryInfo(StringRef prefix, unsigned int types); |
| 167 | |
| 168 | /** Flush Entries. |
| 169 | * Forces an immediate flush of all local entry changes to network. |
| 170 | * Normally this is done on a regularly scheduled interval (see |
| 171 | * NT_SetUpdateRate()). |
| 172 | * |
| 173 | * Note: flushes are rate limited to avoid excessive network traffic. If |
| 174 | * the time between calls is too short, the flush will occur after the minimum |
| 175 | * time elapses (rather than immediately). |
| 176 | */ |
| 177 | void Flush(); |
| 178 | |
| 179 | /* |
| 180 | * Callback Creation Functions |
| 181 | */ |
| 182 | |
| 183 | void SetListenerOnStart(std::function<void()> on_start); |
| 184 | void SetListenerOnExit(std::function<void()> on_exit); |
| 185 | |
| 186 | typedef std::function<void(unsigned int uid, StringRef name, |
| 187 | std::shared_ptr<Value> value, |
| 188 | unsigned int flags)> EntryListenerCallback; |
| 189 | |
| 190 | typedef std::function<void(unsigned int uid, bool connected, |
| 191 | const ConnectionInfo& conn)> |
| 192 | ConnectionListenerCallback; |
| 193 | |
| 194 | unsigned int AddEntryListener(StringRef prefix, EntryListenerCallback callback, |
| 195 | unsigned int flags); |
| 196 | void RemoveEntryListener(unsigned int entry_listener_uid); |
| 197 | unsigned int AddConnectionListener(ConnectionListenerCallback callback, |
| 198 | bool immediate_notify); |
| 199 | void RemoveConnectionListener(unsigned int conn_listener_uid); |
| 200 | |
| 201 | bool NotifierDestroyed(); |
| 202 | |
| 203 | /* |
| 204 | * Remote Procedure Call Functions |
| 205 | */ |
| 206 | |
| 207 | typedef std::function<std::string(StringRef name, StringRef params)> |
| 208 | RpcCallback; |
| 209 | |
| 210 | void CreateRpc(StringRef name, StringRef def, RpcCallback callback); |
| 211 | void CreatePolledRpc(StringRef name, StringRef def); |
| 212 | |
| 213 | bool PollRpc(bool blocking, RpcCallInfo* call_info); |
| 214 | void PostRpcResponse(unsigned int rpc_id, unsigned int call_uid, |
| 215 | StringRef result); |
| 216 | |
| 217 | unsigned int CallRpc(StringRef name, StringRef params); |
| 218 | bool GetRpcResult(bool blocking, unsigned int call_uid, std::string* result); |
| 219 | |
| 220 | std::string PackRpcDefinition(const RpcDefinition& def); |
| 221 | bool UnpackRpcDefinition(StringRef packed, RpcDefinition *def); |
| 222 | std::string PackRpcValues(ArrayRef<std::shared_ptr<Value>> values); |
| 223 | std::vector<std::shared_ptr<Value>> UnpackRpcValues(StringRef packed, |
| 224 | ArrayRef<NT_Type> types); |
| 225 | |
| 226 | /* |
| 227 | * Client/Server Functions |
| 228 | */ |
| 229 | void SetNetworkIdentity(StringRef name); |
| 230 | void StartServer(StringRef persist_filename, const char* listen_address, |
| 231 | unsigned int port); |
| 232 | void StopServer(); |
| 233 | void StartClient(const char* server_name, unsigned int port); |
| 234 | void StopClient(); |
| 235 | void StopRpcServer(); |
| 236 | void StopNotifier(); |
| 237 | void SetUpdateRate(double interval); |
| 238 | std::vector<ConnectionInfo> GetConnections(); |
| 239 | |
| 240 | /* |
| 241 | * Persistent Functions |
| 242 | */ |
| 243 | /* return error string, or nullptr if successful */ |
| 244 | const char* SavePersistent(StringRef filename); |
| 245 | const char* LoadPersistent( |
| 246 | StringRef filename, std::function<void(size_t line, const char* msg)> warn); |
| 247 | |
| 248 | /* |
| 249 | * Utility Functions |
| 250 | */ |
| 251 | |
| 252 | /* timestamp */ |
| 253 | unsigned long long Now(); |
| 254 | |
| 255 | /* logging */ |
| 256 | typedef std::function<void(unsigned int level, const char* file, |
| 257 | unsigned int line, const char* msg)> LogFunc; |
| 258 | void SetLogger(LogFunc func, unsigned int min_level); |
| 259 | |
| 260 | } // namespace nt |
| 261 | |
| 262 | #endif /* NTCORE_CPP_H_ */ |