summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/DHT/ContactInfo.hs
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-04-04 07:20:34 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-04-04 07:20:34 +0400
commit9b2981d38cfa188099cca07337a3b63747e2c527 (patch)
tree337e39b146ba86c9f2a0fd11e426da6f9119f2e2 /src/Network/BitTorrent/DHT/ContactInfo.hs
parentd8e61484166fa6666e4aaa9689cb430f44f8242b (diff)
Move PeerStore to ContactInfo module
Diffstat (limited to 'src/Network/BitTorrent/DHT/ContactInfo.hs')
-rw-r--r--src/Network/BitTorrent/DHT/ContactInfo.hs50
1 files changed, 48 insertions, 2 deletions
diff --git a/src/Network/BitTorrent/DHT/ContactInfo.hs b/src/Network/BitTorrent/DHT/ContactInfo.hs
index 028a4214..2aedf900 100644
--- a/src/Network/BitTorrent/DHT/ContactInfo.hs
+++ b/src/Network/BitTorrent/DHT/ContactInfo.hs
@@ -1,5 +1,19 @@
1module Network.BitTorrent.DHT.ContactInfo 1module Network.BitTorrent.DHT.ContactInfo
2 ( ) where 2 ( PeerStore
3 , Network.BitTorrent.DHT.ContactInfo.lookup
4 , Network.BitTorrent.DHT.ContactInfo.insert
5 ) where
6
7import Data.Default
8import Data.List as L
9import Data.Maybe
10import Data.Monoid
11import Data.HashMap.Strict as HM
12import Data.Serialize
13
14import Data.Torrent.InfoHash
15import Network.BitTorrent.Core.PeerAddr
16
3{- 17{-
4import Data.HashMap.Strict as HM 18import Data.HashMap.Strict as HM
5 19
@@ -90,4 +104,36 @@ prune pref targetSize (Tip _ _) = undefined
90-- | Remove expired entries. 104-- | Remove expired entries.
91splitGT :: Timestamp -> ContactInfo ip -> ContactInfo ip 105splitGT :: Timestamp -> ContactInfo ip -> ContactInfo ip
92splitGT = undefined 106splitGT = 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.
111newtype PeerStore ip = PeerStore (HashMap InfoHash [PeerAddr ip])
112
113-- | Empty store.
114instance Default (PeerStore a) where
115 def = PeerStore HM.empty
116 {-# INLINE def #-}
117
118-- | Monoid under union operation.
119instance 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.
129instance Serialize (PeerStore a) where
130 get = undefined
131 put = undefined
132
133-- | Used in 'get_peers' DHT queries.
134lookup :: InfoHash -> PeerStore a -> [PeerAddr a]
135lookup ih (PeerStore m) = fromMaybe [] $ HM.lookup ih m
136
137-- | Used in 'announce_peer' DHT queries.
138insert :: Eq a => InfoHash -> PeerAddr a -> PeerStore a -> PeerStore a
139insert ih a (PeerStore m) = PeerStore (HM.insertWith L.union ih [a] m)