summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-01-27 04:34:32 -0500
committerjoe <joe@jerkface.net>2017-01-27 04:34:32 -0500
commit77a317310d4f7929335fafe3cfbf53afd45faa82 (patch)
tree220e885dfc44af98998faee7660b9e9ef7363220
parent6a239e20bbc7a84a74721d23587bbf1b8c20c704 (diff)
Switched PSQueue to psqueues for strict spine.
-rw-r--r--bittorrent.cabal4
-rw-r--r--src/Data/Wrapper/PSQ.hs41
-rw-r--r--src/Network/BitTorrent/DHT/ContactInfo.hs2
-rw-r--r--src/Network/BitTorrent/DHT/Routing.hs17
4 files changed, 52 insertions, 12 deletions
diff --git a/bittorrent.cabal b/bittorrent.cabal
index 90053932..2d76e606 100644
--- a/bittorrent.cabal
+++ b/bittorrent.cabal
@@ -90,6 +90,7 @@ library
90 Network.StreamServer 90 Network.StreamServer
91 Network.SocketLike 91 Network.SocketLike
92 other-modules: Paths_bittorrent 92 other-modules: Paths_bittorrent
93 Data.Wrapper.PSQ
93 if !flag(dht-only) 94 if !flag(dht-only)
94 exposed-modules: Network.BitTorrent 95 exposed-modules: Network.BitTorrent
95 Network.BitTorrent.Client 96 Network.BitTorrent.Client
@@ -163,7 +164,8 @@ library
163-- , data-dword 164-- , data-dword
164 , intset >= 0.1 165 , intset >= 0.1
165-- patched build: , intset == 0.1.1.10000 166-- patched build: , intset == 0.1.1.10000
166 , PSQueue >= 1.1 167 -- , PSQueue >= 1.1
168 , psqueues
167 , split >= 0.2 169 , split >= 0.2
168 , text >= 0.11.0 170 , text >= 0.11.0
169 , unordered-containers 171 , unordered-containers
diff --git a/src/Data/Wrapper/PSQ.hs b/src/Data/Wrapper/PSQ.hs
new file mode 100644
index 00000000..e8fa2d98
--- /dev/null
+++ b/src/Data/Wrapper/PSQ.hs
@@ -0,0 +1,41 @@
1{-# LANGUAGE PatternSynonyms #-}
2{-# LANGUAGE CPP #-}
3module Data.Wrapper.PSQ
4#if 0
5 ( module Data.PSQueue ) where ; import Data.PSQueue
6#else
7 ( module Data.Wrapper.PSQ , module OrdPSQ ) where
8
9import Data.OrdPSQ as OrdPSQ hiding (insert, map, singleton, minView)
10import qualified Data.OrdPSQ as OrdPSQ
11
12type PSQ k p = OrdPSQ k p ()
13type Binding k p = (k,p,())
14
15pattern (:->) :: k -> p -> Binding k p
16pattern k :-> p <- (k,p,()) where k :-> p = (k,p,())
17
18key :: Binding k v -> k
19key (k,p,v) = k
20{-# INLINE key #-}
21
22insert :: (Ord k, Ord p) => k -> p -> PSQ k p -> PSQ k p
23insert k p q = OrdPSQ.insert k p () q
24{-# INLINE insert #-}
25
26insertWith :: (Ord k, Ord p) => (p -> p -> p) -> k -> p -> PSQ k p -> PSQ k p
27insertWith f k p0 q = snd $ OrdPSQ.alter f' k q
28 where
29 f' (Just (p,())) = ((),Just (f p0 p, ()))
30 f' Nothing = ((),Nothing)
31{-# INLINE insertWith #-}
32
33singleton :: (Ord k, Ord p) => k -> p -> PSQ k p
34singleton k p = OrdPSQ.singleton k p ()
35{-# INLINE singleton #-}
36
37minView :: (Ord k, Ord p) => PSQ k p -> Maybe (Binding k p, PSQ k p)
38minView q = fmap (\(k,p,(),q') -> (k :-> p, q')) $ OrdPSQ.minView q
39{-# INLINE minView #-}
40
41#endif
diff --git a/src/Network/BitTorrent/DHT/ContactInfo.hs b/src/Network/BitTorrent/DHT/ContactInfo.hs
index ea3b96f0..26bdeae9 100644
--- a/src/Network/BitTorrent/DHT/ContactInfo.hs
+++ b/src/Network/BitTorrent/DHT/ContactInfo.hs
@@ -12,7 +12,7 @@ import Data.List as L
12import Data.Maybe 12import Data.Maybe
13import Data.HashMap.Strict as HM 13import Data.HashMap.Strict as HM
14import Data.Serialize 14import Data.Serialize
15import Data.PSQueue as PSQ 15import Data.Wrapper.PSQ as PSQ
16import Data.Time.Clock.POSIX 16import Data.Time.Clock.POSIX
17import Data.ByteString (ByteString) 17import Data.ByteString (ByteString)
18import Data.Word 18import Data.Word
diff --git a/src/Network/BitTorrent/DHT/Routing.hs b/src/Network/BitTorrent/DHT/Routing.hs
index d64e415e..3c2e30aa 100644
--- a/src/Network/BitTorrent/DHT/Routing.hs
+++ b/src/Network/BitTorrent/DHT/Routing.hs
@@ -70,7 +70,7 @@ import Data.Functor.Identity
70import Data.List as L hiding (insert) 70import Data.List as L hiding (insert)
71import Data.Maybe 71import Data.Maybe
72import Data.Monoid 72import Data.Monoid
73import Data.PSQueue as PSQ 73import Data.Wrapper.PSQ as PSQ
74import Data.Serialize as S hiding (Result, Done) 74import Data.Serialize as S hiding (Result, Done)
75import qualified Data.Sequence as Seq 75import qualified Data.Sequence as Seq
76import Data.Time 76import Data.Time
@@ -181,10 +181,6 @@ runRouting ping_node find_nodes timestamper = go
181-- | Timestamp - last time this node is pinged. 181-- | Timestamp - last time this node is pinged.
182type NodeEntry ip = Binding (NodeInfo ip) Timestamp 182type NodeEntry ip = Binding (NodeInfo ip) Timestamp
183 183
184instance (Serialize k, Serialize v) => Serialize (Binding k v) where
185 get = (:->) <$> get <*> get
186 put (k :-> v) = put k >> put v
187
188-- TODO instance Pretty where 184-- TODO instance Pretty where
189 185
190-- | Number of nodes in a bucket. 186-- | Number of nodes in a bucket.
@@ -239,12 +235,13 @@ data Bucket ip = Bucket { bktNodes :: PSQ (NodeInfo ip) Timestamp
239 , bktQ :: BucketQueue ip 235 , bktQ :: BucketQueue ip
240 } deriving (Show,Generic) 236 } deriving (Show,Generic)
241 237
242instance (Eq ip, Serialize ip) => Serialize (Bucket ip) 238instance (Eq ip, Serialize ip) => Serialize (Bucket ip) where
239 get = Bucket . psqFromPairList <$> get <*> pure (runIdentity $ emptyQueue bucketQ)
240 put = put . psqToPairList . bktNodes
241
242psqFromPairList xs = PSQ.fromList $ map (\(a,b) -> a :-> b) xs
243 243
244instance (Serialize k, Serialize v, Ord k, Ord v) 244psqToPairList psq = map (\(a :-> b) -> (a,b)) $ PSQ.toList psq
245 => Serialize (PSQ k v) where
246 get = PSQ.fromList <$> get
247 put = put . PSQ.toList
248 245
249-- | Update interval, in seconds. 246-- | Update interval, in seconds.
250delta :: NominalDiffTime 247delta :: NominalDiffTime