diff options
author | joe <joe@jerkface.net> | 2017-06-07 05:57:20 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-06-07 05:57:20 -0400 |
commit | 05345c643d0bcebe17f9474d9561da6e90fff34e (patch) | |
tree | c3ad0c1dd86a376b8c177fda57d5ef835e4efdf5 /src/Network/DHT/Mainline.hs | |
parent | a4fe28f0cf95da88f5c2db4e3397c227625aa6ac (diff) |
WIP: Adapting DHT to Tox network (part 4).
Diffstat (limited to 'src/Network/DHT/Mainline.hs')
-rw-r--r-- | src/Network/DHT/Mainline.hs | 48 |
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 #-} | ||
2 | module Network.DHT.Mainline where | 4 | module Network.DHT.Mainline where |
3 | 5 | ||
6 | import Network.Socket | ||
4 | import Network.RPC | 7 | import Network.RPC |
5 | import Network.KRPC.Message | 8 | import Network.KRPC.Message as KRPC |
6 | import Data.BEncode as BE | 9 | import Data.BEncode as BE |
7 | import qualified Data.ByteString.Lazy as L | 10 | import qualified Data.ByteString.Lazy as L |
11 | import Network.BitTorrent.Address as BT (NodeId) | ||
8 | 12 | ||
9 | mainline :: Messaging KMessageOf TransactionId BValue | 13 | instance Envelope KMessageOf where |
10 | mainline = 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 | |||
32 | instance 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 | ||