simple_entity_registry.cpp

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