Now you can download a copy of these docs so you can use them offline! Download now
NetworkTable.cpp
1 #include <map>
2 #include "networktables/NetworkTable.h"
3 #include <string>
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"
18 
19 
20 const char NetworkTable::PATH_SEPARATOR_CHAR = '/';
21 const std::string NetworkTable::PATH_SEPARATOR("/");
22 const int NetworkTable::DEFAULT_PORT = 1735;
23 
24 DefaultThreadManager NetworkTable::threadManager;
25 NetworkTableProvider* NetworkTable::staticProvider = NULL;
26 NetworkTableNode* NetworkTable::staticNode = NULL;
27 void* NetworkTable::streamFactory = NULL;
28 NetworkTableEntryTypeManager* NetworkTable::typeManager = NULL;
29 StreamDeleter streamDeleter = NULL;
30 NetworkTableMode* NetworkTable::mode = &NetworkTableMode::Server;
31 int NetworkTable::port = DEFAULT_PORT;
32 std::string NetworkTable::ipAddress;
33 NTReentrantSemaphore NetworkTable::STATIC_LOCK;
34 
35 
36 
37 
38 
39 
40 void NetworkTable::CheckInit(){
41  printf("[NT] NetworkTable::CheckInit()...\n");
42  {
43  NTSynchronized sync(STATIC_LOCK);
44  if(staticProvider!=NULL)
45  throw new IllegalStateException("Network tables has already been initialized");
46  }
47 }
48 
50  CheckInit();
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");
54 }
55 
56 void NetworkTable::Shutdown()
57 {
58  if (staticProvider!=NULL)
59  {
60  delete staticProvider;
61  staticProvider=NULL;
62  }
63  if (staticNode!=NULL)
64  {
65  delete staticNode;
66  staticNode=NULL;
67  }
68  if (streamDeleter!=NULL && streamFactory!=NULL)
69  {
70  streamDeleter(streamFactory);
71  streamFactory=NULL;
72  streamDeleter=NULL;
73  }
74  if (typeManager!=NULL)
75  {
76  delete typeManager;
77  typeManager=NULL;
78  }
79 }
80 
82  CheckInit();
83  staticProvider = provider;
84 }
85 
87  CheckInit();
88  mode = &NetworkTableMode::Client;
89 }
90 
92  CheckInit();
93  mode = &NetworkTableMode::Server;
94 }
95 
96 void NetworkTable::SetTeam(int team){
97  char tmp[30];
98  sprintf(tmp, "%d.%d.%d.%d\n", 10, team/100, team%100, 2);
99  SetIPAddress(tmp);
100 }
101 
102 void NetworkTable::SetIPAddress(const char* address){
103  CheckInit();
104  ipAddress = address;
105 }
106 
108  printf("[NT] NetworkTable::GetTable()...\n");
109  if(staticProvider==NULL){
110  printf("[NT] \tInitializing...\n");
111  Initialize();
112  }
113  std::string tmp(PATH_SEPARATOR);
114  tmp+=key;
115  printf("[NT] ...Ready to get Table.\n");
116  return (NetworkTable*)staticProvider->GetTable(tmp);
117 }
118 
119 
120 
121 NetworkTable::NetworkTable(std::string _path, NetworkTableProvider& _provider) :
122  path(_path), entryCache(_path), absoluteKeyCache(_path), provider(_provider), node(provider.GetNode()) {
123 }
124 NetworkTable::~NetworkTable(){
125 }
126 
128  return node.IsConnected();
129 }
130 
132  return node.IsServer();
133 }
134 
135 
136 void NetworkTable::AddConnectionListener(IRemoteConnectionListener* listener, bool immediateNotify) {
137  map<IRemoteConnectionListener*, NetworkTableConnectionListenerAdapter*>::iterator itr = connectionListenerMap.find(listener);
138  if(itr != connectionListenerMap.end()){
139  throw IllegalStateException("Cannot add the same listener twice");
140  }
141  else{
143  connectionListenerMap[listener] = adapter;
144  node.AddConnectionListener(adapter, immediateNotify);
145  }
146 }
147 
149  map<IRemoteConnectionListener*, NetworkTableConnectionListenerAdapter*>::iterator itr = connectionListenerMap.find(listener);
150  if(itr != connectionListenerMap.end()){
151  node.RemoveConnectionListener(itr->second);
152  delete itr->second;
153  connectionListenerMap.erase(itr);
154  }
155 }
156 
157 
159  AddTableListener(listener, false);
160 }
161 
162 void NetworkTable::AddTableListener(ITableListener* listener, bool immediateNotify) {
163  std::string tmp(path);
164  tmp+=PATH_SEPARATOR;
165  NetworkTableListenerAdapter* adapter = new NetworkTableListenerAdapter(tmp, this, listener);
166  listenerMap.insert ( pair<ITableListener*,ITableListener*>(listener, adapter) );
167  node.AddTableListener(adapter, immediateNotify);
168 }
169 void NetworkTable::AddTableListener(std::string key, ITableListener* listener, bool immediateNotify) {
170  NetworkTableKeyListenerAdapter* adapter = new NetworkTableKeyListenerAdapter(key, absoluteKeyCache.Get(key), this, listener);
171  listenerMap.insert ( pair<ITableListener*,ITableListener*>(listener, adapter) );
172  node.AddTableListener(adapter, immediateNotify);
173 }
175  NetworkTableSubListenerAdapter* adapter = new NetworkTableSubListenerAdapter(path, this, listener);
176  listenerMap.insert ( pair<ITableListener*,ITableListener*>(listener, adapter) );
177  node.AddTableListener(adapter, true);
178 }
179 
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);
185  delete itr->second;
186  }
187  listenerMap.erase(itrs.first, itrs.second);
188 }
189 
190 NetworkTableEntry* NetworkTable::GetEntry(std::string key){
191  {
192  NTSynchronized sync(LOCK);
193  return entryCache.Get(key);
194  }
195 }
196 
197 
199  {
200  NTSynchronized sync(LOCK);
201  return (NetworkTable*)provider.GetTable(absoluteKeyCache.Get(key));
202  }
203 }
204 
205 
206 bool NetworkTable::ContainsKey(std::string key) {
207  return node.ContainsKey(absoluteKeyCache.Get(key));
208 }
209 
210 bool NetworkTable::ContainsSubTable(std::string key){
211  std::string subtablePrefix(absoluteKeyCache.Get(key));
212  subtablePrefix+=PATH_SEPARATOR;
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){
216  delete keys;
217  return true;
218  }
219  }
220  delete keys;
221  return false;
222 }
223 
224 
225 void NetworkTable::PutNumber(std::string key, double value) {
226  EntryValue eValue;
227  eValue.f = value;
228  PutValue(key, &DefaultEntryTypes::DOUBLE, eValue);
229 }
230 
231 
232 double NetworkTable::GetNumber(std::string key) {
233  return node.GetDouble(absoluteKeyCache.Get(key));
234 }
235 
236 
237 double NetworkTable::GetNumber(std::string key, double defaultValue) {
238  try {
239  return node.GetDouble(absoluteKeyCache.Get(key));
240  } catch (TableKeyNotDefinedException& e) {
241  return defaultValue;
242  }
243 }
244 
245 
246 void NetworkTable::PutString(std::string key, std::string value) {
247  EntryValue eValue;
248  eValue.ptr = &value;
249  PutValue(key, &DefaultEntryTypes::STRING, eValue);
250 }
251 
252 
253 std::string NetworkTable::GetString(std::string key) {
254  return node.GetString(absoluteKeyCache.Get(key));
255 }
256 
257 
258 std::string NetworkTable::GetString(std::string key, std::string defaultValue) {
259  try {
260  return node.GetString(absoluteKeyCache.Get(key));
261  } catch (TableKeyNotDefinedException& e) {
262  return defaultValue;
263  }
264 }
265 
266 
267 void NetworkTable::PutBoolean(std::string key, bool value) {
268  EntryValue eValue;
269  eValue.b = value;
270  PutValue(key, &DefaultEntryTypes::BOOLEAN, eValue);
271 }
272 
273 
274 bool NetworkTable::GetBoolean(std::string key) {
275  return node.GetBoolean(absoluteKeyCache.Get(key));
276 }
277 
278 
279 bool NetworkTable::GetBoolean(std::string key, bool defaultValue) {
280  try {
281  return node.GetBoolean(absoluteKeyCache.Get(key));
282  } catch (TableKeyNotDefinedException& e) {
283  return defaultValue;
284  }
285 }
286 
287 void NetworkTable::PutValue(std::string key, NetworkTableEntryType* type, EntryValue value){
288  NetworkTableEntry* entry = entryCache.Get(key);
289  if(entry!=NULL)
290  node.PutValue(entry, value);//TODO pass type along or do some kind of type check
291  else
292  node.PutValue(absoluteKeyCache.Get(key), type, value);
293 }
294 
295 void NetworkTable::RetrieveValue(std::string key, ComplexData& externalValue) {
296  node.retrieveValue(absoluteKeyCache.Get(key), externalValue);
297 }
298 
299 
300 void NetworkTable::PutValue(std::string key, ComplexData& value){
301  EntryValue eValue;
302  eValue.ptr = &value;
303  PutValue(key, &value.GetType(), eValue);
304 }
305 
306 
308  return node.GetValue(absoluteKeyCache.Get(key));
309 }
310 
311 EntryValue NetworkTable::GetValue(std::string key, EntryValue defaultValue) {
312  try {
313  return node.GetValue(absoluteKeyCache.Get(key));
314  } catch(TableKeyNotDefinedException& e){
315  return defaultValue;
316  }
317 }
318 
319 
320 
321 
322 
323 
324 //NetworkTableKeyCache
325 NetworkTableKeyCache::NetworkTableKeyCache(std::string _path):path(_path){}
326 NetworkTableKeyCache::~NetworkTableKeyCache(){}
327 std::string NetworkTableKeyCache::Calc(const std::string& key){
328  std::string tmp(path);
330  tmp+=key;
331  return tmp;
332 }
333 //Entry Cache
334 EntryCache::EntryCache(std::string& _path):path(_path){}
335 EntryCache::~EntryCache(){}
336 
337 NetworkTableEntry* EntryCache::Get(std::string& key){
338  return cache[key];
339 }
340 
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
Definition: NetworkTable.h:90
NetworkTable * GetSubTable(std::string key)
std::string Calc(const std::string &key)
static NetworkTable * GetTable(std::string key)
bool GetBoolean(std::string key)
static void Initialize()
void PutBoolean(std::string key, bool value)
static void SetClientMode()
bool IsConnected()
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
Definition: NetworkTable.h:94
static void SetTeam(int team)
ITable * GetTable(std::string key)
void PutNumber(std::string key, double value)
ComplexEntryType & GetType()
Definition: ComplexData.cpp:13
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)
Definition: ITable.h:13
std::string GetString(std::string key)
void RemoveTableListener(ITableListener *listener)
void RemoveConnectionListener(IRemoteConnectionListener *listener)

Generated on Sat Apr 26 2014 12:26:45 for WPILibC++ by doxygen 1.8.6