summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/Core/Node.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/BitTorrent/Core/Node.hs')
-rw-r--r--src/Network/BitTorrent/Core/Node.hs18
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)
37import Data.Aeson.TH 37import Data.Aeson.TH
38import Data.Bits 38import Data.Bits
39import Data.ByteString as BS 39import Data.ByteString as BS
40import Data.ByteString.Char8 as BC
41import Data.ByteString.Base16 as Base16
40import Data.BEncode as BE 42import Data.BEncode as BE
41import Data.Default 43import Data.Default
42import Data.Hashable 44import Data.Hashable
43import Data.IP 45import Data.IP
44import Data.List as L 46import Data.List as L
47import Data.Monoid
45import Data.Ord 48import Data.Ord
46import Data.Serialize as S 49import Data.Serialize as S
47import Data.String 50import Data.String
@@ -49,6 +52,8 @@ import Data.Typeable
49import Data.Word 52import Data.Word
50import Network 53import Network
51import System.Entropy 54import System.Entropy
55import Text.PrettyPrint as PP hiding ((<>))
56import Text.PrettyPrint.Class
52 57
53import Data.Torrent.JSON 58import Data.Torrent.JSON
54import Network.BitTorrent.Core.PeerAddr (PeerAddr (..)) 59import 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.
95instance 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.
90testIdBit :: NodeId -> Word -> Bool 99testIdBit :: NodeId -> Word -> Bool
91testIdBit (NodeId bs) i 100testIdBit (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
143instance 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
165instance Serialize a => Serialize (NodeInfo a) where 177instance 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
181instance Pretty ip => Pretty (NodeInfo ip) where
182 pretty NodeInfo {..} = pretty nodeId <> "@(" <> pretty nodeAddr <> ")"
183
184instance Pretty ip => Pretty [NodeInfo ip] where
185 pretty = PP.vcat . PP.punctuate "," . L.map pretty