summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-06-20 04:27:20 -0400
committerjoe <joe@jerkface.net>2017-06-20 04:27:20 -0400
commit89c45d3ca6b5e5a0bb65c74111f0f2fdff4445af (patch)
tree0a7623b89d88f27b5a22e85022b01ff87f06dba9 /src
parent3cfb2bc0b18a9ecf2ae88d8f07894b77dd7c387e (diff)
Removed MonadKRPC references from Network.BitTorrent.DHT.Session
Diffstat (limited to 'src')
-rw-r--r--src/Network/BitTorrent/DHT/Session.hs42
1 files changed, 36 insertions, 6 deletions
diff --git a/src/Network/BitTorrent/DHT/Session.hs b/src/Network/BitTorrent/DHT/Session.hs
index 2d290a95..7e87df6c 100644
--- a/src/Network/BitTorrent/DHT/Session.hs
+++ b/src/Network/BitTorrent/DHT/Session.hs
@@ -86,6 +86,7 @@ import Control.Monad.Reader
86import Control.Monad.Trans.Control 86import Control.Monad.Trans.Control
87import Control.Monad.Trans.Resource 87import Control.Monad.Trans.Resource
88import Data.Typeable 88import Data.Typeable
89import Data.String
89import Data.ByteString 90import Data.ByteString
90import Data.Conduit.Lazy 91import Data.Conduit.Lazy
91import Data.Default 92import Data.Default
@@ -119,6 +120,7 @@ import qualified Network.BitTorrent.DHT.ContactInfo as P
119import Network.DHT.Mainline 120import Network.DHT.Mainline
120import Network.DHT.Routing as R 121import Network.DHT.Routing as R
121import Network.BitTorrent.DHT.Token as T 122import Network.BitTorrent.DHT.Token as T
123import GHC.Stack as GHC
122 124
123{----------------------------------------------------------------------- 125{-----------------------------------------------------------------------
124-- Options 126-- Options
@@ -268,7 +270,7 @@ data Node ip = Node
268 270
269 , resources :: !InternalState 271 , resources :: !InternalState
270#ifdef VERSION_bencoding 272#ifdef VERSION_bencoding
271 , manager :: !(Manager (DHT ip) BValue KMessageOf) -- ^ RPC manager; 273 , manager :: !(Manager IO BValue KMessageOf) -- ^ RPC manager;
272 , routingInfo :: !(TVar (Maybe (R.Info KMessageOf ip ()))) -- ^ search table; 274 , routingInfo :: !(TVar (Maybe (R.Info KMessageOf ip ()))) -- ^ search table;
273#else 275#else
274 , manager :: !(Manager (DHT ip) ByteString Tox.Message) -- ^ RPC manager; 276 , manager :: !(Manager (DHT ip) ByteString Tox.Message) -- ^ RPC manager;
@@ -320,8 +322,10 @@ instance MonadResource (DHT ip) where
320 s <- asks resources 322 s <- asks resources
321 liftIO $ runInternalState m s 323 liftIO $ runInternalState m s
322 324
323instance MonadKRPC (DHT ip) (DHT ip) BValue KMessageOf where 325-- instance MonadKRPC (DHT ip) (DHT ip) BValue KMessageOf where
324 getManager = asks manager 326
327getManager :: DHT ip (Manager IO BValue KMessageOf)
328getManager = asks manager
325 329
326instance MonadLogger (DHT ip) where 330instance MonadLogger (DHT ip) where
327 monadLoggerLog loc src lvl msg = do 331 monadLoggerLog loc src lvl msg = do
@@ -329,11 +333,37 @@ instance MonadLogger (DHT ip) where
329 liftIO $ logger loc src lvl (toLogStr msg) 333 liftIO $ logger loc src lvl (toLogStr msg)
330 334
331#ifdef VERSION_bencoding 335#ifdef VERSION_bencoding
332type NodeHandler ip = Handler (DHT ip) KMessageOf BValue 336type NodeHandler ip = Handler IO KMessageOf BValue
333#else 337#else
334type NodeHandler ip = Handler (DHT ip) Tox.Message ByteString 338type NodeHandler ip = Handler (DHT ip) Tox.Message ByteString
335#endif 339#endif
336 340
341logt :: HasCallStack => LogFun -> Char -> String -> Text -> IO ()
342logt lf c m txt = lf (locFromCS callStack) (fromString m) (lvl c) (fromString $ Text.unpack txt)
343 where
344 lvl 'D' = LevelDebug
345 lvl 'I' = LevelInfo
346 lvl 'W' = LevelWarn
347 lvl 'E' = LevelError
348 lvl ch = LevelOther $ Text.cons ch Text.empty
349
350mkLoggerLoc :: GHC.SrcLoc -> Loc
351mkLoggerLoc loc =
352 Loc { loc_filename = GHC.srcLocFile loc
353 , loc_package = GHC.srcLocPackage loc
354 , loc_module = GHC.srcLocModule loc
355 , loc_start = ( GHC.srcLocStartLine loc
356 , GHC.srcLocStartCol loc)
357 , loc_end = ( GHC.srcLocEndLine loc
358 , GHC.srcLocEndCol loc)
359 }
360
361locFromCS :: GHC.CallStack -> Loc
362locFromCS cs = case getCallStack cs of
363 ((_, loc):_) -> mkLoggerLoc loc
364 _ -> Loc "<unknown>" "<unknown>" "<unknown>" (0,0) (0,0)
365
366
337-- | Run DHT session. You /must/ properly close session using 367-- | Run DHT session. You /must/ properly close session using
338-- 'closeNode' function, otherwise socket or other scarce resources may 368-- 'closeNode' function, otherwise socket or other scarce resources may
339-- leak. 369-- leak.
@@ -357,7 +387,7 @@ newNode hs opts naddr logger mbid = do
357 nodeAddr = toSockAddr naddr 387 nodeAddr = toSockAddr naddr
358 initNode = do 388 initNode = do
359 s <- getInternalState 389 s <- getInternalState
360 (_, m) <- allocate (newManager rpcOpts nodeAddr hs) closeManager 390 (_, m) <- allocate (newManager rpcOpts (logt logger) nodeAddr hs) closeManager
361 liftIO $ do 391 liftIO $ do
362 myId <- maybe genNodeId return mbid 392 myId <- maybe genNodeId return mbid
363 node <- Node opts myId s m 393 node <- Node opts myId s m
@@ -366,7 +396,7 @@ newNode hs opts naddr logger mbid = do
366 <*> newTVarIO S.empty 396 <*> newTVarIO S.empty
367 <*> (newTVarIO =<< nullSessionTokens) 397 <*> (newTVarIO =<< nullSessionTokens)
368 <*> pure logger 398 <*> pure logger
369 runReaderT (unDHT $ KRPC.listen (KRPC.Protocol Proxy Proxy)) node 399 KRPC.listen m (KRPC.Protocol Proxy Proxy)
370 return node 400 return node
371 401
372-- | Some resources like listener thread may live for 402-- | Some resources like listener thread may live for