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
110
111#include "Types.h"
112#include "ObjectIDs.h"
113//#include "Collections.h"
114#include "Utils.h"
115
116namespace cmlabs {
117
129 uint32 size;
130 uint32 cid;
131};
132
144 uint32 size; // auto
145 uint32 cid; // auto
146 uint64 memid; // auto
147 uint64 time; // auto
148 uint64 sendtime; // api
149 uint64 recvtime; // api
150 uint16 origin; // NodeID, api
151 uint16 destination; // NodeID, api
152
153 uint64 ttl; // spec
154 uint16 priority; // spec
155 uint8 policy; // spec
156
157 uint32 from; // user
158 uint32 to; // user
159
160 PsyType type; // api/user
161 uint32 tag; // api/user
162 uint16 status; // api/user
163 uint64 reference; // api
164 uint64 serial; // api/user
166
167 uint32 userSize; // size of user data
168 uint32 userCount; // number of user data entries
169};
170
189 uint32 size;
190 uint32 cid;
191 uint16 cver;
192 uint16 cenc;
193 uint64 memid;
194 uint64 time;
195 uint64 sendtime;
196 uint64 recvtime;
197 uint16 origin;
198 uint16 destination;
199
200 uint64 ttl;
201 uint16 priority;
202 uint8 policy;
203
204 uint32 from;
205 uint32 to;
206
208 uint32 tag;
209 uint16 status;
210 uint64 reference;
211 uint64 serial;
213
214 uint32 userSize;
215 uint32 userCount;
216
221 uint32 chaincount;
222
223 uint32 systemid;
224
230 std::string toJSON(std::map<uint16, std::string>* subtypes = NULL, std::map<uint16, std::string>* subcontexts = NULL, std::map<uint32, std::string>* compNames = NULL);
236 std::string toXML(std::map<uint16, std::string>* subtypes = NULL, std::map<uint16, std::string>* subcontexts = NULL, std::map<uint32, std::string>* compNames = NULL);
237};
238
279public:
286 static DataMessage* FindClosestMessage(uint64 t, std::map<uint64, DataMessage*> &messages);
287
293 static bool ConvertDataFromOlderMessageFormat(const char* data, char **newData);
294
297 static bool UnitTest();
298
302 DataMessage();
308 DataMessage(PsyType type, uint32 from);
317 DataMessage(PsyType type, uint32 from, uint32 to, uint64 ttl = 0, uint16 priority = 0);
328 DataMessage(PsyType type, uint32 from, uint32 to, uint32 tag, uint64 time, uint64 ttl = 0, uint16 priority = 0);
334 DataMessage(char* data, bool copy = false);
339 DataMessage(const char* data);
344 DataMessage(const DataMessage& msg);
352 DataMessage(const char* data, uint32 maxDraftSize);
360 DataMessage(const DataMessage& msg, uint32 maxDraftSize);
364 ~DataMessage();
365
371 return new DataMessage((const char*)(this->data));
372 }
373
383
384 const char* swapMessageData(const char* data);
385
397 bool fillInDraftUserDataFrom(const char* data, uint32 maxDraftSize);
398
405 bool isValid() {return data && GetObjID(data) == DATAMESSAGEID; }
406
407 // User
408
415
416 uint32 getContentType(const char* key);
417
424
425 uint32 getContentSize(const char* key);
426
435
436 bool getAsBool(const char* key);
437
445
446 std::string getAsString(const char* key);
447
455
456 int64 getAsInt(const char* key);
457
465
466 uint64 getAsTime(const char* key);
467
475
476 float64 getAsFloat(const char* key);
477
484
485 uint64 getTime(const char* key);
486
493
494 const char* getString(const char* key);
495
503
504 const char* getString(const char* key, uint32 &size);
505
513
514 bool getInt(const char* key, int64& value);
515
524
525 bool getDouble(const char* key, double& value);
526
535
536 bool getFloat(const char* key, float64& value);
537
545
546 const char* getData(const char* key, uint32& size);
547
556
557 char* getDataCopy(const char* key, uint32& size);
558
566
567 DataMessage* getAttachedMessageCopy(const char* key);
568
569
570
571
579
580 uint64 getTime(const char* key, bool& success);
581
589
590
591 const char* getString(const char* key, bool& success);
592
601
602 const char* getString(const char* key, uint32 &size, bool& success);
603
612
613
614 int64 getInt(const char* key, bool& success);
615
625
626 double getDouble(const char* key, bool& success);
627
636
637 float64 getFloat(const char* key, bool& success);
638
647
648 const char* getData(const char* key, uint32& size, bool& success);
649
660
661 char* getDataCopy(const char* key, uint32& size, bool& success);
662
671
672 DataMessage* getAttachedMessageCopy(const char* key, bool& success);
673
674
683
684 int64 getInt(const char* key);
685
695
696 double getDouble(const char* key);
697
698
708
709 float64 getFloat(const char* key);
710
711 // *** setting all of the above, returns success or fail (usually because the message is not valid)
712
722 bool setTime(const char* key, uint64 value);
723
733 bool setString(const char* key, const char* value);
734
743 bool setInt(const char* key, int64 value);
744
753 bool setDouble(const char* key, double value);
754
764 bool setFloat(const char* key, float64 value);
765
776 bool setData(const char* key, const char* value, uint32 size);
777
788 bool setAttachedMessage(const char* key, DataMessage* msg);
789
795 uint32 getUserSize() {
796 return data->userSize;
797 }
798
804 uint32 getUserCount() {
805 return data->userCount;
806 }
807
808 // Arrays
809 // *** Same as above for single values, but now getting and setting entries in arrays
810 // myval[0] = val, myval[1] = val, myval[2] = val, etc.
811
813
820
821 KeyType getKeyType(const char* key);
822
829 bool isArrayContent(const char* key);
830
837 bool isMapContent(const char* key);
838
845 bool isSingleValue(const char* key);
846
847
854 bool hasKey(const char* key);
855
862 bool hasString(const char* key);
863
870 bool hasInteger(const char* key);
871
878 bool hasFloat(const char* key);
879
886 bool hasTime(const char* key);
887
894 bool hasData(const char* key);
895
902 bool hasMessage(const char* key);
903
904
910 uint32 getContentType(int64 i, const char* key);
911
917 uint32 getContentSize(int64 i, const char* key);
918
927 uint64 getTime(int64 i, const char* key);
928
934 std::string getAsString(int64 i, const char* key);
935
941 int64 getAsInt(int64 i, const char* key);
942
948 uint64 getAsTime(int64 i, const char* key);
949
955 float64 getAsFloat(int64 i, const char* key);
956
962 const char* getString(int64 i, const char* key);
963
970 const char* getString(int64 i, const char* key, uint32 &size);
971
978 bool getInt(int64 i, const char* key, int64& value);
979
986 bool getDouble(int64 i, const char* key, double& value);
987
994 bool getFloat(int64 i, const char* key, float64& value);
995
1002 int64 getInt(int64 i, const char* key, bool& success);
1009 double getDouble(int64 i, const char* key, bool& success);
1016 float64 getFloat(int64 i, const char* key, bool& success);
1023 int64 getInt(int64 i, const char* key);
1029 double getDouble(int64 i, const char* key);
1035 float64 getFloat(int64 i, const char* key);
1036
1043 const char* getData(int64 i, const char* key, uint32& size);
1044
1051 char* getDataCopy(int64 i, const char* key, uint32& size);
1052
1059 DataMessage* getAttachedMessageCopy(int64 i, const char* key);
1060
1067
1068 bool setTime(int64 i, const char* key, uint64 value);
1069
1076 bool setString(int64 i, const char* key, const char* value);
1077
1084 bool setInt(int64 i, const char* key, int64 value);
1085
1092 bool setDouble(int64 i, const char* key, double value);
1093
1100 bool setFloat(int64 i, const char* key, float64 value);
1108 bool setData(int64 i, const char* key, const char* value, uint32 size);
1109 // bool setData(int64 i, const char* key, const void* value, uint32 size);
1110
1117 bool setAttachedMessage(int64 i, const char* key, DataMessage* msg);
1118
1119 // *** Same as above but reading whole arrays in one go
1120 std::map<int64, uint64> getTimeArray(const char* key);
1125 std::map<int64, std::string> getAsStringArray(const char* key);
1130 std::map<int64, int64> getAsIntArray(const char* key);
1135 std::map<int64, float64> getAsFloatArray(const char* key);
1140 std::map<int64, std::string> getStringArray(const char* key);
1146 std::map<int64, int64> getIntArray(const char* key);
1151 std::map<int64, double> getDoubleArray(const char* key);
1156 std::map<int64, float64> getFloatArray(const char* key);
1161 std::map<int64, DataMessage*> getAttachedMessageArray(const char* key);
1162
1167 uint32 getArraySize(const char* key);
1168
1173 uint32 getArrayCount(const char* key);
1174
1179 uint32 getMapSize(const char* key);
1180
1185 uint32 getMapCount(const char* key);
1186
1187
1190 // *** Same as above but setting whole arrays in one go
1197 bool setTimeArray(const char* key, std::map<int64, uint64>& map);
1198
1205 bool setStringArray(const char* key, std::map<int64, std::string>& map);
1206
1213 bool setIntArray(const char* key, std::map<int64, int64>& map);
1214
1221 bool setDoubleArray(const char* key, std::map<int64, double>& map);
1222
1229 bool setFloatArray(const char* key, std::map<int64, float64>& map);
1230
1238 bool setAttachedMessageArray(const char* key, std::map<int64, DataMessage*>& map);
1239
1240 // Maps
1243
1244 // *** Same as for arrays, but now with maps
1245 // myval["up"] = val, myval["down"] = val, myval["left"] = val, etc.
1246
1255 uint32 getContentType(const char* idx, const char* key);
1256
1264 uint32 getContentSize(const char* idx, const char* key);
1272
1273 uint64 getTime(const char* idx, const char* key);
1282 std::string getAsString(const char* idx, const char* key);
1291 int64 getAsInt(const char* idx, const char* key);
1300 uint64 getAsTime(const char* idx, const char* key);
1309 float64 getAsFloat(const char* idx, const char* key);
1317 const char* getString(const char* idx, const char* key);
1326 const char* getString(const char* idx, const char* key, uint32 &size);
1335
1336 bool getInt(const char* idx, const char* key, int64& value);
1346 bool getDouble(const char* idx, const char* key, double& value);
1356 bool getFloat(const char* idx, const char* key, float64& value);
1364 int64 getInt(const char* idx, const char* key, bool& success);
1373 double getDouble(const char* idx, const char* key, bool& success);
1382 float64 getFloat(const char* idx, const char* key, bool& success);
1390 int64 getInt(const char* idx, const char* key);
1399 double getDouble(const char* idx, const char* key);
1407 float64 getFloat(const char* idx, const char* key);
1416 const char* getData(const char* idx, const char* key, uint32& size);
1417
1426 char* getDataCopy(const char* idx, const char* key, uint32& size);
1434 DataMessage* getAttachedMessageCopy(const char* idx, const char* key);
1443 bool setTime(const char* idx, const char* key, uint64 value);
1452 bool setString(const char* idx, const char* key, const char* value);
1461 bool setInt(const char* idx, const char* key, int64 value);
1471 bool setDouble(const char* idx, const char* key, double value);
1481 bool setFloat(const char* idx, const char* key, float64 value);
1491 bool setData(const char* idx, const char* key, const char* value, uint32 size);
1500 bool setAttachedMessage(const char* idx, const char* key, DataMessage* msg);
1501
1508 std::map<std::string, uint64> getTimeMap(const char* key);
1516 std::map<std::string, std::string> getAsStringMap(const char* key);
1524 std::map<std::string, int64> getAsIntMap(const char* key);
1532 std::map<std::string, float64> getAsFloatMap(const char* key);
1539 std::map<std::string, std::string> getStringMap(const char* key);
1546 std::map<std::string, int64> getIntMap(const char* key);
1554 std::map<std::string, double> getDoubleMap(const char* key);
1561 std::map<std::string, float64> getFloatMap(const char* key);
1568 std::map<std::string, DataMessage*> getAttachedMessageMap(const char* key);
1576 bool setTimeMap(const char* key, std::map<std::string, uint64>& map);
1584 bool setStringMap(const char* key, std::map<std::string, std::string>& map);
1592 bool setIntMap(const char* key, std::map<std::string, int64>& map);
1601 bool setDoubleMap(const char* key, std::map<std::string, double>& map);
1610 bool setFloatMap(const char* key, std::map<std::string, float64>& map);
1618 bool setAttachedMessageMap(const char* key, std::map<std::string, DataMessage*>& map);
1619
1625
1626
1627 std::string toJSON(std::map<uint16, std::string>* subtypes = NULL, std::map<uint16, std::string>* subcontexts = NULL, std::map<uint32, std::string>* compNames = NULL);
1628
1633
1634 std::string toXML(std::map<uint16, std::string>* subtypes = NULL, std::map<uint16, std::string>* subcontexts = NULL, std::map<uint32, std::string>* compNames = NULL);
1635
1645
1646 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);
1647
1654
1655 static std::string GetCSVHeader(const char* separator = NULL, const char* preample = NULL);
1656
1657
1664
1668 std::string getUserEntriesAsString();
1669
1675 std::string getUserEntriesAsJSON(bool asText);
1676
1677
1678 // API - inline
1679
1686 char* getRawData() {
1687 if (data->cid != DATAMESSAGEID) return NULL;
1688 return (char*)data;
1689 }
1690
1695
1696 uint16 getOrigin() {
1697 if (data->cid != DATAMESSAGEID) return 0;
1698 return data->origin;
1699 }
1700
1706 if (data->cid != DATAMESSAGEID) return 0;
1707 return data->destination;
1708 }
1709
1715 bool setOrigin(uint16 addr) {
1716 if (data->cid != DATAMESSAGEID) return false;
1717 data->origin = addr;
1718 return true;
1719 }
1720
1726 bool setDestination(uint16 addr) {
1727 if (data->cid != DATAMESSAGEID) return false;
1728 data->destination = addr;
1729 return true;
1730 }
1731
1738 uint32 getSize() {
1739 if (data->cid != DATAMESSAGEID) return 0;
1740 return data->size;
1741 }
1742
1753 bool setMultiple(uint32 from, uint32 to, uint32 tag, uint64 ttl, uint8 policy, uint64 sendtime) {
1754 if (data->cid != DATAMESSAGEID) return false;
1755 if (from) data->from = from;
1756 if (to) data->to = to;
1757 if (tag) data->tag = tag;
1758 if (ttl) data->ttl = ttl;
1759 if (policy) data->policy = policy;
1760 if (sendtime) data->sendtime = sendtime;
1761 return true;
1762 }
1763
1770 uint32 getTag() {
1771 if (data->cid != DATAMESSAGEID) return 0;
1772 return data->tag;
1773 }
1774
1785 bool setTag(uint32 tag) {
1786 if (data->cid != DATAMESSAGEID) return false;
1787 data->tag = tag;
1788 return true;
1789 }
1790
1795 uint64 getReference() {
1796 if (data->cid != DATAMESSAGEID) return 0;
1797 return data->reference;
1798 }
1799
1805 bool setReference(uint64 ref) {
1806 if (data->cid != DATAMESSAGEID) return false;
1807 data->reference = ref;
1808 return true;
1809 }
1810
1811
1816 uint32 getSystemID() {
1817 if (data->cid != DATAMESSAGEID) return 0;
1818 return data->systemid;
1819 }
1820
1826 bool setSystemID(uint32 id) {
1827 if (data->cid != DATAMESSAGEID) return false;
1828 data->systemid = id;
1829 return true;
1830 }
1831
1836 uint64 getMemID() {
1837 if (data->cid != DATAMESSAGEID) return 0;
1838 return data->memid;
1839 }
1840
1847 bool setMemID(uint64 id) {
1848 if (data->cid != DATAMESSAGEID) return false;
1849 data->memid = id;
1850 return true;
1851 }
1852
1857 uint32 getFrom() {
1858 if (data->cid != DATAMESSAGEID) return 0;
1859 return data->from;
1860 }
1861
1872
1873 bool setFrom(uint32 from) {
1874 if (data->cid != DATAMESSAGEID) return false;
1875 data->from = from;
1876 return true;
1877 }
1878
1886
1887 uint32 getTo() {
1888 if (data->cid != DATAMESSAGEID) return 0;
1889 return data->to;
1890 }
1891
1901
1902 bool setTo(uint32 to) {
1903 if (data->cid != DATAMESSAGEID) return false;
1904 data->to = to;
1905 return true;
1906 }
1907
1915
1917 PsyType t = NOTYPE;
1918 if (data->cid != DATAMESSAGEID) return t;
1919 return data->type;
1920 }
1921
1931
1932 bool setType(PsyType &type) {
1933 if (data->cid != DATAMESSAGEID) return false;
1934 data->type = type;
1935 return true;
1936 }
1937
1947
1950 if (data->cid != DATAMESSAGEID) return c;
1951 return data->contextchange;
1952 }
1953
1966
1968 if (data->cid != DATAMESSAGEID) return false;
1969 data->contextchange = context;
1970 return true;
1971 }
1972
1986
1987 uint64 getTTL() {
1988 if (data->cid != DATAMESSAGEID) return 0;
1989 return data->ttl;
1990 }
1991
2001
2002 uint64 getEOL() {
2003 if (data->cid != DATAMESSAGEID) return 0;
2004 if (!data->ttl)
2005 return 0;
2006 return data->time + data->ttl;
2007 }
2008
2023
2024 bool setTTL(uint64 ttl) {
2025 if (data->cid != DATAMESSAGEID) return false;
2026 data->ttl = ttl;
2027 return true;
2028 }
2029
2041
2042 bool setEOL(uint64 eol) {
2043 if (data->cid != DATAMESSAGEID) return false;
2044 int64 ttl = eol - data->time;
2045 if (ttl > 0) {
2046 data->ttl = (uint64)ttl;
2047 return true;
2048 }
2049 else
2050 return false;
2051 }
2052
2062
2063 uint16 getPriority() {
2064 if (data->cid != DATAMESSAGEID) return 0;
2065 return data->priority;
2066 }
2067
2081
2082 bool setPriority(uint16 priority) {
2083 if (data->cid != DATAMESSAGEID) return false;
2084 data->priority = priority;
2085 return true;
2086 }
2087
2097
2098 uint8 getPolicy() {
2099 if (data->cid != DATAMESSAGEID) return 0;
2100 return data->policy;
2101 }
2102
2113
2114 bool setPolicy(uint8 policy) {
2115 if (data->cid != DATAMESSAGEID) return false;
2116 data->policy = policy;
2117 return true;
2118 }
2119
2128
2129 uint64 getSendTime() {
2130 if (data->cid != DATAMESSAGEID) return 0;
2131 return data->sendtime;
2132 }
2133
2144
2145 bool setSendTime(uint64 time) {
2146 if (data->cid != DATAMESSAGEID) return false;
2147 data->sendtime = time;
2148 return true;
2149 }
2150
2159
2160 uint64 getRecvTime() {
2161 if (data->cid != DATAMESSAGEID) return 0;
2162 return data->recvtime;
2163 }
2164
2175
2176 bool setRecvTime(uint64 time) {
2177 if (data->cid != DATAMESSAGEID) return false;
2178 data->recvtime = time;
2179 return true;
2180 }
2181
2191
2193 if (data->cid != DATAMESSAGEID) return 0;
2194 return data->time;
2195 }
2196
2207
2208 bool setCreatedTime(uint64 t) {
2209 if (data->cid != DATAMESSAGEID) return 0;
2210 data->time = t;
2211 return true;
2212 }
2213
2222
2223 uint16 getStatus() {
2224 if (data->cid != DATAMESSAGEID) return 0;
2225 return data->status;
2226 }
2227
2238
2239 bool setStatus(uint16 status) {
2240 if (data->cid != DATAMESSAGEID) return false;
2241 data->status = status;
2242 return true;
2243 }
2244
2252
2253 uint64 getSerial() {
2254 if (data->cid != DATAMESSAGEID) return 0;
2255 return data->serial;
2256 }
2257
2268
2269 bool setSerial(uint64 serial) {
2270 if (data->cid != DATAMESSAGEID) return false;
2271 data->serial = serial;
2272 return true;
2273 }
2274
2275
2276
2277
2278
2279
2280
2281
2282
2297
2298 bool addTimeUsage(uint32 cycleCPUTime, uint32 cycleWallTime, uint32 chainCPUTime = 0, uint32 chainWallTime = 0, uint32 chainCount = 0);
2299
2300
2309
2311 if (data->cid != DATAMESSAGEID) return 0;
2312 return data->cyclecputime;
2313 }
2314
2325
2326 bool setCycleCPUTime(uint32 t) {
2327 if (data->cid != DATAMESSAGEID) return false;
2328 data->cyclecputime = t;
2329 return true;
2330 }
2331
2332
2333
2334
2343
2345 if (data->cid != DATAMESSAGEID) return 0;
2346 return data->cyclewalltime;
2347 }
2348
2359
2360 bool setCycleWallTime(uint32 t) {
2361 if (data->cid != DATAMESSAGEID) return false;
2362 data->cyclewalltime = t;
2363 return true;
2364 }
2365
2366
2367
2368
2369
2378
2380 if (data->cid != DATAMESSAGEID) return 0;
2381 return data->chaincputime;
2382 }
2383
2394
2395 bool setChainCPUTime(uint32 t) {
2396 if (data->cid != DATAMESSAGEID) return false;
2397 data->chaincputime = t;
2398 return true;
2399 }
2400
2401
2402
2403
2412
2414 if (data->cid != DATAMESSAGEID) return 0;
2415 return data->chainwalltime;
2416 }
2417
2428
2429 bool setChainWallTime(uint32 t) {
2430 if (data->cid != DATAMESSAGEID) return false;
2431 data->chainwalltime = t;
2432 return true;
2433 }
2434
2435
2436
2445
2446 uint32 getChainCount() {
2447 if (data->cid != DATAMESSAGEID) return 0;
2448 return data->chaincount;
2449 }
2450
2461
2462 bool setChainCount(uint32 count) {
2463 if (data->cid != DATAMESSAGEID) return false;
2464 data->chaincount= count;
2465 return true;
2466 }
2467
2477
2478 bool deleteEntry(const char* key) {
2479 if (data->cid != DATAMESSAGEID) return false;
2480 return removeEntry(key);
2481 }
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2504
2505 mutable std::string _lastFindKey;
2506 mutable char* _lastFindKeyResult = NULL;
2507
2511 _lastFindKey.clear();
2512 _lastFindKeyResult = NULL;
2513 }
2514
2515protected:
2522 bool setRawData(const char* key, const char* value, uint32 size, uint32 datatype);
2523
2532 char* findKey(const char* key) {
2533 if (data->cid != DATAMESSAGEID) return NULL;
2534 if (!_lastFindKey.empty() && stricmp(key, _lastFindKey.c_str()) == 0)
2535 return _lastFindKeyResult;
2536 char* src = (char*)data + sizeof(DataMessageHeader);
2537 char* srcEnd = (char*)data + data->size;
2538 while (src < srcEnd) {
2539 if (((DataMessageEntryHeader*)src)->cid == CONSTCHARID) {
2540 if (stricmp(key, src+sizeof(DataMessageEntryHeader)) == 0) {
2541 _lastFindKey = key;
2542 _lastFindKeyResult = src;
2543 return src;
2544 }
2545 }
2546 src += ((DataMessageEntryHeader*)src)->size;
2547 }
2548 _lastFindKey = key;
2549 _lastFindKeyResult = NULL;
2550 return NULL;
2551 }
2552
2561
2562 char* findEntry(const char* key) {
2563 char* src = findKey(key);
2564 if (src == NULL) return NULL;
2565 return src + sizeof(DataMessageEntryHeader) + strlen(key) + 1;
2566 }
2567
2577
2578 bool removeEntry(const char* key) {
2579 char* src = findKey(key);
2580 if (src == NULL) return true;
2581 ((DataMessageEntryHeader*)src)->cid = UNUSEDID;
2582 data->userCount--;
2583 data->userSize -= ((DataMessageEntryHeader*)src)->size;
2585 return true;
2586 }
2587
2599
2600 char* findSpace(uint32 space) {
2601 if (data->cid != DATAMESSAGEID) return NULL;
2602 char* src = (char*)data + sizeof(DataMessageHeader);
2603 char* srcEnd = (char*)data + data->size;
2604 while (src < srcEnd) {
2605 if ( (((DataMessageEntryHeader*)src)->cid == UNUSEDID) &&
2606 (((DataMessageEntryHeader*)src)->size == space)) {
2607 // Used to allow anything smaller, but that needs fixing permanently first
2608 // (((DataMessageEntryHeader*)src)->size >= space) ) {
2609 return src;
2610 }
2611 src += ((DataMessageEntryHeader*)src)->size;
2612 }
2613 return NULL;
2614 }
2615
2616};
2617
2618} // namespace cmlabs
2619
2620#endif //_DATAMESSAGE_H_
2621
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
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
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< 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)
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...
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.