-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.hs
More file actions
68 lines (52 loc) · 1.62 KB
/
Copy pathNode.hs
File metadata and controls
68 lines (52 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
{-# LANGUAGE TypeSynonymInstances #-}
module Data.DHT.Node (
NodeID, nodeID,
Node, isGood, lastSeen,
nodeEq, nodeBit, nodeDist,
nodeAge, nodeBools, nodeBytes,
compactNode) where
import Data.Bits
import Data.Time.Clock (NominalDiffTime)
import Data.Time.Clock.POSIX (POSIXTime, getPOSIXTime)
import Data.LargeWord
import Data.ByteString.Lazy (ByteString)
import Data.Binary
import Data.Binary.Put
import Network.Socket
import Control.Monad
class NodeID a where
nodeID :: a -> Word160
instance (NodeID Word160) where
nodeID = id
data Node = Node {
isGood :: Bool,
lastSeen :: POSIXTime,
getNodeID :: Word160,
nodeAddress :: SockAddr
}
instance NodeID Node where
nodeID = getNodeID
nodeEq :: (NodeID a, NodeID b) => a -> b -> Bool
nodeEq a b = nodeID a == nodeID b
nodeBit :: (NodeID a) => Int -> a -> Bool
nodeBit i = (`testBit` i) . nodeID
nodeDist :: (NodeID a, NodeID b) => a -> b -> Word160
nodeDist local target = nodeID local `xor` nodeID target
nodeAge :: Node -> IO NominalDiffTime
nodeAge n = subtract (lastSeen n) `liftM` getPOSIXTime
nodeBools :: (NodeID a) => a -> [Bool]
nodeBools node = map (`nodeBit` node) $ reverse [0..159]
word160Bytes :: Word160 -> ByteString
word160Bytes word = runPut $ do
put . hiHalf . hiHalf $ word
put . loHalf . hiHalf $ word
put . loHalf $ word
nodeBytes :: (NodeID a) => a -> ByteString
nodeBytes = word160Bytes . nodeID
compactNode :: Node -> ByteString
compactNode node = runPut $ do
put $ nodeBytes node
put hostIP
put port
where (SockAddrInet (PortNum port) hostIP) = nodeAddress node
--PARTIAL, fix later