summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/DHT/ContactInfo.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/BitTorrent/DHT/ContactInfo.hs')
-rw-r--r--src/Network/BitTorrent/DHT/ContactInfo.hs52
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 @@
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
15import Network.BitTorrent.Address
16
3{- 17{-
4import Data.HashMap.Strict as HM 18import Data.HashMap.Strict as HM
5 19
6import Data.Torrent.InfoHash 20import Data.Torrent.InfoHash
7import Network.BitTorrent.Core 21import 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.
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)