diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2014-03-05 00:35:47 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2014-03-05 00:35:47 +0400 |
commit | 3c7f12c10caca196a970c0cc8b6c46945c9dee58 (patch) | |
tree | e5a4f8d2d29828ecee64c18881322a3cb7602b93 /src/Network/BitTorrent/DHT | |
parent | 6794c6843e625a3b61fec48e54167a13f5fd093b (diff) |
Minor refactoring
Diffstat (limited to 'src/Network/BitTorrent/DHT')
-rw-r--r-- | src/Network/BitTorrent/DHT/ContactInfo.hs | 77 | ||||
-rw-r--r-- | src/Network/BitTorrent/DHT/Session.hs | 5 |
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 @@ | |||
1 | module Network.BitTorrent.DHT.ContactInfo () where | 1 | module Network.BitTorrent.DHT.ContactInfo |
2 | ( ) where | ||
3 | {- | ||
2 | import Data.HashMap.Strict as HM | 4 | import Data.HashMap.Strict as HM |
3 | 5 | ||
4 | import Data.Torrent.InfoHash | 6 | import 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 | |||
17 | type PeerSet a = [(PeerAddr a, NodeInfo a, Timestamp)] | ||
18 | |||
19 | -- compare PSQueue vs Ordered list | ||
20 | |||
21 | takeNewest :: PeerSet a -> [PeerAddr a] | ||
22 | takeNewest = undefined | ||
23 | |||
24 | dropOld :: Timestamp -> PeerSet a -> PeerSet a | ||
25 | dropOld = undefined | ||
26 | |||
27 | insert :: PeerAddr a -> Timestamp -> PeerSet a -> PeerSet a | ||
28 | insert = undefined | ||
29 | |||
30 | type Mask = Int | ||
31 | type Size = Int | ||
32 | type Timestamp = Int | ||
33 | |||
34 | {----------------------------------------------------------------------- | ||
35 | -- InfoHashMap | ||
36 | -----------------------------------------------------------------------} | ||
37 | |||
38 | -- compare handwritten prefix tree versus IntMap | ||
39 | |||
40 | data Tree a | ||
41 | = Nil | ||
42 | | Tip !InfoHash !(PeerSet a) | ||
43 | | Bin !InfoHash !Mask !Size !Timestamp (Tree a) (Tree a) | ||
44 | |||
45 | insertTree :: InfoHash -> a -> Tree a -> Tree a | ||
46 | insertTree = undefined | ||
47 | |||
48 | type Prio = Int | ||
49 | |||
50 | --shrink :: ContactInfo ip -> Int | ||
51 | shrink Nil = Nil | ||
52 | shrink (Tip _ _) = undefined | ||
53 | shrink (Bin _ _) = undefined | ||
54 | |||
55 | {----------------------------------------------------------------------- | ||
56 | -- InfoHashMap | ||
57 | -----------------------------------------------------------------------} | ||
58 | |||
59 | -- compare new design versus HashMap | ||
60 | |||
61 | data IntMap k p a | ||
62 | type ContactInfo = Map InfoHash Timestamp (Set (PeerAddr IP) Timestamp) | ||
63 | |||
11 | data ContactInfo ip = PeerStore | 64 | data 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 | |||
73 | size :: ContactInfo ip -> Int | ||
74 | size = undefined | ||
75 | |||
76 | prefixSize :: ContactInfo ip -> Int | ||
77 | prefixSize = undefined | ||
78 | |||
79 | lookup :: InfoHash -> ContactInfo ip -> [PeerAddr ip] | ||
80 | lookup = undefined | ||
81 | |||
82 | insert :: InfoHash -> PeerAddr ip -> ContactInfo ip -> ContactInfo ip | ||
83 | insert = undefined | ||
84 | |||
85 | -- | Limit in size. | ||
86 | prune :: NodeId -> Int -> ContactInfo ip -> ContactInfo ip | ||
87 | prune pref targetSize Nil = Nil | ||
88 | prune pref targetSize (Tip _ _) = undefined | ||
89 | |||
90 | -- | Remove expired entries. | ||
91 | splitGT :: Timestamp -> ContactInfo ip -> ContactInfo ip | ||
92 | splitGT = 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 | ||
437 | refreshContacts :: DHT ip () | ||
438 | refreshContacts = undefined | ||
439 | |||
437 | -- | Insert peer to peer store. Used to handle announce requests. | 440 | -- | Insert peer to peer store. Used to handle announce requests. |
438 | insertPeer :: Eq ip => InfoHash -> PeerAddr ip -> DHT ip () | 441 | insertPeer :: Eq ip => InfoHash -> PeerAddr ip -> DHT ip () |
439 | insertPeer ih addr = do | 442 | insertPeer 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. |
444 | lookupPeers :: InfoHash -> DHT ip [PeerAddr ip] | 448 | lookupPeers :: InfoHash -> DHT ip [PeerAddr ip] |
445 | lookupPeers ih = do | 449 | lookupPeers 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 | ||