summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/Core
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-01-12 13:55:14 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-01-12 13:55:14 +0400
commita43a810ff24bc1ab946e5459c0f914aca286c62f (patch)
treeb716159332dd71661da49fe361dd7daf2052a778 /src/Network/BitTorrent/Core
parente3d3fb375ca01aa844e86b8a4c5ca507919518d3 (diff)
Unify all iterative queries
Diffstat (limited to 'src/Network/BitTorrent/Core')
-rw-r--r--src/Network/BitTorrent/Core/NodeInfo.hs7
1 files changed, 7 insertions, 0 deletions
diff --git a/src/Network/BitTorrent/Core/NodeInfo.hs b/src/Network/BitTorrent/Core/NodeInfo.hs
index fe2357a4..2fd7e575 100644
--- a/src/Network/BitTorrent/Core/NodeInfo.hs
+++ b/src/Network/BitTorrent/Core/NodeInfo.hs
@@ -47,6 +47,7 @@ import Data.ByteString.Base16 as Base16
47import Data.BEncode as BE 47import Data.BEncode as BE
48import Data.Default 48import Data.Default
49import Data.Hashable 49import Data.Hashable
50import Data.Foldable
50import Data.IP 51import Data.IP
51import Data.List as L 52import Data.List as L
52import Data.Monoid 53import Data.Monoid
@@ -125,6 +126,12 @@ genNodeId = NodeId <$> getEntropy nodeIdSize
125newtype NodeDistance = NodeDistance BS.ByteString 126newtype NodeDistance = NodeDistance BS.ByteString
126 deriving (Eq, Ord) 127 deriving (Eq, Ord)
127 128
129instance Pretty NodeDistance where
130 pretty (NodeDistance bs) = foldMap bitseq $ BS.unpack bs
131 where
132 listBits w = L.map (testBit w) (L.reverse [0..bitSize w - 1])
133 bitseq = foldMap (int . fromEnum) . listBits
134
128-- | distance(A,B) = |A xor B| Smaller values are closer. 135-- | distance(A,B) = |A xor B| Smaller values are closer.
129distance :: NodeId -> NodeId -> NodeDistance 136distance :: NodeId -> NodeId -> NodeDistance
130distance (NodeId a) (NodeId b) = NodeDistance (BS.pack (BS.zipWith xor a b)) 137distance (NodeId a) (NodeId b) = NodeDistance (BS.pack (BS.zipWith xor a b))