diff options
Diffstat (limited to 'src/Network/BitTorrent/DHT/ContactInfo.hs')
-rw-r--r-- | src/Network/BitTorrent/DHT/ContactInfo.hs | 52 |
1 files changed, 49 insertions, 3 deletions
diff --git a/src/Network/BitTorrent/DHT/ContactInfo.hs b/src/Network/BitTorrent/DHT/ContactInfo.hs index 028a4214..baa240b4 100644 --- a/src/Network/BitTorrent/DHT/ContactInfo.hs +++ b/src/Network/BitTorrent/DHT/ContactInfo.hs | |||
@@ -1,10 +1,24 @@ | |||
1 | module Network.BitTorrent.DHT.ContactInfo | 1 | module Network.BitTorrent.DHT.ContactInfo |
2 | ( ) where | 2 | ( PeerStore |
3 | , Network.BitTorrent.DHT.ContactInfo.lookup | ||
4 | , Network.BitTorrent.DHT.ContactInfo.insert | ||
5 | ) where | ||
6 | |||
7 | import Data.Default | ||
8 | import Data.List as L | ||
9 | import Data.Maybe | ||
10 | import Data.Monoid | ||
11 | import Data.HashMap.Strict as HM | ||
12 | import Data.Serialize | ||
13 | |||
14 | import Data.Torrent | ||
15 | import Network.BitTorrent.Address | ||
16 | |||
3 | {- | 17 | {- |
4 | import Data.HashMap.Strict as HM | 18 | import Data.HashMap.Strict as HM |
5 | 19 | ||
6 | import Data.Torrent.InfoHash | 20 | import Data.Torrent.InfoHash |
7 | import Network.BitTorrent.Core | 21 | import Network.BitTorrent.Address |
8 | 22 | ||
9 | -- increase prefix when table is too large | 23 | -- increase prefix when table is too large |
10 | -- decrease prefix when table is too small | 24 | -- decrease prefix when table is too small |
@@ -90,4 +104,36 @@ prune pref targetSize (Tip _ _) = undefined | |||
90 | -- | Remove expired entries. | 104 | -- | Remove expired entries. |
91 | splitGT :: Timestamp -> ContactInfo ip -> ContactInfo ip | 105 | splitGT :: Timestamp -> ContactInfo ip -> ContactInfo ip |
92 | splitGT = undefined | 106 | splitGT = undefined |
93 | -} \ No newline at end of file | 107 | -} |
108 | |||
109 | -- | Storage used to keep track a set of known peers in client, | ||
110 | -- tracker or DHT sessions. | ||
111 | newtype PeerStore ip = PeerStore (HashMap InfoHash [PeerAddr ip]) | ||
112 | |||
113 | -- | Empty store. | ||
114 | instance Default (PeerStore a) where | ||
115 | def = PeerStore HM.empty | ||
116 | {-# INLINE def #-} | ||
117 | |||
118 | -- | Monoid under union operation. | ||
119 | instance Eq a => Monoid (PeerStore a) where | ||
120 | mempty = def | ||
121 | {-# INLINE mempty #-} | ||
122 | |||
123 | mappend (PeerStore a) (PeerStore b) = | ||
124 | PeerStore (HM.unionWith L.union a b) | ||
125 | {-# INLINE mappend #-} | ||
126 | |||
127 | -- | Can be used to store peers between invocations of the client | ||
128 | -- software. | ||
129 | instance Serialize (PeerStore a) where | ||
130 | get = undefined | ||
131 | put = undefined | ||
132 | |||
133 | -- | Used in 'get_peers' DHT queries. | ||
134 | lookup :: InfoHash -> PeerStore a -> [PeerAddr a] | ||
135 | lookup ih (PeerStore m) = fromMaybe [] $ HM.lookup ih m | ||
136 | |||
137 | -- | Used in 'announce_peer' DHT queries. | ||
138 | insert :: Eq a => InfoHash -> PeerAddr a -> PeerStore a -> PeerStore a | ||
139 | insert ih a (PeerStore m) = PeerStore (HM.insertWith L.union ih [a] m) | ||