simple_entity_registry.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "simple_entity_registry.h"
00016
00017 #include <basis/mutex.h>
00018 #include <structures/string_hash.h>
00019 #include <octopus/entity_defs.h>
00020
00021 using namespace basis;
00022 using namespace octopi;
00023 using namespace structures;
00024 using namespace timely;
00025
00026 namespace octopi {
00027
00028 #undef GRAB_LOCK
00029 #define GRAB_LOCK \
00030 auto_synchronizer l(*_secure_lock)
00031
00032 #undef LOG
00033 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s);
00034
00035 const int ENTITY_HASH_BITS = 8;
00036
00037
00038
00039
00040
00041
00043
00044 class recognized_entity
00045 {
00046 public:
00047 octopus_entity _entity;
00048 time_stamp _last_active;
00049 byte_array _verification;
00050 };
00051
00053
00054 class recognized_entity_list : public string_hash<recognized_entity>
00055 {
00056 public:
00057 recognized_entity_list() : string_hash<recognized_entity>(ENTITY_HASH_BITS) {}
00058 };
00059
00061
00062 simple_entity_registry::simple_entity_registry()
00063 : _secure_lock(new mutex),
00064 _entities(new recognized_entity_list)
00065 {
00066 }
00067
00068 simple_entity_registry::~simple_entity_registry()
00069 {
00070 WHACK(_entities);
00071 WHACK(_secure_lock);
00072 }
00073
00074 bool simple_entity_registry::authorized(const octopus_entity &entity)
00075 {
00076 GRAB_LOCK;
00077 recognized_entity *found = _entities->find(entity.mangled_form());
00078 return !!found;
00079 }
00080
00081 bool simple_entity_registry::refresh_entity(const octopus_entity &entity)
00082 {
00083 GRAB_LOCK;
00084 recognized_entity *found = _entities->find(entity.mangled_form());
00085 if (!found) return false;
00086
00087 found->_last_active = time_stamp();
00088 return true;
00089 }
00090
00091 bool simple_entity_registry::add_entity(const octopus_entity &entity,
00092 const byte_array &verification)
00093 {
00094 GRAB_LOCK;
00095 recognized_entity *found = _entities->find(entity.mangled_form());
00096 if (found) {
00097
00098 found->_last_active = time_stamp();
00099
00100
00101 if (verification.length())
00102 found->_verification = verification;
00103 } else {
00104
00105 recognized_entity *new_one = new recognized_entity;
00106 new_one->_entity = entity;
00107 new_one->_verification = verification;
00108 _entities->add(entity.mangled_form(), new_one);
00109 }
00110 return true;
00111 }
00112
00113 bool simple_entity_registry::zap_entity(const octopus_entity &entity)
00114 {
00115 GRAB_LOCK;
00116 return _entities->zap(entity.mangled_form());
00117 }
00118
00119 bool simple_entity_registry::locate_entity(const octopus_entity &entity,
00120 time_stamp &last_active, byte_array &verification)
00121 {
00122 GRAB_LOCK;
00123 recognized_entity *found = _entities->find(entity.mangled_form());
00124 if (!found) return false;
00125 last_active = found->_last_active;
00126 verification = found->_verification;
00127 return true;
00128 }
00129
00130 bool text_form_applier(const astring &formal(key), recognized_entity &info,
00131 void *datalink)
00132 {
00133 astring *accum = (astring *)datalink;
00134 *accum += astring("ent=") + info._entity.mangled_form() + ", active="
00135 + info._last_active.text_form()
00136 + a_sprintf(", %d veribytes", info._verification.length());
00137 return true;
00138 }
00139
00140 astring simple_entity_registry::text_form()
00141 {
00142 astring to_return;
00143 GRAB_LOCK;
00144 _entities->apply(text_form_applier, &to_return);
00145 return to_return;
00146 }
00147
00148 }
00149