feisty meow concerns codebase 2.140
simple_entity_registry.cpp
Go to the documentation of this file.
1/*****************************************************************************\
2* *
3* Name : simple_entity_registry *
4* Author : Chris Koeritz *
5* *
6*******************************************************************************
7* Copyright (c) 2002-$now By Author. This program is free software; you can *
8* redistribute it and/or modify it under the terms of the GNU General Public *
9* License as published by the Free Software Foundation; either version 2 of *
10* the License or (at your option) any later version. This is online at: *
11* http://www.fsf.org/copyleft/gpl.html *
12* Please send any updates to: fred@gruntose.com *
13\*****************************************************************************/
14
16
17#include <basis/mutex.h>
19#include <octopus/entity_defs.h>
20
21using namespace basis;
22using namespace octopi;
23using namespace structures;
24using namespace timely;
25
26namespace octopi {
27
28#undef GRAB_LOCK
29#define GRAB_LOCK \
30 auto_synchronizer l(*_secure_lock)
31
32#undef LOG
33#define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s);
34
35const int ENTITY_HASH_BITS = 8;
36 // the hash table for entities will be 2^N wide.
37
38// this record is stored for each verified entity. if such a record exists,
39// then the entity has passed through whatever security system is installed
40// in the octopus.
41
43
44class recognized_entity
45{
46public:
47 octopus_entity _entity; // the identifier for this entity.
48 time_stamp _last_active; // when was this entity last active?
49 byte_array _verification; // verification information sent by entity.
50};
51
53
54class recognized_entity_list : public string_hash<recognized_entity>
55{
56public:
57 recognized_entity_list() : string_hash<recognized_entity>(ENTITY_HASH_BITS) {}
58};
59
61
63: _secure_lock(new mutex),
64 _entities(new recognized_entity_list)
65{
66}
67
69{
70 WHACK(_entities);
71 WHACK(_secure_lock);
72}
73
75{
77 recognized_entity *found = _entities->find(entity.mangled_form());
78 return !!found;
79}
80
82{
84 recognized_entity *found = _entities->find(entity.mangled_form());
85 if (!found) return false;
86 // we found their record so update that time stamp.
87 found->_last_active = time_stamp();
88 return true;
89}
90
92 const byte_array &verification)
93{
95 recognized_entity *found = _entities->find(entity.mangled_form());
96 if (found) {
97 // already had a record for this guy so update the time stamp.
98 found->_last_active = time_stamp();
99 // if there's a verification token, make sure we keep their most recent
100 // version of it.
101 if (verification.length())
102 found->_verification = verification;
103 } else {
104 // this is a new entity, so add a new entity record for it.
105 recognized_entity *new_one = new recognized_entity;
106 new_one->_entity = entity;
107 new_one->_verification = verification;
108 _entities->add(entity.mangled_form(), new_one);
109 }
110 return true;
111}
112
114{
115 GRAB_LOCK;
116 return _entities->zap(entity.mangled_form());
117}
118
120 time_stamp &last_active, byte_array &verification)
121{
122 GRAB_LOCK;
123 recognized_entity *found = _entities->find(entity.mangled_form());
124 if (!found) return false;
125 last_active = found->_last_active;
126 verification = found->_verification;
127 return true;
128}
129
130bool text_form_applier(const astring &formal(key), recognized_entity &info,
131 void *datalink)
132{
133 astring *accum = (astring *)datalink;
134 *accum += astring("ent=") + info._entity.mangled_form() + ", active="
135 + info._last_active.text_form()
136 + a_sprintf(", %d veribytes", info._verification.length());
137 return true;
138}
139
141{
142 astring to_return;
143 GRAB_LOCK;
144 _entities->apply(text_form_applier, &to_return);
145 return to_return;
146}
147
148} //namespace.
149
a_sprintf is a specialization of astring that provides printf style support.
Definition astring.h:440
int length() const
Returns the current reported length of the allocated C array.
Definition array.h:115
Provides a dynamically resizable ASCII character string.
Definition astring.h:35
A very common template for a dynamic array of bytes.
Definition byte_array.h:36
Provides a way of identifying users of an octopus object.
Definition entity_defs.h:35
basis::astring mangled_form() const
returns the combined string form of the identifier.
virtual bool locate_entity(const octopus_entity &entity, timely::time_stamp &last_active, basis::byte_array &verification)
retrieves the "security_record" for the "entity" if it exists.
virtual bool zap_entity(const octopus_entity &entity)
removes an "entity" if the entity can be found.
virtual bool refresh_entity(const octopus_entity &entity)
this should be used to refresh the entity's health record.
virtual bool add_entity(const octopus_entity &entity, const basis::byte_array &verification)
adds the "entity" to the list of authorized users.
virtual basis::astring text_form()
shows the contents of the registry.
virtual bool authorized(const octopus_entity &entity)
returns true if the "entity" is a registered and authorized entity.
Implements a hash table indexed on character strings.
Definition string_hash.h:29
Represents a point in time relative to the operating system startup time.
Definition time_stamp.h:38
#define formal(parameter)
This macro just eats what it's passed; it marks unused formal parameters.
Definition definitions.h:48
#define GRAB_LOCK
The guards collection helps in testing preconditions and reporting errors.
Definition array.h:30
void WHACK(contents *&ptr)
deletion with clearing of the pointer.
Definition functions.h:121
bool text_form_applier(const octopus_entity &formal(key), entity_basket &bask, void *data_link)
const int ENTITY_HASH_BITS
A dynamic container class that holds any kind of object via pointers.
Definition amorph.h:55
#include <time.h>