summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Network/BitTorrent/Core/PeerAddr.hs44
-rw-r--r--src/Network/BitTorrent/DHT/ContactInfo.hs50
-rw-r--r--src/Network/BitTorrent/DHT/Session.hs3
3 files changed, 51 insertions, 46 deletions
diff --git a/src/Network/BitTorrent/Core/PeerAddr.hs b/src/Network/BitTorrent/Core/PeerAddr.hs
index 92fb83a7..e9ad7c96 100644
--- a/src/Network/BitTorrent/Core/PeerAddr.hs
+++ b/src/Network/BitTorrent/Core/PeerAddr.hs
@@ -26,9 +26,6 @@ module Network.BitTorrent.Core.PeerAddr
26 , peerSocket 26 , peerSocket
27 27
28 -- * Peer storage 28 -- * Peer storage
29 , PeerStore
30 , Network.BitTorrent.Core.PeerAddr.lookup
31 , Network.BitTorrent.Core.PeerAddr.insert
32 ) where 29 ) where
33 30
34import Control.Applicative 31import Control.Applicative
@@ -39,11 +36,9 @@ import Data.ByteString.Char8 as BS8
39import Data.Char 36import Data.Char
40import Data.Default 37import Data.Default
41import Data.Hashable 38import Data.Hashable
42import Data.HashMap.Strict as HM
43import Data.IP 39import Data.IP
44import Data.List as L 40import Data.List as L
45import Data.List.Split 41import Data.List.Split
46import Data.Maybe
47import Data.Monoid 42import Data.Monoid
48import Data.Serialize as S 43import Data.Serialize as S
49import Data.String 44import Data.String
@@ -55,7 +50,7 @@ import Text.PrettyPrint.Class
55import Text.Read (readMaybe) 50import Text.Read (readMaybe)
56import qualified Text.ParserCombinators.ReadP as RP 51import qualified Text.ParserCombinators.ReadP as RP
57 52
58import Data.Torrent.InfoHash 53--import Data.Torrent
59import Network.BitTorrent.Core.PeerId 54import Network.BitTorrent.Core.PeerId
60 55
61 56
@@ -315,40 +310,3 @@ peerSocket socketType pa = do
315 sock <- socket family socketType defaultProtocol 310 sock <- socket family socketType defaultProtocol
316 connect sock addr 311 connect sock addr
317 return sock 312 return sock
318
319{-----------------------------------------------------------------------
320-- Peer storage
321-----------------------------------------------------------------------}
322-- TODO use more memory efficient representation
323
324-- | Storage used to keep track a set of known peers in client,
325-- tracker or DHT sessions.
326newtype PeerStore ip = PeerStore (HashMap InfoHash [PeerAddr ip])
327
328-- | Empty store.
329instance Default (PeerStore a) where
330 def = PeerStore HM.empty
331 {-# INLINE def #-}
332
333-- | Monoid under union operation.
334instance Eq a => Monoid (PeerStore a) where
335 mempty = def
336 {-# INLINE mempty #-}
337
338 mappend (PeerStore a) (PeerStore b) =
339 PeerStore (HM.unionWith L.union a b)
340 {-# INLINE mappend #-}
341
342-- | Can be used to store peers between invocations of the client
343-- software.
344instance Serialize (PeerStore a) where
345 get = undefined
346 put = undefined
347
348-- | Used in 'get_peers' DHT queries.
349lookup :: InfoHash -> PeerStore a -> [PeerAddr a]
350lookup ih (PeerStore m) = fromMaybe [] $ HM.lookup ih m
351
352-- | Used in 'announce_peer' DHT queries.
353insert :: Eq a => InfoHash -> PeerAddr a -> PeerStore a -> PeerStore a
354insert ih a (PeerStore m) = PeerStore (HM.insertWith L.union ih [a] m)
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)
diff --git a/src/Network/BitTorrent/DHT/Session.hs b/src/Network/BitTorrent/DHT/Session.hs
index e770b1d3..7df25739 100644
--- a/src/Network/BitTorrent/DHT/Session.hs
+++ b/src/Network/BitTorrent/DHT/Session.hs
@@ -95,7 +95,8 @@ import Data.Torrent.InfoHash
95import Network.KRPC hiding (Options, def) 95import Network.KRPC hiding (Options, def)
96import qualified Network.KRPC as KRPC (def) 96import qualified Network.KRPC as KRPC (def)
97import Network.BitTorrent.Core 97import Network.BitTorrent.Core
98import Network.BitTorrent.Core.PeerAddr as P 98import Network.BitTorrent.Core.PeerAddr
99import Network.BitTorrent.DHT.ContactInfo as P
99import Network.BitTorrent.DHT.Message 100import Network.BitTorrent.DHT.Message
100import Network.BitTorrent.DHT.Routing as R 101import Network.BitTorrent.DHT.Routing as R
101import Network.BitTorrent.DHT.Token as T 102import Network.BitTorrent.DHT.Token as T