diff options
author | joe <joe@jerkface.net> | 2017-07-03 18:46:14 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-07-03 18:46:14 -0400 |
commit | 81bcffd68c9997b8e4b5f8c2d4cb1e4db4c62153 (patch) | |
tree | 123f6236b1f4d4f70f2d0fc260e5d0b28159741c /examples | |
parent | be15a1ceba3b87f845d3b56915207457a94394ee (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.hs | 44 |
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 | |||
29 | import System.IO.Error | 29 | import System.IO.Error |
30 | import Text.PrettyPrint.HughesPJClass | 30 | import Text.PrettyPrint.HughesPJClass |
31 | import Text.Printf | 31 | import Text.Printf |
32 | import Text.Read | 32 | import Text.Read hiding (get) |
33 | import Control.Monad.Reader.Class | 33 | import Control.Monad.Reader.Class |
34 | import System.Posix.Process (getProcessID) | 34 | import System.Posix.Process (getProcessID) |
35 | import GHC.Stats | 35 | import GHC.Stats |
36 | import System.Mem | 36 | import System.Mem |
37 | import Data.Word | ||
37 | 38 | ||
38 | import Data.Torrent (InfoHash) | 39 | import Data.Torrent (InfoHash) |
39 | import Network.Address | 40 | import Network.Address |
@@ -66,6 +67,7 @@ import Data.Serialize | |||
66 | import Network.KRPC.Method | 67 | import Network.KRPC.Method |
67 | import Data.Typeable | 68 | import Data.Typeable |
68 | import GHC.Generics | 69 | import GHC.Generics |
70 | import Data.Bool | ||
69 | 71 | ||
70 | mkNodeAddr :: SockAddr -> NodeAddr IPv4 | 72 | mkNodeAddr :: SockAddr -> NodeAddr IPv4 |
71 | mkNodeAddr addr = NodeAddr (fromJust $ fromSockAddr addr) | 73 | mkNodeAddr addr = NodeAddr (fromJust $ fromSockAddr addr) |
@@ -225,29 +227,43 @@ ipType _ = return () | |||
225 | 227 | ||
226 | instance Kademlia Tox.Message where | 228 | instance 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 | ||
232 | instance Pretty (NodeId Tox.Message) where pPrint (Tox.NodeId nid) = encodeHexDoc nid | 234 | instance Pretty (NodeId Tox.Message) where |
235 | pPrint (Tox.NodeId nid) = encodeHexDoc nid | ||
236 | |||
237 | getToxPing 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 | |||
244 | putToxPing isPong n8 = do | ||
245 | put (bool 0 1 isPong :: Word8) | ||
246 | put n8 | ||
247 | |||
248 | instance 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) | ||
251 | instance 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 | ||
234 | instance Serialize (Query Tox.Message (FindNode Tox.Message IPv4)) where | 255 | instance 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" |
237 | instance Serialize (Query Tox.Message (Ping Tox.Message)) where -- TODO | ||
238 | get = error "TODO get 9" | ||
239 | put = error "TODO put 10" | ||
240 | instance Serialize (Response Tox.Message (NodeFound Tox.Message IPv4)) where | 258 | instance 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" |
243 | instance Serialize (Response Tox.Message (Ping Tox.Message)) where -- TODO | 261 | |
244 | get = error "TODO get 13" | 262 | instance 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 |
246 | instance 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" |
249 | instance KRPC Tox.Message (Query Tox.Message (Ping Tox.Message )) | 265 | instance 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" |
252 | instance DataHandlers ByteString Tox.Message where | 268 | instance DataHandlers ByteString Tox.Message where |
253 | 269 | ||