summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-02-03 20:18:46 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-02-03 20:18:46 +0400
commit7135b1f0ad74946687cdee6f7f54c6f481768c56 (patch)
tree7f427f02e2b8fe7bc2744778535e733c7670e4d3 /src/Network/BitTorrent
parentfa496de77c97548797298b06f199b2eaf8a46c41 (diff)
Update client example
Diffstat (limited to 'src/Network/BitTorrent')
-rw-r--r--src/Network/BitTorrent/Client.hs12
1 files changed, 10 insertions, 2 deletions
diff --git a/src/Network/BitTorrent/Client.hs b/src/Network/BitTorrent/Client.hs
index 74072706..fd830239 100644
--- a/src/Network/BitTorrent/Client.hs
+++ b/src/Network/BitTorrent/Client.hs
@@ -13,6 +13,8 @@ module Network.BitTorrent.Client
13 13
14 -- ** Session initialization 14 -- ** Session initialization
15 , newClient 15 , newClient
16 , closeClient
17 , withClient
16 18
17 -- * BitTorrent monad 19 -- * BitTorrent monad
18 , BitTorrent 20 , BitTorrent
@@ -24,6 +26,7 @@ module Network.BitTorrent.Client
24 , addTorrent 26 , addTorrent
25 ) where 27 ) where
26 28
29import Control.Exception
27import Control.Concurrent.STM 30import Control.Concurrent.STM
28import Control.Monad.Logger 31import Control.Monad.Logger
29import Control.Monad.Reader 32import Control.Monad.Reader
@@ -31,6 +34,7 @@ import Control.Monad.Trans.Resource
31import Data.Default 34import Data.Default
32import Data.Function 35import Data.Function
33import Data.HashMap.Strict as HM 36import Data.HashMap.Strict as HM
37import Data.Maybe
34import Data.Ord 38import Data.Ord
35import Data.Text 39import Data.Text
36import Network 40import Network
@@ -50,7 +54,7 @@ data Options = Options
50 , port :: PortNumber 54 , port :: PortNumber
51 , extensions :: [Extension] 55 , extensions :: [Extension]
52 , nodeAddr :: NodeAddr IPv4 56 , nodeAddr :: NodeAddr IPv4
53 , bootNode :: NodeAddr IPv4 57 , bootNode :: Maybe (NodeAddr IPv4)
54 } 58 }
55 59
56instance Default Options where 60instance Default Options where
@@ -60,6 +64,7 @@ instance Default Options where
60 , port = 6882 64 , port = 6882
61 , extensions = [] 65 , extensions = []
62 , nodeAddr = "0.0.0.0:6882" 66 , nodeAddr = "0.0.0.0:6882"
67 , bootNode = Nothing
63 } 68 }
64 69
65data Client = Client 70data Client = Client
@@ -84,7 +89,7 @@ newClient Options {..} logger = do
84 ts <- newTVarIO HM.empty 89 ts <- newTVarIO HM.empty
85 node <- runResourceT $ do 90 node <- runResourceT $ do
86 node <- startNode handlers def nodeAddr logger 91 node <- startNode handlers def nodeAddr logger
87 runDHT node $ bootstrap [bootNode] 92 runDHT node $ bootstrap (maybeToList bootNode)
88 return node 93 return node
89 94
90 return Client 95 return Client
@@ -101,6 +106,9 @@ closeClient Client {..} = do
101 return () 106 return ()
102-- closeNode clientNode 107-- closeNode clientNode
103 108
109withClient :: Options -> LogFun -> (Client -> IO a) -> IO a
110withClient opts log action = bracket (newClient opts log) closeClient action
111
104{----------------------------------------------------------------------- 112{-----------------------------------------------------------------------
105-- BitTorrent monad 113-- BitTorrent monad
106-----------------------------------------------------------------------} 114-----------------------------------------------------------------------}