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