summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-09-01 23:42:32 -0400
committerjoe <joe@jerkface.net>2017-09-01 23:42:32 -0400
commit6e82103d0e7f87127bb5b3f1f395e1d5b7adb1e0 (patch)
tree2157a9ce679ab540902bbf658ae0785dc215a4dd
parent52b32cc5d67723d4285610f21a240cf4d0b3a2b0 (diff)
handlers functino for DHT transport
-rw-r--r--DHTHandlers.hs43
1 files changed, 38 insertions, 5 deletions
diff --git a/DHTHandlers.hs b/DHTHandlers.hs
index d98a5e60..41a4bc06 100644
--- a/DHTHandlers.hs
+++ b/DHTHandlers.hs
@@ -1,4 +1,5 @@
1{-# LANGUAGE PatternSynonyms #-} 1{-# LANGUAGE PatternSynonyms #-}
2{-# LANGUAGE TupleSections #-}
2module DHTHandlers where 3module DHTHandlers where
3 4
4import DHTTransport 5import DHTTransport
@@ -26,7 +27,7 @@ data TransactionId = TransactionId
26 , cryptoNonce :: Nonce24 -- ^ Used during the encryption layer. 27 , cryptoNonce :: Nonce24 -- ^ Used during the encryption layer.
27 } 28 }
28 29
29classify :: DHTMessage ((,) Nonce8) -> MessageClass String PacketKind TransactionId 30classify :: Message -> MessageClass String PacketKind TransactionId
30classify msg = mapMessage (\nonce24 (nonce8,_) -> go msg (TransactionId nonce8 nonce24)) msg 31classify msg = mapMessage (\nonce24 (nonce8,_) -> go msg (TransactionId nonce8 nonce24)) msg
31 where 32 where
32 go (DHTPing {}) = IsQuery PingType 33 go (DHTPing {}) = IsQuery PingType
@@ -47,6 +48,33 @@ data Routing = Routing
47 , committee6 :: TriadCommittee NodeId SockAddr 48 , committee6 :: TriadCommittee NodeId SockAddr
48 } 49 }
49 50
51newRouting :: SockAddr -> TransportCrypto
52 -> (TVar (R.BucketList NodeInfo) -> SockAddr -> STM ()) -- ^ invoked on IPv4 change
53 -> (TVar (R.BucketList NodeInfo) -> SockAddr -> STM ()) -- ^ invoked on IPv6 change
54 -> IO Routing
55newRouting addr crypto update4 update6 = do
56 let tentative_ip4 = fromMaybe (IPv4 $ toEnum 0) (IPv4 <$> fromSockAddr addr)
57 tentative_ip6 = fromMaybe (IPv6 $ toEnum 0) (IPv6 <$> fromSockAddr addr)
58 tentative_info = NodeInfo
59 { nodeId = key2id $ transportPublic crypto
60 , nodeIP = fromMaybe (toEnum 0) (fromSockAddr addr)
61 , nodePort = fromMaybe 0 $ sockAddrPort addr
62 }
63 tentative_info4 = tentative_info { nodeIP = tentative_ip4 }
64 tentative_info6 <-
65 maybe (tentative_info { nodeIP = tentative_ip6 })
66 (\ip6 -> tentative_info { nodeIP = IPv6 ip6 })
67 <$> global6
68 atomically $ do
69 let nobkts = R.defaultBucketCount :: Int
70 tbl4 <- newTVar $ R.nullTable (comparing nodeId) (\s -> hashWithSalt s . nodeId) tentative_info4 nobkts
71 tbl6 <- newTVar $ R.nullTable (comparing nodeId) (\s -> hashWithSalt s . nodeId) tentative_info6 nobkts
72 committee4 <- newTriadCommittee (update4 tbl4) -- $ updateIPVote tbl4 addr4
73 committee6 <- newTriadCommittee (update6 tbl6) -- $ updateIPVote tbl6 addr6
74 sched4 <- newTVar Int.empty
75 sched6 <- newTVar Int.empty
76 return $ Routing tentative_info sched4 tbl4 committee4 sched6 tbl6 committee6
77
50 78
51-- TODO: This should cover more cases 79-- TODO: This should cover more cases
52isLocal (IPv6 ip6) = (ip6 == toEnum 0) 80isLocal (IPv6 ip6) = (ip6 == toEnum 0)
@@ -96,11 +124,11 @@ type Message = DHTMessage ((,) Nonce8)
96type Client = QR.Client String PacketKind TransactionId NodeInfo Message 124type Client = QR.Client String PacketKind TransactionId NodeInfo Message
97 125
98 126
99wrapAssym :: TransactionId -> NodeInfo -> NodeInfo -> dta -> Assym (Nonce8,dta) 127wrapAssym :: TransactionId -> NodeInfo -> NodeInfo -> (Nonce8 -> dta) -> Assym dta
100wrapAssym (TransactionId n8 n24) src dst dta = Assym 128wrapAssym (TransactionId n8 n24) src dst dta = Assym
101 { senderKey = let NodeId pubkey = nodeId src in pubkey 129 { senderKey = let NodeId pubkey = nodeId src in pubkey
102 , assymNonce = n24 130 , assymNonce = n24
103 , assymData = (n8, dta) 131 , assymData = dta n8
104 } 132 }
105 133
106serializer :: PacketKind 134serializer :: PacketKind
@@ -111,7 +139,7 @@ serializer pktkind mkping mkpong = MethodSerializer
111 { methodTimeout = 5 139 { methodTimeout = 5
112 , method = pktkind 140 , method = pktkind
113 -- wrapQuery :: tid -> addr -> addr -> qry -> x 141 -- wrapQuery :: tid -> addr -> addr -> qry -> x
114 , wrapQuery = \tid src dst ping -> mkping $ wrapAssym tid src dst ping 142 , wrapQuery = \tid src dst ping -> mkping $ wrapAssym tid src dst (, ping)
115 -- unwrapResponse :: x -> b 143 -- unwrapResponse :: x -> b
116 , unwrapResponse = fmap (snd . assymData) . mkpong 144 , unwrapResponse = fmap (snd . assymData) . mkpong
117 } 145 }
@@ -180,3 +208,8 @@ transitionCommittee committee (RoutingTransition ni Stranger) = do
180 return () 208 return ()
181transitionCommittee committee _ = return $ return () 209transitionCommittee committee _ = return $ return ()
182 210
211type Handler = MethodHandler String TransactionId NodeInfo Message
212
213handlers :: Routing -> Tox.PacketKind -> Maybe Handler
214handlers routing PingType = handler PongType pingH
215handlers routing GetNodesType = handler SendNodesType $ getNodesH routing