summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/DHT
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-03-05 00:35:47 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-03-05 00:35:47 +0400
commit3c7f12c10caca196a970c0cc8b6c46945c9dee58 (patch)
treee5a4f8d2d29828ecee64c18881322a3cb7602b93 /src/Network/BitTorrent/DHT
parent6794c6843e625a3b61fec48e54167a13f5fd093b (diff)
Minor refactoring
Diffstat (limited to 'src/Network/BitTorrent/DHT')
-rw-r--r--src/Network/BitTorrent/DHT/ContactInfo.hs77
-rw-r--r--src/Network/BitTorrent/DHT/Session.hs5
2 files changed, 81 insertions, 1 deletions
diff --git a/src/Network/BitTorrent/DHT/ContactInfo.hs b/src/Network/BitTorrent/DHT/ContactInfo.hs
index 06d2dac0..028a4214 100644
--- a/src/Network/BitTorrent/DHT/ContactInfo.hs
+++ b/src/Network/BitTorrent/DHT/ContactInfo.hs
@@ -1,4 +1,6 @@
1module Network.BitTorrent.DHT.ContactInfo () where 1module Network.BitTorrent.DHT.ContactInfo
2 ( ) where
3{-
2import Data.HashMap.Strict as HM 4import Data.HashMap.Strict as HM
3 5
4import Data.Torrent.InfoHash 6import Data.Torrent.InfoHash
@@ -8,6 +10,57 @@ import Network.BitTorrent.Core
8-- decrease prefix when table is too small 10-- decrease prefix when table is too small
9-- filter outdated peers 11-- filter outdated peers
10 12
13{-----------------------------------------------------------------------
14-- PeerSet
15-----------------------------------------------------------------------}
16
17type PeerSet a = [(PeerAddr a, NodeInfo a, Timestamp)]
18
19-- compare PSQueue vs Ordered list
20
21takeNewest :: PeerSet a -> [PeerAddr a]
22takeNewest = undefined
23
24dropOld :: Timestamp -> PeerSet a -> PeerSet a
25dropOld = undefined
26
27insert :: PeerAddr a -> Timestamp -> PeerSet a -> PeerSet a
28insert = undefined
29
30type Mask = Int
31type Size = Int
32type Timestamp = Int
33
34{-----------------------------------------------------------------------
35-- InfoHashMap
36-----------------------------------------------------------------------}
37
38-- compare handwritten prefix tree versus IntMap
39
40data Tree a
41 = Nil
42 | Tip !InfoHash !(PeerSet a)
43 | Bin !InfoHash !Mask !Size !Timestamp (Tree a) (Tree a)
44
45insertTree :: InfoHash -> a -> Tree a -> Tree a
46insertTree = undefined
47
48type Prio = Int
49
50--shrink :: ContactInfo ip -> Int
51shrink Nil = Nil
52shrink (Tip _ _) = undefined
53shrink (Bin _ _) = undefined
54
55{-----------------------------------------------------------------------
56-- InfoHashMap
57-----------------------------------------------------------------------}
58
59-- compare new design versus HashMap
60
61data IntMap k p a
62type ContactInfo = Map InfoHash Timestamp (Set (PeerAddr IP) Timestamp)
63
11data ContactInfo ip = PeerStore 64data ContactInfo ip = PeerStore
12 { maxSize :: Int 65 { maxSize :: Int
13 , prefixSize :: Int 66 , prefixSize :: Int
@@ -16,3 +69,25 @@ data ContactInfo ip = PeerStore
16 , count :: Int -- ^ Cached size of the 'peerSet' 69 , count :: Int -- ^ Cached size of the 'peerSet'
17 , peerSet :: HashMap InfoHash [PeerAddr ip] 70 , peerSet :: HashMap InfoHash [PeerAddr ip]
18 } 71 }
72
73size :: ContactInfo ip -> Int
74size = undefined
75
76prefixSize :: ContactInfo ip -> Int
77prefixSize = undefined
78
79lookup :: InfoHash -> ContactInfo ip -> [PeerAddr ip]
80lookup = undefined
81
82insert :: InfoHash -> PeerAddr ip -> ContactInfo ip -> ContactInfo ip
83insert = undefined
84
85-- | Limit in size.
86prune :: NodeId -> Int -> ContactInfo ip -> ContactInfo ip
87prune pref targetSize Nil = Nil
88prune pref targetSize (Tip _ _) = undefined
89
90-- | Remove expired entries.
91splitGT :: Timestamp -> ContactInfo ip -> ContactInfo ip
92splitGT = undefined
93-} \ No newline at end of file
diff --git a/src/Network/BitTorrent/DHT/Session.hs b/src/Network/BitTorrent/DHT/Session.hs
index 755985fc..e770b1d3 100644
--- a/src/Network/BitTorrent/DHT/Session.hs
+++ b/src/Network/BitTorrent/DHT/Session.hs
@@ -434,15 +434,20 @@ insertNode info = fork $ do
434 434
435-- TODO limit dht peer store in size (probably by removing oldest peers) 435-- TODO limit dht peer store in size (probably by removing oldest peers)
436 436
437refreshContacts :: DHT ip ()
438refreshContacts = undefined
439
437-- | Insert peer to peer store. Used to handle announce requests. 440-- | Insert peer to peer store. Used to handle announce requests.
438insertPeer :: Eq ip => InfoHash -> PeerAddr ip -> DHT ip () 441insertPeer :: Eq ip => InfoHash -> PeerAddr ip -> DHT ip ()
439insertPeer ih addr = do 442insertPeer ih addr = do
443 refreshContacts
440 var <- asks contactInfo 444 var <- asks contactInfo
441 liftIO $ atomically $ modifyTVar' var (P.insert ih addr) 445 liftIO $ atomically $ modifyTVar' var (P.insert ih addr)
442 446
443-- | Get peer set for specific swarm. 447-- | Get peer set for specific swarm.
444lookupPeers :: InfoHash -> DHT ip [PeerAddr ip] 448lookupPeers :: InfoHash -> DHT ip [PeerAddr ip]
445lookupPeers ih = do 449lookupPeers ih = do
450 refreshContacts
446 var <- asks contactInfo 451 var <- asks contactInfo
447 liftIO $ P.lookup ih <$> readTVarIO var 452 liftIO $ P.lookup ih <$> readTVarIO var
448 453