Browse Source

rename CSession to Context

rename forkSessionChan to forkContext
master
Philipp Balzarek 13 years ago
parent
commit
3ff3c71a98
  1. 6
      source/Network/Xmpp.hs
  2. 10
      source/Network/Xmpp/Concurrent/Channels.hs
  3. 8
      source/Network/Xmpp/Concurrent/Channels/Basic.hs
  4. 34
      source/Network/Xmpp/Concurrent/Channels/IQ.hs
  5. 14
      source/Network/Xmpp/Concurrent/Channels/Message.hs
  6. 10
      source/Network/Xmpp/Concurrent/Channels/Presence.hs
  7. 2
      source/Network/Xmpp/Concurrent/Channels/Types.hs
  8. 4
      source/Network/Xmpp/Xep/ServiceDiscovery.hs

6
source/Network/Xmpp.hs

@ -29,8 +29,8 @@
module Network.Xmpp module Network.Xmpp
( -- * Session management ( -- * Session management
Session Context
, newSessionChans , newContext
, withConnection , withConnection
, connect , connect
, simpleConnect , simpleConnect
@ -141,7 +141,7 @@ module Network.Xmpp
, iqRequestPayload , iqRequestPayload
, iqResultPayload , iqResultPayload
-- * Threads -- * Threads
, forkCSession , forkContext
-- * Miscellaneous -- * Miscellaneous
, LangTag(..) , LangTag(..)
, exampleParams , exampleParams

10
source/Network/Xmpp/Concurrent/Channels.hs

@ -6,7 +6,7 @@ module Network.Xmpp.Concurrent.Channels
, module Network.Xmpp.Concurrent.Channels.Presence , module Network.Xmpp.Concurrent.Channels.Presence
, module Network.Xmpp.Concurrent.Channels.IQ , module Network.Xmpp.Concurrent.Channels.IQ
, toChans , toChans
, newSessionChans , newContext
, writeWorker , writeWorker
) )
@ -90,9 +90,9 @@ toChans messageC presenceC stanzaC iqHands sta = atomically $ do
iqID (Right iq') = iqResultID iq' iqID (Right iq') = iqResultID iq'
-- | Creates and initializes a new concurrent session. -- | Creates and initializes a new concurrent context.
newSessionChans :: IO CSession newContext :: IO Context
newSessionChans = do newContext = do
messageC <- newTChanIO messageC <- newTChanIO
presenceC <- newTChanIO presenceC <- newTChanIO
outC <- newTChanIO outC <- newTChanIO
@ -116,7 +116,7 @@ newSessionChans = do
, eventHandlers = eh , eventHandlers = eh
, stopThreads = kill >> killThread writer , stopThreads = kill >> killThread writer
} }
return $ CSession { session = sess return $ Context { session = sess
, mShadow = messageC , mShadow = messageC
, pShadow = presenceC , pShadow = presenceC
, sShadow = stanzaC , sShadow = stanzaC

8
source/Network/Xmpp/Concurrent/Channels/Basic.hs

@ -6,16 +6,16 @@ import Network.Xmpp.Concurrent.Channels.Types
import Network.Xmpp.Types import Network.Xmpp.Types
-- | Get a duplicate of the stanza channel -- | Get a duplicate of the stanza channel
getStanzaChan :: CSession -> IO (TChan Stanza) getStanzaChan :: Context -> IO (TChan Stanza)
getStanzaChan session = atomically $ dupTChan (sShadow session) getStanzaChan session = atomically $ dupTChan (sShadow session)
-- | Send a stanza to the server. -- | Send a stanza to the server.
sendStanza :: Stanza -> CSession -> IO () sendStanza :: Stanza -> Context -> IO ()
sendStanza a session = atomically $ writeTChan (outCh session) a sendStanza a session = atomically $ writeTChan (outCh session) a
-- | Create a forked session object -- | Create a forked session object
forkCSession :: CSession -> IO CSession forkContext :: Context -> IO Context
forkCSession session = do forkContext session = do
mCH' <- newIORef Nothing mCH' <- newIORef Nothing
pCH' <- newIORef Nothing pCH' <- newIORef Nothing
return $ session {messagesRef = mCH' , presenceRef = pCH'} return $ session {messagesRef = mCH' , presenceRef = pCH'}

34
source/Network/Xmpp/Concurrent/Channels/IQ.hs

@ -23,28 +23,28 @@ sendIQ :: Maybe Int -- ^ Timeout
-> Maybe LangTag -- ^ Language tag of the payload (@Nothing@ for -> Maybe LangTag -- ^ Language tag of the payload (@Nothing@ for
-- default) -- default)
-> Element -- ^ The IQ body (there has to be exactly one) -> Element -- ^ The IQ body (there has to be exactly one)
-> CSession -> Context
-> IO (TMVar IQResponse) -> IO (TMVar IQResponse)
sendIQ timeOut to tp lang body csession = do -- TODO: Add timeout sendIQ timeOut to tp lang body context = do -- TODO: Add timeout
newId <- idGenerator (session csession) newId <- idGenerator (session context)
ref <- atomically $ do ref <- atomically $ do
resRef <- newEmptyTMVar resRef <- newEmptyTMVar
(byNS, byId) <- readTVar (iqHandlers csession) (byNS, byId) <- readTVar (iqHandlers context)
writeTVar (iqHandlers csession) (byNS, Map.insert newId resRef byId) writeTVar (iqHandlers context) (byNS, Map.insert newId resRef byId)
-- TODO: Check for id collisions (shouldn't happen?) -- TODO: Check for id collisions (shouldn't happen?)
return resRef return resRef
sendStanza (IQRequestS $ IQRequest newId Nothing to lang tp body) csession sendStanza (IQRequestS $ IQRequest newId Nothing to lang tp body) context
case timeOut of case timeOut of
Nothing -> return () Nothing -> return ()
Just t -> void . forkIO $ do Just t -> void . forkIO $ do
threadDelay t threadDelay t
doTimeOut (iqHandlers csession) newId ref doTimeOut (iqHandlers context) newId ref
return ref return ref
where where
doTimeOut handlers iqid var = atomically $ do doTimeOut handlers iqid var = atomically $ do
p <- tryPutTMVar var IQResponseTimeout p <- tryPutTMVar var IQResponseTimeout
when p $ do when p $ do
(byNS, byId) <- readTVar (iqHandlers csession) (byNS, byId) <- readTVar (iqHandlers context)
writeTVar handlers (byNS, Map.delete iqid byId) writeTVar handlers (byNS, Map.delete iqid byId)
return () return ()
@ -54,10 +54,10 @@ sendIQ' :: Maybe Jid
-> IQRequestType -> IQRequestType
-> Maybe LangTag -> Maybe LangTag
-> Element -> Element
-> CSession -> Context
-> IO IQResponse -> IO IQResponse
sendIQ' to tp lang body csession = do sendIQ' to tp lang body context = do
ref <- sendIQ (Just 3000000) to tp lang body csession ref <- sendIQ (Just 3000000) to tp lang body context
atomically $ takeTMVar ref atomically $ takeTMVar ref
@ -68,10 +68,10 @@ sendIQ' to tp lang body csession = do
-- to interfere with existing consumers. -- to interfere with existing consumers.
listenIQChan :: IQRequestType -- ^ Type of IQs to receive (@Get@ or @Set@) listenIQChan :: IQRequestType -- ^ Type of IQs to receive (@Get@ or @Set@)
-> Text -- ^ Namespace of the child element -> Text -- ^ Namespace of the child element
-> CSession -> Context
-> IO (Either (TChan IQRequestTicket) (TChan IQRequestTicket)) -> IO (Either (TChan IQRequestTicket) (TChan IQRequestTicket))
listenIQChan tp ns csession = do listenIQChan tp ns context = do
let handlers = (iqHandlers csession) let handlers = (iqHandlers context)
atomically $ do atomically $ do
(byNS, byID) <- readTVar handlers (byNS, byID) <- readTVar handlers
iqCh <- newTChan iqCh <- newTChan
@ -87,12 +87,12 @@ listenIQChan tp ns csession = do
answerIQ :: IQRequestTicket answerIQ :: IQRequestTicket
-> Either StanzaError (Maybe Element) -> Either StanzaError (Maybe Element)
-> CSession -> Context
-> IO Bool -> IO Bool
answerIQ (IQRequestTicket answerIQ (IQRequestTicket
sentRef sentRef
(IQRequest iqid from _to lang _tp bd)) (IQRequest iqid from _to lang _tp bd))
answer csession = do answer context = do
let response = case answer of let response = case answer of
Left err -> IQErrorS $ IQError iqid Nothing from lang err (Just bd) Left err -> IQErrorS $ IQError iqid Nothing from lang err (Just bd)
Right res -> IQResultS $ IQResult iqid Nothing from lang res Right res -> IQResultS $ IQResult iqid Nothing from lang res
@ -102,6 +102,6 @@ answerIQ (IQRequestTicket
False -> do False -> do
writeTVar sentRef True writeTVar sentRef True
writeTChan (outCh csession) response writeTChan (outCh context) response
return True return True
True -> return False True -> return False

14
source/Network/Xmpp/Concurrent/Channels/Message.hs

@ -10,7 +10,7 @@ import Network.Xmpp.Concurrent.Channels.Basic
-- | Get the inbound stanza channel, duplicates from master if necessary. Please -- | Get the inbound stanza channel, duplicates from master if necessary. Please
-- note that once duplicated it will keep filling up, call 'dropMessageChan' to -- note that once duplicated it will keep filling up, call 'dropMessageChan' to
-- allow it to be garbage collected. -- allow it to be garbage collected.
getMessageChan :: CSession -> IO (TChan (Either MessageError Message)) getMessageChan :: Context -> IO (TChan (Either MessageError Message))
getMessageChan session = do getMessageChan session = do
mCh <- readIORef . messagesRef $ session mCh <- readIORef . messagesRef $ session
case mCh of case mCh of
@ -22,19 +22,19 @@ getMessageChan session = do
-- | Drop the local end of the inbound stanza channel from our context so it can -- | Drop the local end of the inbound stanza channel from our context so it can
-- be GC-ed. -- be GC-ed.
dropMessageChan :: CSession -> IO () dropMessageChan :: Context -> IO ()
dropMessageChan session = writeIORef (messagesRef session) Nothing dropMessageChan session = writeIORef (messagesRef session) Nothing
-- | Read an element from the inbound stanza channel, acquiring a copy of the -- | Read an element from the inbound stanza channel, acquiring a copy of the
-- channel as necessary. -- channel as necessary.
pullMessage :: CSession -> IO (Either MessageError Message) pullMessage :: Context -> IO (Either MessageError Message)
pullMessage session = do pullMessage session = do
c <- getMessageChan session c <- getMessageChan session
atomically $ readTChan c atomically $ readTChan c
-- | Pulls a (non-error) message and returns it if the given predicate returns -- | Pulls a (non-error) message and returns it if the given predicate returns
-- @True@. -- @True@.
waitForMessage :: (Message -> Bool) -> CSession -> IO Message waitForMessage :: (Message -> Bool) -> Context -> IO Message
waitForMessage f session = do waitForMessage f session = do
s <- pullMessage session s <- pullMessage session
case s of case s of
@ -43,7 +43,7 @@ waitForMessage f session = do
| otherwise -> waitForMessage f session | otherwise -> waitForMessage f session
-- | Pulls an error message and returns it if the given predicate returns @True@. -- | Pulls an error message and returns it if the given predicate returns @True@.
waitForMessageError :: (MessageError -> Bool) -> CSession -> IO MessageError waitForMessageError :: (MessageError -> Bool) -> Context -> IO MessageError
waitForMessageError f session = do waitForMessageError f session = do
s <- pullMessage session s <- pullMessage session
case s of case s of
@ -55,7 +55,7 @@ waitForMessageError f session = do
-- | Pulls a message and returns it if the given predicate returns @True@. -- | Pulls a message and returns it if the given predicate returns @True@.
filterMessages :: (MessageError -> Bool) filterMessages :: (MessageError -> Bool)
-> (Message -> Bool) -> (Message -> Bool)
-> CSession -> IO (Either MessageError Message) -> Context -> IO (Either MessageError Message)
filterMessages f g session = do filterMessages f g session = do
s <- pullMessage session s <- pullMessage session
case s of case s of
@ -65,5 +65,5 @@ filterMessages f g session = do
| otherwise -> filterMessages f g session | otherwise -> filterMessages f g session
-- | Send a message stanza. -- | Send a message stanza.
sendMessage :: Message -> CSession -> IO () sendMessage :: Message -> Context -> IO ()
sendMessage m session = sendStanza (MessageS m) session sendMessage m session = sendStanza (MessageS m) session

10
source/Network/Xmpp/Concurrent/Channels/Presence.hs

@ -8,7 +8,7 @@ import Network.Xmpp.Concurrent.Types
import Network.Xmpp.Concurrent.Channels.Basic import Network.Xmpp.Concurrent.Channels.Basic
-- | Analogous to 'getMessageChan'. -- | Analogous to 'getMessageChan'.
getPresenceChan :: CSession -> IO (TChan (Either PresenceError Presence)) getPresenceChan :: Context -> IO (TChan (Either PresenceError Presence))
getPresenceChan session = do getPresenceChan session = do
pCh <- readIORef $ (presenceRef session) pCh <- readIORef $ (presenceRef session)
case pCh of case pCh of
@ -20,20 +20,20 @@ getPresenceChan session = do
-- | Analogous to 'dropMessageChan'. -- | Analogous to 'dropMessageChan'.
dropPresenceChan :: CSession -> IO () dropPresenceChan :: Context -> IO ()
dropPresenceChan session = writeIORef (presenceRef session) Nothing dropPresenceChan session = writeIORef (presenceRef session) Nothing
-- | Read an element from the inbound stanza channel, acquiring a copy of the -- | Read an element from the inbound stanza channel, acquiring a copy of the
-- channel as necessary. -- channel as necessary.
pullPresence :: CSession -> IO (Either PresenceError Presence) pullPresence :: Context -> IO (Either PresenceError Presence)
pullPresence session = do pullPresence session = do
c <- getPresenceChan session c <- getPresenceChan session
atomically $ readTChan c atomically $ readTChan c
-- | Pulls a (non-error) presence and returns it if the given predicate returns -- | Pulls a (non-error) presence and returns it if the given predicate returns
-- @True@. -- @True@.
waitForPresence :: (Presence -> Bool) -> CSession -> IO Presence waitForPresence :: (Presence -> Bool) -> Context -> IO Presence
waitForPresence f session = do waitForPresence f session = do
s <- pullPresence session s <- pullPresence session
case s of case s of
@ -42,5 +42,5 @@ waitForPresence f session = do
| otherwise -> waitForPresence f session | otherwise -> waitForPresence f session
-- | Send a presence stanza. -- | Send a presence stanza.
sendPresence :: Presence -> CSession -> IO () sendPresence :: Presence -> Context -> IO ()
sendPresence p session = sendStanza (PresenceS p) session sendPresence p session = sendStanza (PresenceS p) session

2
source/Network/Xmpp/Concurrent/Channels/Types.hs

@ -8,7 +8,7 @@ import Network.Xmpp.Concurrent.Types
import Network.Xmpp.Types import Network.Xmpp.Types
-- | Session with Channels -- | Session with Channels
data CSession = CSession data Context = Context
{ session :: Session { session :: Session
-- The original master channels that the reader puts stanzas -- The original master channels that the reader puts stanzas
-- into. These are cloned by @get{STanza,Message,Presence}Chan -- into. These are cloned by @get{STanza,Message,Presence}Chan

4
source/Network/Xmpp/Xep/ServiceDiscovery.hs

@ -86,7 +86,7 @@ xpQueryInfo = xpWrap (\(nd, (feats, ids)) -> QIR nd ids feats)
-- | Query an entity for it's identity and features -- | Query an entity for it's identity and features
queryInfo :: Jid -- ^ Entity to query queryInfo :: Jid -- ^ Entity to query
-> Maybe Text.Text -- ^ Node -> Maybe Text.Text -- ^ Node
-> CSession -> Context
-> IO (Either DiscoError QueryInfoResult) -> IO (Either DiscoError QueryInfoResult)
queryInfo to node session = do queryInfo to node session = do
res <- sendIQ' (Just to) Get Nothing queryBody session res <- sendIQ' (Just to) Get Nothing queryBody session
@ -149,7 +149,7 @@ xpQueryItems = xpElem (itemsN "query")
-- | Query an entity for Items of a node -- | Query an entity for Items of a node
queryItems :: Jid -- ^ Entity to query queryItems :: Jid -- ^ Entity to query
-> Maybe Text.Text -- ^ Node -> Maybe Text.Text -- ^ Node
-> CSession -> Context
-> IO (Either DiscoError (Maybe Text.Text, [Item])) -> IO (Either DiscoError (Maybe Text.Text, [Item]))
queryItems to node session = do queryItems to node session = do
res <- sendIQ' (Just to) Get Nothing queryBody session res <- sendIQ' (Just to) Get Nothing queryBody session

Loading…
Cancel
Save