WPILib 2012
WPILibRoboticsLibraryforFRC
NetworkTables/NetworkTable.h
00001 /*----------------------------------------------------------------------------*/
00002 /* Copyright (c) FIRST 2011. All Rights Reserved.                             */
00003 /* Open Source Software - may be modified and shared by FRC teams. The code   */
00004 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib.  */
00005 /*----------------------------------------------------------------------------*/
00006 
00007 #ifndef __NETWORK_TABLE_H__
00008 #define __NETWORK_TABLE_H__
00009 
00010 #include "ErrorBase.h"
00011 #include <map>
00012 #include <set>
00013 #include <vector>
00014 
00015 namespace NetworkTables
00016 {
00017     class Buffer;
00018     class Connection;
00019     class Entry;
00020     class Key;
00021     class NetworkQueue;
00022     class TableAssignment;
00023     class TableEntry;
00024 }
00025 
00026 class NetworkTableChangeListener;
00027 class NetworkTableAdditionListener;
00028 class NetworkTableConnectionListener;
00029 
00030 class NetworkTable : public ErrorBase {
00031         friend class NetworkTables::Connection;
00032         friend class NetworkTables::Key;
00033         friend class NetworkTables::TableAssignment;
00034         friend class NetworkTables::TableEntry;
00035 public:
00036         NetworkTable();
00037         ~NetworkTable();
00038         static void Initialize();
00039         static NetworkTable *GetTable(const char *tableName);
00040         static NetworkTable *GetTable(int id);
00041         std::vector<const char *> GetKeys();
00042         void BeginTransaction();
00043         void EndTransaction();
00044         void AddChangeListener(const char *keyName, NetworkTableChangeListener *listener);
00045         void AddChangeListenerAny(NetworkTableChangeListener *listener);
00046         void RemoveChangeListener(const char *keyName, NetworkTableChangeListener *listener);
00047         void RemoveChangeListenerAny(NetworkTableChangeListener *listener);
00048         void AddAdditionListener(NetworkTableAdditionListener *listener);
00049         void RemoveAdditionListener(NetworkTableAdditionListener *listener);
00050         void AddConnectionListener(NetworkTableConnectionListener *listener, bool immediateNotify);
00051         void RemoveConnectionListener(NetworkTableConnectionListener *listener);
00052         bool IsConnected();
00053         bool ContainsKey(const char *keyName);
00054         NetworkTables::Entry *GetEntry(const char *keyName);
00055         
00056         int GetInt(const char *keyName);
00057         bool GetBoolean(const char *keyName);
00058         double GetDouble(const char *keyName);
00059         int GetString(const char *keyName, char *value, int len);
00060         std::string GetString(std::string keyName);
00061         NetworkTable *GetSubTable(const char *keyName);
00062         void PutInt(const char *keyName, int value);
00063         void PutBoolean(const char *keyName, bool value);
00064         void PutDouble(const char *keyName, double value);
00065         void PutString(const char *keyName, const char *value);
00066         void PutString(std::string keyName, std::string value);
00067         void PutSubTable(const char *keyName, NetworkTable *value);
00068         
00069 private:
00070         static UINT32 GrabId();
00071         void ProcessTransaction(bool confirmed, NetworkTables::NetworkQueue *transaction);
00072         UINT32 GetId() {return m_id;}
00073         void AddConnection(NetworkTables::Connection *connection);
00074         void RemoveConnection(NetworkTables::Connection *connection);
00075         NetworkTables::Key *GetKey(const char *keyName);
00076         void Put(const char *keyName, std::auto_ptr<NetworkTables::Entry> value);
00077         void Send(NetworkTables::Entry *entry);
00078         void Got(bool confirmed, NetworkTables::Key *key, std::auto_ptr<NetworkTables::Entry> value);
00079         void AlertListeners(bool isNew, bool confirmed, const char *keyName, NetworkTables::Entry *value);
00080         void EncodeName(NetworkTables::Buffer *buffer);
00081 
00083         SEM_ID m_dataLock;
00085         typedef std::map<std::string, NetworkTables::Key *> DataMap;
00086         DataMap m_data;
00088         std::set<NetworkTables::Connection *> m_connections;
00090         SEM_ID m_listenerLock;
00092         std::set<NetworkTableChangeListener *> m_listenToAllListeners;
00094         typedef std::map<std::string, std::set<NetworkTableChangeListener *> > ListenersMap;
00095         ListenersMap m_listeners;
00097         std::set<NetworkTableAdditionListener *> m_additionListeners;
00099         std::set<NetworkTableConnectionListener *> m_connectionListeners;
00101         UINT32 m_id;
00103         NetworkTables::NetworkQueue *m_transaction;
00105         int m_transactionCount;
00107         NetworkTables::NetworkQueue *m_hasChanged;
00109         NetworkTables::NetworkQueue *m_hasAdded;
00110 
00112         typedef std::map<std::string, NetworkTable *> TableNameMap;
00113         static TableNameMap _tableNameMap;
00115         typedef std::map<UINT32, NetworkTable *> TableIdMap;
00116         static TableIdMap _tableIdMap;
00118         static UINT32 _currentId;
00120         static bool _initialized;
00122         static SEM_ID _staticMemberMutex;
00124         DISALLOW_COPY_AND_ASSIGN(NetworkTable);
00125 };
00126 
00127 #endif // __NETWORK_TABLE_H__
 All Classes Functions Variables