| 1 | #include <boost/interprocess/managed_shared_memory.hpp>
|
|---|
| 2 | #include <boost/interprocess/containers/map.hpp>
|
|---|
| 3 |
|
|---|
| 4 | #include <sys/time.h>
|
|---|
| 5 | #include <unistd.h>
|
|---|
| 6 | #include <cstdio>
|
|---|
| 7 | #include <string.h>
|
|---|
| 8 |
|
|---|
| 9 | using namespace boost::interprocess;
|
|---|
| 10 |
|
|---|
| 11 | // I'm lazy to invent The Proper Type for Routes Dictionary Keys
|
|---|
| 12 | // timeval + pid would perfectly work and cheap to implement
|
|---|
| 13 | struct RouteKey: timeval {
|
|---|
| 14 | protected:
|
|---|
| 15 | bool init(const char* _id) {
|
|---|
| 16 | return (3 == sscanf(_id, "%lX.%lX.%X", &this->tv_sec, &this->tv_usec, &this->pid));
|
|---|
| 17 | }
|
|---|
| 18 | public:
|
|---|
| 19 | pid_t pid;
|
|---|
| 20 | bool operator==(const RouteKey& _o) const {
|
|---|
| 21 | return (this->tv_sec == _o.tv_sec &&
|
|---|
| 22 | this->tv_usec == _o.tv_usec &&
|
|---|
| 23 | this->pid == _o.pid
|
|---|
| 24 | );
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | bool operator<(const RouteKey& _o) const {
|
|---|
| 28 | return (this->tv_sec < _o.tv_sec ||
|
|---|
| 29 | this->tv_usec < _o.tv_usec ||
|
|---|
| 30 | this->pid < _o.pid
|
|---|
| 31 | );
|
|---|
| 32 | }
|
|---|
| 33 | RouteKey(const char* _id) {
|
|---|
| 34 | if (!init(_id)) {
|
|---|
| 35 | throw std::invalid_argument("Invalid Route Key format");
|
|---|
| 36 | }
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | // no-throw variant of the construct-from-id
|
|---|
| 40 | RouteKey(bool& _success, const char* _id) throw () {
|
|---|
| 41 | _success = init(_id);
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | operator const std::string() const {
|
|---|
| 45 | char buf[40 + 1 + 40 + 1 + 40 + 1];
|
|---|
| 46 | sprintf(buf, "%lX.%lX.%X", this->tv_sec, this->tv_usec, this->pid);
|
|---|
| 47 | return std::string(buf);
|
|---|
| 48 | }
|
|---|
| 49 | };
|
|---|
| 50 |
|
|---|
| 51 | #define ROUTE_SEGMENT_NAME "TestSegment"
|
|---|
| 52 | #define ROUTE_MAP_NAME "TestRoutes"
|
|---|
| 53 | #define ROUTE_SEGMENT_SIZE 1024 * 1024 * 20
|
|---|
| 54 |
|
|---|
| 55 | typedef std::pair<const RouteKey, int> RouteMapValue;
|
|---|
| 56 | typedef allocator<RouteMapValue, managed_shared_memory::segment_manager> RouteMapValueAllocator;
|
|---|
| 57 | typedef map<RouteKey, int, std::less<RouteKey>, RouteMapValueAllocator> RouteMap;
|
|---|
| 58 |
|
|---|
| 59 | void addItem(const char* _k)
|
|---|
| 60 | {
|
|---|
| 61 | managed_shared_memory segment(open_only, ROUTE_SEGMENT_NAME);
|
|---|
| 62 | RouteMapValueAllocator alloc(segment.get_segment_manager());
|
|---|
| 63 | RouteMap *mymap = segment.find<RouteMap>(ROUTE_MAP_NAME).first;
|
|---|
| 64 | RouteKey k(_k);
|
|---|
| 65 | RouteMapValue v(k, 1);
|
|---|
| 66 | mymap->insert(v);
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | int findItem(const char* _k)
|
|---|
| 70 | {
|
|---|
| 71 | managed_shared_memory segment(open_only, ROUTE_SEGMENT_NAME);
|
|---|
| 72 | RouteMapValueAllocator alloc(segment.get_segment_manager());
|
|---|
| 73 | RouteMap *mymap = segment.find<RouteMap>(ROUTE_MAP_NAME).first;
|
|---|
| 74 | RouteKey k(_k);
|
|---|
| 75 | RouteMap::const_iterator it = mymap->find(k);
|
|---|
| 76 | if (it == mymap->end()) {
|
|---|
| 77 | printf("element %s is NOT found\n", ((const std::string&)k).c_str());
|
|---|
| 78 | return 55;
|
|---|
| 79 | }
|
|---|
| 80 | printf("element %s is in the map\n", ((const std::string&)k).c_str());
|
|---|
| 81 | return 0;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | void listItems()
|
|---|
| 85 | {
|
|---|
| 86 | managed_shared_memory segment(open_only, ROUTE_SEGMENT_NAME);
|
|---|
| 87 | RouteMapValueAllocator alloc(segment.get_segment_manager());
|
|---|
| 88 | RouteMap *mymap = segment.find<RouteMap>(ROUTE_MAP_NAME).first;
|
|---|
| 89 | printf("RouteMap content (%zd element(s)):\n", mymap->size());
|
|---|
| 90 | for(RouteMap::const_iterator it = mymap->begin(); it != mymap->end(); ++it) {
|
|---|
| 91 | printf(" \"%s\"\n", ((const std::string&)it->first).c_str());
|
|---|
| 92 | }
|
|---|
| 93 | printf("end of RouteMap content\n");
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | void createSegment()
|
|---|
| 97 | {
|
|---|
| 98 | managed_shared_memory segment(open_or_create, ROUTE_SEGMENT_NAME, ROUTE_SEGMENT_SIZE);
|
|---|
| 99 | RouteMapValueAllocator alloc(segment.get_segment_manager());
|
|---|
| 100 | RouteMap *mymap = segment.find_or_construct<RouteMap>(ROUTE_MAP_NAME) //object name
|
|---|
| 101 | (std::less<RouteKey>() //first ctor parameter
|
|---|
| 102 | ,alloc); //second ctor parameter
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | int main(int argc, char* argv[])
|
|---|
| 106 | {
|
|---|
| 107 | if (argc < 2) {
|
|---|
| 108 | fprintf(stderr, "Invalid usage\n");
|
|---|
| 109 | return 2;
|
|---|
| 110 | }
|
|---|
| 111 | if (0 == strcmp("create", argv[1])) {
|
|---|
| 112 | createSegment();
|
|---|
| 113 | return 0;
|
|---|
| 114 | } else if (0 == strcmp("add", argv[1])) {
|
|---|
| 115 | if (argc < 3) {
|
|---|
| 116 | fprintf(stderr, "insufficient parameters for add\n");
|
|---|
| 117 | return 3;
|
|---|
| 118 | }
|
|---|
| 119 | addItem(argv[2]);
|
|---|
| 120 | return 0;
|
|---|
| 121 | } else if (0 == strcmp("find", argv[1])) {
|
|---|
| 122 | if (argc < 3) {
|
|---|
| 123 | fprintf(stderr, "insufficient parameters for find\n");
|
|---|
| 124 | return 4;
|
|---|
| 125 | }
|
|---|
| 126 | return findItem(argv[2]);
|
|---|
| 127 | } if (0 == strcmp("list", argv[1])) {
|
|---|
| 128 | listItems();
|
|---|
| 129 | return 0;
|
|---|
| 130 | } else {
|
|---|
| 131 | fprintf(stderr, "unknown command\n");
|
|---|
| 132 | return 5;
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|