summaryrefslogtreecommitdiff
path: root/src/Network/DHT/Mainline.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-06-07 05:57:20 -0400
committerjoe <joe@jerkface.net>2017-06-07 05:57:20 -0400
commit05345c643d0bcebe17f9474d9561da6e90fff34e (patch)
treec3ad0c1dd86a376b8c177fda57d5ef835e4efdf5 /src/Network/DHT/Mainline.hs
parenta4fe28f0cf95da88f5c2db4e3397c227625aa6ac (diff)
WIP: Adapting DHT to Tox network (part 4).
Diffstat (limited to 'src/Network/DHT/Mainline.hs')
-rw-r--r--src/Network/DHT/Mainline.hs48
1 files changed, 33 insertions, 15 deletions
diff --git a/src/Network/DHT/Mainline.hs b/src/Network/DHT/Mainline.hs
index 7cd33c0d..540b74f9 100644
--- a/src/Network/DHT/Mainline.hs
+++ b/src/Network/DHT/Mainline.hs
@@ -1,22 +1,40 @@
1{-# LANGUAGE LambdaCase #-} 1{-# LANGUAGE LambdaCase #-}
2{-# LANGUAGE MultiParamTypeClasses #-}
3{-# LANGUAGE TypeFamilies #-}
2module Network.DHT.Mainline where 4module Network.DHT.Mainline where
3 5
6import Network.Socket
4import Network.RPC 7import Network.RPC
5import Network.KRPC.Message 8import Network.KRPC.Message as KRPC
6import Data.BEncode as BE 9import Data.BEncode as BE
7import qualified Data.ByteString.Lazy as L 10import qualified Data.ByteString.Lazy as L
11import Network.BitTorrent.Address as BT (NodeId)
8 12
9mainline :: Messaging KMessageOf TransactionId BValue 13instance Envelope KMessageOf where
10mainline = Messaging 14 type TransactionID KMessageOf = KRPC.TransactionId
11 { messageClass = \case Q _ -> Query 15 type NodeId KMessageOf = BT.NodeId
12 R _ -> Response 16
13 E _ -> Error 17 envelopePayload (Q q) = queryArgs q
14 , messageTransaction = \case Q q -> queryId q 18 envelopePayload (R r) = respVals r
15 R r -> respId r 19 envelopePayload (E _) = error "TODO: messagePayload for KError"
16 E e -> errorId e 20
17 , messagePayload = \case Q q -> queryArgs q 21 envelopeTransaction (Q q) = queryId q
18 R r -> respVals r 22 envelopeTransaction (R r) = respId r
19 E e -> error "TODO: messagePayload for KError" 23 envelopeTransaction (E e) = errorId e
20 , encodePayload = fmap (L.toStrict . BE.encode) 24
21 , decodePayload = sequence . fmap BE.decode 25 envelopeClass (Q _) = Query
22 } 26 envelopeClass (R _) = Response
27 envelopeClass (E _) = Error
28
29 buildReply self addr qry response =
30 (R (KResponse response (envelopeTransaction qry) (Just $ ReflectedIP addr)))
31
32instance WireFormat BValue KMessageOf where
33 type SerializableTo BValue = BEncode
34 type CipherContext BValue KMessageOf = ()
35
36 decodeHeaders _ bs = BE.decode bs >>= BE.fromBEncode
37 decodePayload kmsg = mapM BE.fromBEncode kmsg
38
39 encodeHeaders _ kmsg = L.toStrict $ BE.encode kmsg
40 encodePayload msg = fmap BE.toBEncode msg