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 "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 NetworkTableMode* NetworkTable::mode = &NetworkTableMode::Server;
27 int NetworkTable::port = DEFAULT_PORT;
28 std::string NetworkTable::ipAddress;
29 ReentrantSemaphore NetworkTable::STATIC_LOCK;
30 
31 
32 
33 
34 
35 
36 void NetworkTable::CheckInit(){
37  {
38  Synchronized sync(STATIC_LOCK);
39  if(staticProvider!=NULL)
40  throw new IllegalStateException("Network tables has already been initialized");
41  }
42 }
43 
45  CheckInit();
46  staticProvider = new NetworkTableProvider(*(mode->CreateNode(ipAddress.c_str(), port, threadManager)));
47 }
48 
50  CheckInit();
51  staticProvider = provider;
52 }
53 
55  CheckInit();
56  mode = &NetworkTableMode::Server;
57 }
58 
60  CheckInit();
61  mode = &NetworkTableMode::Server;
62 }
63 
64 void NetworkTable::SetTeam(int team){
65  char tmp[30];
66  sprintf(tmp, "%d.%d.%d.%d\n", 10, team/100, team%100, 2);
67  SetIPAddress(tmp);
68 }
69 
70 void NetworkTable::SetIPAddress(const char* address){
71  CheckInit();
72  ipAddress = address;
73 }
74 
76  if(staticProvider==NULL){
77  Initialize();
78  }
79  std::string tmp(PATH_SEPARATOR);
80  tmp+=key;
81  return (NetworkTable*)staticProvider->GetTable(tmp);
82 }
83 
84 
85 
86 NetworkTable::NetworkTable(std::string _path, NetworkTableProvider& _provider) :
87  path(_path), entryCache(_path), absoluteKeyCache(_path), provider(_provider), node(provider.GetNode()) {
88 }
89 NetworkTable::~NetworkTable(){
90 }
91 
92 bool NetworkTable::IsConnected() {
93  return node.IsConnected();
94 }
95 
96 bool NetworkTable::IsServer() {
97  return node.IsServer();
98 }
99 
100 
101 void NetworkTable::AddConnectionListener(IRemoteConnectionListener* listener, bool immediateNotify) {
102  map<IRemoteConnectionListener*, NetworkTableConnectionListenerAdapter*>::iterator itr = connectionListenerMap.find(listener);
103  if(itr != connectionListenerMap.end()){
104  throw IllegalStateException("Cannot add the same listener twice");
105  }
106  else{
108  connectionListenerMap[listener] = adapter;
109  node.AddConnectionListener(adapter, immediateNotify);
110  }
111 }
112 
113 void NetworkTable::RemoveConnectionListener(IRemoteConnectionListener* listener) {
114  map<IRemoteConnectionListener*, NetworkTableConnectionListenerAdapter*>::iterator itr = connectionListenerMap.find(listener);
115  if(itr != connectionListenerMap.end()){
116  node.RemoveConnectionListener(itr->second);
117  delete itr->second;
118  connectionListenerMap.erase(itr);
119  }
120 }
121 
122 
123 void NetworkTable::AddTableListener(ITableListener* listener) {
124  AddTableListener(listener, false);
125 }
126 
127 void NetworkTable::AddTableListener(ITableListener* listener, bool immediateNotify) {
128  std::string tmp(path);
129  tmp+=PATH_SEPARATOR;
130  NetworkTableListenerAdapter* adapter = new NetworkTableListenerAdapter(tmp, this, listener);
131  listenerMap.insert ( pair<ITableListener*,ITableListener*>(listener, adapter) );
132  node.AddTableListener(adapter, immediateNotify);
133 }
134 void NetworkTable::AddTableListener(std::string key, ITableListener* listener, bool immediateNotify) {
135  NetworkTableKeyListenerAdapter* adapter = new NetworkTableKeyListenerAdapter(key, absoluteKeyCache.Get(key), this, listener);
136  listenerMap.insert ( pair<ITableListener*,ITableListener*>(listener, adapter) );
137  node.AddTableListener(adapter, immediateNotify);
138 }
139 void NetworkTable::AddSubTableListener(ITableListener* listener) {
140  NetworkTableSubListenerAdapter* adapter = new NetworkTableSubListenerAdapter(path, this, listener);
141  listenerMap.insert ( pair<ITableListener*,ITableListener*>(listener, adapter) );
142  node.AddTableListener(adapter, true);
143 }
144 
145 void NetworkTable::RemoveTableListener(ITableListener* listener) {
146  multimap<ITableListener*,ITableListener*>::iterator itr;
147  pair<multimap<ITableListener*,ITableListener*>::iterator,multimap<ITableListener*,ITableListener*>::iterator> itrs = listenerMap.equal_range(listener);
148  for (itr=itrs.first; itr!=itrs.second; ++itr){
149  node.RemoveTableListener(itr->second);
150  delete itr->second;
151  }
152  listenerMap.erase(itrs.first, itrs.second);
153 }
154 
155 NetworkTableEntry* NetworkTable::GetEntry(std::string key){
156  {
157  Synchronized sync(LOCK);
158  return entryCache.Get(key);
159  }
160 }
161 
162 
164  {
165  Synchronized sync(LOCK);
166  return (NetworkTable*)provider.GetTable(absoluteKeyCache.Get(key));
167  }
168 }
169 
170 
171 bool NetworkTable::ContainsKey(std::string key) {
172  return node.ContainsKey(absoluteKeyCache.Get(key));
173 }
174 
175 bool NetworkTable::ContainsSubTable(std::string key){
176  std::string subtablePrefix(absoluteKeyCache.Get(key));
177  subtablePrefix+=PATH_SEPARATOR;
178  std::vector<std::string>* keys = node.GetEntryStore().keys();
179  for(unsigned int i = 0; i<keys->size(); ++i){
180  if(keys->at(i).compare(0, subtablePrefix.size(), subtablePrefix)==0){
181  delete keys;
182  return true;
183  }
184  }
185  delete keys;
186  return false;
187 }
188 
189 
190 void NetworkTable::PutNumber(std::string key, double value) {
191  EntryValue eValue;
192  eValue.f = value;
193  PutValue(key, &DefaultEntryTypes::DOUBLE, eValue);
194 }
195 
196 
197 double NetworkTable::GetNumber(std::string key) {
198  return node.GetDouble(absoluteKeyCache.Get(key));
199 }
200 
201 
202 double NetworkTable::GetNumber(std::string key, double defaultValue) {
203  try {
204  return node.GetDouble(absoluteKeyCache.Get(key));
205  } catch (TableKeyNotDefinedException& e) {
206  return defaultValue;
207  }
208 }
209 
210 
211 void NetworkTable::PutString(std::string key, std::string value) {
212  EntryValue eValue;
213  eValue.ptr = &value;
214  PutValue(key, &DefaultEntryTypes::STRING, eValue);
215 }
216 
217 
218 std::string NetworkTable::GetString(std::string key) {
219  return node.GetString(absoluteKeyCache.Get(key));
220 }
221 
222 
223 std::string NetworkTable::GetString(std::string key, std::string defaultValue) {
224  try {
225  return node.GetString(absoluteKeyCache.Get(key));
226  } catch (TableKeyNotDefinedException& e) {
227  return defaultValue;
228  }
229 }
230 
231 
232 void NetworkTable::PutBoolean(std::string key, bool value) {
233  EntryValue eValue;
234  eValue.b = value;
235  PutValue(key, &DefaultEntryTypes::BOOLEAN, eValue);
236 }
237 
238 
239 bool NetworkTable::GetBoolean(std::string key) {
240  return node.GetBoolean(absoluteKeyCache.Get(key));
241 }
242 
243 
244 bool NetworkTable::GetBoolean(std::string key, bool defaultValue) {
245  try {
246  return node.GetBoolean(absoluteKeyCache.Get(key));
247  } catch (TableKeyNotDefinedException& e) {
248  return defaultValue;
249  }
250 }
251 
252 void NetworkTable::PutValue(std::string key, NetworkTableEntryType* type, EntryValue value){
253  NetworkTableEntry* entry = entryCache.Get(key);
254  if(entry!=NULL)
255  node.PutValue(entry, value);//TODO pass type along or do some kind of type check
256  else
257  node.PutValue(absoluteKeyCache.Get(key), type, value);
258 }
259 
260 void NetworkTable::RetrieveValue(std::string key, ComplexData& externalValue) {
261  node.retrieveValue(absoluteKeyCache.Get(key), externalValue);
262 }
263 
264 
265 void NetworkTable::PutValue(std::string key, ComplexData& value){
266  EntryValue eValue;
267  eValue.ptr = &value;
268  PutValue(key, &value.GetType(), eValue);
269 }
270 
271 
273  return node.GetValue(absoluteKeyCache.Get(key));
274 }
275 
276 EntryValue NetworkTable::GetValue(std::string key, EntryValue defaultValue) {
277  try {
278  return node.GetValue(absoluteKeyCache.Get(key));
279  } catch(TableKeyNotDefinedException& e){
280  return defaultValue;
281  }
282 }
283 
284 
285 
286 
287 
288 
289 //NetworkTableKeyCache
290 NetworkTableKeyCache::NetworkTableKeyCache(std::string _path):path(_path){}
291 NetworkTableKeyCache::~NetworkTableKeyCache(){}
292 std::string NetworkTableKeyCache::Calc(const std::string& key){
293  std::string tmp(path);
295  tmp+=key;
296  return tmp;
297 }
298 //Entry Cache
299 EntryCache::EntryCache(std::string& _path):path(_path){}
300 EntryCache::~EntryCache(){}
301 
302 NetworkTableEntry* EntryCache::Get(std::string& key){
303  return cache[key];
304 }
305 

Generated on Tue Feb 5 2013 00:55:09 for WPILibC++ by doxygen 1.8.1.2