diff options
Diffstat (limited to 'src/Network')
-rw-r--r-- | src/Network/BitTorrent/Extension.hs | 9 | ||||
-rw-r--r-- | src/Network/BitTorrent/Peer/Addr.hs | 11 | ||||
-rw-r--r-- | src/Network/BitTorrent/Peer/ClientInfo.hs | 17 | ||||
-rw-r--r-- | src/Network/BitTorrent/Peer/ID.hs | 5 | ||||
-rw-r--r-- | src/Network/BitTorrent/PeerWire/Block.hs | 14 | ||||
-rw-r--r-- | src/Network/BitTorrent/PeerWire/Handshake.hs | 9 | ||||
-rw-r--r-- | src/Network/BitTorrent/PeerWire/Message.hs | 16 |
7 files changed, 50 insertions, 31 deletions
diff --git a/src/Network/BitTorrent/Extension.hs b/src/Network/BitTorrent/Extension.hs index e37f3afb..4b71f91f 100644 --- a/src/Network/BitTorrent/Extension.hs +++ b/src/Network/BitTorrent/Extension.hs | |||
@@ -9,20 +9,21 @@ | |||
9 | -- | 9 | -- |
10 | -- > See http://www.bittorrent.org/beps/bep_0004.html | 10 | -- > See http://www.bittorrent.org/beps/bep_0004.html |
11 | -- | 11 | -- |
12 | {-# LANGUAGE OverloadedStrings #-} | ||
12 | module Network.BitTorrent.Extension | 13 | module Network.BitTorrent.Extension |
13 | ( Capabilities, ppCaps, defaultCaps, enabledCaps | 14 | ( Capabilities, ppCaps, defaultCaps, enabledCaps |
14 | , Extension, ppExtension, encodeExts, decodeExts | 15 | , Extension, ppExtension, encodeExts, decodeExts |
15 | ) where | 16 | ) where |
16 | 17 | ||
17 | import Data.Bits | 18 | import Data.Bits |
18 | import Data.List | ||
19 | import Data.Word | 19 | import Data.Word |
20 | import Text.PrettyPrint | ||
20 | 21 | ||
21 | 22 | ||
22 | type Capabilities = Word64 | 23 | type Capabilities = Word64 |
23 | 24 | ||
24 | ppCaps :: Capabilities -> String | 25 | ppCaps :: Capabilities -> Doc |
25 | ppCaps = intercalate ", " . map ppExtension . decodeExts | 26 | ppCaps = hcat . punctuate ", " . map ppExtension . decodeExts |
26 | 27 | ||
27 | defaultCaps :: Capabilities | 28 | defaultCaps :: Capabilities |
28 | defaultCaps = 0 | 29 | defaultCaps = 0 |
@@ -38,7 +39,7 @@ data Extension = ExtDHT -- ^ BEP 5 | |||
38 | | ExtFast -- ^ BEP 6 | 39 | | ExtFast -- ^ BEP 6 |
39 | deriving (Show, Eq, Ord, Enum, Bounded) | 40 | deriving (Show, Eq, Ord, Enum, Bounded) |
40 | 41 | ||
41 | ppExtension :: Extension -> String | 42 | ppExtension :: Extension -> Doc |
42 | ppExtension ExtDHT = "DHT" | 43 | ppExtension ExtDHT = "DHT" |
43 | ppExtension ExtFast = "Fast Extension" | 44 | ppExtension ExtFast = "Fast Extension" |
44 | 45 | ||
diff --git a/src/Network/BitTorrent/Peer/Addr.hs b/src/Network/BitTorrent/Peer/Addr.hs index 1c2ac2eb..5c05180a 100644 --- a/src/Network/BitTorrent/Peer/Addr.hs +++ b/src/Network/BitTorrent/Peer/Addr.hs | |||
@@ -6,6 +6,7 @@ | |||
6 | -- Portability : non-portable | 6 | -- Portability : non-portable |
7 | -- | 7 | -- |
8 | {-# LANGUAGE OverloadedStrings #-} | 8 | {-# LANGUAGE OverloadedStrings #-} |
9 | {-# LANGUAGE RecordWildCards #-} | ||
9 | {-# OPTIONS -fno-warn-orphans #-} | 10 | {-# OPTIONS -fno-warn-orphans #-} |
10 | module Network.BitTorrent.Peer.Addr | 11 | module Network.BitTorrent.Peer.Addr |
11 | ( PeerAddr(..) | 12 | ( PeerAddr(..) |
@@ -17,6 +18,7 @@ import Control.Applicative | |||
17 | import Data.BEncode | 18 | import Data.BEncode |
18 | import Data.Bits | 19 | import Data.Bits |
19 | import Data.Word | 20 | import Data.Word |
21 | import Text.PrettyPrint | ||
20 | import Network | 22 | import Network |
21 | import Network.Socket | 23 | import Network.Socket |
22 | 24 | ||
@@ -73,6 +75,9 @@ connectToPeer p = do | |||
73 | connect sock (peerSockAddr p) | 75 | connect sock (peerSockAddr p) |
74 | return sock | 76 | return sock |
75 | 77 | ||
76 | ppPeer :: PeerAddr -> String | 78 | ppPeer :: PeerAddr -> Doc |
77 | ppPeer p = maybe "" (++ " at ") ((ppClientInfo . clientInfo) <$> peerID p) | 79 | ppPeer p @ PeerAddr {..} = case peerID of |
78 | ++ show (peerSockAddr p) | 80 | Just pid -> ppClientInfo (clientInfo pid) <+> "at" <+> paddr |
81 | Nothing -> paddr | ||
82 | where | ||
83 | paddr = text (show (peerSockAddr p)) | ||
diff --git a/src/Network/BitTorrent/Peer/ClientInfo.hs b/src/Network/BitTorrent/Peer/ClientInfo.hs index ebbecbd7..bf7fcaab 100644 --- a/src/Network/BitTorrent/Peer/ClientInfo.hs +++ b/src/Network/BitTorrent/Peer/ClientInfo.hs | |||
@@ -12,6 +12,7 @@ | |||
12 | -- > See http://bittorrent.org/beps/bep_0020.html for more information. | 12 | -- > See http://bittorrent.org/beps/bep_0020.html for more information. |
13 | -- | 13 | -- |
14 | {-# LANGUAGE OverloadedStrings #-} | 14 | {-# LANGUAGE OverloadedStrings #-} |
15 | {-# LANGUAGE RecordWildCards #-} | ||
15 | module Network.BitTorrent.Peer.ClientInfo | 16 | module Network.BitTorrent.Peer.ClientInfo |
16 | ( -- * Info | 17 | ( -- * Info |
17 | ClientInfo(..), clientInfo, ppClientInfo, unknownClient | 18 | ClientInfo(..), clientInfo, ppClientInfo, unknownClient |
@@ -30,6 +31,7 @@ import Control.Applicative | |||
30 | import Data.ByteString (ByteString) | 31 | import Data.ByteString (ByteString) |
31 | import qualified Data.ByteString.Char8 as BC | 32 | import qualified Data.ByteString.Char8 as BC |
32 | import Data.Serialize.Get | 33 | import Data.Serialize.Get |
34 | import Text.PrettyPrint | ||
33 | 35 | ||
34 | import Network.BitTorrent.Peer.ID | 36 | import Network.BitTorrent.Peer.ID |
35 | 37 | ||
@@ -163,8 +165,8 @@ parseImpl = f . BC.unpack | |||
163 | f _ = IUnknown | 165 | f _ = IUnknown |
164 | 166 | ||
165 | -- | Format client implementation info in human readable form. | 167 | -- | Format client implementation info in human readable form. |
166 | ppClientImpl :: ClientImpl -> String | 168 | ppClientImpl :: ClientImpl -> Doc |
167 | ppClientImpl = tail . show | 169 | ppClientImpl = text . tail . show |
168 | 170 | ||
169 | unknownImpl :: ClientImpl | 171 | unknownImpl :: ClientImpl |
170 | unknownImpl = IUnknown | 172 | unknownImpl = IUnknown |
@@ -174,8 +176,8 @@ unknownImpl = IUnknown | |||
174 | type ClientVersion = ByteString | 176 | type ClientVersion = ByteString |
175 | 177 | ||
176 | -- | Format client implementation version in human readable form. | 178 | -- | Format client implementation version in human readable form. |
177 | ppClientVersion :: ClientVersion -> String | 179 | ppClientVersion :: ClientVersion -> Doc |
178 | ppClientVersion = BC.unpack | 180 | ppClientVersion = text . BC.unpack |
179 | 181 | ||
180 | unknownVersion :: ClientVersion | 182 | unknownVersion :: ClientVersion |
181 | unknownVersion = "0000" | 183 | unknownVersion = "0000" |
@@ -189,9 +191,10 @@ data ClientInfo = ClientInfo { | |||
189 | } deriving (Show, Eq, Ord) | 191 | } deriving (Show, Eq, Ord) |
190 | 192 | ||
191 | -- | Format client implementation in human readable form. | 193 | -- | Format client implementation in human readable form. |
192 | ppClientInfo :: ClientInfo -> String | 194 | ppClientInfo :: ClientInfo -> Doc |
193 | ppClientInfo ci = ppClientImpl (ciImpl ci) ++ " version " | 195 | ppClientInfo ClientInfo {..} = |
194 | ++ ppClientVersion (ciVersion ci) | 196 | ppClientImpl ciImpl <+> "version" <+> ppClientVersion ciVersion |
197 | |||
195 | 198 | ||
196 | -- | Unrecognized client implementation. | 199 | -- | Unrecognized client implementation. |
197 | unknownClient :: ClientInfo | 200 | unknownClient :: ClientInfo |
diff --git a/src/Network/BitTorrent/Peer/ID.hs b/src/Network/BitTorrent/Peer/ID.hs index f93409ed..9bf0ae31 100644 --- a/src/Network/BitTorrent/Peer/ID.hs +++ b/src/Network/BitTorrent/Peer/ID.hs | |||
@@ -44,6 +44,7 @@ import Data.URLEncoded | |||
44 | import Data.Version (Version(Version), versionBranch) | 44 | import Data.Version (Version(Version), versionBranch) |
45 | import Data.Time.Clock (getCurrentTime) | 45 | import Data.Time.Clock (getCurrentTime) |
46 | import Data.Time.Format (formatTime) | 46 | import Data.Time.Format (formatTime) |
47 | import Text.PrettyPrint (text, Doc) | ||
47 | import System.Locale (defaultTimeLocale) | 48 | import System.Locale (defaultTimeLocale) |
48 | 49 | ||
49 | 50 | ||
@@ -64,8 +65,8 @@ instance Serialize PeerID where | |||
64 | instance URLShow PeerID where | 65 | instance URLShow PeerID where |
65 | urlShow = BC.unpack . getPeerID | 66 | urlShow = BC.unpack . getPeerID |
66 | 67 | ||
67 | ppPeerID :: PeerID -> String | 68 | ppPeerID :: PeerID -> Doc |
68 | ppPeerID = BC.unpack . getPeerID | 69 | ppPeerID = text . BC.unpack . getPeerID |
69 | 70 | ||
70 | 71 | ||
71 | -- | Azureus-style encoding have the following layout: | 72 | -- | Azureus-style encoding have the following layout: |
diff --git a/src/Network/BitTorrent/PeerWire/Block.hs b/src/Network/BitTorrent/PeerWire/Block.hs index fbc65338..dea37321 100644 --- a/src/Network/BitTorrent/PeerWire/Block.hs +++ b/src/Network/BitTorrent/PeerWire/Block.hs | |||
@@ -1,3 +1,5 @@ | |||
1 | {-# LANGUAGE RecordWildCards #-} | ||
2 | {-# LANGUAGE OverloadedStrings #-} | ||
1 | module Network.BitTorrent.PeerWire.Block | 3 | module Network.BitTorrent.PeerWire.Block |
2 | ( BlockIx(..) | 4 | ( BlockIx(..) |
3 | , Block(..), blockSize | 5 | , Block(..), blockSize |
@@ -16,6 +18,7 @@ import Data.ByteString (ByteString) | |||
16 | import qualified Data.ByteString as B | 18 | import qualified Data.ByteString as B |
17 | import Data.Int | 19 | import Data.Int |
18 | import Data.Serialize | 20 | import Data.Serialize |
21 | import Text.PrettyPrint | ||
19 | 22 | ||
20 | 23 | ||
21 | type BlockLIx = Int | 24 | type BlockLIx = Int |
@@ -51,10 +54,11 @@ instance Serialize BlockIx where | |||
51 | putInt (ixLength ix) | 54 | putInt (ixLength ix) |
52 | {-# INLINE put #-} | 55 | {-# INLINE put #-} |
53 | 56 | ||
54 | ppBlockIx :: BlockIx -> String | 57 | ppBlockIx :: BlockIx -> Doc |
55 | ppBlockIx ix = "piece = " ++ show (ixPiece ix) ++ ", " | 58 | ppBlockIx BlockIx {..} = |
56 | ++ "offset = " ++ show (ixOffset ix) ++ ", " | 59 | "piece = " <> int ixPiece <> "," <+> |
57 | ++ "length = " ++ show (ixLength ix) | 60 | "offset = " <> int ixOffset <> "," <+> |
61 | "length = " <> int ixLength | ||
58 | 62 | ||
59 | data Block = Block { | 63 | data Block = Block { |
60 | -- | Zero-based piece index. | 64 | -- | Zero-based piece index. |
@@ -67,7 +71,7 @@ data Block = Block { | |||
67 | , blkData :: ByteString | 71 | , blkData :: ByteString |
68 | } deriving (Show, Eq) | 72 | } deriving (Show, Eq) |
69 | 73 | ||
70 | ppBlock :: Block -> String | 74 | ppBlock :: Block -> Doc |
71 | ppBlock = ppBlockIx . blockIx | 75 | ppBlock = ppBlockIx . blockIx |
72 | 76 | ||
73 | blockSize :: Block -> Int | 77 | blockSize :: Block -> Int |
diff --git a/src/Network/BitTorrent/PeerWire/Handshake.hs b/src/Network/BitTorrent/PeerWire/Handshake.hs index 6f4598ae..ff768cae 100644 --- a/src/Network/BitTorrent/PeerWire/Handshake.hs +++ b/src/Network/BitTorrent/PeerWire/Handshake.hs | |||
@@ -11,6 +11,7 @@ | |||
11 | -- peer. | 11 | -- peer. |
12 | -- | 12 | -- |
13 | {-# LANGUAGE OverloadedStrings #-} | 13 | {-# LANGUAGE OverloadedStrings #-} |
14 | {-# LANGUAGE RecordWildCards #-} | ||
14 | module Network.BitTorrent.PeerWire.Handshake | 15 | module Network.BitTorrent.PeerWire.Handshake |
15 | ( Handshake(..), handshakeCaps | 16 | ( Handshake(..), handshakeCaps |
16 | , handshake | 17 | , handshake |
@@ -27,6 +28,8 @@ import Data.ByteString (ByteString) | |||
27 | import qualified Data.ByteString as B | 28 | import qualified Data.ByteString as B |
28 | import qualified Data.ByteString.Char8 as BC | 29 | import qualified Data.ByteString.Char8 as BC |
29 | import Data.Serialize as S | 30 | import Data.Serialize as S |
31 | import Text.PrettyPrint | ||
32 | |||
30 | import Network | 33 | import Network |
31 | import Network.Socket.ByteString | 34 | import Network.Socket.ByteString |
32 | 35 | ||
@@ -77,9 +80,9 @@ handshakeCaps :: Handshake -> Capabilities | |||
77 | handshakeCaps = hsReserved | 80 | handshakeCaps = hsReserved |
78 | 81 | ||
79 | -- | Format handshake in human readable form. | 82 | -- | Format handshake in human readable form. |
80 | ppHandshake :: Handshake -> String | 83 | ppHandshake :: Handshake -> Doc |
81 | ppHandshake hs = BC.unpack (hsProtocol hs) ++ " " | 84 | ppHandshake Handshake {..} = |
82 | ++ ppClientInfo (clientInfo (hsPeerID hs)) | 85 | text (BC.unpack hsProtocol) <+> ppClientInfo (clientInfo hsPeerID) |
83 | 86 | ||
84 | -- | Get handshake message size in bytes from the length of protocol string. | 87 | -- | Get handshake message size in bytes from the length of protocol string. |
85 | handshakeSize :: Word8 -> Int | 88 | handshakeSize :: Word8 -> Int |
diff --git a/src/Network/BitTorrent/PeerWire/Message.hs b/src/Network/BitTorrent/PeerWire/Message.hs index f5ad2693..6515fdf2 100644 --- a/src/Network/BitTorrent/PeerWire/Message.hs +++ b/src/Network/BitTorrent/PeerWire/Message.hs | |||
@@ -1,3 +1,4 @@ | |||
1 | {-# LANGUAGE OverloadedStrings #-} | ||
1 | module Network.BitTorrent.PeerWire.Message | 2 | module Network.BitTorrent.PeerWire.Message |
2 | ( Message(..) | 3 | ( Message(..) |
3 | , Bitfield | 4 | , Bitfield |
@@ -7,6 +8,7 @@ module Network.BitTorrent.PeerWire.Message | |||
7 | import Control.Applicative | 8 | import Control.Applicative |
8 | import qualified Data.ByteString as B | 9 | import qualified Data.ByteString as B |
9 | import Data.Serialize | 10 | import Data.Serialize |
11 | import Text.PrettyPrint | ||
10 | import Network | 12 | import Network |
11 | 13 | ||
12 | import Network.BitTorrent.PeerWire.Block | 14 | import Network.BitTorrent.PeerWire.Block |
@@ -141,10 +143,10 @@ instance Serialize Message where | |||
141 | -- compact and suitable for logging: only useful information but not | 143 | -- compact and suitable for logging: only useful information but not |
142 | -- payload bytes. | 144 | -- payload bytes. |
143 | -- | 145 | -- |
144 | ppMessage :: Message -> String | 146 | ppMessage :: Message -> Doc |
145 | ppMessage (Bitfield _) = "Bitfield " | 147 | ppMessage (Bitfield _) = "Bitfield" |
146 | ppMessage (Piece blk) = "Piece " ++ ppBlock blk | 148 | ppMessage (Piece blk) = "Piece" <+> ppBlock blk |
147 | ppMessage (Cancel ix) = "Cancel " ++ ppBlockIx ix | 149 | ppMessage (Cancel ix) = "Cancel" <+> ppBlockIx ix |
148 | ppMessage (SuggestPiece pix) = "Suggest" ++ show pix | 150 | ppMessage (SuggestPiece pix) = "Suggest" <+> int pix |
149 | ppMessage (RejectRequest ix) = "Reject" ++ ppBlockIx ix | 151 | ppMessage (RejectRequest ix) = "Reject" <+> ppBlockIx ix |
150 | ppMessage msg = show msg \ No newline at end of file | 152 | ppMessage msg = text (show msg) \ No newline at end of file |