diff options
-rw-r--r-- | src/Network/BitTorrent/Core/PeerAddr.hs | 48 |
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 | ||
27 | import Control.Applicative | 32 | import Control.Applicative |
@@ -32,19 +37,23 @@ import Data.BEncode.BDict (BKey) | |||
32 | import Data.ByteString.Char8 as BS8 | 37 | import Data.ByteString.Char8 as BS8 |
33 | import Data.Char | 38 | import Data.Char |
34 | import Data.Default | 39 | import Data.Default |
40 | import Data.HashMap.Strict as HM | ||
35 | import Data.IP | 41 | import Data.IP |
36 | import Data.List as L | 42 | import Data.List as L |
37 | import Data.List.Split | 43 | import Data.List.Split |
44 | import Data.Maybe | ||
45 | import Data.Monoid | ||
38 | import Data.Serialize as S | 46 | import Data.Serialize as S |
39 | import Data.String | 47 | import Data.String |
40 | import Data.Typeable | 48 | import Data.Typeable |
41 | import Data.Word | 49 | import Data.Word |
42 | import Network.Socket | 50 | import Network.Socket |
43 | import Text.PrettyPrint | 51 | import Text.PrettyPrint hiding ((<>)) |
44 | import Text.PrettyPrint.Class | 52 | import Text.PrettyPrint.Class |
45 | import Text.Read (readMaybe) | 53 | import Text.Read (readMaybe) |
46 | import qualified Text.ParserCombinators.ReadP as RP | 54 | import qualified Text.ParserCombinators.ReadP as RP |
47 | 55 | ||
56 | import Data.Torrent.InfoHash | ||
48 | import Network.BitTorrent.Core.PeerId | 57 | import 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. | ||
257 | newtype PeerStore a = PeerStore (HashMap InfoHash [PeerAddr a]) | ||
258 | |||
259 | -- | Empty store. | ||
260 | instance Default (PeerStore a) where | ||
261 | def = PeerStore HM.empty | ||
262 | {-# INLINE def #-} | ||
263 | |||
264 | -- | Monoid under union operation. | ||
265 | instance 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. | ||
275 | instance Serialize (PeerStore a) where | ||
276 | get = undefined | ||
277 | put = undefined | ||
278 | |||
279 | -- | Used in 'get_peers' DHT queries. | ||
280 | lookup :: InfoHash -> PeerStore a -> [PeerAddr a] | ||
281 | lookup ih (PeerStore m) = fromMaybe [] $ HM.lookup ih m | ||
282 | |||
283 | -- | Used in 'announce_peer' DHT queries. | ||
284 | insert :: Eq a => InfoHash -> PeerAddr a -> PeerStore a -> PeerStore a | ||
285 | insert ih a (PeerStore m) = PeerStore (HM.insertWith L.union ih [a] m) | ||