NodeCache.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2005-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #ifndef GNUTELLA__BOOTSTRAPPING__NODECACHE_H
00024 #define GNUTELLA__BOOTSTRAPPING__NODECACHE_H
00025 
00026 #include "Imports.h"
00027 
00028 // Open containing namespaces:
00029 namespace Gnutella {
00030 namespace Bootstrapping {
00032 
00033 typedef QSet <NodeAddress> NodeSet;
00034 class NodeCachePrivate;
00035 
00036 class NodeCache : public QObject
00037 {
00038     Q_OBJECT
00039     REFERENCE_OBJECT (NodeCache)
00040 
00041 public:
00043 
00056     enum NodeAvailability
00057     {
00058         UnknownAvailability,        
00059         CheckingAvailability,       
00060         NodeAvailable,              
00061         NodeBusy,                   
00062         NodeUnavailable,            
00063         AvailabilityCount           
00064     };
00065 
00066                 NodeCache();
00067                 ~NodeCache();
00068 
00069     bool        loadNodes (const QString &filename);
00070     bool        storeNodes (const QString &filename);
00071 
00072     void        addNode (const NodeInfo &, NodeAvailability);
00073     void        updateNode (const NodeAddress &, NodeAvailability);
00074     NodeSet     getNodes (int count, NodeAvailability, bool freshNodes);
00075 
00076     template <typename Pred>
00077     NodeSet     getNodes (int count, NodeAvailability availability, bool freshNodes, const Pred &pred);
00078 
00079 signals:
00080     void nodeAdded();
00081 
00082 private:
00083     class Predicate
00084     {
00085     public:
00086         virtual ~Predicate() {}
00087         virtual bool operator()(const NodeInfo &) const = 0;
00088     };
00089 
00090     NodeSet     getNodes (int count, NodeAvailability availability, bool freshNodes, const Predicate &pred);
00091     NodeCachePrivate    *d;
00092 };
00093 
00094 template <typename Pred>
00095 NodeSet NodeCache::getNodes (int count, NodeCache::NodeAvailability availability, bool freshNodes, const Pred &pred)
00096 {
00097     class PredicateImp : public Predicate
00098     {
00099     public:
00100         PredicateImp (const Pred &pred) : predicate (pred) {}
00101         bool operator()(const NodeInfo &hostInfo) const { return predicate(hostInfo); }
00102     private:
00103         const Pred &predicate;
00104     };
00105 
00106     PredicateImp predicateImp (pred);
00107 
00108     return getNodes (count, availability, freshNodes,
00109         static_cast <const Predicate &> (predicateImp));
00110 }
00111 
00112 // Close containing namespaces:
00113 } // namespace Bootstrapping
00114 } // namespace Gnutella
00116 
00117 #endif // GNUTELLA__BOOTSTRAPPING__NODECACHE_H