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.hs | 8 +++++++- src/Network/BitTorrent/Core/Node.hs | 18 ++++++++++++++++++ src/Network/BitTorrent/Core/PeerAddr.hs | 18 +++++++++++++++++- 3 files changed, 42 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/Network/BitTorrent/Core.hs b/src/Network/BitTorrent/Core.hs index 6024f5a5..2ddee517 100644 --- a/src/Network/BitTorrent/Core.hs +++ b/src/Network/BitTorrent/Core.hs @@ -20,8 +20,11 @@ module Network.BitTorrent.Core import Control.Applicative import Data.IP import Data.Serialize +import Data.Time import Data.Typeable import Network.Socket (SockAddr (..), PortNumber) +import Text.PrettyPrint as PP hiding ((<>)) +import Text.PrettyPrint.Class import Network.BitTorrent.Core.Fingerprint as Core import Network.BitTorrent.Core.Node as Core @@ -29,7 +32,10 @@ import Network.BitTorrent.Core.PeerId as Core import Network.BitTorrent.Core.PeerAddr as Core -class (Eq a, Serialize a, Typeable a) => Address a where +instance Pretty UTCTime where + pretty = PP.text . show + +class (Eq a, Serialize a, Typeable a, Pretty a) => Address a where toSockAddr :: a -> SockAddr fromSockAddr :: SockAddr -> Maybe a 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