diff options
author | Sam T <pxqr.sta@gmail.com> | 2013-07-11 22:10:02 +0400 |
---|---|---|
committer | Sam T <pxqr.sta@gmail.com> | 2013-07-11 22:10:02 +0400 |
commit | f6cad9f8b4aab905dfb1a77ccdc85eeb4b52bd22 (patch) | |
tree | a1c1749a69454d41c41365f6e434bdb4889656c8 /src/Network/BitTorrent/DHT.hs | |
parent | 63b36a8b157171fb37f845075716e160e74e5f01 (diff) |
~ Add newNodeSession.
Diffstat (limited to 'src/Network/BitTorrent/DHT.hs')
-rw-r--r-- | src/Network/BitTorrent/DHT.hs | 40 |
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 #-} |
3 | module Network.BitTorrent.DHT | 3 | module 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 | ||
15 | import Control.Applicative | 17 | import Control.Applicative |
18 | import Control.Concurrent | ||
16 | import Control.Concurrent.STM | 19 | import Control.Concurrent.STM |
17 | import Control.Monad | 20 | import Control.Monad |
18 | import Control.Exception | 21 | import 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 | -- |
47 | genNodeID :: IO NodeId | 50 | genNodeId :: IO NodeId |
48 | genNodeID = getEntropy 20 | 51 | genNodeId = getEntropy 20 |
49 | 52 | ||
50 | instance Serialize PortNumber where | 53 | instance Serialize PortNumber where |
51 | get = fromIntegral <$> getWord16be | 54 | get = fromIntegral <$> getWord16be |
@@ -132,6 +135,9 @@ insertNode = HM.insert | |||
132 | 135 | ||
133 | type Alpha = Int | 136 | type Alpha = Int |
134 | 137 | ||
138 | defaultAlpha :: Alpha | ||
139 | defaultAlpha = 8 | ||
140 | |||
135 | -- TODO | 141 | -- TODO |
136 | kclosest :: Int -> NodeId -> RoutingTable -> [NodeId] | 142 | kclosest :: Int -> NodeId -> RoutingTable -> [NodeId] |
137 | kclosest = undefined | 143 | kclosest = undefined |
@@ -154,6 +160,15 @@ instance Eq NodeSession where | |||
154 | instance Ord NodeSession where | 160 | instance Ord NodeSession where |
155 | compare = comparing nodeId | 161 | compare = comparing nodeId |
156 | 162 | ||
163 | newNodeSession :: PortNumber -> IO NodeSession | ||
164 | newNodeSession lport | ||
165 | = NodeSession | ||
166 | <$> genNodeId | ||
167 | <*> newTVarIO HM.empty | ||
168 | <*> newTVarIO HM.empty | ||
169 | <*> pure defaultAlpha | ||
170 | <*> pure lport | ||
171 | |||
157 | assignToken :: NodeSession -> NodeId -> IO Token | 172 | assignToken :: NodeSession -> NodeId -> IO Token |
158 | assignToken _ _ = return "" | 173 | assignToken _ _ = 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 | ||
312 | dhtServer :: PortNumber -> NodeSession -> IO () | 327 | dhtTracker :: NodeSession -> InfoHash -> Chan PeerAddr -> IO () |
313 | dhtServer p s = server p | 328 | dhtTracker = undefined |
314 | [ pingM ==> pingS s undefined | 329 | |
315 | , findNodeM ==> findNodeS s undefined | 330 | dhtServer :: NodeSession -> PortNumber -> IO () |
316 | , getPeersM ==> getPeersS s undefined | 331 | dhtServer 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 | ||