zwirn-0.2.2.0: src/zwirn-lang/Zwirn/Stream/Handshake.hs
module Zwirn.Stream.Handshake where
import Control.Concurrent.MVar (MVar, swapMVar)
import Control.Monad (void)
import Data.Maybe (catMaybes, isJust)
import qualified Sound.Osc as O
import qualified Sound.Osc.Transport.Fd.Udp as O
import Zwirn.Stream.Target
-- handshake is in the responsibility of a specific listener implementation
-- these functions can be used to implement it
sendHandshake :: O.Udp -> RemoteAddress -> IO ()
sendHandshake udp = O.sendTo udp (O.Packet_Message $ O.Message "/dirt/handshake" [])
isHandshakeMsg :: O.Message -> Bool
isHandshakeMsg (O.Message "/dirt/hello" _) = True
isHandshakeMsg (O.Message "/dirt/handshake/reply" _) = True
isHandshakeMsg _ = False
actOnHandshake :: O.Message -> O.Udp -> RemoteAddress -> MVar [Int] -> IO ()
actOnHandshake (O.Message "/dirt/hello" _) udp remote _ = sendHandshake udp remote
actOnHandshake (O.Message "/dirt/handshake/reply" xs) _ _ bussesMV = void $ swapMVar bussesMV $ bufferIndices xs
where
bufferIndices [] = []
bufferIndices (x : xs')
| x == O.AsciiString (O.ascii "&controlBusIndices") = catMaybes $ takeWhile isJust $ map O.datum_integral xs'
| otherwise = bufferIndices xs'
actOnHandshake _ _ _ _ = return ()