summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-07-11 22:10:02 +0400
committerSam T <pxqr.sta@gmail.com>2013-07-11 22:10:02 +0400
commitf6cad9f8b4aab905dfb1a77ccdc85eeb4b52bd22 (patch)
treea1c1749a69454d41c41365f6e434bdb4889656c8
parent63b36a8b157171fb37f845075716e160e74e5f01 (diff)
~ Add newNodeSession.
-rw-r--r--src/Network/BitTorrent/DHT.hs40
1 files changed, 30 insertions, 10 deletions
diff --git a/src/Network/BitTorrent/DHT.hs b/src/Network/BitTorrent/DHT.hs
index e7b9ec13..f3c993c3 100644
--- a/src/Network/BitTorrent/DHT.hs
+++ b/src/Network/BitTorrent/DHT.hs
@@ -2,8 +2,10 @@
2{-# LANGUAGE RecordWildCards #-} 2{-# LANGUAGE RecordWildCards #-}
3module Network.BitTorrent.DHT 3module Network.BitTorrent.DHT
4 ( 4 (
5 newNodeSession
6
5 -- * Tracker 7 -- * Tracker
6 ping 8 , ping
7 , findNode 9 , findNode
8 , getPeers 10 , getPeers
9 , announcePeer 11 , announcePeer
@@ -13,6 +15,7 @@ module Network.BitTorrent.DHT
13 ) where 15 ) where
14 16
15import Control.Applicative 17import Control.Applicative
18import Control.Concurrent
16import Control.Concurrent.STM 19import Control.Concurrent.STM
17import Control.Monad 20import Control.Monad
18import Control.Exception 21import Control.Exception
@@ -44,8 +47,8 @@ type NodeId = ByteString
44-- | Generate random NodeID used for the entire session. 47-- | Generate random NodeID used for the entire session.
45-- Distribution of ID's should be as uniform as possible. 48-- Distribution of ID's should be as uniform as possible.
46-- 49--
47genNodeID :: IO NodeId 50genNodeId :: IO NodeId
48genNodeID = getEntropy 20 51genNodeId = getEntropy 20
49 52
50instance Serialize PortNumber where 53instance Serialize PortNumber where
51 get = fromIntegral <$> getWord16be 54 get = fromIntegral <$> getWord16be
@@ -132,6 +135,9 @@ insertNode = HM.insert
132 135
133type Alpha = Int 136type Alpha = Int
134 137
138defaultAlpha :: Alpha
139defaultAlpha = 8
140
135-- TODO 141-- TODO
136kclosest :: Int -> NodeId -> RoutingTable -> [NodeId] 142kclosest :: Int -> NodeId -> RoutingTable -> [NodeId]
137kclosest = undefined 143kclosest = undefined
@@ -154,6 +160,15 @@ instance Eq NodeSession where
154instance Ord NodeSession where 160instance Ord NodeSession where
155 compare = comparing nodeId 161 compare = comparing nodeId
156 162
163newNodeSession :: PortNumber -> IO NodeSession
164newNodeSession lport
165 = NodeSession
166 <$> genNodeId
167 <*> newTVarIO HM.empty
168 <*> newTVarIO HM.empty
169 <*> pure defaultAlpha
170 <*> pure lport
171
157assignToken :: NodeSession -> NodeId -> IO Token 172assignToken :: NodeSession -> NodeId -> IO Token
158assignToken _ _ = return "" 173assignToken _ _ = return ""
159 174
@@ -309,10 +324,15 @@ announcePeerS ses @ NodeSession {..} NodeAddr {..} (nid, ih, port, token) = do
309 modifyTVar contactInfo $ insertPeer ih peerAddr 324 modifyTVar contactInfo $ insertPeer ih peerAddr
310 return nodeId 325 return nodeId
311 326
312dhtServer :: PortNumber -> NodeSession -> IO () 327dhtTracker :: NodeSession -> InfoHash -> Chan PeerAddr -> IO ()
313dhtServer p s = server p 328dhtTracker = undefined
314 [ pingM ==> pingS s undefined 329
315 , findNodeM ==> findNodeS s undefined 330dhtServer :: NodeSession -> PortNumber -> IO ()
316 , getPeersM ==> getPeersS s undefined 331dhtServer s p = server p methods
317 , announcePeerM ==> announcePeerS s undefined 332 where
318 ] \ No newline at end of file 333 methods =
334 [ pingM ==> pingS s undefined
335 , findNodeM ==> findNodeS s undefined
336 , getPeersM ==> getPeersS s undefined
337 , announcePeerM ==> announcePeerS s undefined
338 ] \ No newline at end of file