NodeCache.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef GNUTELLA__BOOTSTRAPPING__NODECACHE_H
00024 #define GNUTELLA__BOOTSTRAPPING__NODECACHE_H
00025
00026 #include "Imports.h"
00027
00028
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
00113 }
00114 }
00116
00117 #endif // GNUTELLA__BOOTSTRAPPING__NODECACHE_H