summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-07-03 18:46:14 -0400
committerjoe <joe@jerkface.net>2017-07-03 18:46:14 -0400
commit81bcffd68c9997b8e4b5f8c2d4cb1e4db4c62153 (patch)
tree123f6236b1f4d4f70f2d0fc260e5d0b28159741c /examples
parentbe15a1ceba3b87f845d3b56915207457a94394ee (diff)
Moved node-id out of Query/Response. It can be either in the
envelope (Tox) or in the query/response payload (Mainline).
Diffstat (limited to 'examples')
-rw-r--r--examples/dhtd.hs44
1 files changed, 30 insertions, 14 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs
index dd038d70..3fca5482 100644
--- a/examples/dhtd.hs
+++ b/examples/dhtd.hs
@@ -29,11 +29,12 @@ import System.IO
29import System.IO.Error 29import System.IO.Error
30import Text.PrettyPrint.HughesPJClass 30import Text.PrettyPrint.HughesPJClass
31import Text.Printf 31import Text.Printf
32import Text.Read 32import Text.Read hiding (get)
33import Control.Monad.Reader.Class 33import Control.Monad.Reader.Class
34import System.Posix.Process (getProcessID) 34import System.Posix.Process (getProcessID)
35import GHC.Stats 35import GHC.Stats
36import System.Mem 36import System.Mem
37import Data.Word
37 38
38import Data.Torrent (InfoHash) 39import Data.Torrent (InfoHash)
39import Network.Address 40import Network.Address
@@ -66,6 +67,7 @@ import Data.Serialize
66import Network.KRPC.Method 67import Network.KRPC.Method
67import Data.Typeable 68import Data.Typeable
68import GHC.Generics 69import GHC.Generics
70import Data.Bool
69 71
70mkNodeAddr :: SockAddr -> NodeAddr IPv4 72mkNodeAddr :: SockAddr -> NodeAddr IPv4
71mkNodeAddr addr = NodeAddr (fromJust $ fromSockAddr addr) 73mkNodeAddr addr = NodeAddr (fromJust $ fromSockAddr addr)
@@ -225,29 +227,43 @@ ipType _ = return ()
225 227
226instance Kademlia Tox.Message where 228instance Kademlia Tox.Message where
227 data DHTData Tox.Message ip = ToxData 229 data DHTData Tox.Message ip = ToxData
228 namePing _ = Tox.Ping 230 namePing _ = Tox.Ping
229 nameFindNodes _ = Tox.GetNodes 231 nameFindNodes _ = Tox.GetNodes
230 initializeDHTData = return ToxData 232 initializeDHTData = return ToxData
231 233
232instance Pretty (NodeId Tox.Message) where pPrint (Tox.NodeId nid) = encodeHexDoc nid 234instance Pretty (NodeId Tox.Message) where
235 pPrint (Tox.NodeId nid) = encodeHexDoc nid
236
237getToxPing isPong c n = do
238 q'r <- get :: Get Word8
239 when (bool 0 1 isPong /= q'r) $
240 fail "Tox ping/pong parse fail."
241 n8 <- get :: Get Tox.Nonce8
242 return $ c (n n8) Ping
243
244putToxPing isPong n8 = do
245 put (bool 0 1 isPong :: Word8)
246 put n8
247
248instance Serialize (Query Tox.Message (Ping Tox.Message)) where
249 get = getToxPing False Network.DHT.Types.Query Tox.QueryNonce
250 put (Network.DHT.Types.Query extra Ping) = putToxPing False (Tox.qryNonce extra)
251instance Serialize (Response Tox.Message (Ping Tox.Message)) where
252 get = getToxPing True Network.DHT.Types.Response Tox.ResponseNonce
253 put (Network.DHT.Types.Response extra Ping) = putToxPing True (Tox.rspNonce extra)
233 254
234instance Serialize (Query Tox.Message (FindNode Tox.Message IPv4)) where 255instance Serialize (Query Tox.Message (FindNode Tox.Message IPv4)) where
235 get = error "TODO get 7" 256 get = error "TODO get 7"
236 put = error "TODO put 8" 257 put = error "TODO put 8"
237instance Serialize (Query Tox.Message (Ping Tox.Message)) where -- TODO
238 get = error "TODO get 9"
239 put = error "TODO put 10"
240instance Serialize (Response Tox.Message (NodeFound Tox.Message IPv4)) where 258instance Serialize (Response Tox.Message (NodeFound Tox.Message IPv4)) where
241 get = error "TODO get 11" 259 get = error "TODO get 11"
242 put = error "TODO put 12" 260 put = error "TODO put 12"
243instance Serialize (Response Tox.Message (Ping Tox.Message)) where -- TODO 261
244 get = error "TODO get 13" 262instance KRPC Tox.Message (Query Tox.Message (FindNode Tox.Message IPv4))
245 put = error "TODO put 14" 263 (Response Tox.Message (NodeFound Tox.Message IPv4)) where
246instance KRPC Tox.Message (Query Tox.Message (FindNode Tox.Message IPv4))
247 (Response Tox.Message (NodeFound Tox.Message IPv4)) where
248 method = error "TODO method 15" 264 method = error "TODO method 15"
249instance KRPC Tox.Message (Query Tox.Message (Ping Tox.Message )) 265instance KRPC Tox.Message (Query Tox.Message (Ping Tox.Message))
250 (Response Tox.Message (Ping Tox.Message )) where 266 (Response Tox.Message (Ping Tox.Message)) where
251 method = error "TODO method 16" 267 method = error "TODO method 16"
252instance DataHandlers ByteString Tox.Message where 268instance DataHandlers ByteString Tox.Message where
253 269