| 1 |
|
| 2 |
module Sound.Osc.Transport.Fd where |
| 3 |
|
| 4 |
import Control.Exception {- base -} |
| 5 |
import Data.List {- base -} |
| 6 |
import Data.Maybe {- base -} |
| 7 |
|
| 8 |
import Sound.Osc.Datum {- hosc -} |
| 9 |
import Sound.Osc.Packet {- hosc -} |
| 10 |
import qualified Sound.Osc.Wait as Wait {- hosc -} |
| 11 |
|
| 12 |
|
| 13 |
class Transport t where |
| 14 |
|
| 15 |
sendPacket :: t -> PacketOf Message -> IO () |
| 16 |
|
| 17 |
|
| 18 |
recvPacket :: t -> IO (PacketOf Message) |
| 19 |
|
| 20 |
|
| 21 |
recvPacketOr :: t -> IO (Either String Packet) |
| 22 |
|
| 23 |
|
| 24 |
close :: t -> IO () |
| 25 |
|
| 26 |
|
| 27 |
withTransport :: Transport t => IO t -> (t -> IO a) -> IO a |
| 28 |
withTransport u = bracket u close |
| 29 |
|
| 30 |
-- * Send |
| 31 |
|
| 32 |
|
| 33 |
sendMessage :: Transport t => t -> Message -> IO () |
| 34 |
sendMessage t = sendPacket t . Packet_Message |
| 35 |
|
| 36 |
|
| 37 |
sendBundle :: Transport t => t -> BundleOf Message -> IO () |
| 38 |
sendBundle t = sendPacket t . Packet_Bundle |
| 39 |
|
| 40 |
-- * Receive |
| 41 |
|
| 42 |
|
| 43 |
recvBundle :: (Transport t) => t -> IO (BundleOf Message) |
| 44 |
recvBundle = fmap packet_to_bundle . recvPacket |
| 45 |
|
| 46 |
|
| 47 |
recvMessage :: (Transport t) => t -> IO (Maybe Message) |
| 48 |
recvMessage = fmap packet_to_message . recvPacket |
| 49 |
|
| 50 |
|
| 51 |
recvMessage_err :: (Transport t) => t -> IO Message |
| 52 |
recvMessage_err = fmap (fromMaybe (error "recvMessage")) . recvMessage |
| 53 |
|
| 54 |
|
| 55 |
recvMessages :: (Transport t) => t -> IO [Message] |
| 56 |
recvMessages = fmap packetMessages . recvPacket |
| 57 |
|
| 58 |
-- * Wait |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
waitUntil :: (Transport t) => t -> (PacketOf Message -> Bool) -> IO (PacketOf Message) |
| 64 |
waitUntil t f = Wait.untilPredicate f (recvPacket t) |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
waitFor :: (Transport t) => t -> (PacketOf Message -> Maybe a) -> IO a |
| 70 |
waitFor t f = Wait.untilMaybe f (recvPacket t) |
| 71 |
|
| 72 |
|
| 73 |
waitImmediate :: Transport t => t -> IO (PacketOf Message) |
| 74 |
waitImmediate t = waitUntil t packet_is_immediate |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
waitMessage :: Transport t => t -> IO Message |
| 80 |
waitMessage t = waitFor t packet_to_message |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
waitAddress :: Transport t => t -> Address_Pattern -> IO (PacketOf Message) |
| 86 |
waitAddress t s = |
| 87 |
let f o = if packet_has_address s o then Just o else Nothing |
| 88 |
in waitFor t f |
| 89 |
|
| 90 |
|
| 91 |
waitReply :: Transport t => t -> Address_Pattern -> IO Message |
| 92 |
waitReply t s = |
| 93 |
let f = |
| 94 |
fromMaybe (error "waitReply: message not located?") |
| 95 |
. find (message_has_address s) |
| 96 |
. packetMessages |
| 97 |
in fmap f (waitAddress t s) |
| 98 |
|
| 99 |
|
| 100 |
waitDatum :: Transport t => t -> Address_Pattern -> IO [Datum] |
| 101 |
waitDatum t = fmap messageDatum . waitReply t |