CMSDK  2.0.1
DataMapsMemory.h
1 #if !defined(_DATAMAPSMEMORY_H_)
2 #define _DATAMAPSMEMORY_H_
3 
4 #include "MemoryManager.h"
5 #include "PsyTime.h"
6 
7 namespace cmlabs {
8 
10  uint64 size;
11  uint32 cid;
12  uint64 createdTime; // time of creation
13  uint32 typesMaxCount;
14  uint32 typesBitFieldSize;
15  uint64 typesSize;
16  uint32 contextsMaxCount;
17  uint32 contextsBitFieldSize;
18  uint64 contextsSize;
19  uint32 tagsMaxCount;
20  uint32 tagsBitFieldSize;
21  uint64 tagsSize;
22  uint32 cranksMaxCount;
23  uint32 cranksBitFieldSize;
24  uint64 cranksSize;
25 // uint32 componentsMaxCount;
26 // uint32 componentsBitFieldSize;
27 // uint32 componentsSize;
28  uint32 requestsMaxCount;
29  uint32 requestsBitFieldSize;
30  uint64 requestsSize;
31 };
32 
34  uint64 size; // total size of map structure in bytes
35  uint32 maxID; // maximum number of entries allocated
36  uint32 count; // number of entries
37  uint32 bitFieldSize; // number of bytes in the bitfield
38  // Bitfield
39  // Entries
40 };
41 
42 struct TypeMapEntry {
43  uint16 id; // id, corresponds to the index in map
44  uint8 status; // status = 0: not there, 1: in-sync, 2: ready
45  uint64 time; // timestamp when entered
46  char name[MAXKEYNAMELEN+1]; // name in text
47  std::string toXML() {
48  return utils::StringFormat("\t<type id=\"%u\" name=\"%s\" status=\"%u\" time=\"%llu\" />\n", id, name, status, time);
49  }
50  static std::string GetOuterXMLName() { return "types"; }
51  bool writeToMsg(uint32 n, DataMessage* msg) {
52  if (!msg) return false;
53  msg->setInt((int64)n, "ID", id);
54  msg->setInt((int64)n, "Status", status);
55  msg->setString((int64)n, "Name", name);
56  msg->setTime((int64)n, "CreatedTime", time);
57  return true;
58  }
59  bool writeIDToMsg(const char* arrayname, uint32 n, DataMessage* msg) {
60  if (!msg) return false;
61  msg->setString((int64)n, arrayname, name);
62  return true;
63  }
64 };
65 
67  uint16 id; // id, corresponds to the index in map
68  uint8 status; // status = 0: not there, 1: in-sync, 2: ready
69  uint64 time; // timestamp when entered
70  char name[MAXKEYNAMELEN+1]; // name in text
71  std::string toXML() {
72  return utils::StringFormat("\t<context id=\"%u\" name=\"%s\" status=\"%u\" time=\"%llu\" />\n", id, name, status, time);
73  }
74  static std::string GetOuterXMLName() { return "contexts"; }
75  bool writeToMsg(uint32 n, DataMessage* msg) {
76  if (!msg) return false;
77  msg->setInt((int64)n, "ID", id);
78  msg->setInt((int64)n, "Status", status);
79  msg->setString((int64)n, "Name", name);
80  msg->setTime((int64)n, "CreatedTime", time);
81  return true;
82  }
83  bool writeIDToMsg(const char* arrayname, uint32 n, DataMessage* msg) {
84  if (!msg) return false;
85  msg->setString((int64)n, arrayname, name);
86  return true;
87  }
88 };
89 
90 struct TagMapEntry {
91  uint32 id; // id, corresponds to the index in map
92  uint8 status; // status = 0: not there, 1: in-sync, 2: ready
93  uint64 time; // timestamp when entered
94  char name[MAXKEYNAMELEN+1]; // name in text
95  std::string toXML() {
96  return utils::StringFormat("\t<tag id=\"%u\" name=\"%s\" status=\"%u\" time=\"%llu\" />\n", id, name, status, time);
97  }
98  static std::string GetOuterXMLName() { return "tags"; }
99  bool writeToMsg(uint32 n, DataMessage* msg) {
100  if (!msg) return false;
101  msg->setInt((int64)n, "ID", id);
102  msg->setInt((int64)n, "Status", status);
103  msg->setString((int64)n, "Name", name);
104  msg->setTime((int64)n, "CreatedTime", time);
105  return true;
106  }
107  bool writeIDToMsg(const char* arrayname, uint32 n, DataMessage* msg) {
108  if (!msg) return false;
109  msg->setString((int64)n, arrayname, name);
110  return true;
111  }
112 };
113 
115  uint16 id; // id, corresponds to the index in map
116  uint8 status; // status = 0: not there, 1: in-sync, 2: ready
117  uint64 time; // timestamp when entered
118  uint32 compID; // id of parent component
119  char name[MAXKEYNAMELEN + 1]; // name in text
120  char language[MAXKEYNAMELEN + 1]; // name in text
121  char function[MAXKEYNAMELEN+1]; // name in text
122  char libraryFilename[MAXKEYNAMELEN+1]; // name in text
123  char script[MAXSCRIPTLEN+1]; // script in text
124  std::string toXML() {
125  if (strlen(script))
126  return utils::StringFormat("\t<crank id=\"%u\" name=\"%s\" status=\"%u\" time=\"%llu\" compid=\"%u\" function=\"%s\" library=\"%s\">\n\t\t%s\n\t</crank>\n",
127  id, name, status, time, compID, function, libraryFilename, script);
128  else
129  return utils::StringFormat("\t<crank id=\"%u\" name=\"%s\" status=\"%u\" time=\"%llu\" compid=\"%u\" function=\"%s\" library=\"%s\" />\n",
130  id, name, status, time, compID, function, libraryFilename);
131  }
132  static std::string GetOuterXMLName() { return "cranks"; }
133  bool writeToMsg(uint32 n, DataMessage* msg) {
134  if (!msg) return false;
135  msg->setInt((int64)n, "ID", id);
136  msg->setInt((int64)n, "CompID", compID);
137  msg->setInt((int64)n, "Status", status);
138  msg->setString((int64)n, "Name", name);
139  msg->setString((int64)n, "Language", language);
140  msg->setString((int64)n, "Function", function);
141  msg->setString((int64)n, "LibraryFilename", libraryFilename);
142  msg->setString((int64)n, "Script", script);
143  msg->setTime((int64)n, "CreatedTime", time);
144  return true;
145  }
146  bool writeIDToMsg(const char* arrayname, uint32 n, DataMessage* msg) {
147  if (!msg) return false;
148  msg->setString((int64)n, arrayname, name);
149  return true;
150  }
151 };
152 
153 #define REQ_CREATED 1
154 #define REQ_RECEIVED_LOCAL 2
155 #define REQ_RECEIVED_REMOTE 3
156 #define REQ_PROCESSING_LOCAL 4
157 #define REQ_PROCESSING_REMOTE 5
158 #define REQ_REPLY_READY 6
159 #define REQ_FAILED_TO_SEND 7
160 #define REQ_FAILED 8
161 #define REQ_FAILED_DATA 9
162 #define REQ_FAILED_NODATA 10
163 #define REQ_FAILED_DATA_EOL 11
164 #define REQ_SUCCESS 12
165 #define REQ_SUCCESS_DATA 13
166 #define REQ_SUCCESS_NODATA 14
167 #define REQ_SUCCESS_DATA_EOL 15
168 
170  uint32 id; // id, corresponds to the index in map
171  uint64 time; // timestamp when entered
172  char name[MAXKEYNAMELEN+1]; // Internal use only for named event
173  uint64 lastUpdate; // timestamp when entered
174  uint8 status; // status of the process
175  uint32 from; // id of component making the request
176  uint32 to; // id of component processing the request
177  uint32 remoteID; // id of request at origin node, if any
178  uint64 dataMessageID; // id of Data Message
179  uint64 dataMessageEOL; // eol of Data Message
180  std::string toXML() {
181  return utils::StringFormat("\t<request id=\"%u\" name=\"%s\" status=\"%u\" time=\"%llu\" lastupdate=\"%llu\" from=\"%u\" to=\"%u\" remoteid=\"%u\" msgid=\"%llu\" msgeol=\"%llu\" />\n",
182  id, name, status, time, lastUpdate, from, to, remoteID, dataMessageID, dataMessageEOL);
183  }
184  static std::string GetOuterXMLName() { return "requests"; }
185 };
186 
187 #define CHECKDATAMAPSMEMORYSERIAL if (serial != master->getDataMapsShmemSerial()) {if (!open()) {mutex->leave();return 0;}}
188 
189 class MasterMemory;
191 public:
192  DataMapsMemory(MasterMemory* master);
193  ~DataMapsMemory();
194 
195  bool open();
196  bool create(uint32 typesMaxCount, uint32 contextsMaxCount, uint32 tagsMaxCount, uint32 cranksMaxCount, uint32 requestsMaxCount);
197 
198  bool getMemoryUsage(uint64& alloc, uint64& usage);
199  bool maintenance();
200 
201  std::string printFriendlyHTML();
202  std::string toXML();
203 
204  bool writeIDsToMsg(DataMessage* msg);
205 
206  // TypeLevelMap
207  bool writeTypeLevelsToMsg(DataMessage* msg);
208  bool confirmTypeLevelID(uint16 id);
209  bool cancelTypeLevelID(uint16 id);
210  bool createNewTypeLevel(uint16 id, const char* name, uint64 time, uint16& existingID);
211  bool syncTypeLevels(DataMessage* msg);
212  bool getTypeLevelName(uint16 id, char* name, uint32 maxSize);
213  bool getTypeLevelID(const char* name, uint16 &id);
214  uint8 lookupTypeLevelID(const char* name, uint16 &id); // status = 0: not there, 1: in-sync, 2: ready
215  bool deleteTypeLevel(const char* name);
216  bool deleteTypeLevel(uint16 id);
217  uint64 getTypeLevelCreateTime(uint16 id);
218  std::string printAllTypes();
219 
220  // ContextLevelMap
221  bool writeContextLevelsToMsg(DataMessage* msg);
222  bool confirmContextLevelID(uint16 id);
223  bool cancelContextLevelID(uint16 id);
224  bool createNewContextLevel(uint16 id, const char* name, uint64 time, uint16& existingID);
225  bool syncContextLevels(DataMessage* msg);
226  bool getContextLevelName(uint16 id, char* name, uint32 maxSize);
227  bool getContextLevelID(const char* name, uint16 &id);
228  uint8 lookupContextLevelID(const char* name, uint16 &id); // status = 0: not there, 1: in-sync, 2: ready
229  bool deleteContextLevel(const char* name);
230  bool deleteContextLevel(uint16 id);
231  uint64 getContextLevelCreateTime(uint16 id);
232  std::string printAllContexts();
233 
234  // TagMap
235  bool writeTagsToMsg(DataMessage* msg);
236  bool confirmTagID(uint32 id);
237  bool cancelTagID(uint32 id);
238  bool createNewTag(uint32 id, const char* name, uint64 time, uint32& existingID);
239  bool syncTags(DataMessage* msg);
240  bool getTagName(uint32 id, char* name, uint32 maxSize);
241  bool getTagID(const char* name, uint32 &id);
242  uint8 lookupTagID(const char* name, uint32 &id); // status = 0: not there, 1: in-sync, 2: ready
243  bool deleteTag(const char* name);
244  bool deleteTag(uint32 id);
245  uint64 getTagCreateTime(uint32 id);
246 
247  // CrankMap
248  bool writeCranksToMsg(DataMessage* msg);
249  bool confirmCrankID(uint16 id);
250  bool cancelCrankID(uint16 id);
251  bool createNewCrank(uint16 id, uint32 compID, const char* name, const char* function, const char* libraryFilename, const char* language, const char* script, uint64 time, uint16& existingID);
252  bool syncCranks(DataMessage* msg);
253  bool getCrankName(uint16 id, char* name, uint32 maxSize);
254  bool getCrankFunction(uint16 id, char* function, uint32 maxSize);
255  bool getCrankLanguage(uint16 id, char* language, uint32 maxSize);
256  bool getCrankLibraryFilename(uint16 id, char* libraryFilename, uint32 maxSize);
257  bool getCrankID(const char* name, uint16 &id);
258  uint8 lookupCrankID(const char* name, uint16 &id); // status = 0: not there, 1: in-sync, 2: ready
259  uint32 getCrankCompID(uint16 id);
260  bool getCrankScript(uint16 id, char* script, uint32 maxSize);
261  bool deleteCrank(uint16 id);
262  bool deleteCrank(const char* name);
263  uint64 getCrankCreateTime(uint16 id);
264 
265  // RequestMap
266  bool createNewRequest(uint32 from, uint32 to, uint32 remoteID, uint32 &id);
267  bool getRequestInfo(uint32 id, uint32& from, uint32& to, uint32& remoteID);
268  bool getRequestStatus(uint32 id, uint8& status, uint64& time, uint64& msgID, uint64& msgEOL);
269  bool setRequestStatus(uint32 id, uint8 status);
270  bool setRequestStatus(uint32 id, uint8 status, uint64 msgID, uint64 msgEOL);
271  bool waitForRequestReply(uint32 id, uint32 timeout, uint8& status, uint64& time, uint64& msgID, uint64& msgEOL);
272  bool deleteRequest(uint32 id);
273  uint32 getRequestCount();
274 
275 private:
276  bool resize(uint16 port, uint32 serial, uint32 typesMaxCount, uint32 contextsMaxCount, uint32 tagsMaxCount, uint32 cranksMaxCount, uint32 requestsMaxCount);
277 
278  utils::Mutex* mutex;
279  MasterMemory* master;
280  DataMapsMemoryStruct* header;
281  uint64 memorySize;
282  uint16 port;
283  uint32 serial;
284 
285  // headers for each map type
286  GenericMapHeader* typeMapHeader;
287  GenericMapHeader* contextMapHeader;
288  GenericMapHeader* tagMapHeader;
289  GenericMapHeader* crankMapHeader;
290 // GenericMapHeader* componentMapHeader;
291  GenericMapHeader* requestMapHeader;
292 };
293 
294 template <typename T, typename ID>
296 public:
297  // Generic MemoryMap
298  static T* CreateEntry(char* data, ID id, const char* name);
299  static T* CreateFirstFreeEntry(char* data, ID &id, const char* name);
300  static T* GetEntry(char* data, ID id);
301  static bool ConfirmEntry(char* data, ID id);
302  static bool CancelEntry(char* data, ID id);
303  static bool DeleteEntry(char* data, ID id);
304  static uint64 GetEntryTime(char* data, ID id);
305  static bool GetEntryName(char* data, ID id, char* name, uint32 maxSize);
306  static ID GetEntryID(char* data, const char* name, bool force = false);
307  static uint8 LookupEntryID(char* data, const char* name, ID& id);
308  static uint32 GetCount(char* data);
309  static uint32 GetUsage(char* data);
310  static std::string PrintAllEntries(char* data);
311  static std::string PrintAllEntriesHTML(char* data);
312  static std::string ToXML(char* data);
313  static bool WriteAllEntriesToMsg(char* data, DataMessage* msg);
314  static bool WriteAllIDsToMsg(const char* arrayname, char* data, DataMessage* msg);
315 };
316 
317 } // namespace cmlabs
318 
319 #include "MemoryMaps.tpl.h"
320 
321 #endif //_DATAMAPSMEMORY_H_
322 
Definition: MemoryManager.h:169
Definition: DataMapsMemory.h:66
Definition: DataMapsMemory.h:9
Definition: DataMapsMemory.h:190
Definition: Bitmap.h:7
bool setTime(const char *key, uint64 value)
Definition: DataMessage.cpp:1782
Definition: DataMapsMemory.h:33
Definition: DataMapsMemory.h:114
Definition: DataMapsMemory.h:295
Definition: DataMapsMemory.h:169
Definition: DataMapsMemory.h:90
bool setString(const char *key, const char *value)
Definition: DataMessage.cpp:1776
Definition: Utils.h:276
bool setInt(const char *key, int64 value)
Definition: DataMessage.cpp:1786
Definition: DataMapsMemory.h:42
Definition: DataMessage.h:95