diff options
author | Sam T <pxqr.sta@gmail.com> | 2013-06-12 07:34:34 +0400 |
---|---|---|
committer | Sam T <pxqr.sta@gmail.com> | 2013-06-12 07:34:34 +0400 |
commit | 5c3c114e0e84339f88892e08010bd8b1408431d1 (patch) | |
tree | 70763a0fa0fc3c9f7ecbf1242b479a55b07cf1e2 /src/Network | |
parent | 8b005c4eb0f58db974c342efe0821240f39a6331 (diff) |
~ Minor fixes.
* Annotate all required fields as strict. These are always used and
there is no reason to keep them lazy.
* Augment user errors with location.
Diffstat (limited to 'src/Network')
-rw-r--r-- | src/Network/BitTorrent/Exchange/Protocol.hs | 6 | ||||
-rw-r--r-- | src/Network/BitTorrent/Tracker.hs | 6 | ||||
-rw-r--r-- | src/Network/BitTorrent/Tracker/Protocol.hs | 33 |
3 files changed, 26 insertions, 19 deletions
diff --git a/src/Network/BitTorrent/Exchange/Protocol.hs b/src/Network/BitTorrent/Exchange/Protocol.hs index 6b97d8d1..d2d3da6c 100644 --- a/src/Network/BitTorrent/Exchange/Protocol.hs +++ b/src/Network/BitTorrent/Exchange/Protocol.hs | |||
@@ -181,7 +181,7 @@ handshake sock hs = do | |||
181 | 181 | ||
182 | case checkIH (S.decode resp) of | 182 | case checkIH (S.decode resp) of |
183 | Right hs' -> return hs' | 183 | Right hs' -> return hs' |
184 | Left msg -> throw $ userError msg | 184 | Left msg -> throwIO $ userError $ msg ++ " in handshake body." |
185 | where | 185 | where |
186 | checkIH (Right hs') | 186 | checkIH (Right hs') |
187 | | hsInfoHash hs /= hsInfoHash hs' | 187 | | hsInfoHash hs /= hsInfoHash hs' |
@@ -234,10 +234,10 @@ ppBlockIx BlockIx {..} = | |||
234 | 234 | ||
235 | data Block = Block { | 235 | data Block = Block { |
236 | -- | Zero-based piece index. | 236 | -- | Zero-based piece index. |
237 | blkPiece :: !PieceLIx | 237 | blkPiece :: {-# UNPACK #-} !PieceLIx |
238 | 238 | ||
239 | -- | Zero-based byte offset within the piece. | 239 | -- | Zero-based byte offset within the piece. |
240 | , blkOffset :: !Int | 240 | , blkOffset :: {-# UNPACK #-} !Int |
241 | 241 | ||
242 | -- | Payload. | 242 | -- | Payload. |
243 | , blkData :: !ByteString | 243 | , blkData :: !ByteString |
diff --git a/src/Network/BitTorrent/Tracker.hs b/src/Network/BitTorrent/Tracker.hs index cb776431..e1f9ff76 100644 --- a/src/Network/BitTorrent/Tracker.hs +++ b/src/Network/BitTorrent/Tracker.hs | |||
@@ -172,6 +172,7 @@ withTracker initProgress conn action = bracket start end (action . fst) | |||
172 | where | 172 | where |
173 | start = do | 173 | start = do |
174 | resp <- askTracker (startedReq conn initProgress) | 174 | resp <- askTracker (startedReq conn initProgress) |
175 | print resp | ||
175 | se <- newSession initProgress (respInterval resp) (respPeers resp) | 176 | se <- newSession initProgress (respInterval resp) (respPeers resp) |
176 | tid <- forkIO (syncSession se) | 177 | tid <- forkIO (syncSession se) |
177 | return (se, tid) | 178 | return (se, tid) |
@@ -179,10 +180,13 @@ withTracker initProgress conn action = bracket start end (action . fst) | |||
179 | syncSession se @ TSession {..} = forever $ do | 180 | syncSession se @ TSession {..} = forever $ do |
180 | waitInterval se | 181 | waitInterval se |
181 | pr <- getProgress se | 182 | pr <- getProgress se |
183 | print "tracker req" | ||
182 | resp <- tryJust isIOException $ do | 184 | resp <- tryJust isIOException $ do |
183 | askTracker (regularReq defaultNumWant conn pr) | 185 | askTracker (regularReq defaultNumWant conn pr) |
186 | print "tracker resp" | ||
184 | case resp of | 187 | case resp of |
185 | Right (OK {..}) -> do | 188 | Right (ok @ OK {..}) -> do |
189 | print ok | ||
186 | writeIORef seInterval respInterval | 190 | writeIORef seInterval respInterval |
187 | writeList2Chan sePeers respPeers | 191 | writeList2Chan sePeers respPeers |
188 | _ -> return () | 192 | _ -> return () |
diff --git a/src/Network/BitTorrent/Tracker/Protocol.hs b/src/Network/BitTorrent/Tracker/Protocol.hs index c94a2dfc..13127e7c 100644 --- a/src/Network/BitTorrent/Tracker/Protocol.hs +++ b/src/Network/BitTorrent/Tracker/Protocol.hs | |||
@@ -67,28 +67,28 @@ data Event = Started | |||
67 | -- to keep track lists of active peer for a particular torrent. | 67 | -- to keep track lists of active peer for a particular torrent. |
68 | -- | 68 | -- |
69 | data TRequest = TRequest { -- TODO peer here -- TODO detach announce | 69 | data TRequest = TRequest { -- TODO peer here -- TODO detach announce |
70 | reqAnnounce :: URI | 70 | reqAnnounce :: !URI |
71 | -- ^ Announce url of the torrent usually obtained from 'Torrent'. | 71 | -- ^ Announce url of the torrent usually obtained from 'Torrent'. |
72 | 72 | ||
73 | , reqInfoHash :: InfoHash | 73 | , reqInfoHash :: !InfoHash |
74 | -- ^ Hash of info part of the torrent usually obtained from | 74 | -- ^ Hash of info part of the torrent usually obtained from |
75 | -- 'Torrent'. | 75 | -- 'Torrent'. |
76 | 76 | ||
77 | , reqPeerID :: PeerID | 77 | , reqPeerID :: !PeerID |
78 | -- ^ ID of the peer doing request. | 78 | -- ^ ID of the peer doing request. |
79 | 79 | ||
80 | , reqPort :: PortNumber | 80 | , reqPort :: !PortNumber |
81 | -- ^ Port to listen to for connections from other | 81 | -- ^ Port to listen to for connections from other |
82 | -- peers. Normally, tracker should respond with this port when | 82 | -- peers. Normally, tracker should respond with this port when |
83 | -- some peer request the tracker with the same info hash. | 83 | -- some peer request the tracker with the same info hash. |
84 | 84 | ||
85 | , reqUploaded :: Integer | 85 | , reqUploaded :: !Integer |
86 | -- ^ Number of bytes that the peer has uploaded in the swarm. | 86 | -- ^ Number of bytes that the peer has uploaded in the swarm. |
87 | 87 | ||
88 | , reqDownloaded :: Integer | 88 | , reqDownloaded :: !Integer |
89 | -- ^ Number of bytes downloaded in the swarm by the peer. | 89 | -- ^ Number of bytes downloaded in the swarm by the peer. |
90 | 90 | ||
91 | , reqLeft :: Integer | 91 | , reqLeft :: !Integer |
92 | -- ^ Number of bytes needed in order to complete download. | 92 | -- ^ Number of bytes needed in order to complete download. |
93 | 93 | ||
94 | , reqIP :: Maybe HostAddress | 94 | , reqIP :: Maybe HostAddress |
@@ -111,25 +111,25 @@ data TRequest = TRequest { -- TODO peer here -- TODO detach announce | |||
111 | data TResponse = | 111 | data TResponse = |
112 | Failure Text -- ^ Failure reason in human readable form. | 112 | Failure Text -- ^ Failure reason in human readable form. |
113 | | OK { | 113 | | OK { |
114 | respWarning :: Maybe Text | 114 | respWarning :: Maybe Text |
115 | -- ^ Human readable warning. | 115 | -- ^ Human readable warning. |
116 | 116 | ||
117 | , respInterval :: Int | 117 | , respInterval :: !Int |
118 | -- ^ Recommended interval to wait between requests. | 118 | -- ^ Recommended interval to wait between requests. |
119 | 119 | ||
120 | , respMinInterval :: Maybe Int | 120 | , respMinInterval :: Maybe Int |
121 | -- ^ Minimal amount of time between requests. A peer /should/ | 121 | -- ^ Minimal amount of time between requests. A peer /should/ |
122 | -- make timeout with at least 'respMinInterval' value, | 122 | -- make timeout with at least 'respMinInterval' value, |
123 | -- otherwise tracker might not respond. If not specified the | 123 | -- otherwise tracker might not respond. If not specified the |
124 | -- same applies to 'respInterval'. | 124 | -- same applies to 'respInterval'. |
125 | 125 | ||
126 | , respComplete :: Maybe Int | 126 | , respComplete :: Maybe Int |
127 | -- ^ Number of peers completed the torrent. (seeders) | 127 | -- ^ Number of peers completed the torrent. (seeders) |
128 | 128 | ||
129 | , respIncomplete :: Maybe Int | 129 | , respIncomplete :: Maybe Int |
130 | -- ^ Number of peers downloading the torrent. (leechers) | 130 | -- ^ Number of peers downloading the torrent. (leechers) |
131 | 131 | ||
132 | , respPeers :: [PeerAddr] | 132 | , respPeers :: ![PeerAddr] |
133 | -- ^ Peers that must be contacted. | 133 | -- ^ Peers that must be contacted. |
134 | } deriving Show | 134 | } deriving Show |
135 | 135 | ||
@@ -227,11 +227,14 @@ askTracker req = do | |||
227 | 227 | ||
228 | rawResp <- simpleHTTP r | 228 | rawResp <- simpleHTTP r |
229 | respBody <- getResponseBody rawResp | 229 | respBody <- getResponseBody rawResp |
230 | print $ respBody | ||
230 | checkResult $ decoded respBody | 231 | checkResult $ decoded respBody |
231 | where | 232 | where |
232 | mkHTTPRequest :: URI -> Request ByteString | 233 | mkHTTPRequest :: URI -> Request ByteString |
233 | mkHTTPRequest uri = Request uri GET [] "" | 234 | mkHTTPRequest uri = Request uri GET [] "" |
234 | 235 | ||
235 | checkResult (Left err) = ioError (userError err) | 236 | checkResult (Left err) |
236 | checkResult (Right (Failure err)) = ioError (userError (show err)) | 237 | = ioError $ userError $ err ++ " in tracker response" |
238 | checkResult (Right (Failure err)) | ||
239 | = ioError $ userError $ show err ++ " in tracker response" | ||
237 | checkResult (Right resp) = return resp | 240 | checkResult (Right resp) = return resp |