From 143f73d83852aa1c676b9bc09d1b624e526ba294 Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Sat, 28 Dec 2013 12:19:03 +0400 Subject: Add pretty instances for NodeInfos --- src/Network/BitTorrent/Core/Node.hs | 18 ++++++++++++++++++ src/Network/BitTorrent/Core/PeerAddr.hs | 18 +++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) (limited to 'src/Network/BitTorrent/Core') 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) import Data.Aeson.TH import Data.Bits import Data.ByteString as BS +import Data.ByteString.Char8 as BC +import Data.ByteString.Base16 as Base16 import Data.BEncode as BE import Data.Default import Data.Hashable import Data.IP import Data.List as L +import Data.Monoid import Data.Ord import Data.Serialize as S import Data.String @@ -49,6 +52,8 @@ import Data.Typeable import Data.Word import Network import System.Entropy +import Text.PrettyPrint as PP hiding ((<>)) +import Text.PrettyPrint.Class import Data.Torrent.JSON import Network.BitTorrent.Core.PeerAddr (PeerAddr (..)) @@ -86,6 +91,10 @@ instance IsString NodeId where | otherwise = error "fromString: invalid NodeId length" {-# INLINE fromString #-} +-- | base16 encoded. +instance Pretty NodeId where + pretty (NodeId nid) = PP.text $ BC.unpack $ Base16.encode nid + -- | Test if the nth bit is set. testIdBit :: NodeId -> Word -> Bool testIdBit (NodeId bs) i @@ -131,6 +140,9 @@ instance Hashable a => Hashable (NodeAddr a) where hashWithSalt s NodeAddr {..} = hashWithSalt s (nodeHost, nodePort) {-# INLINE hashWithSalt #-} +instance Pretty ip => Pretty (NodeAddr ip) where + pretty NodeAddr {..} = pretty nodeHost <> ":" <> pretty nodePort + -- | Example: -- -- @nodePort \"127.0.0.1:6881\" == 6881@ @@ -165,3 +177,9 @@ instance Eq a => Ord (NodeInfo a) where instance Serialize a => Serialize (NodeInfo a) where get = NodeInfo <$> get <*> get put NodeInfo {..} = put nodeId >> put nodeAddr + +instance Pretty ip => Pretty (NodeInfo ip) where + pretty NodeInfo {..} = pretty nodeId <> "@(" <> pretty nodeAddr <> ")" + +instance Pretty ip => Pretty [NodeInfo ip] where + pretty = PP.vcat . PP.punctuate "," . L.map pretty diff --git a/src/Network/BitTorrent/Core/PeerAddr.hs b/src/Network/BitTorrent/Core/PeerAddr.hs index 9f5c7c5d..4905f910 100644 --- a/src/Network/BitTorrent/Core/PeerAddr.hs +++ b/src/Network/BitTorrent/Core/PeerAddr.hs @@ -50,7 +50,7 @@ import Data.String import Data.Typeable import Data.Word import Network.Socket -import Text.PrettyPrint hiding ((<>)) +import Text.PrettyPrint as PP hiding ((<>)) import Text.PrettyPrint.Class import Text.Read (readMaybe) import qualified Text.ParserCombinators.ReadP as RP @@ -86,6 +86,10 @@ instance Hashable PortNumber where hashWithSalt s = hashWithSalt s . fromEnum {-# INLINE hashWithSalt #-} +instance Pretty PortNumber where + pretty = PP.int . fromEnum + {-# INLINE pretty #-} + {----------------------------------------------------------------------- -- IP addr -----------------------------------------------------------------------} @@ -160,6 +164,18 @@ instance Serialize IPv6 where put ip = put $ toHostAddress6 ip get = fromHostAddress6 <$> get +instance Pretty IPv4 where + pretty = PP.text . show + {-# INLINE pretty #-} + +instance Pretty IPv6 where + pretty = PP.text . show + {-# INLINE pretty #-} + +instance Pretty IP where + pretty = PP.text . show + {-# INLINE pretty #-} + {----------------------------------------------------------------------- -- Peer addr -----------------------------------------------------------------------} -- cgit v1.2.3