diff options
author | joe <joe@jerkface.net> | 2017-06-21 22:34:40 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-06-21 22:34:40 -0400 |
commit | 012d138b1061d967ef3a05dfb7dc819d199b3902 (patch) | |
tree | 1f8929792a6d7120983087b17528e0eb9da480f6 /src/Network/BitTorrent/DHT/Query.hs | |
parent | 89c45d3ca6b5e5a0bb65c74111f0f2fdff4445af (diff) |
Propogated the deletion of MonadKRPC to Network.BitTorrent.DHT.Query.
Diffstat (limited to 'src/Network/BitTorrent/DHT/Query.hs')
-rw-r--r-- | src/Network/BitTorrent/DHT/Query.hs | 73 |
1 files changed, 39 insertions, 34 deletions
diff --git a/src/Network/BitTorrent/DHT/Query.hs b/src/Network/BitTorrent/DHT/Query.hs index 68c67900..254b347c 100644 --- a/src/Network/BitTorrent/DHT/Query.hs +++ b/src/Network/BitTorrent/DHT/Query.hs | |||
@@ -80,6 +80,7 @@ import Text.PrettyPrint as PP hiding ((<>), ($$)) | |||
80 | import Text.PrettyPrint.HughesPJClass hiding ((<>),($$)) | 80 | import Text.PrettyPrint.HughesPJClass hiding ((<>),($$)) |
81 | import Data.Time | 81 | import Data.Time |
82 | import Data.Time.Clock.POSIX | 82 | import Data.Time.Clock.POSIX |
83 | import Data.Hashable (Hashable) | ||
83 | 84 | ||
84 | import Network.DatagramServer as KRPC hiding (Options, def) | 85 | import Network.DatagramServer as KRPC hiding (Options, def) |
85 | import Network.KRPC.Method as KRPC | 86 | import Network.KRPC.Method as KRPC |
@@ -109,13 +110,9 @@ import Control.Monad.Trans.Control | |||
109 | 110 | ||
110 | nodeHandler :: ( Address ip | 111 | nodeHandler :: ( Address ip |
111 | , KRPC (Query a) (Response b) | 112 | , KRPC (Query a) (Response b) |
112 | #ifdef VERSION_bencoding | 113 | ) |
113 | , KRPC.Envelope (Query a) (Response b) ~ BValue ) | 114 | => (NodeInfo KMessageOf ip () -> Maybe ReflectedIP -> IO ()) -> (NodeAddr ip -> IO (NodeId KMessageOf)) -> (Char -> String -> Text -> IO ()) -> QueryMethod KMessageOf -> (NodeAddr ip -> a -> IO b) -> NodeHandler |
114 | #else | 115 | nodeHandler insertNode myNodeIdAccordingTo logm method action = handler method $ \ sockAddr qry -> do |
115 | , KPRC.Envelope (Query a) (Response b) ~ ByteString ) | ||
116 | #endif | ||
117 | => QueryMethod KMessageOf -> (NodeAddr ip -> a -> DHT ip b) -> NodeHandler ip | ||
118 | nodeHandler method action = handler method $ \ sockAddr qry -> do | ||
119 | #ifdef VERSION_bencoding | 116 | #ifdef VERSION_bencoding |
120 | let remoteId = queringNodeId qry | 117 | let remoteId = queringNodeId qry |
121 | read_only = queryIsReadOnly qry | 118 | read_only = queryIsReadOnly qry |
@@ -131,53 +128,55 @@ nodeHandler method action = handler method $ \ sockAddr qry -> do | |||
131 | let ni = NodeInfo remoteId naddr () | 128 | let ni = NodeInfo remoteId naddr () |
132 | -- Do not route read-only nodes. (bep 43) | 129 | -- Do not route read-only nodes. (bep 43) |
133 | if read_only | 130 | if read_only |
134 | then $(logWarnS) "nodeHandler" $ "READ-ONLY " <> T.pack (show $ pPrint ni) | 131 | then logm 'W' "nodeHandler" $ "READ-ONLY " <> T.pack (show $ pPrint ni) |
135 | else insertNode ni Nothing >> return () -- TODO need to block. why? | 132 | else insertNode ni Nothing >> return () -- TODO need to block. why? |
136 | Response | 133 | Response |
137 | <$> myNodeIdAccordingTo naddr | 134 | <$> myNodeIdAccordingTo naddr |
138 | <*> action naddr q | 135 | <*> action naddr q |
139 | 136 | ||
140 | -- | Default 'Ping' handler. | 137 | -- | Default 'Ping' handler. |
141 | pingH :: Address ip => NodeHandler ip | 138 | pingH :: NodeAddr ip -> Ping -> IO Ping |
142 | #ifdef VERSION_bencoding | 139 | pingH _ Ping = return Ping |
143 | pingH = nodeHandler "ping" $ \ _ Ping -> return Ping | 140 | -- pingH = nodeHandler $ \ _ p@PingPayload{} -> return p { isPong = True } |
144 | #else | ||
145 | pingH = nodeHandler $ \ _ p@PingPayload{} -> return p { isPong = True } | ||
146 | #endif | ||
147 | 141 | ||
148 | -- | Default 'FindNode' handler. | 142 | -- | Default 'FindNode' handler. |
149 | findNodeH :: Address ip => NodeHandler ip | 143 | findNodeH :: (NodeId KMessageOf -> IO [NodeInfo KMessageOf ip ()]) -> NodeAddr ip -> FindNode ip -> IO (NodeFound ip) |
150 | findNodeH = nodeHandler "find-nodes" $ \ _ (FindNode nid) -> do | 144 | findNodeH getclosest _ (FindNode nid) = NodeFound <$> getclosest nid |
151 | NodeFound <$> getClosest nid | ||
152 | 145 | ||
153 | #ifdef VERSION_bencoding | ||
154 | -- | Default 'GetPeers' handler. | 146 | -- | Default 'GetPeers' handler. |
155 | getPeersH :: Ord ip => Address ip => NodeHandler ip | 147 | getPeersH :: Hashable ip => (InfoHash -> IO (PeerList ip)) -> TVar SessionTokens -> NodeAddr ip -> GetPeers ip -> IO (GotPeers ip) |
156 | getPeersH = nodeHandler "get_peers" $ \ naddr (GetPeers ih) -> do | 148 | getPeersH getPeerList toks naddr (GetPeers ih) = do |
157 | ps <- getPeerList ih | 149 | ps <- getPeerList ih |
158 | tok <- grantToken naddr | 150 | tok <- grantToken toks naddr |
159 | return $ GotPeers ps tok | 151 | return $ GotPeers ps tok |
160 | 152 | ||
161 | -- | Default 'Announce' handler. | 153 | -- | Default 'Announce' handler. |
162 | announceH :: Ord ip => Address ip => NodeHandler ip | 154 | announceH :: ( Ord ip, Hashable ip ) => TVar (PeerStore ip) -> TVar SessionTokens -> NodeAddr ip -> Announce -> IO Announced |
163 | announceH = nodeHandler "announce_peer" $ \ naddr @ NodeAddr {..} (Announce {..}) -> do | 155 | announceH peers toks naddr @ NodeAddr {..} (Announce {..}) = do |
164 | valid <- checkToken naddr sessionToken | 156 | valid <- checkToken toks naddr sessionToken |
165 | unless valid $ do | 157 | unless valid $ do |
166 | throwIO $ InvalidParameter "token" | 158 | throwIO $ InvalidParameter "token" |
167 | 159 | ||
168 | let annPort = if impliedPort then nodePort else port | 160 | let annPort = if impliedPort then nodePort else port |
169 | peerAddr = PeerAddr Nothing nodeHost annPort | 161 | peerAddr = PeerAddr Nothing nodeHost annPort |
170 | insertPeer topic announcedName peerAddr | 162 | insertPeer peers topic announcedName peerAddr |
171 | return Announced | 163 | return Announced |
172 | 164 | ||
173 | -- | Includes all default query handlers. | 165 | -- | Includes all default query handlers. |
174 | defaultHandlers :: Ord ip => Address ip => [NodeHandler ip] | 166 | defaultHandlers :: forall ip. (Eq ip, Ord ip, Address ip) => LogFun -> DHT ip [NodeHandler] |
175 | defaultHandlers = [pingH, findNodeH, getPeersH, announceH] | 167 | defaultHandlers logger = do |
176 | #else | 168 | groknode <- insertNode1 |
177 | -- | Includes all default query handlers. | 169 | toks <- asks sessionTokens |
178 | defaultHandlers :: Ord ip => Address ip => [NodeHandler ip] | 170 | getclosest <- getClosest1 |
179 | defaultHandlers = [pingH, findNodeH] | 171 | mynid <- myNodeIdAccordingTo1 |
180 | #endif | 172 | peers <- asks contactInfo |
173 | getpeers <- getPeerList1 | ||
174 | let handler :: KRPC (Query a) (Response b) => QueryMethod KMessageOf -> (NodeAddr ip -> a -> IO b) -> NodeHandler | ||
175 | handler = nodeHandler groknode mynid (logt logger) | ||
176 | return [ handler "ping" $ pingH | ||
177 | , handler "find-nodes" $ findNodeH getclosest | ||
178 | , handler "get_peers" $ getPeersH getpeers toks | ||
179 | , handler "announce_peer" $ announceH peers toks ] | ||
181 | 180 | ||
182 | {----------------------------------------------------------------------- | 181 | {----------------------------------------------------------------------- |
183 | -- Basic queries | 182 | -- Basic queries |
@@ -324,6 +323,11 @@ logc c = $(logInfoS) "insertNode" . T.pack . (c :) . (':' :) | |||
324 | -- routing table. | 323 | -- routing table. |
325 | insertNode :: forall ip. Address ip => NodeInfo KMessageOf ip () -> Maybe ReflectedIP -> DHT ip () | 324 | insertNode :: forall ip. Address ip => NodeInfo KMessageOf ip () -> Maybe ReflectedIP -> DHT ip () |
326 | insertNode info witnessed_ip0 = do | 325 | insertNode info witnessed_ip0 = do |
326 | f <- insertNode1 | ||
327 | liftIO $ f info witnessed_ip0 | ||
328 | |||
329 | insertNode1 :: forall ip. Address ip => DHT ip (NodeInfo KMessageOf ip () -> Maybe ReflectedIP -> IO ()) | ||
330 | insertNode1 = do | ||
327 | bc <- optBucketCount <$> asks options | 331 | bc <- optBucketCount <$> asks options |
328 | nid <- asks tentativeNodeId | 332 | nid <- asks tentativeNodeId |
329 | logm0 <- embed_ (uncurry logc) | 333 | logm0 <- embed_ (uncurry logc) |
@@ -349,7 +353,7 @@ insertNode info witnessed_ip0 = do | |||
349 | , grokNode = DHT.insertNode params state | 353 | , grokNode = DHT.insertNode params state |
350 | , grokAddress = \_ _ -> return () -- :: Maybe SockAddr -> ReflectedIP -> IO () | 354 | , grokAddress = \_ _ -> return () -- :: Maybe SockAddr -> ReflectedIP -> IO () |
351 | } | 355 | } |
352 | liftIO $ DHT.insertNode params state info witnessed_ip0 | 356 | return $ \info witnessed_ip0 -> DHT.insertNode params state info witnessed_ip0 |
353 | 357 | ||
354 | -- | Throws exception if node is not responding. | 358 | -- | Throws exception if node is not responding. |
355 | queryNode :: forall a b ip. Address ip => KRPC (Query a) (Response b) | 359 | queryNode :: forall a b ip. Address ip => KRPC (Query a) (Response b) |
@@ -362,7 +366,8 @@ queryNode' addr q = do | |||
362 | nid <- myNodeIdAccordingTo addr | 366 | nid <- myNodeIdAccordingTo addr |
363 | let read_only = False -- TODO: check for NAT issues. (BEP 43) | 367 | let read_only = False -- TODO: check for NAT issues. (BEP 43) |
364 | let KRPC.Method name = KRPC.method :: KRPC.Method (Query a) (Response b) | 368 | let KRPC.Method name = KRPC.method :: KRPC.Method (Query a) (Response b) |
365 | (Response remoteId r, witnessed_ip) <- query' name (toSockAddr addr) (Query nid read_only q) | 369 | mgr <- asks manager |
370 | (Response remoteId r, witnessed_ip) <- liftIO $ query' mgr name (toSockAddr addr) (Query nid read_only q) | ||
366 | -- \$(logDebugS) "queryNode" $ "Witnessed address: " <> T.pack (show witnessed_ip) | 371 | -- \$(logDebugS) "queryNode" $ "Witnessed address: " <> T.pack (show witnessed_ip) |
367 | -- <> " by " <> T.pack (show (toSockAddr addr)) | 372 | -- <> " by " <> T.pack (show (toSockAddr addr)) |
368 | _ <- insertNode (NodeInfo remoteId addr ()) witnessed_ip | 373 | _ <- insertNode (NodeInfo remoteId addr ()) witnessed_ip |