diff options
Diffstat (limited to 'src/Network/BitTorrent')
-rw-r--r-- | src/Network/BitTorrent/Core/PeerAddr.hs | 44 | ||||
-rw-r--r-- | src/Network/BitTorrent/DHT/ContactInfo.hs | 50 | ||||
-rw-r--r-- | src/Network/BitTorrent/DHT/Session.hs | 3 |
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 | ||
34 | import Control.Applicative | 31 | import Control.Applicative |
@@ -39,11 +36,9 @@ import Data.ByteString.Char8 as BS8 | |||
39 | import Data.Char | 36 | import Data.Char |
40 | import Data.Default | 37 | import Data.Default |
41 | import Data.Hashable | 38 | import Data.Hashable |
42 | import Data.HashMap.Strict as HM | ||
43 | import Data.IP | 39 | import Data.IP |
44 | import Data.List as L | 40 | import Data.List as L |
45 | import Data.List.Split | 41 | import Data.List.Split |
46 | import Data.Maybe | ||
47 | import Data.Monoid | 42 | import Data.Monoid |
48 | import Data.Serialize as S | 43 | import Data.Serialize as S |
49 | import Data.String | 44 | import Data.String |
@@ -55,7 +50,7 @@ import Text.PrettyPrint.Class | |||
55 | import Text.Read (readMaybe) | 50 | import Text.Read (readMaybe) |
56 | import qualified Text.ParserCombinators.ReadP as RP | 51 | import qualified Text.ParserCombinators.ReadP as RP |
57 | 52 | ||
58 | import Data.Torrent.InfoHash | 53 | --import Data.Torrent |
59 | import Network.BitTorrent.Core.PeerId | 54 | import 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. | ||
326 | newtype PeerStore ip = PeerStore (HashMap InfoHash [PeerAddr ip]) | ||
327 | |||
328 | -- | Empty store. | ||
329 | instance Default (PeerStore a) where | ||
330 | def = PeerStore HM.empty | ||
331 | {-# INLINE def #-} | ||
332 | |||
333 | -- | Monoid under union operation. | ||
334 | instance 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. | ||
344 | instance Serialize (PeerStore a) where | ||
345 | get = undefined | ||
346 | put = undefined | ||
347 | |||
348 | -- | Used in 'get_peers' DHT queries. | ||
349 | lookup :: InfoHash -> PeerStore a -> [PeerAddr a] | ||
350 | lookup ih (PeerStore m) = fromMaybe [] $ HM.lookup ih m | ||
351 | |||
352 | -- | Used in 'announce_peer' DHT queries. | ||
353 | insert :: Eq a => InfoHash -> PeerAddr a -> PeerStore a -> PeerStore a | ||
354 | insert 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 @@ | |||
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.InfoHash | ||
15 | import Network.BitTorrent.Core.PeerAddr | ||
16 | |||
3 | {- | 17 | {- |
4 | import Data.HashMap.Strict as HM | 18 | import Data.HashMap.Strict as HM |
5 | 19 | ||
@@ -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) | ||
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 | |||
95 | import Network.KRPC hiding (Options, def) | 95 | import Network.KRPC hiding (Options, def) |
96 | import qualified Network.KRPC as KRPC (def) | 96 | import qualified Network.KRPC as KRPC (def) |
97 | import Network.BitTorrent.Core | 97 | import Network.BitTorrent.Core |
98 | import Network.BitTorrent.Core.PeerAddr as P | 98 | import Network.BitTorrent.Core.PeerAddr |
99 | import Network.BitTorrent.DHT.ContactInfo as P | ||
99 | import Network.BitTorrent.DHT.Message | 100 | import Network.BitTorrent.DHT.Message |
100 | import Network.BitTorrent.DHT.Routing as R | 101 | import Network.BitTorrent.DHT.Routing as R |
101 | import Network.BitTorrent.DHT.Token as T | 102 | import Network.BitTorrent.DHT.Token as T |