name_cache.cpp

Go to the documentation of this file.
00001 #ifndef NAME_CACHE_IMPLEMENTATION_FILE
00002 #define NAME_CACHE_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : name_cache                                                        *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1992-$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 "dns_entry.h"
00019 #include "name_cache.h"
00020 
00021 #include <basis/function.h>
00022 #include <basis/log_base.h>
00023 #include <basis/mutex.h>
00024 #include <data_struct/symbol_table.cpp>
00025 #include <mechanisms/ithread.h>
00026 
00027 const int RESOLUTION_INTERVAL = 500;
00028   // the number of milliseconds between attempts to resolve the names.
00029 
00030 class name_cache_resolver : public ithread
00031 {
00032 public:
00033   name_cache_resolver(name_cache &parent)
00034   : ithread(RESOLUTION_INTERVAL), _parent(parent) {}
00035 
00036   void perform_activity(void *formal(data)) {
00037 //hmmm: resolve names that are not yet.
00038   }
00039 
00040 private:
00041   name_cache &_parent;
00042 };
00043 
00045 
00046 class dns_entry_table : public symbol_table<dns_entry> {};
00047 
00049 
00050 name_cache::name_cache(int max_entries)
00051 : _max_cache(max_entries),
00052   _lock(new mutex),
00053   _name_list(new dns_entry_table)
00054 {}
00055 
00056 name_cache::~name_cache()
00057 {
00058   WHACK(_name_list);
00059   WHACK(_lock);
00060 }
00061 
00062 void name_cache::add(const istring &name, const dns_entry &to_add)
00063 {
00064   auto_synchronizer l(*_lock);
00065   dns_entry *found = _name_list->find(name);
00066   if (!found) {
00067     // add a new entry.
00068     if (_name_list->symbols() >= _max_cache) {
00069       _name_list->whack(_name_list->name(0));
00070 //this is bad; make it zap least recently used.
00071     }
00072     _name_list->add(name, to_add);
00073     return;
00074   }
00076   // replace the existing info if the new info claims to be resolved.
00077   if (to_add._resolved) *found = to_add;
00078 }
00079 
00080 bool name_cache::remove(const istring &to_whack)
00081 {
00082   auto_synchronizer l(*_lock);
00083   return _name_list->whack(to_whack) == common::OKAY;
00084 }
00085 
00086 dns_entry name_cache::lookup(const istring &name) const
00087 {
00088   auto_synchronizer l(*_lock);
00089   dns_entry *found = _name_list->find(name);
00090   if (!found) return dns_entry();  // doesn't exist.
00091   return *found;
00092 }
00093 
00094 void name_cache::show_names(istring &to_fill)
00095 {
00096   auto_synchronizer l(*_lock);
00097   for (int i = 0; i < _name_list->symbols(); i++) {
00098     dns_entry &found = (*_name_list)[i];
00099     to_fill += found.text_form() + log_base::platform_ending();
00100   }
00101 }
00102 
00103 
00104 #endif //NAME_CACHE_IMPLEMENTATION_FILE
00105 

Generated on Fri Nov 28 04:28:55 2008 for HOOPLE Libraries by  doxygen 1.5.1