diff options
author | joe <joe@jerkface.net> | 2017-06-08 00:00:56 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-06-08 00:00:56 -0400 |
commit | d6fac9a8df0ce872ede54d6a71ca6d6c750eadc9 (patch) | |
tree | c4a7cd804714796bc918091ebb29f4ad4009a401 /src/Network/RPC.hs | |
parent | 05345c643d0bcebe17f9474d9561da6e90fff34e (diff) |
WIP: Adapting DHT to Tox network (part 5).
Diffstat (limited to 'src/Network/RPC.hs')
-rw-r--r-- | src/Network/RPC.hs | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/src/Network/RPC.hs b/src/Network/RPC.hs index 727422fd..7fb0e571 100644 --- a/src/Network/RPC.hs +++ b/src/Network/RPC.hs | |||
@@ -1,16 +1,22 @@ | |||
1 | {-# LANGUAGE ConstraintKinds #-} | 1 | {-# LANGUAGE ConstraintKinds #-} |
2 | {-# LANGUAGE DeriveDataTypeable #-} | ||
3 | {-# LANGUAGE FlexibleContexts #-} | ||
2 | {-# LANGUAGE FunctionalDependencies #-} | 4 | {-# LANGUAGE FunctionalDependencies #-} |
3 | {-# LANGUAGE MultiParamTypeClasses #-} | 5 | {-# LANGUAGE MultiParamTypeClasses #-} |
4 | {-# LANGUAGE RankNTypes #-} | 6 | {-# LANGUAGE RankNTypes #-} |
5 | {-# LANGUAGE ScopedTypeVariables #-} | 7 | {-# LANGUAGE ScopedTypeVariables #-} |
6 | {-# LANGUAGE TypeFamilies #-} | 8 | {-# LANGUAGE TypeFamilies #-} |
7 | {-# LANGUAGE DeriveDataTypeable #-} | ||
8 | module Network.RPC where | 9 | module Network.RPC where |
9 | 10 | ||
11 | import Data.Bits | ||
10 | import Data.ByteString (ByteString) | 12 | import Data.ByteString (ByteString) |
11 | import Data.Kind (Constraint) | 13 | import Data.Kind (Constraint) |
12 | import Data.Data | 14 | import Data.Data |
13 | import Network.Socket | 15 | import Network.Socket |
16 | import Text.PrettyPrint.HughesPJClass hiding (($$), (<>)) | ||
17 | import Data.Serialize as S | ||
18 | import qualified Data.ByteString.Char8 as Char8 | ||
19 | import Data.ByteString.Base16 as Base16 | ||
14 | 20 | ||
15 | data MessageClass = Error | Query | Response | 21 | data MessageClass = Error | Query | Response |
16 | deriving (Eq,Ord,Enum,Bounded,Data,Show,Read) | 22 | deriving (Eq,Ord,Enum,Bounded,Data,Show,Read) |
@@ -36,6 +42,22 @@ class Envelope envelope where | |||
36 | -- Returns: response message envelope | 42 | -- Returns: response message envelope |
37 | buildReply :: NodeId envelope -> SockAddr -> envelope a -> b -> envelope b | 43 | buildReply :: NodeId envelope -> SockAddr -> envelope a -> b -> envelope b |
38 | 44 | ||
45 | -- | In Kademlia, the distance metric is XOR and the result is | ||
46 | -- interpreted as an unsigned integer. | ||
47 | newtype NodeDistance nodeid = NodeDistance nodeid | ||
48 | deriving (Eq, Ord) | ||
49 | |||
50 | -- | distance(A,B) = |A xor B| Smaller values are closer. | ||
51 | distance :: Bits nid => nid -> nid -> NodeDistance nid | ||
52 | distance a b = NodeDistance $ xor a b | ||
53 | |||
54 | instance Serialize nodeid => Show (NodeDistance nodeid) where | ||
55 | show (NodeDistance w) = Char8.unpack $ Base16.encode $ S.encode w | ||
56 | |||
57 | instance Serialize nodeid => Pretty (NodeDistance nodeid) where | ||
58 | pPrint n = text $ show n | ||
59 | |||
60 | |||
39 | class Envelope envelope => WireFormat raw envelope where | 61 | class Envelope envelope => WireFormat raw envelope where |
40 | type SerializableTo raw :: * -> Constraint | 62 | type SerializableTo raw :: * -> Constraint |
41 | type CipherContext raw envelope | 63 | type CipherContext raw envelope |