diff options
Diffstat (limited to 'src/Network/BitTorrent/Core/Node.hs')
-rw-r--r-- | src/Network/BitTorrent/Core/Node.hs | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/Network/BitTorrent/Core/Node.hs b/src/Network/BitTorrent/Core/Node.hs index 9501d59e..090bfd4e 100644 --- a/src/Network/BitTorrent/Core/Node.hs +++ b/src/Network/BitTorrent/Core/Node.hs | |||
@@ -37,11 +37,14 @@ import Data.Aeson (ToJSON, FromJSON) | |||
37 | import Data.Aeson.TH | 37 | import Data.Aeson.TH |
38 | import Data.Bits | 38 | import Data.Bits |
39 | import Data.ByteString as BS | 39 | import Data.ByteString as BS |
40 | import Data.ByteString.Char8 as BC | ||
41 | import Data.ByteString.Base16 as Base16 | ||
40 | import Data.BEncode as BE | 42 | import Data.BEncode as BE |
41 | import Data.Default | 43 | import Data.Default |
42 | import Data.Hashable | 44 | import Data.Hashable |
43 | import Data.IP | 45 | import Data.IP |
44 | import Data.List as L | 46 | import Data.List as L |
47 | import Data.Monoid | ||
45 | import Data.Ord | 48 | import Data.Ord |
46 | import Data.Serialize as S | 49 | import Data.Serialize as S |
47 | import Data.String | 50 | import Data.String |
@@ -49,6 +52,8 @@ import Data.Typeable | |||
49 | import Data.Word | 52 | import Data.Word |
50 | import Network | 53 | import Network |
51 | import System.Entropy | 54 | import System.Entropy |
55 | import Text.PrettyPrint as PP hiding ((<>)) | ||
56 | import Text.PrettyPrint.Class | ||
52 | 57 | ||
53 | import Data.Torrent.JSON | 58 | import Data.Torrent.JSON |
54 | import Network.BitTorrent.Core.PeerAddr (PeerAddr (..)) | 59 | import Network.BitTorrent.Core.PeerAddr (PeerAddr (..)) |
@@ -86,6 +91,10 @@ instance IsString NodeId where | |||
86 | | otherwise = error "fromString: invalid NodeId length" | 91 | | otherwise = error "fromString: invalid NodeId length" |
87 | {-# INLINE fromString #-} | 92 | {-# INLINE fromString #-} |
88 | 93 | ||
94 | -- | base16 encoded. | ||
95 | instance Pretty NodeId where | ||
96 | pretty (NodeId nid) = PP.text $ BC.unpack $ Base16.encode nid | ||
97 | |||
89 | -- | Test if the nth bit is set. | 98 | -- | Test if the nth bit is set. |
90 | testIdBit :: NodeId -> Word -> Bool | 99 | testIdBit :: NodeId -> Word -> Bool |
91 | testIdBit (NodeId bs) i | 100 | testIdBit (NodeId bs) i |
@@ -131,6 +140,9 @@ instance Hashable a => Hashable (NodeAddr a) where | |||
131 | hashWithSalt s NodeAddr {..} = hashWithSalt s (nodeHost, nodePort) | 140 | hashWithSalt s NodeAddr {..} = hashWithSalt s (nodeHost, nodePort) |
132 | {-# INLINE hashWithSalt #-} | 141 | {-# INLINE hashWithSalt #-} |
133 | 142 | ||
143 | instance Pretty ip => Pretty (NodeAddr ip) where | ||
144 | pretty NodeAddr {..} = pretty nodeHost <> ":" <> pretty nodePort | ||
145 | |||
134 | -- | Example: | 146 | -- | Example: |
135 | -- | 147 | -- |
136 | -- @nodePort \"127.0.0.1:6881\" == 6881@ | 148 | -- @nodePort \"127.0.0.1:6881\" == 6881@ |
@@ -165,3 +177,9 @@ instance Eq a => Ord (NodeInfo a) where | |||
165 | instance Serialize a => Serialize (NodeInfo a) where | 177 | instance Serialize a => Serialize (NodeInfo a) where |
166 | get = NodeInfo <$> get <*> get | 178 | get = NodeInfo <$> get <*> get |
167 | put NodeInfo {..} = put nodeId >> put nodeAddr | 179 | put NodeInfo {..} = put nodeId >> put nodeAddr |
180 | |||
181 | instance Pretty ip => Pretty (NodeInfo ip) where | ||
182 | pretty NodeInfo {..} = pretty nodeId <> "@(" <> pretty nodeAddr <> ")" | ||
183 | |||
184 | instance Pretty ip => Pretty [NodeInfo ip] where | ||
185 | pretty = PP.vcat . PP.punctuate "," . L.map pretty | ||