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 NETWORKTABLE_H_ |
| 9 | #define NETWORKTABLE_H_ |
| 10 | |
| 11 | #include <functional> |
| 12 | #include <mutex> |
| 13 | #include <vector> |
| 14 | |
| 15 | #include "tables/ITable.h" |
| 16 | |
| 17 | /** |
| 18 | * A network table that knows its subtable path. |
| 19 | */ |
| 20 | class NetworkTable : public ITable { |
| 21 | private: |
| 22 | struct private_init {}; |
| 23 | |
| 24 | std::string m_path; |
| 25 | std::mutex m_mutex; |
| 26 | typedef std::pair<ITableListener*, unsigned int> Listener; |
| 27 | std::vector<Listener> m_listeners; |
| 28 | |
| 29 | static std::string s_ip_address; |
| 30 | static std::string s_persistent_filename; |
| 31 | static bool s_client; |
| 32 | static bool s_running; |
| 33 | static unsigned int s_port; |
| 34 | |
| 35 | public: |
| 36 | NetworkTable(llvm::StringRef path, const private_init&); |
| 37 | virtual ~NetworkTable(); |
| 38 | |
| 39 | /** |
| 40 | * The path separator for sub-tables and keys |
| 41 | * |
| 42 | */ |
| 43 | static const char PATH_SEPARATOR_CHAR; |
| 44 | |
| 45 | /** |
| 46 | * @throws IOException |
| 47 | */ |
| 48 | static void Initialize(); |
| 49 | static void Shutdown(); |
| 50 | |
| 51 | /** |
| 52 | * set that network tables should be a client |
| 53 | * This must be called before initialize or GetTable |
| 54 | */ |
| 55 | static void SetClientMode(); |
| 56 | |
| 57 | /** |
| 58 | * set that network tables should be a server |
| 59 | * This must be called before initialize or GetTable |
| 60 | */ |
| 61 | static void SetServerMode(); |
| 62 | |
| 63 | /** |
| 64 | * set the team the robot is configured for (this will set the mdns address |
| 65 | * that network tables will connect to in client mode) |
| 66 | * This must be called before initialize or GetTable |
| 67 | * @param team the team number |
| 68 | */ |
| 69 | static void SetTeam(int team); |
| 70 | |
| 71 | /** |
| 72 | * @param address the adress that network tables will connect to in client |
| 73 | * mode |
| 74 | */ |
| 75 | static void SetIPAddress(llvm::StringRef address); |
| 76 | |
| 77 | /** |
| 78 | * @param port the port number that network tables will connect to in client |
| 79 | * mode or listen to in server mode |
| 80 | */ |
| 81 | static void SetPort(unsigned int port); |
| 82 | |
| 83 | /** |
| 84 | * Sets the persistent filename. |
| 85 | * @param filename the filename that the network tables server uses for |
| 86 | * automatic loading and saving of persistent values |
| 87 | */ |
| 88 | static void SetPersistentFilename(llvm::StringRef filename); |
| 89 | |
| 90 | /** |
| 91 | * Sets the network identity. |
| 92 | * This is provided in the connection info on the remote end. |
| 93 | * @param name identity |
| 94 | */ |
| 95 | static void SetNetworkIdentity(llvm::StringRef name); |
| 96 | |
| 97 | /** |
| 98 | * Deletes ALL keys in ALL subtables. Use with caution! |
| 99 | */ |
| 100 | static void GlobalDeleteAll(); |
| 101 | |
| 102 | /** |
| 103 | * Flushes all updated values immediately to the network. |
| 104 | * Note: This is rate-limited to protect the network from flooding. |
| 105 | * This is primarily useful for synchronizing network updates with |
| 106 | * user code. |
| 107 | */ |
| 108 | static void Flush(); |
| 109 | |
| 110 | /** |
| 111 | * Set the periodic update rate. |
| 112 | * |
| 113 | * @param interval update interval in seconds (range 0.1 to 1.0) |
| 114 | */ |
| 115 | static void SetUpdateRate(double interval); |
| 116 | |
| 117 | /** |
| 118 | * Saves persistent keys to a file. The server does this automatically. |
| 119 | * |
| 120 | * @param filename file name |
| 121 | * @return Error (or nullptr). |
| 122 | */ |
| 123 | static const char* SavePersistent(llvm::StringRef filename); |
| 124 | |
| 125 | /** |
| 126 | * Loads persistent keys from a file. The server does this automatically. |
| 127 | * |
| 128 | * @param filename file name |
| 129 | * @param warn callback function called for warnings |
| 130 | * @return Error (or nullptr). |
| 131 | */ |
| 132 | static const char* LoadPersistent( |
| 133 | llvm::StringRef filename, |
| 134 | std::function<void(size_t line, const char* msg)> warn); |
| 135 | |
| 136 | /** |
| 137 | * Gets the table with the specified key. If the table does not exist, a new |
| 138 | *table will be created.<br> |
| 139 | * This will automatically initialize network tables if it has not been |
| 140 | *already |
| 141 | * |
| 142 | * @param key |
| 143 | * the key name |
| 144 | * @return the network table requested |
| 145 | */ |
| 146 | static std::shared_ptr<NetworkTable> GetTable(llvm::StringRef key); |
| 147 | |
| 148 | void AddTableListener(ITableListener* listener) override; |
| 149 | void AddTableListener(ITableListener* listener, |
| 150 | bool immediateNotify) override; |
| 151 | void AddTableListenerEx(ITableListener* listener, |
| 152 | unsigned int flags) override; |
| 153 | void AddTableListener(llvm::StringRef key, ITableListener* listener, |
| 154 | bool immediateNotify) override; |
| 155 | void AddTableListenerEx(llvm::StringRef key, ITableListener* listener, |
| 156 | unsigned int flags) override; |
| 157 | void AddSubTableListener(ITableListener* listener) override; |
| 158 | void AddSubTableListener(ITableListener* listener, bool localNotify) override; |
| 159 | void RemoveTableListener(ITableListener* listener) override; |
| 160 | |
| 161 | /** |
| 162 | * Returns the table at the specified key. If there is no table at the |
| 163 | * specified key, it will create a new table |
| 164 | * |
| 165 | * @param key |
| 166 | * the key name |
| 167 | * @return the networktable to be returned |
| 168 | */ |
| 169 | std::shared_ptr<ITable> GetSubTable(llvm::StringRef key) const override; |
| 170 | |
| 171 | /** |
| 172 | * Determines whether the given key is in this table. |
| 173 | * |
| 174 | * @param key the key to search for |
| 175 | * @return true if the table as a value assigned to the given key |
| 176 | */ |
| 177 | bool ContainsKey(llvm::StringRef key) const override; |
| 178 | |
| 179 | /** |
| 180 | * Determines whether there exists a non-empty subtable for this key |
| 181 | * in this table. |
| 182 | * |
| 183 | * @param key the key to search for |
| 184 | * @return true if there is a subtable with the key which contains at least |
| 185 | * one key/subtable of its own |
| 186 | */ |
| 187 | bool ContainsSubTable(llvm::StringRef key) const override; |
| 188 | |
| 189 | /** |
| 190 | * @param types bitmask of types; 0 is treated as a "don't care". |
| 191 | * @return keys currently in the table |
| 192 | */ |
| 193 | std::vector<std::string> GetKeys(int types = 0) const override; |
| 194 | |
| 195 | /** |
| 196 | * @return subtables currently in the table |
| 197 | */ |
| 198 | std::vector<std::string> GetSubTables() const override; |
| 199 | |
| 200 | /** |
| 201 | * Makes a key's value persistent through program restarts. |
| 202 | * |
| 203 | * @param key the key to make persistent |
| 204 | */ |
| 205 | void SetPersistent(llvm::StringRef key) override; |
| 206 | |
| 207 | /** |
| 208 | * Stop making a key's value persistent through program restarts. |
| 209 | * The key cannot be null. |
| 210 | * |
| 211 | * @param key the key name |
| 212 | */ |
| 213 | void ClearPersistent(llvm::StringRef key) override; |
| 214 | |
| 215 | /** |
| 216 | * Returns whether the value is persistent through program restarts. |
| 217 | * The key cannot be null. |
| 218 | * |
| 219 | * @param key the key name |
| 220 | */ |
| 221 | bool IsPersistent(llvm::StringRef key) const override; |
| 222 | |
| 223 | /** |
| 224 | * Sets flags on the specified key in this table. The key can |
| 225 | * not be null. |
| 226 | * |
| 227 | * @param key the key name |
| 228 | * @param flags the flags to set (bitmask) |
| 229 | */ |
| 230 | void SetFlags(llvm::StringRef key, unsigned int flags) override; |
| 231 | |
| 232 | /** |
| 233 | * Clears flags on the specified key in this table. The key can |
| 234 | * not be null. |
| 235 | * |
| 236 | * @param key the key name |
| 237 | * @param flags the flags to clear (bitmask) |
| 238 | */ |
| 239 | void ClearFlags(llvm::StringRef key, unsigned int flags) override; |
| 240 | |
| 241 | /** |
| 242 | * Returns the flags for the specified key. |
| 243 | * |
| 244 | * @param key the key name |
| 245 | * @return the flags, or 0 if the key is not defined |
| 246 | */ |
| 247 | unsigned int GetFlags(llvm::StringRef key) const override; |
| 248 | |
| 249 | /** |
| 250 | * Deletes the specified key in this table. |
| 251 | * |
| 252 | * @param key the key name |
| 253 | */ |
| 254 | void Delete(llvm::StringRef key) override; |
| 255 | |
| 256 | /** |
| 257 | * Put a number in the table |
| 258 | * |
| 259 | * @param key the key to be assigned to |
| 260 | * @param value the value that will be assigned |
| 261 | * @return False if the table key already exists with a different type |
| 262 | */ |
| 263 | bool PutNumber(llvm::StringRef key, double value) override; |
| 264 | |
| 265 | /** |
| 266 | * Gets the number associated with the given name. |
| 267 | * |
| 268 | * @param key the key to look up |
| 269 | * @return the value associated with the given key |
| 270 | * @throws TableKeyNotDefinedException if there is no value associated with |
| 271 | * the given key |
| 272 | * @deprecated This exception-raising method has been replaced by the |
| 273 | * default-taking method. |
| 274 | */ |
| 275 | NT_DEPRECATED("Raises an exception if key not found; " |
| 276 | "use GetNumber(StringRef key, double defaultValue) instead") |
| 277 | virtual double GetNumber(llvm::StringRef key) const override; |
| 278 | |
| 279 | /** |
| 280 | * Gets the number associated with the given name. |
| 281 | * |
| 282 | * @param key the key to look up |
| 283 | * @param defaultValue the value to be returned if no value is found |
| 284 | * @return the value associated with the given key or the given default value |
| 285 | * if there is no value associated with the key |
| 286 | */ |
| 287 | virtual double GetNumber(llvm::StringRef key, |
| 288 | double defaultValue) const override; |
| 289 | |
| 290 | /** |
| 291 | * Put a string in the table |
| 292 | * |
| 293 | * @param key the key to be assigned to |
| 294 | * @param value the value that will be assigned |
| 295 | * @return False if the table key already exists with a different type |
| 296 | */ |
| 297 | virtual bool PutString(llvm::StringRef key, llvm::StringRef value) override; |
| 298 | |
| 299 | /** |
| 300 | * Gets the string associated with the given name. |
| 301 | * |
| 302 | * @param key the key to look up |
| 303 | * @return the value associated with the given key |
| 304 | * @throws TableKeyNotDefinedException if there is no value associated with |
| 305 | * the given key |
| 306 | * @deprecated This exception-raising method has been replaced by the |
| 307 | * default-taking method. |
| 308 | */ |
| 309 | NT_DEPRECATED("Raises an exception if key not found; " |
| 310 | "use GetString(StringRef key, StringRef defaultValue) instead") |
| 311 | virtual std::string GetString(llvm::StringRef key) const override; |
| 312 | |
| 313 | /** |
| 314 | * Gets the string associated with the given name. If the key does not |
| 315 | * exist or is of different type, it will return the default value. |
| 316 | * |
| 317 | * @param key the key to look up |
| 318 | * @param defaultValue the value to be returned if no value is found |
| 319 | * @return the value associated with the given key or the given default value |
| 320 | * if there is no value associated with the key |
| 321 | */ |
| 322 | virtual std::string GetString(llvm::StringRef key, |
| 323 | llvm::StringRef defaultValue) const override; |
| 324 | |
| 325 | /** |
| 326 | * Put a boolean in the table |
| 327 | * |
| 328 | * @param key the key to be assigned to |
| 329 | * @param value the value that will be assigned |
| 330 | * @return False if the table key already exists with a different type |
| 331 | */ |
| 332 | virtual bool PutBoolean(llvm::StringRef key, bool value) override; |
| 333 | |
| 334 | /** |
| 335 | * Gets the boolean associated with the given name. |
| 336 | * |
| 337 | * @param key the key to look up |
| 338 | * @return the value associated with the given key |
| 339 | * @throws TableKeyNotDefinedException if there is no value associated with |
| 340 | * the given key |
| 341 | * @deprecated This exception-raising method has been replaced by the |
| 342 | * default-taking method. |
| 343 | */ |
| 344 | NT_DEPRECATED("Raises an exception if key not found; " |
| 345 | "use GetBoolean(StringRef key, bool defaultValue) instead") |
| 346 | virtual bool GetBoolean(llvm::StringRef key) const override; |
| 347 | |
| 348 | /** |
| 349 | * Gets the boolean associated with the given name. If the key does not |
| 350 | * exist or is of different type, it will return the default value. |
| 351 | * |
| 352 | * @param key the key to look up |
| 353 | * @param defaultValue the value to be returned if no value is found |
| 354 | * @return the value associated with the given key or the given default value |
| 355 | * if there is no value associated with the key |
| 356 | */ |
| 357 | virtual bool GetBoolean(llvm::StringRef key, |
| 358 | bool defaultValue) const override; |
| 359 | |
| 360 | /** |
| 361 | * Put a value in the table |
| 362 | * |
| 363 | * @param key the key to be assigned to |
| 364 | * @param value the value that will be assigned |
| 365 | * @return False if the table key already exists with a different type |
| 366 | */ |
| 367 | bool PutValue(llvm::StringRef key, std::shared_ptr<nt::Value> value) override; |
| 368 | |
| 369 | /** |
| 370 | * Gets the value associated with a key as an object |
| 371 | * |
| 372 | * @param key the key of the value to look up |
| 373 | * @return the value associated with the given key, or nullptr if the key |
| 374 | * does not exist |
| 375 | */ |
| 376 | std::shared_ptr<nt::Value> GetValue(llvm::StringRef key) const override; |
| 377 | }; |
| 378 | |
| 379 | #endif // NETWORKTABLE_H_ |