diff options
Diffstat (limited to 'src/Network/BitTorrent/Core')
-rw-r--r-- | src/Network/BitTorrent/Core/Node.hs | 18 | ||||
-rw-r--r-- | src/Network/BitTorrent/Core/PeerAddr.hs | 18 |
2 files changed, 35 insertions, 1 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 | ||
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 | |||
50 | import Data.Typeable | 50 | import Data.Typeable |
51 | import Data.Word | 51 | import Data.Word |
52 | import Network.Socket | 52 | import Network.Socket |
53 | import Text.PrettyPrint hiding ((<>)) | 53 | import Text.PrettyPrint as PP hiding ((<>)) |
54 | import Text.PrettyPrint.Class | 54 | import Text.PrettyPrint.Class |
55 | import Text.Read (readMaybe) | 55 | import Text.Read (readMaybe) |
56 | import qualified Text.ParserCombinators.ReadP as RP | 56 | import 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 | ||
89 | instance 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 | ||
167 | instance Pretty IPv4 where | ||
168 | pretty = PP.text . show | ||
169 | {-# INLINE pretty #-} | ||
170 | |||
171 | instance Pretty IPv6 where | ||
172 | pretty = PP.text . show | ||
173 | {-# INLINE pretty #-} | ||
174 | |||
175 | instance Pretty IP where | ||
176 | pretty = PP.text . show | ||
177 | {-# INLINE pretty #-} | ||
178 | |||
163 | {----------------------------------------------------------------------- | 179 | {----------------------------------------------------------------------- |
164 | -- Peer addr | 180 | -- Peer addr |
165 | -----------------------------------------------------------------------} | 181 | -----------------------------------------------------------------------} |