module Sound.OpenSoundControl.Transport ( Transport(..)
, withTransport
, wait ) where
import Sound.OpenSoundControl.OSC (OSC(..))
import Control.Exception (bracket)
class Transport t where
send :: t -> OSC -> IO ()
recv :: t -> IO OSC
close :: t -> IO ()
hasAddress :: String -> OSC -> Bool
hasAddress addr (Message s _) = s == addr
hasAddress _ (Bundle _ _) = False
untilM :: Monad m => (a -> Bool) -> m a -> m a
untilM p act = recurse
where recurse = act >>= (\r -> if p r then return r else recurse)
wait :: Transport t => t -> String -> IO OSC
wait t s = untilM (hasAddress s) (recv t)
withTransport :: Transport t => IO t -> (t -> IO a) -> IO a
withTransport u = bracket u close