diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Network/BitTorrent/DHT/Session.hs | 42 |
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 | |||
86 | import Control.Monad.Trans.Control | 86 | import Control.Monad.Trans.Control |
87 | import Control.Monad.Trans.Resource | 87 | import Control.Monad.Trans.Resource |
88 | import Data.Typeable | 88 | import Data.Typeable |
89 | import Data.String | ||
89 | import Data.ByteString | 90 | import Data.ByteString |
90 | import Data.Conduit.Lazy | 91 | import Data.Conduit.Lazy |
91 | import Data.Default | 92 | import Data.Default |
@@ -119,6 +120,7 @@ import qualified Network.BitTorrent.DHT.ContactInfo as P | |||
119 | import Network.DHT.Mainline | 120 | import Network.DHT.Mainline |
120 | import Network.DHT.Routing as R | 121 | import Network.DHT.Routing as R |
121 | import Network.BitTorrent.DHT.Token as T | 122 | import Network.BitTorrent.DHT.Token as T |
123 | import 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 | ||
323 | instance MonadKRPC (DHT ip) (DHT ip) BValue KMessageOf where | 325 | -- instance MonadKRPC (DHT ip) (DHT ip) BValue KMessageOf where |
324 | getManager = asks manager | 326 | |
327 | getManager :: DHT ip (Manager IO BValue KMessageOf) | ||
328 | getManager = asks manager | ||
325 | 329 | ||
326 | instance MonadLogger (DHT ip) where | 330 | instance 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 |
332 | type NodeHandler ip = Handler (DHT ip) KMessageOf BValue | 336 | type NodeHandler ip = Handler IO KMessageOf BValue |
333 | #else | 337 | #else |
334 | type NodeHandler ip = Handler (DHT ip) Tox.Message ByteString | 338 | type NodeHandler ip = Handler (DHT ip) Tox.Message ByteString |
335 | #endif | 339 | #endif |
336 | 340 | ||
341 | logt :: HasCallStack => LogFun -> Char -> String -> Text -> IO () | ||
342 | logt 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 | |||
350 | mkLoggerLoc :: GHC.SrcLoc -> Loc | ||
351 | mkLoggerLoc 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 | |||
361 | locFromCS :: GHC.CallStack -> Loc | ||
362 | locFromCS 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 |