diff options
author | joe <joe@jerkface.net> | 2017-07-13 02:29:39 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-07-13 02:29:39 -0400 |
commit | 44e3db0b2199b10112f448b42e060dd62afe63bd (patch) | |
tree | 34f718e3124e54cde819b44595c3d1b9a0817b13 /Mainline.hs | |
parent | 41a9b6cde4d087b11c95f12a015d02bf0848ca04 (diff) |
Fleshed out a little more of the Mainline DHT client.
Diffstat (limited to 'Mainline.hs')
-rw-r--r-- | Mainline.hs | 27 |
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 | |||
16 | import Data.ByteString.Lazy (toStrict) | 16 | import Data.ByteString.Lazy (toStrict) |
17 | import Data.Data | 17 | import Data.Data |
18 | import Data.IP | 18 | import Data.IP |
19 | import Data.Monoid | ||
19 | import qualified Data.Serialize as S | 20 | import qualified Data.Serialize as S |
20 | import Data.Typeable | 21 | import Data.Typeable |
21 | import Data.Word | 22 | import 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 | ||
153 | classify :: Message BValue -> MessageClass String Method TransactionId | ||
154 | classify (Q { msgID = tid, qryMethod = meth }) = IsQuery meth tid | ||
155 | classify (R { msgID = tid }) = IsResponse tid | ||
156 | |||
157 | encodePayload () tid self dest b = R (nodeId self) tid (Right $ BE.toBEncode b) (Just $ nodeAddr dest) | ||
158 | |||
159 | errorPayload () tid self dest e = R (nodeId self) tid (Left e) (Just $ nodeAddr dest) | ||
160 | |||
161 | decodePayload :: BEncode a => Message BValue -> Either String a | ||
162 | decodePayload msg = BE.fromBEncode $ qryPayload msg | ||
163 | |||
164 | handler f = Just $ MethodHandler decodePayload encodePayload f | ||
165 | |||
166 | handlers :: Method -> Maybe (MethodHandler String TransactionId NodeInfo (Message BValue) ()) | ||
167 | handlers (Method "ping" ) = handler pingH | ||
168 | handlers (Method "find_node") = error "find_node" | ||
169 | handlers (Method "get_peers") = error "get_peers" | ||
170 | handlers (Method meth ) = Just $ MethodHandler decodePayload errorPayload (defaultH meth) | ||
171 | |||
172 | data Ping = Ping | ||
173 | |||
174 | pingH :: NodeInfo -> Ping -> IO Ping | ||
175 | pingH = error "pingH" | ||
176 | |||
177 | defaultH :: ByteString -> NodeInfo -> BValue -> IO Error | ||
178 | defaultH meth _ _ = return $ Error MethodUnknown ("Unknown method " <> meth) | ||