summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-12-28 12:19:03 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-12-28 12:19:03 +0400
commit143f73d83852aa1c676b9bc09d1b624e526ba294 (patch)
treee322795b1f86f367ef7ee96013624939530a4e0c
parentdcf0f3aef288b5a35c2ff03e98e0b38ce1ac6213 (diff)
Add pretty instances for NodeInfos
-rw-r--r--src/Network/BitTorrent/Core.hs8
-rw-r--r--src/Network/BitTorrent/Core/Node.hs18
-rw-r--r--src/Network/BitTorrent/Core/PeerAddr.hs18
3 files changed, 42 insertions, 2 deletions
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
20import Control.Applicative 20import Control.Applicative
21import Data.IP 21import Data.IP
22import Data.Serialize 22import Data.Serialize
23import Data.Time
23import Data.Typeable 24import Data.Typeable
24import Network.Socket (SockAddr (..), PortNumber) 25import Network.Socket (SockAddr (..), PortNumber)
26import Text.PrettyPrint as PP hiding ((<>))
27import Text.PrettyPrint.Class
25 28
26import Network.BitTorrent.Core.Fingerprint as Core 29import Network.BitTorrent.Core.Fingerprint as Core
27import Network.BitTorrent.Core.Node as Core 30import Network.BitTorrent.Core.Node as Core
@@ -29,7 +32,10 @@ import Network.BitTorrent.Core.PeerId as Core
29import Network.BitTorrent.Core.PeerAddr as Core 32import Network.BitTorrent.Core.PeerAddr as Core
30 33
31 34
32class (Eq a, Serialize a, Typeable a) => Address a where 35instance Pretty UTCTime where
36 pretty = PP.text . show
37
38class (Eq a, Serialize a, Typeable a, Pretty a) => Address a where
33 toSockAddr :: a -> SockAddr 39 toSockAddr :: a -> SockAddr
34 fromSockAddr :: SockAddr -> Maybe a 40 fromSockAddr :: SockAddr -> Maybe a
35 41
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
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
50import Data.Typeable 50import Data.Typeable
51import Data.Word 51import Data.Word
52import Network.Socket 52import Network.Socket
53import Text.PrettyPrint hiding ((<>)) 53import Text.PrettyPrint as PP hiding ((<>))
54import Text.PrettyPrint.Class 54import Text.PrettyPrint.Class
55import Text.Read (readMaybe) 55import Text.Read (readMaybe)
56import qualified Text.ParserCombinators.ReadP as RP 56import qualified Text.ParserCombinators.ReadP as RP
@@ -86,6 +86,10 @@ instance Hashable PortNumber where
86 hashWithSalt s = hashWithSalt s . fromEnum 86 hashWithSalt s = hashWithSalt s . fromEnum
87 {-# INLINE hashWithSalt #-} 87 {-# INLINE hashWithSalt #-}
88 88
89instance Pretty PortNumber where
90 pretty = PP.int . fromEnum
91 {-# INLINE pretty #-}
92
89{----------------------------------------------------------------------- 93{-----------------------------------------------------------------------
90-- IP addr 94-- IP addr
91-----------------------------------------------------------------------} 95-----------------------------------------------------------------------}
@@ -160,6 +164,18 @@ instance Serialize IPv6 where
160 put ip = put $ toHostAddress6 ip 164 put ip = put $ toHostAddress6 ip
161 get = fromHostAddress6 <$> get 165 get = fromHostAddress6 <$> get
162 166
167instance Pretty IPv4 where
168 pretty = PP.text . show
169 {-# INLINE pretty #-}
170
171instance Pretty IPv6 where
172 pretty = PP.text . show
173 {-# INLINE pretty #-}
174
175instance Pretty IP where
176 pretty = PP.text . show
177 {-# INLINE pretty #-}
178
163{----------------------------------------------------------------------- 179{-----------------------------------------------------------------------
164-- Peer addr 180-- Peer addr
165-----------------------------------------------------------------------} 181-----------------------------------------------------------------------}