simple_entity_registry.cpp

Go to the documentation of this file.
00001 #ifndef SIMPLE_ENTITY_REGISTRY_IMPLEMENTATION_FILE
00002 #define SIMPLE_ENTITY_REGISTRY_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : simple_entity_registry                                            *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 2002-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
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   // the hash table for entities will be 2^N wide.
00033 
00034 // this record is stored for each verified entity.  if such a record exists,
00035 // then the entity has passed through whatever security system is installed
00036 // in the octopus.
00037 
00039 
00040 class recognized_entity
00041 {
00042 public:
00043   octopus_entity _entity;    // the identifier for this entity.
00044   time_stamp _last_active;   // when was this entity last active?
00045   byte_array _verification;  // verification information sent by entity.
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   // we found their record so update that time stamp.
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     // already had a record for this guy so update the time stamp.
00094     found->_last_active = time_stamp();
00095     // if there's a verification token, make sure we keep their most recent
00096     // version of it.
00097     if (verification.length())
00098       found->_verification = verification;
00099   } else {
00100     // this is a new entity, so add a new entity record for it.
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 

Generated on Fri Nov 21 04:29:59 2008 for HOOPLE Libraries by  doxygen 1.5.1