diff options
Diffstat (limited to 'src/Network')
-rw-r--r-- | src/Network/BitTorrent/Core/PeerId.hs | 11 | ||||
-rw-r--r-- | src/Network/BitTorrent/Tracker/Message.hs | 13 |
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 | |||
46 | import Data.List.Split as L | 46 | import Data.List.Split as L |
47 | import Data.Maybe (fromMaybe, catMaybes) | 47 | import Data.Maybe (fromMaybe, catMaybes) |
48 | import Data.Monoid | 48 | import Data.Monoid |
49 | import Data.Hashable | ||
49 | import Data.Serialize as S | 50 | import Data.Serialize as S |
50 | import Data.String | 51 | import Data.String |
51 | import Data.Time.Clock (getCurrentTime) | 52 | import Data.Time.Clock (getCurrentTime) |
52 | import Data.Time.Format (formatTime) | 53 | import Data.Time.Format (formatTime) |
53 | import Data.URLEncoded | 54 | import Data.URLEncoded |
54 | import Data.Version (Version(Version), versionBranch) | 55 | import Data.Version (Version(Version), versionBranch) |
56 | import Network.HTTP.Types.QueryLike | ||
55 | import System.Entropy (getEntropy) | 57 | import System.Entropy (getEntropy) |
56 | import System.Locale (defaultTimeLocale) | 58 | import System.Locale (defaultTimeLocale) |
57 | import Text.PrettyPrint hiding ((<>)) | 59 | import Text.PrettyPrint hiding ((<>)) |
@@ -70,12 +72,17 @@ newtype PeerId = PeerId { getPeerId :: ByteString } | |||
70 | peerIdLen :: Int | 72 | peerIdLen :: Int |
71 | peerIdLen = 20 | 73 | peerIdLen = 20 |
72 | 74 | ||
75 | instance Hashable PeerId where | ||
76 | hash = hash . getPeerId | ||
77 | {-# INLINE hash #-} | ||
78 | |||
73 | instance Serialize PeerId where | 79 | instance Serialize PeerId where |
74 | put = putByteString . getPeerId | 80 | put = putByteString . getPeerId |
75 | get = PeerId <$> getBytes peerIdLen | 81 | get = PeerId <$> getBytes peerIdLen |
76 | 82 | ||
77 | instance URLShow PeerId where | 83 | instance QueryValueLike PeerId where |
78 | urlShow = BC.unpack . getPeerId | 84 | toQueryValue (PeerId pid) = Just pid |
85 | {-# INLINE toQueryValue #-} | ||
79 | 86 | ||
80 | instance IsString PeerId where | 87 | instance 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 | |||
66 | import Data.URLEncoded as URL | 66 | import Data.URLEncoded as URL |
67 | import Data.Word | 67 | import Data.Word |
68 | import Network | 68 | import Network |
69 | import Network.HTTP.Types.QueryLike | ||
69 | import Network.HTTP.Types.URI hiding (urlEncode) | 70 | import Network.HTTP.Types.URI hiding (urlEncode) |
70 | import Network.Socket | 71 | import Network.Socket |
71 | import Network.URI | 72 | import Network.URI |
@@ -173,15 +174,21 @@ instance URLShow Word32 where | |||
173 | -- | HTTP tracker protocol compatible encoding. | 174 | -- | HTTP tracker protocol compatible encoding. |
174 | instance URLEncode AnnounceQuery where | 175 | instance 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 | ||
186 | instance 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. |
186 | instance Serialize AnnounceQuery where | 193 | instance Serialize AnnounceQuery where |
187 | put AnnounceQuery {..} = do | 194 | put AnnounceQuery {..} = do |