summaryrefslogtreecommitdiff
path: root/Mainline.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-07-13 02:29:39 -0400
committerjoe <joe@jerkface.net>2017-07-13 02:29:39 -0400
commit44e3db0b2199b10112f448b42e060dd62afe63bd (patch)
tree34f718e3124e54cde819b44595c3d1b9a0817b13 /Mainline.hs
parent41a9b6cde4d087b11c95f12a015d02bf0848ca04 (diff)
Fleshed out a little more of the Mainline DHT client.
Diffstat (limited to 'Mainline.hs')
-rw-r--r--Mainline.hs27
1 files changed, 27 insertions, 0 deletions
diff --git a/Mainline.hs b/Mainline.hs
index 01d683d1..e2ab2d7f 100644
--- a/Mainline.hs
+++ b/Mainline.hs
@@ -16,6 +16,7 @@ import Data.ByteString as B
16import Data.ByteString.Lazy (toStrict) 16import Data.ByteString.Lazy (toStrict)
17import Data.Data 17import Data.Data
18import Data.IP 18import Data.IP
19import Data.Monoid
19import qualified Data.Serialize as S 20import qualified Data.Serialize as S
20import Data.Typeable 21import Data.Typeable
21import Data.Word 22import Data.Word
@@ -149,3 +150,29 @@ newClient addr = do
149 let net = layerTransport parsePacket encodePacket udp 150 let net = layerTransport parsePacket encodePacket udp
150 return net 151 return net
151 152
153classify :: Message BValue -> MessageClass String Method TransactionId
154classify (Q { msgID = tid, qryMethod = meth }) = IsQuery meth tid
155classify (R { msgID = tid }) = IsResponse tid
156
157encodePayload () tid self dest b = R (nodeId self) tid (Right $ BE.toBEncode b) (Just $ nodeAddr dest)
158
159errorPayload () tid self dest e = R (nodeId self) tid (Left e) (Just $ nodeAddr dest)
160
161decodePayload :: BEncode a => Message BValue -> Either String a
162decodePayload msg = BE.fromBEncode $ qryPayload msg
163
164handler f = Just $ MethodHandler decodePayload encodePayload f
165
166handlers :: Method -> Maybe (MethodHandler String TransactionId NodeInfo (Message BValue) ())
167handlers (Method "ping" ) = handler pingH
168handlers (Method "find_node") = error "find_node"
169handlers (Method "get_peers") = error "get_peers"
170handlers (Method meth ) = Just $ MethodHandler decodePayload errorPayload (defaultH meth)
171
172data Ping = Ping
173
174pingH :: NodeInfo -> Ping -> IO Ping
175pingH = error "pingH"
176
177defaultH :: ByteString -> NodeInfo -> BValue -> IO Error
178defaultH meth _ _ = return $ Error MethodUnknown ("Unknown method " <> meth)