diff options
Diffstat (limited to 'src/Network')
-rw-r--r-- | src/Network/BitTorrent/Core/PeerAddr.hs | 17 | ||||
-rw-r--r-- | src/Network/BitTorrent/Core/PeerId.hs | 7 |
2 files changed, 11 insertions, 13 deletions
diff --git a/src/Network/BitTorrent/Core/PeerAddr.hs b/src/Network/BitTorrent/Core/PeerAddr.hs index 5570bfc1..b145c0d9 100644 --- a/src/Network/BitTorrent/Core/PeerAddr.hs +++ b/src/Network/BitTorrent/Core/PeerAddr.hs | |||
@@ -19,7 +19,6 @@ module Network.BitTorrent.Core.PeerAddr | |||
19 | , defaultPorts | 19 | , defaultPorts |
20 | , peerSockAddr | 20 | , peerSockAddr |
21 | , connectToPeer | 21 | , connectToPeer |
22 | , ppPeer | ||
23 | ) where | 22 | ) where |
24 | 23 | ||
25 | import Control.Applicative | 24 | import Control.Applicative |
@@ -34,6 +33,7 @@ import Data.Typeable | |||
34 | import Data.Word | 33 | import Data.Word |
35 | import Network.Socket | 34 | import Network.Socket |
36 | import Text.PrettyPrint | 35 | import Text.PrettyPrint |
36 | import Text.PrettyPrint.Class | ||
37 | 37 | ||
38 | import Data.Torrent.Client | 38 | import Data.Torrent.Client |
39 | import Network.BitTorrent.Core.PeerId | 39 | import Network.BitTorrent.Core.PeerId |
@@ -89,6 +89,13 @@ instance Serialize PeerAddr where | |||
89 | get = PeerAddr Nothing <$> get <*> get | 89 | get = PeerAddr Nothing <$> get <*> get |
90 | {-# INLINE get #-} | 90 | {-# INLINE get #-} |
91 | 91 | ||
92 | instance Pretty PeerAddr where | ||
93 | pretty p @ PeerAddr {..} | ||
94 | | Just pid <- peerID = pretty (clientInfo pid) <+> "at" <+> paddr | ||
95 | | otherwise = paddr | ||
96 | where | ||
97 | paddr = text (show (peerSockAddr p)) | ||
98 | |||
92 | -- | Ports typically reserved for bittorrent P2P listener. | 99 | -- | Ports typically reserved for bittorrent P2P listener. |
93 | defaultPorts :: [PortNumber] | 100 | defaultPorts :: [PortNumber] |
94 | defaultPorts = [6881..6889] | 101 | defaultPorts = [6881..6889] |
@@ -117,11 +124,3 @@ connectToPeer p = do | |||
117 | sock <- socket AF_INET Stream Network.Socket.defaultProtocol | 124 | sock <- socket AF_INET Stream Network.Socket.defaultProtocol |
118 | connect sock (peerSockAddr p) | 125 | connect sock (peerSockAddr p) |
119 | return sock | 126 | return sock |
120 | |||
121 | -- | Pretty print peer address in human readable form. | ||
122 | ppPeer :: PeerAddr -> Doc | ||
123 | ppPeer p @ PeerAddr {..} | ||
124 | | Just pid <- peerID = ppClientInfo (clientInfo pid) <+> "at" <+> paddr | ||
125 | | otherwise = paddr | ||
126 | where | ||
127 | paddr = text (show (peerSockAddr p)) | ||
diff --git a/src/Network/BitTorrent/Core/PeerId.hs b/src/Network/BitTorrent/Core/PeerId.hs index a212df3d..6aebe8d4 100644 --- a/src/Network/BitTorrent/Core/PeerId.hs +++ b/src/Network/BitTorrent/Core/PeerId.hs | |||
@@ -16,7 +16,6 @@ | |||
16 | module Network.BitTorrent.Core.PeerId | 16 | module Network.BitTorrent.Core.PeerId |
17 | ( -- * PeerId | 17 | ( -- * PeerId |
18 | PeerId (getPeerId) | 18 | PeerId (getPeerId) |
19 | , ppPeerId | ||
20 | 19 | ||
21 | -- * Generation | 20 | -- * Generation |
22 | , genPeerId | 21 | , genPeerId |
@@ -57,6 +56,7 @@ import Data.Version (Version(Version), versionBranch) | |||
57 | import System.Entropy (getEntropy) | 56 | import System.Entropy (getEntropy) |
58 | import System.Locale (defaultTimeLocale) | 57 | import System.Locale (defaultTimeLocale) |
59 | import Text.PrettyPrint hiding ((<>)) | 58 | import Text.PrettyPrint hiding ((<>)) |
59 | import Text.PrettyPrint.Class | ||
60 | import Text.Read (readMaybe) | 60 | import Text.Read (readMaybe) |
61 | import Paths_bittorrent (version) | 61 | import Paths_bittorrent (version) |
62 | 62 | ||
@@ -83,9 +83,8 @@ instance IsString PeerId where | |||
83 | where | 83 | where |
84 | bs = fromString str | 84 | bs = fromString str |
85 | 85 | ||
86 | -- | Format peer id in human readable form. | 86 | instance Pretty PeerId where |
87 | ppPeerId :: PeerId -> Doc | 87 | pretty = text . BC.unpack . getPeerId |
88 | ppPeerId = text . BC.unpack . getPeerId | ||
89 | 88 | ||
90 | {----------------------------------------------------------------------- | 89 | {----------------------------------------------------------------------- |
91 | -- Encoding | 90 | -- Encoding |