From 086e81d141c0e2435563b2b59e8065dcd27ce5a2 Mon Sep 17 00:00:00 2001 From: Sam T Date: Thu, 20 Jun 2013 23:10:06 +0400 Subject: ~ Merge kademlia package. --- src/Data/Kademlia/Common.hs | 48 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/Data/Kademlia/Common.hs (limited to 'src/Data/Kademlia/Common.hs') diff --git a/src/Data/Kademlia/Common.hs b/src/Data/Kademlia/Common.hs new file mode 100644 index 00000000..874120d8 --- /dev/null +++ b/src/Data/Kademlia/Common.hs @@ -0,0 +1,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 -- cgit v1.2.3