diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2014-02-03 20:18:46 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2014-02-03 20:18:46 +0400 |
commit | 7135b1f0ad74946687cdee6f7f54c6f481768c56 (patch) | |
tree | 7f427f02e2b8fe7bc2744778535e733c7670e4d3 /src/Network | |
parent | fa496de77c97548797298b06f199b2eaf8a46c41 (diff) |
Update client example
Diffstat (limited to 'src/Network')
-rw-r--r-- | src/Network/BitTorrent.hs | 5 | ||||
-rw-r--r-- | src/Network/BitTorrent/Client.hs | 12 |
2 files changed, 14 insertions, 3 deletions
diff --git a/src/Network/BitTorrent.hs b/src/Network/BitTorrent.hs index 21528efd..31fe3ada 100644 --- a/src/Network/BitTorrent.hs +++ b/src/Network/BitTorrent.hs | |||
@@ -7,5 +7,8 @@ | |||
7 | -- | 7 | -- |
8 | {-# LANGUAGE RecordWildCards #-} | 8 | {-# LANGUAGE RecordWildCards #-} |
9 | module Network.BitTorrent | 9 | module Network.BitTorrent |
10 | ( | 10 | ( module BT |
11 | ) where | 11 | ) where |
12 | |||
13 | import Data.Torrent as BT | ||
14 | import Network.BitTorrent.Client \ No newline at end of file | ||
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 | ||
29 | import Control.Exception | ||
27 | import Control.Concurrent.STM | 30 | import Control.Concurrent.STM |
28 | import Control.Monad.Logger | 31 | import Control.Monad.Logger |
29 | import Control.Monad.Reader | 32 | import Control.Monad.Reader |
@@ -31,6 +34,7 @@ import Control.Monad.Trans.Resource | |||
31 | import Data.Default | 34 | import Data.Default |
32 | import Data.Function | 35 | import Data.Function |
33 | import Data.HashMap.Strict as HM | 36 | import Data.HashMap.Strict as HM |
37 | import Data.Maybe | ||
34 | import Data.Ord | 38 | import Data.Ord |
35 | import Data.Text | 39 | import Data.Text |
36 | import Network | 40 | import 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 | ||
56 | instance Default Options where | 60 | instance 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 | ||
65 | data Client = Client | 70 | data 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 | ||
109 | withClient :: Options -> LogFun -> (Client -> IO a) -> IO a | ||
110 | withClient opts log action = bracket (newClient opts log) closeClient action | ||
111 | |||
104 | {----------------------------------------------------------------------- | 112 | {----------------------------------------------------------------------- |
105 | -- BitTorrent monad | 113 | -- BitTorrent monad |
106 | -----------------------------------------------------------------------} | 114 | -----------------------------------------------------------------------} |