summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-08-16 08:16:29 +0400
committerSam T <pxqr.sta@gmail.com>2013-08-16 08:16:29 +0400
commit1c19636c20e918388ef7f16faa8c6fb617d917d8 (patch)
tree9872b0a6f73ad541353ea2dc76095c626c16d8f4
parent8e35565ad8c759fe0c69b70fe7c1f68c811259f0 (diff)
~ Some stubs for UDP tracker.
-rw-r--r--src/Network/BitTorrent/Tracker/UDP.hs46
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
21import Data.Word 21import Data.Word
22import Data.Text 22import Data.Text
23import Data.Text.Encoding 23import Data.Text.Encoding
24import Network.Socket hiding (Connected)
25import Network.Socket.ByteString as BS
24 26
25import Data.Torrent () 27import Data.Torrent ()
26import Network.BitTorrent.Tracker.Protocol 28import 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.
30newtype ConnId = ConnId { getConnId :: Word64 } 32newtype 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.
34newtype TransId = TransId { getTransId :: Word32 } 36newtype TransId = TransId { getTransId :: Word32 }
35 deriving (Show, Serialize) 37 deriving (Show, Eq, Serialize)
36
37genConnectionId :: IO ConnId
38genConnectionId = return (ConnId 0)
39 38
40genTransactionId :: IO TransId 39genTransactionId :: IO TransId
41genTransactionId = return (TransId 0) 40genTransactionId = return (TransId 0)
42 41
42initialConnectionId :: ConnId
43initialConnectionId = ConnId 0
44
43data Request = Connect 45data 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
159maxPacketSize :: Int
160maxPacketSize = 98 -- announce request packet
161
162call :: Request -> IO Response
163call 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
180data Connection = Connection
181
182type URI = ()
183
184connectTracker :: URI -> IO Connection
185connectTracker = undefined
186
187announceTracker :: Connection -> AnnounceQuery -> IO AnnounceInfo
188announceTracker = undefined
189
190scrape :: Connection -> ScrapeQuery -> IO [ScrapeInfo]
191scrape = undefined \ No newline at end of file