From 84798bfef62a001ded1bd628d846612f0b0ade80 Mon Sep 17 00:00:00 2001 From: joe Date: Thu, 8 Jun 2017 23:26:30 -0400 Subject: Generalized Network.DatagramServer --- src/Network/DHT/Mainline.hs | 71 +-------------------------------------------- 1 file changed, 1 insertion(+), 70 deletions(-) (limited to 'src/Network/DHT') diff --git a/src/Network/DHT/Mainline.hs b/src/Network/DHT/Mainline.hs index 7b3d6d55..2ecb9845 100644 --- a/src/Network/DHT/Mainline.hs +++ b/src/Network/DHT/Mainline.hs @@ -24,46 +24,12 @@ import Data.LargeWord import Data.Serialize as S import Data.String import Data.Typeable +import Network.DatagramServer.Mainline (NodeId(..)) import Network.DatagramServer.Mainline as KRPC import Network.DatagramServer.Types as RPC import Text.PrettyPrint as PP hiding ((<>)) import Text.PrettyPrint.HughesPJClass hiding (($$), (<>)) -nodeIdSize = finiteBitSize (undefined :: NodeId KMessageOf) `div` 8 - -instance BEncode (NodeId KMessageOf) where - toBEncode (NodeId w) = toBEncode $ S.encode w - fromBEncode bval = fromBEncode bval >>= S.decode - --- instance BEncode NodeId where TODO - --- TODO: put this somewhere appropriate -instance (Serialize a, Serialize b) => Serialize (LargeKey a b) where - put (LargeKey lo hi) = put hi >> put lo - get = flip LargeKey <$> get <*> get - -instance Serialize (NodeId KMessageOf) where - get = NodeId <$> get - {-# INLINE get #-} - put (NodeId bs) = put bs - {-# INLINE put #-} - --- | ASCII encoded. -instance IsString (NodeId KMessageOf) where - fromString str - | length str == nodeIdSize = NodeId (either error id $ S.decode (fromString str :: ByteString)) - | length str == 2 * nodeIdSize = NodeId (either error id $ S.decode (fst $ Base16.decode $ fromString str)) - | otherwise = error "fromString: invalid NodeId length" - {-# INLINE fromString #-} - --- | Meaningless node id, for testing purposes only. -instance Default (NodeId KMessageOf) where - def = NodeId 0 - --- | base16 encoded. -instance Pretty (NodeId KMessageOf) where - pPrint (NodeId nid) = PP.text $ Char8.unpack $ Base16.encode $ S.encode nid - -- | KRPC 'compact list' compatible encoding: contact information for -- nodes is encoded as a 26-byte string. Also known as "Compact node -- info" the 20-byte Node ID in network byte order has the compact @@ -115,38 +81,3 @@ bep42 addr (NodeId r) -instance Envelope KMessageOf where - type TransactionID KMessageOf = KRPC.TransactionId - - -- | Each node has a globally unique identifier known as the \"node - -- ID.\" - -- - -- Normally, /this/ node id should be saved between invocations - -- of the client software. - newtype NodeId KMessageOf = NodeId Word160 - deriving (Show, Eq, Ord, Typeable, Bits, FiniteBits) - - envelopePayload (Q q) = queryArgs q - envelopePayload (R r) = respVals r - envelopePayload (E _) = error "TODO: messagePayload for KError" - - envelopeTransaction (Q q) = queryId q - envelopeTransaction (R r) = respId r - envelopeTransaction (E e) = errorId e - - envelopeClass (Q _) = Query - envelopeClass (R _) = Response - envelopeClass (E _) = Error - - buildReply self addr qry response = - (R (KResponse response (envelopeTransaction qry) (Just $ ReflectedIP addr))) - -instance WireFormat BValue KMessageOf where - type SerializableTo BValue = BEncode - type CipherContext BValue KMessageOf = () - - decodeHeaders _ bs = BE.decode bs >>= BE.fromBEncode - decodePayload kmsg = mapM BE.fromBEncode kmsg - - encodeHeaders _ kmsg = L.toStrict $ BE.encode kmsg - encodePayload msg = fmap BE.toBEncode msg -- cgit v1.2.3