diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-29 08:18:36 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-29 08:18:36 +0400 |
commit | 2c30e11e4ed379afb7b3fc5c35de9913085f090e (patch) | |
tree | 2b4c4774050fab860a2f6c65efb5288f621ed751 | |
parent | 1e1ab84ec903aac67b5d1f337cfb290975fc77e1 (diff) |
Add hashable instances for all address types
-rw-r--r-- | src/Network/BitTorrent/Core.hs | 10 | ||||
-rw-r--r-- | src/Network/BitTorrent/Core/PeerAddr.hs | 15 |
2 files changed, 24 insertions, 1 deletions
diff --git a/src/Network/BitTorrent/Core.hs b/src/Network/BitTorrent/Core.hs index 2ddee517..5717e4de 100644 --- a/src/Network/BitTorrent/Core.hs +++ b/src/Network/BitTorrent/Core.hs | |||
@@ -9,7 +9,10 @@ | |||
9 | -- | 9 | -- |
10 | module Network.BitTorrent.Core | 10 | module Network.BitTorrent.Core |
11 | ( module Core | 11 | ( module Core |
12 | |||
13 | -- * Address class | ||
12 | , Address (..) | 14 | , Address (..) |
15 | , fromAddr | ||
13 | 16 | ||
14 | -- * Re-exports from Data.IP | 17 | -- * Re-exports from Data.IP |
15 | , IPv4 | 18 | , IPv4 |
@@ -19,6 +22,7 @@ module Network.BitTorrent.Core | |||
19 | 22 | ||
20 | import Control.Applicative | 23 | import Control.Applicative |
21 | import Data.IP | 24 | import Data.IP |
25 | import Data.Hashable | ||
22 | import Data.Serialize | 26 | import Data.Serialize |
23 | import Data.Time | 27 | import Data.Time |
24 | import Data.Typeable | 28 | import Data.Typeable |
@@ -35,10 +39,14 @@ import Network.BitTorrent.Core.PeerAddr as Core | |||
35 | instance Pretty UTCTime where | 39 | instance Pretty UTCTime where |
36 | pretty = PP.text . show | 40 | pretty = PP.text . show |
37 | 41 | ||
38 | class (Eq a, Serialize a, Typeable a, Pretty a) => Address a where | 42 | class (Eq a, Serialize a, Typeable a, Hashable a, Pretty a) |
43 | => Address a where | ||
39 | toSockAddr :: a -> SockAddr | 44 | toSockAddr :: a -> SockAddr |
40 | fromSockAddr :: SockAddr -> Maybe a | 45 | fromSockAddr :: SockAddr -> Maybe a |
41 | 46 | ||
47 | fromAddr :: (Address a, Address b) => a -> Maybe b | ||
48 | fromAddr = fromSockAddr . toSockAddr | ||
49 | |||
42 | -- | Note that port is zeroed. | 50 | -- | Note that port is zeroed. |
43 | instance Address IPv4 where | 51 | instance Address IPv4 where |
44 | toSockAddr = SockAddrInet 0 . toHostAddress | 52 | toSockAddr = SockAddrInet 0 . toHostAddress |
diff --git a/src/Network/BitTorrent/Core/PeerAddr.hs b/src/Network/BitTorrent/Core/PeerAddr.hs index 4905f910..d634716c 100644 --- a/src/Network/BitTorrent/Core/PeerAddr.hs +++ b/src/Network/BitTorrent/Core/PeerAddr.hs | |||
@@ -176,6 +176,17 @@ instance Pretty IP where | |||
176 | pretty = PP.text . show | 176 | pretty = PP.text . show |
177 | {-# INLINE pretty #-} | 177 | {-# INLINE pretty #-} |
178 | 178 | ||
179 | instance Hashable IPv4 where | ||
180 | hashWithSalt = hashUsing toHostAddress | ||
181 | {-# INLINE hashWithSalt #-} | ||
182 | |||
183 | instance Hashable IPv6 where | ||
184 | hashWithSalt s a = hashWithSalt s (toHostAddress6 a) | ||
185 | |||
186 | instance Hashable IP where | ||
187 | hashWithSalt s (IPv4 h) = hashWithSalt s h | ||
188 | hashWithSalt s (IPv6 h) = hashWithSalt s h | ||
189 | |||
179 | {----------------------------------------------------------------------- | 190 | {----------------------------------------------------------------------- |
180 | -- Peer addr | 191 | -- Peer addr |
181 | -----------------------------------------------------------------------} | 192 | -----------------------------------------------------------------------} |
@@ -267,6 +278,10 @@ instance Pretty a => Pretty (PeerAddr a) where | |||
267 | where | 278 | where |
268 | paddr = pretty peerHost <> ":" <> text (show peerPort) | 279 | paddr = pretty peerHost <> ":" <> text (show peerPort) |
269 | 280 | ||
281 | instance Hashable a => Hashable (PeerAddr a) where | ||
282 | hashWithSalt s PeerAddr {..} = | ||
283 | s `hashWithSalt` peerId `hashWithSalt` peerHost `hashWithSalt` peerPort | ||
284 | |||
270 | -- | Ports typically reserved for bittorrent P2P listener. | 285 | -- | Ports typically reserved for bittorrent P2P listener. |
271 | defaultPorts :: [PortNumber] | 286 | defaultPorts :: [PortNumber] |
272 | defaultPorts = [6881..6889] | 287 | defaultPorts = [6881..6889] |