CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
DataMessage.h
Go to the documentation of this file.
1
21
97
98#if !defined(_DATAMESSAGE_H_)
99#define _DATAMESSAGE_H_
100
101// Data Message Version History
102// Initial version:
103// - data->cid = DATAMESSAGEOLDID
104// - does not contain cver, cenc, cyclecputime, cyclewalltime, chaincputime, chainwalltime
105// Version 10:
106// - data->cid = DATAMESSAGEID
107// - now contains cver, cenc, cyclecputime, cyclewalltime, chaincputime, chainwalltime
108
109#define CURRENTDATAMESSAGEVERSION 10
111#define DATAMESSAGE_CENC_PACKED 0x0100
112
113#include "Types.h"
114#include "ObjectIDs.h"
115//#include "Collections.h"
116#include "Utils.h"
117
118namespace cmlabs {
119
131 uint32 size;
132 uint32 cid;
133};
134
146 uint32 size; // auto
147 uint32 cid; // auto
148 uint64 memid; // auto
149 uint64 time; // auto
150 uint64 sendtime; // api
151 uint64 recvtime; // api
152 uint16 origin; // NodeID, api
153 uint16 destination; // NodeID, api
154
155 uint64 ttl; // spec
156 uint16 priority; // spec
157 uint8 policy; // spec
158
159 uint32 from; // user
160 uint32 to; // user
161
162 PsyType type; // api/user
163 uint32 tag; // api/user
164 uint16 status; // api/user
165 uint64 reference; // api
166 uint64 serial; // api/user
168
169 uint32 userSize; // size of user data
170 uint32 userCount; // number of user data entries
171};
172
191 uint32 size;
192 uint32 cid;
193 uint16 cver;
194 uint16 cenc;
195 uint64 memid;
196 uint64 time;
197 uint64 sendtime;
198 uint64 recvtime;
199 uint16 origin;
200 uint16 destination;
201
202 uint64 ttl;
203 uint16 priority;
204 uint8 policy;
205
206 uint32 from;
207 uint32 to;
208
210 uint32 tag;
211 uint16 status;
212 uint64 reference;
213 uint64 serial;
215
216 uint32 userSize;
217 uint32 userCount;
218
223 uint32 chaincount;
224
225 uint32 systemid;
226
232 std::string toJSON(std::map<uint16, std::string>* subtypes = NULL, std::map<uint16, std::string>* subcontexts = NULL, std::map<uint32, std::string>* compNames = NULL);
238 std::string toXML(std::map<uint16, std::string>* subtypes = NULL, std::map<uint16, std::string>* subcontexts = NULL, std::map<uint32, std::string>* compNames = NULL);
239};
240
281public:
288 static DataMessage* FindClosestMessage(uint64 t, std::map<uint64, DataMessage*> &messages);
289
295 static bool ConvertDataFromOlderMessageFormat(const char* data, char **newData);
296
299 static bool UnitTest();
300
304 DataMessage();
310 DataMessage(PsyType type, uint32 from);
319 DataMessage(PsyType type, uint32 from, uint32 to, uint64 ttl = 0, uint16 priority = 0);
330 DataMessage(PsyType type, uint32 from, uint32 to, uint32 tag, uint64 time, uint64 ttl = 0, uint16 priority = 0);
336 DataMessage(char* data, bool copy = false);
341 DataMessage(const char* data);
346 DataMessage(const DataMessage& msg);
354 DataMessage(const char* data, uint32 maxDraftSize);
362 DataMessage(const DataMessage& msg, uint32 maxDraftSize);
366 ~DataMessage();
367
373 return new DataMessage((const char*)(this->data));
374 }
375
385
386 const char* swapMessageData(const char* data);
387
413 char* toPacked(uint32& size) const;
417 static DataMessage* FromPacked(const char* data, uint32 size);
419 static bool IsPacked(const char* data, uint32 size);
421
433 bool fillInDraftUserDataFrom(const char* data, uint32 maxDraftSize);
434
441 bool isValid() {return data && GetObjID(data) == DATAMESSAGEID; }
442
443 // User
444
451
452 uint32 getContentType(const char* key);
453
460
461 uint32 getContentSize(const char* key);
462
471
472 bool getAsBool(const char* key);
473
481
482 std::string getAsString(const char* key);
483
491
492 int64 getAsInt(const char* key);
493
501
502 uint64 getAsTime(const char* key);
503
511
512 float64 getAsFloat(const char* key);
513
520
521 uint64 getTime(const char* key);
522
529
530 const char* getString(const char* key);
531
539
540 const char* getString(const char* key, uint32 &size);
541
549
550 bool getInt(const char* key, int64& value);
551
560
561 bool getDouble(const char* key, double& value);
562
571
572 bool getFloat(const char* key, float64& value);
573
581
582 const char* getData(const char* key, uint32& size);
583
592
593 char* getDataCopy(const char* key, uint32& size);
594
602
603 DataMessage* getAttachedMessageCopy(const char* key);
604
605
606
607
615
616 uint64 getTime(const char* key, bool& success);
617
625
626
627 const char* getString(const char* key, bool& success);
628
637
638 const char* getString(const char* key, uint32 &size, bool& success);
639
648
649
650 int64 getInt(const char* key, bool& success);
651
661
662 double getDouble(const char* key, bool& success);
663
672
673 float64 getFloat(const char* key, bool& success);
674
683
684 const char* getData(const char* key, uint32& size, bool& success);
685
696
697 char* getDataCopy(const char* key, uint32& size, bool& success);
698
707
708 DataMessage* getAttachedMessageCopy(const char* key, bool& success);
709
710
719
720 int64 getInt(const char* key);
721
731
732 double getDouble(const char* key);
733
734
744
745 float64 getFloat(const char* key);
746
747 // *** setting all of the above, returns success or fail (usually because the message is not valid)
748
758 bool setTime(const char* key, uint64 value);
759
769 bool setString(const char* key, const char* value);
770
779 bool setInt(const char* key, int64 value);
780
789 bool setDouble(const char* key, double value);
790
800 bool setFloat(const char* key, float64 value);
801
812 bool setData(const char* key, const char* value, uint32 size);
813
824 bool setAttachedMessage(const char* key, DataMessage* msg);
825
831 uint32 getUserSize() {
832 return data->userSize;
833 }
834
840 uint32 getUserCount() {
841 return data->userCount;
842 }
843
844 // Arrays
845 // *** Same as above for single values, but now getting and setting entries in arrays
846 // myval[0] = val, myval[1] = val, myval[2] = val, etc.
847
849
856
857 KeyType getKeyType(const char* key);
858
867 std::map<std::string, KeyType> getAllKeys();
868
875 bool isArrayContent(const char* key);
876
883 bool isMapContent(const char* key);
884
891 bool isSingleValue(const char* key);
892
893
900 bool hasKey(const char* key);
901
908 bool hasString(const char* key);
909
916 bool hasInteger(const char* key);
917
924 bool hasFloat(const char* key);
925
932 bool hasTime(const char* key);
933
940 bool hasData(const char* key);
941
948 bool hasMessage(const char* key);
949
950
956 uint32 getContentType(int64 i, const char* key);
957
963 uint32 getContentSize(int64 i, const char* key);
964
973 uint64 getTime(int64 i, const char* key);
974
980 std::string getAsString(int64 i, const char* key);
981
987 int64 getAsInt(int64 i, const char* key);
988
994 uint64 getAsTime(int64 i, const char* key);
995
1001 float64 getAsFloat(int64 i, const char* key);
1002
1008 const char* getString(int64 i, const char* key);
1009
1016 const char* getString(int64 i, const char* key, uint32 &size);
1017
1024 bool getInt(int64 i, const char* key, int64& value);
1025
1032 bool getDouble(int64 i, const char* key, double& value);
1033
1040 bool getFloat(int64 i, const char* key, float64& value);
1041
1048 int64 getInt(int64 i, const char* key, bool& success);
1055 double getDouble(int64 i, const char* key, bool& success);
1062 float64 getFloat(int64 i, const char* key, bool& success);
1069 int64 getInt(int64 i, const char* key);
1075 double getDouble(int64 i, const char* key);
1081 float64 getFloat(int64 i, const char* key);
1082
1089 const char* getData(int64 i, const char* key, uint32& size);
1090
1097 char* getDataCopy(int64 i, const char* key, uint32& size);
1098
1105 DataMessage* getAttachedMessageCopy(int64 i, const char* key);
1106
1113
1114 bool setTime(int64 i, const char* key, uint64 value);
1115
1122 bool setString(int64 i, const char* key, const char* value);
1123
1130 bool setInt(int64 i, const char* key, int64 value);
1131
1138 bool setDouble(int64 i, const char* key, double value);
1139
1146 bool setFloat(int64 i, const char* key, float64 value);
1154 bool setData(int64 i, const char* key, const char* value, uint32 size);
1155 // bool setData(int64 i, const char* key, const void* value, uint32 size);
1156
1163 bool setAttachedMessage(int64 i, const char* key, DataMessage* msg);
1164
1165 // *** Same as above but reading whole arrays in one go
1166 std::map<int64, uint64> getTimeArray(const char* key);
1171 std::map<int64, std::string> getAsStringArray(const char* key);
1176 std::map<int64, int64> getAsIntArray(const char* key);
1181 std::map<int64, float64> getAsFloatArray(const char* key);
1186 std::map<int64, std::string> getStringArray(const char* key);
1192 std::map<int64, int64> getIntArray(const char* key);
1197 std::map<int64, double> getDoubleArray(const char* key);
1202 std::map<int64, float64> getFloatArray(const char* key);
1207 std::map<int64, DataMessage*> getAttachedMessageArray(const char* key);
1208
1213 uint32 getArraySize(const char* key);
1214
1219 uint32 getArrayCount(const char* key);
1220
1225 uint32 getMapSize(const char* key);
1226
1231 uint32 getMapCount(const char* key);
1232
1233
1236 // *** Same as above but setting whole arrays in one go
1243 bool setTimeArray(const char* key, std::map<int64, uint64>& map);
1244
1251 bool setStringArray(const char* key, std::map<int64, std::string>& map);
1252
1259 bool setIntArray(const char* key, std::map<int64, int64>& map);
1260
1267 bool setDoubleArray(const char* key, std::map<int64, double>& map);
1268
1275 bool setFloatArray(const char* key, std::map<int64, float64>& map);
1276
1284 bool setAttachedMessageArray(const char* key, std::map<int64, DataMessage*>& map);
1285
1286 // Maps
1289
1290 // *** Same as for arrays, but now with maps
1291 // myval["up"] = val, myval["down"] = val, myval["left"] = val, etc.
1292
1301 uint32 getContentType(const char* idx, const char* key);
1302
1310 uint32 getContentSize(const char* idx, const char* key);
1318
1319 uint64 getTime(const char* idx, const char* key);
1328 std::string getAsString(const char* idx, const char* key);
1337 int64 getAsInt(const char* idx, const char* key);
1346 uint64 getAsTime(const char* idx, const char* key);
1355 float64 getAsFloat(const char* idx, const char* key);
1363 const char* getString(const char* idx, const char* key);
1372 const char* getString(const char* idx, const char* key, uint32 &size);
1381
1382 bool getInt(const char* idx, const char* key, int64& value);
1392 bool getDouble(const char* idx, const char* key, double& value);
1402 bool getFloat(const char* idx, const char* key, float64& value);
1410 int64 getInt(const char* idx, const char* key, bool& success);
1419 double getDouble(const char* idx, const char* key, bool& success);
1428 float64 getFloat(const char* idx, const char* key, bool& success);
1436 int64 getInt(const char* idx, const char* key);
1445 double getDouble(const char* idx, const char* key);
1453 float64 getFloat(const char* idx, const char* key);
1462 const char* getData(const char* idx, const char* key, uint32& size);
1463
1472 char* getDataCopy(const char* idx, const char* key, uint32& size);
1480 DataMessage* getAttachedMessageCopy(const char* idx, const char* key);
1489 bool setTime(const char* idx, const char* key, uint64 value);
1498 bool setString(const char* idx, const char* key, const char* value);
1507 bool setInt(const char* idx, const char* key, int64 value);
1517 bool setDouble(const char* idx, const char* key, double value);
1527 bool setFloat(const char* idx, const char* key, float64 value);
1537 bool setData(const char* idx, const char* key, const char* value, uint32 size);
1546 bool setAttachedMessage(const char* idx, const char* key, DataMessage* msg);
1547
1554 std::map<std::string, uint64> getTimeMap(const char* key);
1562 std::map<std::string, std::string> getAsStringMap(const char* key);
1570 std::map<std::string, int64> getAsIntMap(const char* key);
1578 std::map<std::string, float64> getAsFloatMap(const char* key);
1585 std::map<std::string, std::string> getStringMap(const char* key);
1592 std::map<std::string, int64> getIntMap(const char* key);
1600 std::map<std::string, double> getDoubleMap(const char* key);
1607 std::map<std::string, float64> getFloatMap(const char* key);
1614 std::map<std::string, DataMessage*> getAttachedMessageMap(const char* key);
1622 bool setTimeMap(const char* key, std::map<std::string, uint64>& map);
1630 bool setStringMap(const char* key, std::map<std::string, std::string>& map);
1638 bool setIntMap(const char* key, std::map<std::string, int64>& map);
1647 bool setDoubleMap(const char* key, std::map<std::string, double>& map);
1656 bool setFloatMap(const char* key, std::map<std::string, float64>& map);
1664 bool setAttachedMessageMap(const char* key, std::map<std::string, DataMessage*>& map);
1665
1671
1672
1673 std::string toJSON(std::map<uint16, std::string>* subtypes = NULL, std::map<uint16, std::string>* subcontexts = NULL, std::map<uint32, std::string>* compNames = NULL);
1674
1679
1680 std::string toXML(std::map<uint16, std::string>* subtypes = NULL, std::map<uint16, std::string>* subcontexts = NULL, std::map<uint32, std::string>* compNames = NULL);
1681
1691
1692 std::string toCSV(const char* separator = NULL, const char* preample = NULL, std::map<uint16, std::string>* subtypes = NULL, std::map<uint16, std::string>* subcontexts = NULL, std::map<uint32, std::string>* compNames = NULL);
1693
1700
1701 static std::string GetCSVHeader(const char* separator = NULL, const char* preample = NULL);
1702
1703
1710
1714 std::string getUserEntriesAsString();
1715
1721 std::string getUserEntriesAsJSON(bool asText);
1722
1723
1724 // API - inline
1725
1732 char* getRawData() {
1733 if (data->cid != DATAMESSAGEID) return NULL;
1734 return (char*)data;
1735 }
1736
1741
1742 uint16 getOrigin() {
1743 if (data->cid != DATAMESSAGEID) return 0;
1744 return data->origin;
1745 }
1746
1752 if (data->cid != DATAMESSAGEID) return 0;
1753 return data->destination;
1754 }
1755
1761 bool setOrigin(uint16 addr) {
1762 if (data->cid != DATAMESSAGEID) return false;
1763 data->origin = addr;
1764 return true;
1765 }
1766
1772 bool setDestination(uint16 addr) {
1773 if (data->cid != DATAMESSAGEID) return false;
1774 data->destination = addr;
1775 return true;
1776 }
1777
1784 uint32 getSize() {
1785 if (data->cid != DATAMESSAGEID) return 0;
1786 return data->size;
1787 }
1788
1799 bool setMultiple(uint32 from, uint32 to, uint32 tag, uint64 ttl, uint8 policy, uint64 sendtime) {
1800 if (data->cid != DATAMESSAGEID) return false;
1801 if (from) data->from = from;
1802 if (to) data->to = to;
1803 if (tag) data->tag = tag;
1804 if (ttl) data->ttl = ttl;
1805 if (policy) data->policy = policy;
1806 if (sendtime) data->sendtime = sendtime;
1807 return true;
1808 }
1809
1816 uint32 getTag() {
1817 if (data->cid != DATAMESSAGEID) return 0;
1818 return data->tag;
1819 }
1820
1831 bool setTag(uint32 tag) {
1832 if (data->cid != DATAMESSAGEID) return false;
1833 data->tag = tag;
1834 return true;
1835 }
1836
1841 uint64 getReference() {
1842 if (data->cid != DATAMESSAGEID) return 0;
1843 return data->reference;
1844 }
1845
1851 bool setReference(uint64 ref) {
1852 if (data->cid != DATAMESSAGEID) return false;
1853 data->reference = ref;
1854 return true;
1855 }
1856
1857
1862 uint32 getSystemID() {
1863 if (data->cid != DATAMESSAGEID) return 0;
1864 return data->systemid;
1865 }
1866
1872 bool setSystemID(uint32 id) {
1873 if (data->cid != DATAMESSAGEID) return false;
1874 data->systemid = id;
1875 return true;
1876 }
1877
1882 uint64 getMemID() {
1883 if (data->cid != DATAMESSAGEID) return 0;
1884 return data->memid;
1885 }
1886
1893 bool setMemID(uint64 id) {
1894 if (data->cid != DATAMESSAGEID) return false;
1895 data->memid = id;
1896 return true;
1897 }
1898
1903 uint32 getFrom() {
1904 if (data->cid != DATAMESSAGEID) return 0;
1905 return data->from;
1906 }
1907
1918
1919 bool setFrom(uint32 from) {
1920 if (data->cid != DATAMESSAGEID) return false;
1921 data->from = from;
1922 return true;
1923 }
1924
1932
1933 uint32 getTo() {
1934 if (data->cid != DATAMESSAGEID) return 0;
1935 return data->to;
1936 }
1937
1947
1948 bool setTo(uint32 to) {
1949 if (data->cid != DATAMESSAGEID) return false;
1950 data->to = to;
1951 return true;
1952 }
1953
1961
1963 PsyType t = NOTYPE;
1964 if (data->cid != DATAMESSAGEID) return t;
1965 return data->type;
1966 }
1967
1977
1978 bool setType(PsyType &type) {
1979 if (data->cid != DATAMESSAGEID) return false;
1980 data->type = type;
1981 return true;
1982 }
1983
1993
1996 if (data->cid != DATAMESSAGEID) return c;
1997 return data->contextchange;
1998 }
1999
2012
2014 if (data->cid != DATAMESSAGEID) return false;
2015 data->contextchange = context;
2016 return true;
2017 }
2018
2032
2033 uint64 getTTL() {
2034 if (data->cid != DATAMESSAGEID) return 0;
2035 return data->ttl;
2036 }
2037
2047
2048 uint64 getEOL() {
2049 if (data->cid != DATAMESSAGEID) return 0;
2050 if (!data->ttl)
2051 return 0;
2052 return data->time + data->ttl;
2053 }
2054
2069
2070 bool setTTL(uint64 ttl) {
2071 if (data->cid != DATAMESSAGEID) return false;
2072 data->ttl = ttl;
2073 return true;
2074 }
2075
2087
2088 bool setEOL(uint64 eol) {
2089 if (data->cid != DATAMESSAGEID) return false;
2090 int64 ttl = eol - data->time;
2091 if (ttl > 0) {
2092 data->ttl = (uint64)ttl;
2093 return true;
2094 }
2095 else
2096 return false;
2097 }
2098
2108
2109 uint16 getPriority() {
2110 if (data->cid != DATAMESSAGEID) return 0;
2111 return data->priority;
2112 }
2113
2127
2128 bool setPriority(uint16 priority) {
2129 if (data->cid != DATAMESSAGEID) return false;
2130 data->priority = priority;
2131 return true;
2132 }
2133
2143
2144 uint8 getPolicy() {
2145 if (data->cid != DATAMESSAGEID) return 0;
2146 return data->policy;
2147 }
2148
2159
2160 bool setPolicy(uint8 policy) {
2161 if (data->cid != DATAMESSAGEID) return false;
2162 data->policy = policy;
2163 return true;
2164 }
2165
2174
2175 uint64 getSendTime() {
2176 if (data->cid != DATAMESSAGEID) return 0;
2177 return data->sendtime;
2178 }
2179
2190
2191 bool setSendTime(uint64 time) {
2192 if (data->cid != DATAMESSAGEID) return false;
2193 data->sendtime = time;
2194 return true;
2195 }
2196
2205
2206 uint64 getRecvTime() {
2207 if (data->cid != DATAMESSAGEID) return 0;
2208 return data->recvtime;
2209 }
2210
2221
2222 bool setRecvTime(uint64 time) {
2223 if (data->cid != DATAMESSAGEID) return false;
2224 data->recvtime = time;
2225 return true;
2226 }
2227
2237
2239 if (data->cid != DATAMESSAGEID) return 0;
2240 return data->time;
2241 }
2242
2253
2254 bool setCreatedTime(uint64 t) {
2255 if (data->cid != DATAMESSAGEID) return 0;
2256 data->time = t;
2257 return true;
2258 }
2259
2268
2269 uint16 getStatus() {
2270 if (data->cid != DATAMESSAGEID) return 0;
2271 return data->status;
2272 }
2273
2284
2285 bool setStatus(uint16 status) {
2286 if (data->cid != DATAMESSAGEID) return false;
2287 data->status = status;
2288 return true;
2289 }
2290
2298
2299 uint64 getSerial() {
2300 if (data->cid != DATAMESSAGEID) return 0;
2301 return data->serial;
2302 }
2303
2314
2315 bool setSerial(uint64 serial) {
2316 if (data->cid != DATAMESSAGEID) return false;
2317 data->serial = serial;
2318 return true;
2319 }
2320
2321
2322
2323
2324
2325
2326
2327
2328
2343
2344 bool addTimeUsage(uint32 cycleCPUTime, uint32 cycleWallTime, uint32 chainCPUTime = 0, uint32 chainWallTime = 0, uint32 chainCount = 0);
2345
2346
2355
2357 if (data->cid != DATAMESSAGEID) return 0;
2358 return data->cyclecputime;
2359 }
2360
2371
2372 bool setCycleCPUTime(uint32 t) {
2373 if (data->cid != DATAMESSAGEID) return false;
2374 data->cyclecputime = t;
2375 return true;
2376 }
2377
2378
2379
2380
2389
2391 if (data->cid != DATAMESSAGEID) return 0;
2392 return data->cyclewalltime;
2393 }
2394
2405
2406 bool setCycleWallTime(uint32 t) {
2407 if (data->cid != DATAMESSAGEID) return false;
2408 data->cyclewalltime = t;
2409 return true;
2410 }
2411
2412
2413
2414
2415
2424
2426 if (data->cid != DATAMESSAGEID) return 0;
2427 return data->chaincputime;
2428 }
2429
2440
2441 bool setChainCPUTime(uint32 t) {
2442 if (data->cid != DATAMESSAGEID) return false;
2443 data->chaincputime = t;
2444 return true;
2445 }
2446
2447
2448
2449
2458
2460 if (data->cid != DATAMESSAGEID) return 0;
2461 return data->chainwalltime;
2462 }
2463
2474
2475 bool setChainWallTime(uint32 t) {
2476 if (data->cid != DATAMESSAGEID) return false;
2477 data->chainwalltime = t;
2478 return true;
2479 }
2480
2481
2482
2491
2492 uint32 getChainCount() {
2493 if (data->cid != DATAMESSAGEID) return 0;
2494 return data->chaincount;
2495 }
2496
2507
2508 bool setChainCount(uint32 count) {
2509 if (data->cid != DATAMESSAGEID) return false;
2510 data->chaincount= count;
2511 return true;
2512 }
2513
2525
2526 bool deleteEntry(const char* key) {
2527 if (data->cid != DATAMESSAGEID) return false;
2528 removeEntry(key);
2530 return true;
2531 }
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2554
2555 mutable std::string _lastFindKey;
2556 mutable char* _lastFindKeyResult = NULL;
2557
2561 _lastFindKey.clear();
2562 _lastFindKeyResult = NULL;
2563 }
2564
2565protected:
2572 bool setRawData(const char* key, const char* value, uint32 size, uint32 datatype);
2573
2582 char* findKey(const char* key) {
2583 if (data->cid != DATAMESSAGEID) return NULL;
2584 if (!_lastFindKey.empty() && stricmp(key, _lastFindKey.c_str()) == 0)
2585 return _lastFindKeyResult;
2586 char* src = (char*)data + sizeof(DataMessageHeader);
2587 char* srcEnd = (char*)data + data->size;
2588 while (src < srcEnd) {
2589 if (((DataMessageEntryHeader*)src)->cid == CONSTCHARID) {
2590 if (stricmp(key, src+sizeof(DataMessageEntryHeader)) == 0) {
2591 _lastFindKey = key;
2592 _lastFindKeyResult = src;
2593 return src;
2594 }
2595 }
2596 src += ((DataMessageEntryHeader*)src)->size;
2597 }
2598 _lastFindKey = key;
2599 _lastFindKeyResult = NULL;
2600 return NULL;
2601 }
2602
2611
2612 char* findEntry(const char* key) {
2613 char* src = findKey(key);
2614 if (src == NULL) return NULL;
2615 return src + sizeof(DataMessageEntryHeader) + strlen(key) + 1;
2616 }
2617
2627
2630 uint32 removeCollectionEntries(const char* key);
2631
2632 bool removeEntry(const char* key) {
2633 char* src = findKey(key);
2634 if (src == NULL) return true;
2635 ((DataMessageEntryHeader*)src)->cid = UNUSEDID;
2636 data->userCount--;
2637 data->userSize -= ((DataMessageEntryHeader*)src)->size;
2639 return true;
2640 }
2641
2653
2654 char* findSpace(uint32 space) {
2655 if (data->cid != DATAMESSAGEID) return NULL;
2656 char* src = (char*)data + sizeof(DataMessageHeader);
2657 char* srcEnd = (char*)data + data->size;
2658 while (src < srcEnd) {
2659 if ( (((DataMessageEntryHeader*)src)->cid == UNUSEDID) &&
2660 (((DataMessageEntryHeader*)src)->size == space)) {
2661 // Used to allow anything smaller, but that needs fixing permanently first
2662 // (((DataMessageEntryHeader*)src)->size >= space) ) {
2663 return src;
2664 }
2665 src += ((DataMessageEntryHeader*)src)->size;
2666 }
2667 return NULL;
2668 }
2669
2670};
2671
2672} // namespace cmlabs
2673
2674#endif //_DATAMESSAGE_H_
2675
Object type ids used to tag and verify every binary structure in CMSDK memory.
#define CONSTCHARID
Definition ObjectIDs.h:38
#define DATAMESSAGEID
Definition ObjectIDs.h:75
#define GetObjID(data)
Extract the cid field from a binary object block: the uint32 at byte offset 4 (after the leading size...
Definition ObjectIDs.h:26
#define UNUSEDID
Definition ObjectIDs.h:37
Core fixed-width scalar typedefs and the PsyType / PsyContext hierarchical identifiers.
static struct PsyType NOTYPE
The empty/unset message type (isValid() == false).
Definition Types.h:436
static struct PsyContext NOCONTEXT
The empty/unset context (isValid() == false).
Definition Types.h:437
Cross-platform utility toolbox for CMSDK: threading, synchronization, shared memory,...
#define stricmp
Definition Utils.h:132
The central Psyclone data container: a self-contained binary message with typed, named user entries.
bool setOrigin(uint16 addr)
setOrigin(uint16 addr) Set origin to address addr and return new origin
bool setTime(const char *key, uint64 value)
setTime(const char* key, uint64 value)
std::map< int64, float64 > getAsFloatArray(const char *key)
getAsFloatArray(const char* key)
uint16 getOrigin()
getOrigin() Get the NodeID of the node the message originated on
bool setFloatArray(const char *key, std::map< int64, float64 > &map)
setFloatArray(const char* key, std::map<int64, float64>& map) Sets the whole float array named key in...
bool setChainCount(uint32 count)
setChainCount(uint32 count)
static bool UnitTest()
Run the built-in self test for the DataMessage implementation.
DataMessage()
DataMessage() Creates an empty DataMessage.
bool getDouble(const char *key, double &value)
getDouble(const char* key, double& value)
~DataMessage()
~DataMessage() Message destructor
std::string toXML(std::map< uint16, std::string > *subtypes=NULL, std::map< uint16, std::string > *subcontexts=NULL, std::map< uint32, std::string > *compNames=NULL)
toXML()
bool setTTL(uint64 ttl)
setTTL(uint64 ttl)
bool deleteEntry(const char *key)
deleteEntry(const char* key)
uint32 getSystemID()
getSystemID() Get and return message system id
uint32 removeCollectionEntries(const char *key)
removeEntry(const char* key)
bool setEOL(uint64 eol)
setEOL(uint64 eol)
static DataMessage * FindClosestMessage(uint64 t, std::map< uint64, DataMessage * > &messages)
FindClosestMessage(uint64 t, std::map<uint64, DataMessage*> &messages) In a map of time -> DataMessag...
bool setPriority(uint16 priority)
setPriority(uint16 priority)
bool setFloat(const char *key, float64 value)
setFloat(const char* key, float64 value)
std::map< int64, int64 > getIntArray(const char *key)
getIntArray(const char* key)
bool setMemID(uint64 id)
setMemID(uint64 id) Set memory ID to id
bool setChainWallTime(uint32 t)
setChainWallTime(uint32 t)
bool addTimeUsage(uint32 cycleCPUTime, uint32 cycleWallTime, uint32 chainCPUTime=0, uint32 chainWallTime=0, uint32 chainCount=0)
addTimeUsage()
std::map< std::string, double > getDoubleMap(const char *key)
returns the user double entry with this key
uint32 getFrom()
getFrom() Get the sender id
bool setString(const char *key, const char *value)
setString(const char* key, const char* value)
bool getInt(const char *key, int64 &value)
getInt(const char* key, int64& value)
bool setInt(const char *key, int64 value)
setInt(const char* key, int64 value)
bool isSingleValue(const char *key)
isSingleValue(const char* key)
bool hasData(const char *key)
Check if key variable exists and is a data entry.
uint32 getMapCount(const char *key)
getMapCount(const char* key) Returns the number of elements in map with name '<key>'
std::map< std::string, uint64 > getTimeMap(const char *key)
returns the user time entry with this key
std::map< std::string, int64 > getIntMap(const char *key)
returns the user integer entry with this key
bool setChainCPUTime(uint32 t)
setChainCPUTime(uint32 t)
bool setTag(uint32 tag)
setTag(uint32 tag)
std::string _lastFindKey
Internal: last key looked up (single-entry lookup cache; mutable so const-style getters can update it...
uint16 getDestination()
getDestination() Get the NodeID of the destination node
bool setDestination(uint16 addr)
setDestination(uint16 addr) Set destination to address addr and return new destination
PsyContext getContextChange()
getContextChange()
uint32 getArraySize(const char *key)
getArraySize(const char* key) Returns the number of elements in array with name '<key>'
char * getDataCopy(const char *key, uint32 &size)
getDataCopy(const char* key, uint32& size)
uint16 getCycleWallTime()
getCycleWallTime()
bool setDoubleMap(const char *key, std::map< std::string, double > &map)
sets the user double entry with this key to the value provided
static bool IsPacked(const char *data, uint32 size)
True if data (at least 8 bytes) starts a packed message.
std::string getAsString(const char *key)
getAsString(const char* key)
DataMessageHeader * data
Pointer to the message's flat memory block (header + user entries).
bool setStatus(uint16 status)
setStatus(uint16 status)
std::string toJSON(std::map< uint16, std::string > *subtypes=NULL, std::map< uint16, std::string > *subcontexts=NULL, std::map< uint32, std::string > *compNames=NULL)
toJSON()
std::map< int64, float64 > getFloatArray(const char *key)
getFloatArray(const char* key)
std::string toCSV(const char *separator=NULL, const char *preample=NULL, std::map< uint16, std::string > *subtypes=NULL, std::map< uint16, std::string > *subcontexts=NULL, std::map< uint32, std::string > *compNames=NULL)
toCSV()
PsyType getType()
getType()
bool hasFloat(const char *key)
Check if key variable exists and is a float.
bool setIntMap(const char *key, std::map< std::string, int64 > &map)
sets the user integer entry with this key
char * findKey(const char *key)
findKey(const char* key) Internal function: linear scan of the entry list for an entry whose key matc...
char * findEntry(const char *key)
findEntry(const char* key)
const char * getData(const char *key, uint32 &size)
getData(const char* key, uint32& size)
uint16 getChainCPUTime()
getChainCPUTime()
uint16 getStatus()
getStatus()
bool getAsBool(const char *key)
getAsBool(const char* key)
uint32 getUserCount()
getUserCount()
uint32 getSize()
getSize() Get message size Many types of data of any size can be put into a message as user entries; ...
uint64 getReference()
getReference() Get and return message reference id
static bool ConvertDataFromOlderMessageFormat(const char *data, char **newData)
Convert a binary block in the legacy (pre-version-10, DATAMESSAGEOLDID) format to the current DataMes...
uint32 copyUserEntriesFromMessage(DataMessage *msg)
copyUserEntriesFromMessage(DataMessage* msg) Copies all user data from a message into this message,...
std::string getUserEntriesAsJSON(bool asText)
getUserEntriesAsJSON(bool asText) Returns the JSON representation of the full message including all u...
char * _lastFindKeyResult
Internal: cached location of the last key's entry inside the block; NULL if not found.
bool setAttachedMessage(const char *key, DataMessage *msg)
setAttachedMessage(const char* key, DataMessage* msg)
bool setTimeArray(const char *key, std::map< int64, uint64 > &map)
setTimeArray(const char* key, std::map<int64, uint64>& map) Sets the whole time array named key in on...
bool setRecvTime(uint64 time)
setRecvTime(uint64 time)
std::map< std::string, std::string > getStringMap(const char *key)
returns the user string entry with this key
std::map< std::string, KeyType > getAllKeys()
getAllKeys()
char * toPacked(uint32 &size) const
Encode this message in the packed wire form.
std::map< int64, std::string > getStringArray(const char *key)
getStringArray(const char* key)
bool setAttachedMessageArray(const char *key, std::map< int64, DataMessage * > &map)
setAttachedMessageArray(const char* key, std::map<int64, DataMessage*>& map) Sets the whole array of ...
bool removeEntry(const char *key)
bool hasTime(const char *key)
Check if key variable exists and is a time.
std::map< int64, std::string > getAsStringArray(const char *key)
getAsStringArray(const char* key)
bool setIntArray(const char *key, std::map< int64, int64 > &map)
setIntArray(const char* key, std::map<int64, int64>& map) Sets the whole integer array named key in o...
bool setFrom(uint32 from)
setFrom(uint32 from)
std::map< std::string, DataMessage * > getAttachedMessageMap(const char *key)
returns a copy of the user message entry with this key
bool setTo(uint32 to)
setTo(uint32 to)
bool setTimeMap(const char *key, std::map< std::string, uint64 > &map)
sets the user time entry with this key
bool setCreatedTime(uint64 t)
setCreatedTime(uint64 t)
uint32 getContentSize(const char *key)
getContentSize(const char* key)
void invalidateFindKeyCache()
Internal: clears the single-entry key-lookup cache.
uint32 getArrayCount(const char *key)
getArrayCount(const char* key) Returns the number of elements in array with name '<key>'
bool setReference(uint64 ref)
setReference(uint64 ref) Set message reference
std::map< std::string, int64 > getAsIntMap(const char *key)
returns the user entry as an integer map regardless of what type it is
bool setFloatMap(const char *key, std::map< std::string, float64 > &map)
sets the user float entry with this key to the value provided
uint64 getAsTime(const char *key)
getAsTime(const char* key)
uint16 getPriority()
getPriority()
bool isMapContent(const char *key)
isMapContent(const char* key)
bool hasKey(const char *key)
Check if key variable exists.
uint64 getTime(const char *key)
getTime(const char* key)
bool setSystemID(uint32 id)
setSystemID(uint32 id) Set message system id
bool setType(PsyType &type)
setType(PsyType &type)
bool getFloat(const char *key, float64 &value)
getFloat(const char* key, float64& value)
uint64 getTTL()
getTTL()
bool setRawData(const char *key, const char *value, uint32 size, uint32 datatype)
Internal: low-level entry writer used by all typed set...() overloads.
bool setDouble(const char *key, double value)
setDouble(const char* key, double value)
std::map< std::string, std::string > getAsStringMap(const char *key)
returns the user entry as a string regardless of what type it is
int64 getAsInt(const char *key)
getAsInt(const char* key)
uint16 getChainWallTime()
getChainWallTime()
bool setDoubleArray(const char *key, std::map< int64, double > &map)
setDoubleArray(const char* key, std::map<int64, double>& map) Sets the whole double array named key i...
bool setStringMap(const char *key, std::map< std::string, std::string > &map)
sets the user string entry with this key
bool isArrayContent(const char *key)
isArrayContent(const char* key)
DataMessage * getAttachedMessageCopy(const char *key)
getAttachedMessageCopy(const char* key)
uint64 getMemID()
getMemID() Get and return memory ID
bool isValid()
isValid() Checks that the message memory block exists and carries the current-format object id (DATAM...
uint64 getEOL()
getEOL()
uint64 getSerial()
getSerial()
bool setPolicy(uint8 policy)
setPolicy(uint8 policy)
uint8 getPolicy()
getPolicy()
uint64 getCreatedTime()
getCreatedTime()
bool hasMessage(const char *key)
Check if key variable exists and is a message.
char * getRawData()
getRawData() Retrieve a pointer to the raw binary block of the message (header + entries).
uint32 getTag()
getTag() Retrieve and return message tag id Tags can be used to segment data temporally,...
const char * swapMessageData(const char *data)
swapMessageData(const char* data)
std::map< int64, uint64 > getTimeArray(const char *key)
uint32 getMapSize(const char *key)
getMapSize(const char* key) Returns the number of elements in map with name '<key>'
std::map< int64, double > getDoubleArray(const char *key)
getDoubleArray(const char* key)
bool setData(const char *key, const char *value, uint32 size)
setData(const char* key, const char* value, uint32 size)
bool hasString(const char *key)
Check if key variable exists and is a string.
std::map< int64, DataMessage * > getAttachedMessageArray(const char *key)
getAttachedMessageArray(const char* key)
const char * getString(const char *key)
getString(const char* key)
char * findSpace(uint32 space)
findSpace(uint32 space)
static std::string GetCSVHeader(const char *separator=NULL, const char *preample=NULL)
toCSVHeader()
bool fillInDraftUserDataFrom(const char *data, uint32 maxDraftSize)
fillInDraftUserDataFrom(const char* data, uint32 maxDraftSize)
uint32 getTo()
getTo()
KeyType getKeyType(const char *key)
getKeyType(const char* key)
uint32 getContentType(const char *key)
getContentType(const char* key)
bool setSerial(uint64 serial)
setSerial(uint64 serial)
bool setCycleWallTime(uint32 t)
setCycleWallTime(uint32 t)
uint32 getChainCount()
getChainCount()
bool setStringArray(const char *key, std::map< int64, std::string > &map)
setStringArray(const char* key, std::map<int64, std::string>& map) Sets the whole string array named ...
uint64 getRecvTime()
getRecvTime()
uint16 getCycleCPUTime()
getCycleCPUTime()
uint64 getSendTime()
getSendTime()
DataMessage * copy()
copy() Creates a full deep copy of the message and returns it.
bool hasInteger(const char *key)
Check if key variable exists and is an integer.
std::map< int64, int64 > getAsIntArray(const char *key)
getAsIntArray(const char* key)
bool setAttachedMessageMap(const char *key, std::map< std::string, DataMessage * > &map)
sets the user message entry with this key to the provided message
bool setMultiple(uint32 from, uint32 to, uint32 tag, uint64 ttl, uint8 policy, uint64 sendtime)
setMultiple(uint32 from, uint32 to, uint32 tag, uint64 ttl, uint8 policy, uint64 sendtime) Set multip...
static DataMessage * FromPacked(const char *data, uint32 size)
Decode a packed message (as written by toPacked() or the Kotlin codec).
bool setContextChange(PsyContext &context)
setContextChange(PsyContext& context)
uint32 getUserSize()
getUserSize()
bool setSendTime(uint64 time)
setSendTime(uint64 time)
std::string getUserEntriesAsString()
getUserEntriesAsString() Returns the textual representation of the full message including all user en...
float64 getAsFloat(const char *key)
getAsFloat(const char* key)
std::map< std::string, float64 > getFloatMap(const char *key)
returns the user float entry with this key
bool setCycleCPUTime(uint32 t)
setCycleCPUTime(uint32 t)
std::map< std::string, float64 > getAsFloatMap(const char *key)
returns the user entry as a float map regardless of what type it is
Hierarchical execution-context identifier.
Definition Types.h:284
Hierarchical message type identifier — the key used for publish/subscribe matching in Psyclone.
Definition Types.h:123
Header preceding every user entry inside a DataMessage's flat memory block.
uint32 cid
Content type id of the payload (CONSTCHARID, TIMEID, INTID, DOUBLEID, CHARDATAID, DATAMESSAGEID,...
uint32 size
Total size of this entry in bytes, including this header, the key and the payload.
Legacy (pre-version-10) DataMessage wire header, kept only for format conversion.
The current (version 10) DataMessage wire/shared-memory header.
uint64 sendtime
Time the message was posted/sent, in microseconds.
std::string toJSON(std::map< uint16, std::string > *subtypes=NULL, std::map< uint16, std::string > *subcontexts=NULL, std::map< uint32, std::string > *compNames=NULL)
Render the header fields as a JSON object (header only, no user entries).
uint64 reference
Reference id linking this message to another (e.g.
uint16 destination
NodeID of the target node (0 = local/any).
uint32 chaincputime
Accumulated CPU time across the whole causal message chain, in microseconds.
uint64 serial
Serial number, e.g.
uint64 time
Creation time in microseconds since epoch (see PsyTime).
uint64 memid
Shared-memory id assigned when the message is stored in a memory map.
uint32 size
Total size of the whole message block in bytes (header + all entries).
uint32 userCount
Number of user entries currently in use.
uint16 status
User/application status code.
PsyType type
Hierarchical message type (e.g.
uint16 priority
Scheduling priority; higher values are processed first by whiteboards and modules.
uint32 userSize
Total size in bytes of all user entries currently in use.
uint8 policy
Delivery policy flags, e.g.
uint32 cyclewalltime
Wall-clock time spent in this cycle, in microseconds (profiling).
uint32 cid
Object id, always DATAMESSAGEID for a valid current-format message; used by isValid().
uint16 cver
Message format version (CURRENTDATAMESSAGEVERSION).
uint32 chainwalltime
Accumulated wall time across the whole causal message chain, in microseconds.
uint64 ttl
Time-to-live in microseconds; 0 means deliver-and-forget (not retained in memory).
std::string toXML(std::map< uint16, std::string > *subtypes=NULL, std::map< uint16, std::string > *subcontexts=NULL, std::map< uint32, std::string > *compNames=NULL)
Render the header fields as an XML fragment (header only, no user entries).
PsyContext contextchange
Context switch this message announces (NOCONTEXT if none).
uint32 systemid
Id of the Psyclone system instance that sent the message.
uint32 cyclecputime
CPU time spent processing the triggering message in this cycle, in microseconds (profiling).
uint32 to
Component id of the addressee; 0 for publish/subscribe distribution.
uint16 origin
NodeID of the node the message originated on.
uint32 chaincount
Number of messages in the causal chain leading to (and including) this one.
uint32 from
Component id of the sender.
uint64 recvtime
Time the message was received by the current component, in microseconds.
uint16 cenc
Encoding flags (reserved for compression/encryption of the payload).
uint32 tag
User tag id for temporal/semantic grouping of related messages.