2 #include "networktables/NetworkTable.h"
4 #include "networktables2/thread/NTThreadManager.h"
5 #include "networktables2/thread/DefaultThreadManager.h"
6 #include "networktables2/NetworkTableEntry.h"
7 #include "networktables2/util/StringCache.h"
8 #include "networktables/NetworkTableProvider.h"
9 #include "networktables/NetworkTableMode.h"
10 #include "OSAL/Synchronized.h"
11 #include "tables/TableKeyNotDefinedException.h"
12 #include "networktables2/type/DefaultEntryTypes.h"
13 #include "tables/ITableListener.h"
14 #include "networktables/NetworkTableConnectionListenerAdapter.h"
15 #include "networktables/NetworkTableKeyListenerAdapter.h"
16 #include "networktables/NetworkTableListenerAdapter.h"
17 #include "networktables/NetworkTableSubListenerAdapter.h"
20 const char NetworkTable::PATH_SEPARATOR_CHAR =
'/';
27 void* NetworkTable::streamFactory = NULL;
29 StreamDeleter streamDeleter = NULL;
31 int NetworkTable::port = DEFAULT_PORT;
32 std::string NetworkTable::ipAddress;
40 void NetworkTable::CheckInit(){
41 printf(
"[NT] NetworkTable::CheckInit()...\n");
44 if(staticProvider!=NULL)
51 printf(
"[NT] NetworkTable::Initialize()...\n");
52 staticProvider =
new NetworkTableProvider(*(staticNode = mode->
CreateNode(ipAddress.c_str(), port, threadManager, streamFactory, streamDeleter, typeManager)));
53 printf(
"[NT] ...NetworkTable::Initialize().\n");
56 void NetworkTable::Shutdown()
58 if (staticProvider!=NULL)
60 delete staticProvider;
68 if (streamDeleter!=NULL && streamFactory!=NULL)
70 streamDeleter(streamFactory);
74 if (typeManager!=NULL)
83 staticProvider = provider;
88 mode = &NetworkTableMode::Client;
93 mode = &NetworkTableMode::Server;
98 sprintf(tmp,
"%d.%d.%d.%d\n", 10, team/100, team%100, 2);
108 printf(
"[NT] NetworkTable::GetTable()...\n");
109 if(staticProvider==NULL){
110 printf(
"[NT] \tInitializing...\n");
115 printf(
"[NT] ...Ready to get Table.\n");
122 path(_path), entryCache(_path), absoluteKeyCache(_path), provider(_provider), node(provider.GetNode()) {
124 NetworkTable::~NetworkTable(){
137 map<IRemoteConnectionListener*, NetworkTableConnectionListenerAdapter*>::iterator itr = connectionListenerMap.find(listener);
138 if(itr != connectionListenerMap.end()){
143 connectionListenerMap[listener] = adapter;
149 map<IRemoteConnectionListener*, NetworkTableConnectionListenerAdapter*>::iterator itr = connectionListenerMap.find(listener);
150 if(itr != connectionListenerMap.end()){
153 connectionListenerMap.erase(itr);
163 std::string tmp(path);
166 listenerMap.insert ( pair<ITableListener*,ITableListener*>(listener, adapter) );
167 node.AddTableListener(adapter, immediateNotify);
171 listenerMap.insert ( pair<ITableListener*,ITableListener*>(listener, adapter) );
172 node.AddTableListener(adapter, immediateNotify);
176 listenerMap.insert ( pair<ITableListener*,ITableListener*>(listener, adapter) );
177 node.AddTableListener(adapter,
true);
181 multimap<ITableListener*,ITableListener*>::iterator itr;
182 pair<multimap<ITableListener*,ITableListener*>::iterator,multimap<ITableListener*,ITableListener*>::iterator> itrs = listenerMap.equal_range(listener);
183 for (itr=itrs.first; itr!=itrs.second; ++itr){
184 node.RemoveTableListener(itr->second);
187 listenerMap.erase(itrs.first, itrs.second);
193 return entryCache.Get(key);
207 return node.
ContainsKey(absoluteKeyCache.Get(key));
211 std::string subtablePrefix(absoluteKeyCache.Get(key));
213 std::vector<std::string>* keys = node.
GetEntryStore().keys();
214 for(
unsigned int i = 0; i<keys->size(); ++i){
215 if(keys->at(i).compare(0, subtablePrefix.size(), subtablePrefix)==0){
228 PutValue(key, &DefaultEntryTypes::DOUBLE, eValue);
233 return node.GetDouble(absoluteKeyCache.Get(key));
239 return node.GetDouble(absoluteKeyCache.Get(key));
249 PutValue(key, &DefaultEntryTypes::STRING, eValue);
254 return node.GetString(absoluteKeyCache.Get(key));
260 return node.GetString(absoluteKeyCache.Get(key));
270 PutValue(key, &DefaultEntryTypes::BOOLEAN, eValue);
275 return node.GetBoolean(absoluteKeyCache.Get(key));
281 return node.GetBoolean(absoluteKeyCache.Get(key));
292 node.
PutValue(absoluteKeyCache.Get(key), type, value);
295 void NetworkTable::RetrieveValue(std::string key,
ComplexData& externalValue) {
296 node.retrieveValue(absoluteKeyCache.Get(key), externalValue);
303 PutValue(key, &value.
GetType(), eValue);
308 return node.GetValue(absoluteKeyCache.Get(key));
313 return node.GetValue(absoluteKeyCache.Get(key));
325 NetworkTableKeyCache::NetworkTableKeyCache(std::string _path):path(_path){}
326 NetworkTableKeyCache::~NetworkTableKeyCache(){}
328 std::string tmp(path);
334 EntryCache::EntryCache(std::string& _path):path(_path){}
335 EntryCache::~EntryCache(){}
void PutValue(std::string &name, NetworkTableEntryType *type, EntryValue value)
virtual NetworkTableNode * CreateNode(const char *ipAddress, int port, NTThreadManager &threadManager, void *&streamFactory_out, StreamDeleter &streamDeleter_out, NetworkTableEntryTypeManager *&typeManager_out)=0
AbstractNetworkTableEntryStore & GetEntryStore()
static const std::string PATH_SEPARATOR
NetworkTable * GetSubTable(std::string key)
std::string Calc(const std::string &key)
static NetworkTable * GetTable(std::string key)
bool GetBoolean(std::string key)
void PutBoolean(std::string key, bool value)
static void SetClientMode()
static void SetTableProvider(NetworkTableProvider *provider)
void AddConnectionListener(IRemoteConnectionListener *listener, bool immediateNotify)
bool ContainsKey(std::string key)
static void SetServerMode()
double GetNumber(std::string key)
void AddSubTableListener(ITableListener *listener)
virtual bool IsServer()=0
static void SetIPAddress(const char *address)
static const int DEFAULT_PORT
static void SetTeam(int team)
ITable * GetTable(std::string key)
void PutNumber(std::string key, double value)
ComplexEntryType & GetType()
virtual bool IsConnected()=0
void RemoveConnectionListener(IRemoteConnectionListener *listener)
bool ContainsSubTable(std::string key)
EntryValue GetValue(std::string key)
void AddConnectionListener(IRemoteConnectionListener *listener, bool immediateNotify)
bool ContainsKey(std::string &key)
void AddTableListener(ITableListener *listener)
void PutString(std::string key, std::string value)
std::string GetString(std::string key)
void RemoveTableListener(ITableListener *listener)
void RemoveConnectionListener(IRemoteConnectionListener *listener)