diff options
author | Sam T <pxqr.sta@gmail.com> | 2013-08-16 08:16:29 +0400 |
---|---|---|
committer | Sam T <pxqr.sta@gmail.com> | 2013-08-16 08:16:29 +0400 |
commit | 1c19636c20e918388ef7f16faa8c6fb617d917d8 (patch) | |
tree | 9872b0a6f73ad541353ea2dc76095c626c16d8f4 | |
parent | 8e35565ad8c759fe0c69b70fe7c1f68c811259f0 (diff) |
~ Some stubs for UDP tracker.
-rw-r--r-- | src/Network/BitTorrent/Tracker/UDP.hs | 46 |
1 files changed, 41 insertions, 5 deletions
diff --git a/src/Network/BitTorrent/Tracker/UDP.hs b/src/Network/BitTorrent/Tracker/UDP.hs index 369750bd..43de7663 100644 --- a/src/Network/BitTorrent/Tracker/UDP.hs +++ b/src/Network/BitTorrent/Tracker/UDP.hs | |||
@@ -21,6 +21,8 @@ import Data.Serialize | |||
21 | import Data.Word | 21 | import Data.Word |
22 | import Data.Text | 22 | import Data.Text |
23 | import Data.Text.Encoding | 23 | import Data.Text.Encoding |
24 | import Network.Socket hiding (Connected) | ||
25 | import Network.Socket.ByteString as BS | ||
24 | 26 | ||
25 | import Data.Torrent () | 27 | import Data.Torrent () |
26 | import Network.BitTorrent.Tracker.Protocol | 28 | import Network.BitTorrent.Tracker.Protocol |
@@ -28,18 +30,18 @@ import Network.BitTorrent.Tracker.Protocol | |||
28 | 30 | ||
29 | -- | Connection Id is used for entire tracker session. | 31 | -- | Connection Id is used for entire tracker session. |
30 | newtype ConnId = ConnId { getConnId :: Word64 } | 32 | newtype ConnId = ConnId { getConnId :: Word64 } |
31 | deriving (Show, Serialize) | 33 | deriving (Show, Eq, Serialize) |
32 | 34 | ||
33 | -- | Transaction Id is used for within UDP RPC. | 35 | -- | Transaction Id is used for within UDP RPC. |
34 | newtype TransId = TransId { getTransId :: Word32 } | 36 | newtype TransId = TransId { getTransId :: Word32 } |
35 | deriving (Show, Serialize) | 37 | deriving (Show, Eq, Serialize) |
36 | |||
37 | genConnectionId :: IO ConnId | ||
38 | genConnectionId = return (ConnId 0) | ||
39 | 38 | ||
40 | genTransactionId :: IO TransId | 39 | genTransactionId :: IO TransId |
41 | genTransactionId = return (TransId 0) | 40 | genTransactionId = return (TransId 0) |
42 | 41 | ||
42 | initialConnectionId :: ConnId | ||
43 | initialConnectionId = ConnId 0 | ||
44 | |||
43 | data Request = Connect | 45 | data Request = Connect |
44 | | Announce AnnounceQuery | 46 | | Announce AnnounceQuery |
45 | | Scrape ScrapeQuery | 47 | | Scrape ScrapeQuery |
@@ -153,3 +155,37 @@ instance Serialize (Transaction Response) where | |||
153 | Left ex -> fail (show ex) | 155 | Left ex -> fail (show ex) |
154 | Right msg -> return $ Failed msg | 156 | Right msg -> return $ Failed msg |
155 | | otherwise = fail "unknown message id" | 157 | | otherwise = fail "unknown message id" |
158 | |||
159 | maxPacketSize :: Int | ||
160 | maxPacketSize = 98 -- announce request packet | ||
161 | |||
162 | call :: Request -> IO Response | ||
163 | call request = do | ||
164 | tid <- genTransactionId | ||
165 | let trans = Transaction initialConnectionId tid request | ||
166 | |||
167 | let addr = error "TODO" | ||
168 | sock <- socket AF_INET Datagram defaultProtocol | ||
169 | BS.sendAllTo sock (encode trans) addr | ||
170 | (resp, addr') <- BS.recvFrom sock 4096 | ||
171 | if addr' /= addr | ||
172 | then error "address mismatch" | ||
173 | else case decode resp of | ||
174 | Left msg -> error msg | ||
175 | Right (Transaction {..}) -> do | ||
176 | if tid /= transId | ||
177 | then error "transaction id mismatch" | ||
178 | else return body | ||
179 | |||
180 | data Connection = Connection | ||
181 | |||
182 | type URI = () | ||
183 | |||
184 | connectTracker :: URI -> IO Connection | ||
185 | connectTracker = undefined | ||
186 | |||
187 | announceTracker :: Connection -> AnnounceQuery -> IO AnnounceInfo | ||
188 | announceTracker = undefined | ||
189 | |||
190 | scrape :: Connection -> ScrapeQuery -> IO [ScrapeInfo] | ||
191 | scrape = undefined \ No newline at end of file | ||