summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/Exchange/Session.hs
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-04-03 00:03:09 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-04-03 00:03:09 +0400
commit84185337992d02d344f05cf83b3f013ca9b1bf5f (patch)
tree7191eeba15b3612ec37791cd94acbeeebf8a9519 /src/Network/BitTorrent/Exchange/Session.hs
parent521e05a8363dd6505a4cd9db41545c5197900a27 (diff)
Add exchange session state updates eventstream
Diffstat (limited to 'src/Network/BitTorrent/Exchange/Session.hs')
-rw-r--r--src/Network/BitTorrent/Exchange/Session.hs25
1 files changed, 24 insertions, 1 deletions
diff --git a/src/Network/BitTorrent/Exchange/Session.hs b/src/Network/BitTorrent/Exchange/Session.hs
index 32f8eabd..c4664827 100644
--- a/src/Network/BitTorrent/Exchange/Session.hs
+++ b/src/Network/BitTorrent/Exchange/Session.hs
@@ -16,13 +16,18 @@ module Network.BitTorrent.Exchange.Session
16 , connect 16 , connect
17 , establish 17 , establish
18 18
19 -- * Events 19 -- * Query
20 , waitMetadata 20 , waitMetadata
21 , takeMetadata 21 , takeMetadata
22
23 -- * Events
24 , SessionEvent (..)
25 , subscription
22 ) where 26 ) where
23 27
24import Control.Applicative 28import Control.Applicative
25import Control.Concurrent 29import Control.Concurrent
30import Control.Concurrent.Chan.Split as CS
26import Control.Concurrent.STM 31import Control.Concurrent.STM
27import Control.Exception hiding (Handler) 32import Control.Exception hiding (Handler)
28import Control.Lens 33import Control.Lens
@@ -91,6 +96,7 @@ data Session = Session
91 { sessionPeerId :: !(PeerId) 96 { sessionPeerId :: !(PeerId)
92 , sessionTopic :: !(InfoHash) 97 , sessionTopic :: !(InfoHash)
93 , sessionLogger :: !(LogFun) 98 , sessionLogger :: !(LogFun)
99 , sessionEvents :: !(SendPort SessionEvent)
94 100
95 , metadata :: !(MVar Metadata.Status) 101 , metadata :: !(MVar Metadata.Status)
96 , infodict :: !(MVar (Cached InfoDict)) 102 , infodict :: !(MVar (Cached InfoDict))
@@ -137,11 +143,13 @@ newSession logFun addr rootPath dict = do
137 pSetVar <- newTVarIO S.empty 143 pSetVar <- newTVarIO S.empty
138 eSetVar <- newTVarIO M.empty 144 eSetVar <- newTVarIO M.empty
139 chan <- newChan 145 chan <- newChan
146 eventStream <- newSendPort
140 147
141 return Session 148 return Session
142 { sessionPeerId = pid 149 { sessionPeerId = pid
143 , sessionTopic = idInfoHash dict 150 , sessionTopic = idInfoHash dict
144 , sessionLogger = logFun 151 , sessionLogger = logFun
152 , sessionEvents = eventStream
145 153
146 , metadata = metadataVar 154 , metadata = metadataVar
147 , infodict = infodictVar 155 , infodict = infodictVar
@@ -171,6 +179,21 @@ withSession :: ()
171withSession = error "withSession" 179withSession = error "withSession"
172 180
173{----------------------------------------------------------------------- 181{-----------------------------------------------------------------------
182-- Session events
183-----------------------------------------------------------------------}
184
185data SessionEvent
186 = ConnectingTo (PeerAddr IP)
187 | ConnectionEstablished (PeerAddr IP)
188 | ConnectionAborted
189 | ConnectionClosed (PeerAddr IP)
190 | SessionClosed
191 deriving Show
192
193subscription :: Session -> IO (ReceivePort SessionEvent)
194subscription Session {..} = listen sessionEvents
195
196{-----------------------------------------------------------------------
174-- Logging 197-- Logging
175-----------------------------------------------------------------------} 198-----------------------------------------------------------------------}
176 199