-- | Monad class implementing an Open Sound Control transport.
module Sound.Osc.Transport.Monad where

import Control.Monad {- base -}
import Control.Monad.IO.Class {- base -}
import Data.List {- base -}
import Data.Maybe {- base -}

import qualified Control.Monad.Trans.Reader as R {- transformers -}

import qualified Sound.Osc.Datum as Datum {- hosc -}
import qualified Sound.Osc.Transport.Fd as Fd {- hosc -}
import qualified Sound.Osc.Packet as Packet {- hosc -}
import qualified Sound.Osc.Wait as Wait {- hosc -}

-- | Sender monad.
class Monad m => SendOsc m where
   -- | Encode and send an Osc packet.
   sendPacket :: Packet.Packet -> m ()

-- | Receiver monad.
class Monad m => RecvOsc m where
   -- | Receive and decode an Osc packet.
   recvPacket :: m Packet.Packet

-- | 'DuplexOsc' is the union of 'SendOsc' and 'RecvOsc'.
class (SendOsc m,RecvOsc m) => DuplexOsc m where

-- | 'Transport' is 'DuplexOsc' with a 'MonadIO' constraint.
class (DuplexOsc m,MonadIO m) => Transport m where

-- | 'SendOsc' over 'ReaderT'.
instance (Fd.Transport t,MonadIO io) => SendOsc (R.ReaderT t io) where
   sendPacket :: Packet -> ReaderT t io ()
sendPacket Packet
p = forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
R.ReaderT (forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> b -> a -> c
flip forall t. Transport t => t -> Packet -> IO ()
Fd.sendPacket Packet
p)

-- | 'RecvOsc' over 'ReaderT'.
instance (Fd.Transport t,MonadIO io) => RecvOsc (R.ReaderT t io) where
   recvPacket :: ReaderT t io Packet
recvPacket = forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
R.ReaderT (forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. Transport t => t -> IO Packet
Fd.recvPacket)

-- | 'DuplexOsc' over 'ReaderT'.
instance (Fd.Transport t,MonadIO io) => DuplexOsc (R.ReaderT t io) where

-- | 'Transport' over 'ReaderT'.
instance (Fd.Transport t,MonadIO io) => Transport (R.ReaderT t io) where

-- | Transport connection.
type Connection t a = R.ReaderT t IO a

-- | Bracket Open Sound Control communication.
withTransport :: Fd.Transport t => IO t -> Connection t r -> IO r
withTransport :: forall t r. Transport t => IO t -> Connection t r -> IO r
withTransport IO t
u = forall t a. Transport t => IO t -> (t -> IO a) -> IO a
Fd.withTransport IO t
u forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
R.runReaderT

-- | 'void' of 'withTransport'.
withTransport_ :: Fd.Transport t => IO t -> Connection t r -> IO ()
withTransport_ :: forall t r. Transport t => IO t -> Connection t r -> IO ()
withTransport_ IO t
u = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t r. Transport t => IO t -> Connection t r -> IO r
withTransport IO t
u

-- * Send

-- | Type restricted synonym for 'sendOsc'.
sendMessage :: SendOsc m => Packet.Message -> m ()
sendMessage :: forall (m :: * -> *). SendOsc m => Message -> m ()
sendMessage = forall (m :: * -> *). SendOsc m => Packet -> m ()
sendPacket forall b c a. (b -> c) -> (a -> b) -> a -> c
. Message -> Packet
Packet.Packet_Message

-- | Type restricted synonym for 'sendOsc'.
sendBundle :: SendOsc m => Packet.Bundle -> m ()
sendBundle :: forall (m :: * -> *). SendOsc m => Bundle -> m ()
sendBundle = forall (m :: * -> *). SendOsc m => Packet -> m ()
sendPacket forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle -> Packet
Packet.Packet_Bundle

-- * Receive

-- | Variant of 'recvPacket' that runs 'packet_to_bundle'.
recvBundle :: (RecvOsc m) => m Packet.Bundle
recvBundle :: forall (m :: * -> *). RecvOsc m => m Bundle
recvBundle = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Packet -> Bundle
Packet.packet_to_bundle forall (m :: * -> *). RecvOsc m => m Packet
recvPacket

-- | Variant of 'recvPacket' that runs 'packet_to_message'.
recvMessage :: (RecvOsc m) => m (Maybe Packet.Message)
recvMessage :: forall (m :: * -> *). RecvOsc m => m (Maybe Message)
recvMessage = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Packet -> Maybe Message
Packet.packet_to_message forall (m :: * -> *). RecvOsc m => m Packet
recvPacket

-- | Erroring variant.
recvMessage_err :: RecvOsc m => m Packet.Message
recvMessage_err :: forall (m :: * -> *). RecvOsc m => m Message
recvMessage_err = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. a -> Maybe a -> a
fromMaybe (forall a. HasCallStack => [Char] -> a
error [Char]
"recvMessage")) forall (m :: * -> *). RecvOsc m => m (Maybe Message)
recvMessage

-- | Variant of 'recvPacket' that runs 'packetMessages'.
recvMessages :: (RecvOsc m) => m [Packet.Message]
recvMessages :: forall (m :: * -> *). RecvOsc m => m [Message]
recvMessages = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Packet -> [Message]
Packet.packetMessages forall (m :: * -> *). RecvOsc m => m Packet
recvPacket

-- * Wait

-- | Wait for a 'Packet' where the supplied predicate is 'True',
-- discarding intervening packets.
waitUntil :: (RecvOsc m) => (Packet.Packet -> Bool) -> m Packet.Packet
waitUntil :: forall (m :: * -> *). RecvOsc m => (Packet -> Bool) -> m Packet
waitUntil Packet -> Bool
f = forall (m :: * -> *) a. Monad m => (a -> Bool) -> m a -> m a
Wait.untilPredicate Packet -> Bool
f forall (m :: * -> *). RecvOsc m => m Packet
recvPacket

-- | Wait for a 'Packet' where the supplied function does not give
-- 'Nothing', discarding intervening packets.
waitFor :: (RecvOsc m) => (Packet.Packet -> Maybe a) -> m a
waitFor :: forall (m :: * -> *) a. RecvOsc m => (Packet -> Maybe a) -> m a
waitFor Packet -> Maybe a
f = forall (m :: * -> *) a b. Monad m => (a -> Maybe b) -> m a -> m b
Wait.untilMaybe Packet -> Maybe a
f forall (m :: * -> *). RecvOsc m => m Packet
recvPacket

-- | 'waitUntil' 'packet_is_immediate'.
waitImmediate :: RecvOsc m => m Packet.Packet
waitImmediate :: forall (m :: * -> *). RecvOsc m => m Packet
waitImmediate = forall (m :: * -> *). RecvOsc m => (Packet -> Bool) -> m Packet
waitUntil Packet -> Bool
Packet.packet_is_immediate

-- | 'waitFor' 'packet_to_message', ie. an incoming 'Message' or
-- immediate mode 'Bundle' with one element.
waitMessage :: RecvOsc m => m Packet.Message
waitMessage :: forall (m :: * -> *). RecvOsc m => m Message
waitMessage = forall (m :: * -> *) a. RecvOsc m => (Packet -> Maybe a) -> m a
waitFor Packet -> Maybe Message
Packet.packet_to_message

-- | A 'waitFor' for variant using 'packet_has_address' to match on
-- the 'Address_Pattern' of incoming 'Packets'.
waitAddress :: RecvOsc m => Packet.Address_Pattern -> m Packet.Packet
waitAddress :: forall (m :: * -> *). RecvOsc m => [Char] -> m Packet
waitAddress [Char]
s =
    let f :: Packet -> Maybe Packet
f Packet
o = if [Char] -> Packet -> Bool
Packet.packet_has_address [Char]
s Packet
o then forall a. a -> Maybe a
Just Packet
o else forall a. Maybe a
Nothing
    in forall (m :: * -> *) a. RecvOsc m => (Packet -> Maybe a) -> m a
waitFor Packet -> Maybe Packet
f

-- | Variant on 'waitAddress' that returns matching 'Message'.
waitReply :: RecvOsc m => Packet.Address_Pattern -> m Packet.Message
waitReply :: forall (m :: * -> *). RecvOsc m => [Char] -> m Message
waitReply [Char]
s =
    let f :: Packet -> Message
f = forall a. a -> Maybe a -> a
fromMaybe (forall a. HasCallStack => [Char] -> a
error [Char]
"waitReply: message not located?") forall b c a. (b -> c) -> (a -> b) -> a -> c
.
            forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ([Char] -> Message -> Bool
Packet.message_has_address [Char]
s) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
            Packet -> [Message]
Packet.packetMessages
    in forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Packet -> Message
f (forall (m :: * -> *). RecvOsc m => [Char] -> m Packet
waitAddress [Char]
s)

-- | Variant of 'waitReply' that runs 'messageDatum'.
waitDatum :: RecvOsc m => Packet.Address_Pattern -> m [Datum.Datum]
waitDatum :: forall (m :: * -> *). RecvOsc m => [Char] -> m [Datum]
waitDatum = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Message -> [Datum]
Packet.messageDatum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). RecvOsc m => [Char] -> m Message
waitReply