summaryrefslogtreecommitdiff
path: root/src/Network
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network')
-rw-r--r--src/Network/BitTorrent/Core/PeerId.hs11
-rw-r--r--src/Network/BitTorrent/Tracker/Message.hs13
2 files changed, 19 insertions, 5 deletions
diff --git a/src/Network/BitTorrent/Core/PeerId.hs b/src/Network/BitTorrent/Core/PeerId.hs
index a2b03e92..f5a40f29 100644
--- a/src/Network/BitTorrent/Core/PeerId.hs
+++ b/src/Network/BitTorrent/Core/PeerId.hs
@@ -46,12 +46,14 @@ import Data.List as L
46import Data.List.Split as L 46import Data.List.Split as L
47import Data.Maybe (fromMaybe, catMaybes) 47import Data.Maybe (fromMaybe, catMaybes)
48import Data.Monoid 48import Data.Monoid
49import Data.Hashable
49import Data.Serialize as S 50import Data.Serialize as S
50import Data.String 51import Data.String
51import Data.Time.Clock (getCurrentTime) 52import Data.Time.Clock (getCurrentTime)
52import Data.Time.Format (formatTime) 53import Data.Time.Format (formatTime)
53import Data.URLEncoded 54import Data.URLEncoded
54import Data.Version (Version(Version), versionBranch) 55import Data.Version (Version(Version), versionBranch)
56import Network.HTTP.Types.QueryLike
55import System.Entropy (getEntropy) 57import System.Entropy (getEntropy)
56import System.Locale (defaultTimeLocale) 58import System.Locale (defaultTimeLocale)
57import Text.PrettyPrint hiding ((<>)) 59import Text.PrettyPrint hiding ((<>))
@@ -70,12 +72,17 @@ newtype PeerId = PeerId { getPeerId :: ByteString }
70peerIdLen :: Int 72peerIdLen :: Int
71peerIdLen = 20 73peerIdLen = 20
72 74
75instance Hashable PeerId where
76 hash = hash . getPeerId
77 {-# INLINE hash #-}
78
73instance Serialize PeerId where 79instance Serialize PeerId where
74 put = putByteString . getPeerId 80 put = putByteString . getPeerId
75 get = PeerId <$> getBytes peerIdLen 81 get = PeerId <$> getBytes peerIdLen
76 82
77instance URLShow PeerId where 83instance QueryValueLike PeerId where
78 urlShow = BC.unpack . getPeerId 84 toQueryValue (PeerId pid) = Just pid
85 {-# INLINE toQueryValue #-}
79 86
80instance IsString PeerId where 87instance IsString PeerId where
81 fromString str 88 fromString str
diff --git a/src/Network/BitTorrent/Tracker/Message.hs b/src/Network/BitTorrent/Tracker/Message.hs
index 59ef2027..e5d8b25a 100644
--- a/src/Network/BitTorrent/Tracker/Message.hs
+++ b/src/Network/BitTorrent/Tracker/Message.hs
@@ -66,6 +66,7 @@ import Data.Typeable
66import Data.URLEncoded as URL 66import Data.URLEncoded as URL
67import Data.Word 67import Data.Word
68import Network 68import Network
69import Network.HTTP.Types.QueryLike
69import Network.HTTP.Types.URI hiding (urlEncode) 70import Network.HTTP.Types.URI hiding (urlEncode)
70import Network.Socket 71import Network.Socket
71import Network.URI 72import Network.URI
@@ -173,15 +174,21 @@ instance URLShow Word32 where
173-- | HTTP tracker protocol compatible encoding. 174-- | HTTP tracker protocol compatible encoding.
174instance URLEncode AnnounceQuery where 175instance URLEncode AnnounceQuery where
175 urlEncode AnnounceQuery {..} = mconcat 176 urlEncode AnnounceQuery {..} = mconcat
176 [ s "peer_id" %= reqPeerId 177 [ -- s "peer_id" %= reqPeerId
177 , s "port" %= reqPort 178 s "port" %= reqPort
178 , urlEncode reqProgress 179-- , urlEncode reqProgress
179 , s "ip" %=? reqIP 180 , s "ip" %=? reqIP
180 , s "numwant" %=? reqNumWant 181 , s "numwant" %=? reqNumWant
181 , s "event" %=? reqEvent 182 , s "event" %=? reqEvent
182 ] 183 ]
183 where s :: String -> String; s = id; {-# INLINE s #-} 184 where s :: String -> String; s = id; {-# INLINE s #-}
184 185
186instance QueryLike AnnounceQuery where
187 toQuery AnnounceQuery {..} =
188 [ ("info_hash", toQueryValue reqInfoHash)
189 , ("peer_id" , toQueryValue reqPeerId)
190 ]
191
185-- | UDP tracker protocol compatible encoding. 192-- | UDP tracker protocol compatible encoding.
186instance Serialize AnnounceQuery where 193instance Serialize AnnounceQuery where
187 put AnnounceQuery {..} = do 194 put AnnounceQuery {..} = do