summaryrefslogtreecommitdiff
path: root/src/Data/Kademlia/Common.hs
blob: 874120d897247b6af9d9a227ba903b96cc4742c5 (plain)
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
{-# OPTIONS -fno-warn-orphans #-}
{-# LANGUAGE RecordWildCards #-}
module Data.Kademlia.Common
       (NodeID, NodeInfo
       ) where

import Control.Applicative
import Data.ByteString
import Network
import Network.Socket
import Data.Serialize


type NodeID = ByteString
type Distance = NodeID

-- WARN is the 'system' random suitable for this?
-- | Generate random NodeID used for the entire session.
--   Distribution of ID's should be as uniform as possible.
--
genNodeID :: IO NodeID
genNodeID = undefined -- randomIO

instance Serialize PortNumber where
  get = fromIntegral <$> getWord16be
  put = putWord16be . fromIntegral


data NodeAddr = NodeAddr {
    nodeIP   :: HostAddress
  , nodePort :: PortNumber
  } deriving (Show, Eq)

instance Serialize NodeAddr where
  get = NodeAddr <$> getWord32be <*> get
  put NodeAddr {..} = do
    putWord32be nodeIP
    put         nodePort


data NodeInfo = NodeInfo {
    nodeID   :: NodeID
  , nodeAddr :: NodeAddr
  } deriving (Show, Eq)

instance Serialize NodeInfo where
  get = NodeInfo <$> getByteString 20 <*> get
  put NodeInfo {..} = put nodeID >> put nodeAddr