summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Network/BitTorrent/Core/PeerAddr.hs48
1 files changed, 47 insertions, 1 deletions
diff --git a/src/Network/BitTorrent/Core/PeerAddr.hs b/src/Network/BitTorrent/Core/PeerAddr.hs
index e7441929..bc4a1078 100644
--- a/src/Network/BitTorrent/Core/PeerAddr.hs
+++ b/src/Network/BitTorrent/Core/PeerAddr.hs
@@ -22,6 +22,11 @@ module Network.BitTorrent.Core.PeerAddr
22 PeerAddr(..) 22 PeerAddr(..)
23 , defaultPorts 23 , defaultPorts
24 , peerSockAddr 24 , peerSockAddr
25
26 -- * Peer storage
27 , PeerStore
28 , Network.BitTorrent.Core.PeerAddr.lookup
29 , Network.BitTorrent.Core.PeerAddr.insert
25 ) where 30 ) where
26 31
27import Control.Applicative 32import Control.Applicative
@@ -32,19 +37,23 @@ import Data.BEncode.BDict (BKey)
32import Data.ByteString.Char8 as BS8 37import Data.ByteString.Char8 as BS8
33import Data.Char 38import Data.Char
34import Data.Default 39import Data.Default
40import Data.HashMap.Strict as HM
35import Data.IP 41import Data.IP
36import Data.List as L 42import Data.List as L
37import Data.List.Split 43import Data.List.Split
44import Data.Maybe
45import Data.Monoid
38import Data.Serialize as S 46import Data.Serialize as S
39import Data.String 47import Data.String
40import Data.Typeable 48import Data.Typeable
41import Data.Word 49import Data.Word
42import Network.Socket 50import Network.Socket
43import Text.PrettyPrint 51import Text.PrettyPrint hiding ((<>))
44import Text.PrettyPrint.Class 52import Text.PrettyPrint.Class
45import Text.Read (readMaybe) 53import Text.Read (readMaybe)
46import qualified Text.ParserCombinators.ReadP as RP 54import qualified Text.ParserCombinators.ReadP as RP
47 55
56import Data.Torrent.InfoHash
48import Network.BitTorrent.Core.PeerId 57import Network.BitTorrent.Core.PeerId
49 58
50 59
@@ -237,3 +246,40 @@ peerSockAddr PeerAddr {..} =
237 case peerHost of 246 case peerHost of
238 IPv4 ipv4 -> SockAddrInet peerPort (toHostAddress ipv4) 247 IPv4 ipv4 -> SockAddrInet peerPort (toHostAddress ipv4)
239 IPv6 ipv6 -> SockAddrInet6 peerPort 0 (toHostAddress6 ipv6) 0 248 IPv6 ipv6 -> SockAddrInet6 peerPort 0 (toHostAddress6 ipv6) 0
249
250{-----------------------------------------------------------------------
251-- Peer storage
252-----------------------------------------------------------------------}
253-- TODO use more memory efficient representation
254
255-- | Storage used to keep track a set of known peers in client,
256-- tracker or DHT sessions.
257newtype PeerStore a = PeerStore (HashMap InfoHash [PeerAddr a])
258
259-- | Empty store.
260instance Default (PeerStore a) where
261 def = PeerStore HM.empty
262 {-# INLINE def #-}
263
264-- | Monoid under union operation.
265instance Eq a => Monoid (PeerStore a) where
266 mempty = def
267 {-# INLINE mempty #-}
268
269 mappend (PeerStore a) (PeerStore b) =
270 PeerStore (HM.unionWith L.union a b)
271 {-# INLINE mappend #-}
272
273-- | Can be used to store peers between invocations of the client
274-- software.
275instance Serialize (PeerStore a) where
276 get = undefined
277 put = undefined
278
279-- | Used in 'get_peers' DHT queries.
280lookup :: InfoHash -> PeerStore a -> [PeerAddr a]
281lookup ih (PeerStore m) = fromMaybe [] $ HM.lookup ih m
282
283-- | Used in 'announce_peer' DHT queries.
284insert :: Eq a => InfoHash -> PeerAddr a -> PeerStore a -> PeerStore a
285insert ih a (PeerStore m) = PeerStore (HM.insertWith L.union ih [a] m)