|
| 1 | +{-# LANGUAGE FlexibleContexts #-} |
| 2 | +{-# LANGUAGE PartialTypeSignatures #-} |
| 3 | +{-# LANGUAGE PolyKinds #-} |
| 4 | +{-# LANGUAGE RankNTypes #-} |
| 5 | + |
| 6 | +-- | The codec for the local message notification miniprotocol |
| 7 | +-- |
| 8 | +module DMQ.Protocol.LocalMsgNotification.Codec |
| 9 | + ( codecLocalMsgNotification |
| 10 | + , decodeLocalMsgNotification |
| 11 | + ) where |
| 12 | + |
| 13 | +import Codec.CBOR.Decoding qualified as CBOR |
| 14 | +import Codec.CBOR.Encoding qualified as CBOR |
| 15 | +import Codec.CBOR.Read qualified as CBOR |
| 16 | +import Control.Monad.Class.MonadST |
| 17 | +import Data.ByteString.Lazy (ByteString) |
| 18 | +import Data.Functor ((<&>)) |
| 19 | +import Data.Kind |
| 20 | +import Data.List.NonEmpty qualified as NonEmpty |
| 21 | +import Text.Printf |
| 22 | + |
| 23 | +import DMQ.Protocol.LocalMsgNotification.Type |
| 24 | +import Network.TypedProtocol.Codec.CBOR |
| 25 | + |
| 26 | +codecLocalMsgNotification |
| 27 | + :: forall msg m. |
| 28 | + MonadST m |
| 29 | + => (msg -> CBOR.Encoding) |
| 30 | + -> (forall s. CBOR.Decoder s msg) |
| 31 | + -> Codec (LocalMsgNotification msg) CBOR.DeserialiseFailure m ByteString |
| 32 | +codecLocalMsgNotification encodeMsg decodeMsg = |
| 33 | + mkCodecCborLazyBS |
| 34 | + encode |
| 35 | + decode |
| 36 | + where |
| 37 | + encode :: forall st st'. |
| 38 | + Message (LocalMsgNotification msg) st st' |
| 39 | + -> CBOR.Encoding |
| 40 | + encode (MsgRequest blocking) = |
| 41 | + CBOR.encodeListLen 2 |
| 42 | + <> CBOR.encodeWord 0 |
| 43 | + <> CBOR.encodeBool (case blocking of |
| 44 | + SingBlocking -> True |
| 45 | + SingNonBlocking -> False) |
| 46 | + |
| 47 | + encode (MsgReply msgs hasMore) = |
| 48 | + CBOR.encodeListLen 3 |
| 49 | + <> CBOR.encodeWord 1 |
| 50 | + <> CBOR.encodeListLenIndef |
| 51 | + <> foldr (\msg r -> |
| 52 | + encodeMsg msg <> r) |
| 53 | + CBOR.encodeBreak |
| 54 | + msgs |
| 55 | + <> CBOR.encodeBool hasMore' |
| 56 | + where |
| 57 | + hasMore' = case hasMore of |
| 58 | + HasMore -> True |
| 59 | + DoesNotHaveMore -> False |
| 60 | + |
| 61 | + encode MsgServerDone = |
| 62 | + CBOR.encodeListLen 1 |
| 63 | + <> CBOR.encodeWord 2 |
| 64 | + |
| 65 | + encode MsgClientDone = |
| 66 | + CBOR.encodeListLen 1 |
| 67 | + <> CBOR.encodeWord 3 |
| 68 | + |
| 69 | + decode :: forall (st :: LocalMsgNotification msg). |
| 70 | + ActiveState st |
| 71 | + => StateToken st |
| 72 | + -> forall s. CBOR.Decoder s (SomeMessage st) |
| 73 | + decode stok = do |
| 74 | + len <- CBOR.decodeListLen |
| 75 | + key <- CBOR.decodeWord |
| 76 | + decodeLocalMsgNotification decodeMsg stok len key |
| 77 | + |
| 78 | + |
| 79 | +decodeLocalMsgNotification :: forall (msg :: Type) (st' :: LocalMsgNotification msg) s. |
| 80 | + ActiveState st' |
| 81 | + => (forall s'. CBOR.Decoder s' msg) |
| 82 | + -> StateToken st' |
| 83 | + -> Int |
| 84 | + -> Word |
| 85 | + -> CBOR.Decoder s (SomeMessage st') |
| 86 | +decodeLocalMsgNotification decodeMsg = decode |
| 87 | + where |
| 88 | + decode :: StateToken st' |
| 89 | + -> Int |
| 90 | + -> Word |
| 91 | + -> CBOR.Decoder s (SomeMessage st') |
| 92 | + decode stok len key = case (stok, len, key) of |
| 93 | + (SingIdle, 1, 3) -> return $ SomeMessage MsgClientDone |
| 94 | + (SingIdle, 2, 0) -> do |
| 95 | + blocking <- CBOR.decodeBool |
| 96 | + return $! if blocking |
| 97 | + then SomeMessage (MsgRequest SingBlocking) |
| 98 | + else SomeMessage (MsgRequest SingNonBlocking) |
| 99 | + (SingBusy SingBlocking, 1, 2) -> |
| 100 | + return $ SomeMessage MsgServerDone |
| 101 | + (SingBusy blocking, 3, 1) -> do |
| 102 | + CBOR.decodeListLenIndef |
| 103 | + msgs <- CBOR.decodeSequenceLenIndef |
| 104 | + (flip (:)) [] reverse |
| 105 | + decodeMsg |
| 106 | + more <- CBOR.decodeBool <&> \case |
| 107 | + True -> HasMore |
| 108 | + False -> DoesNotHaveMore |
| 109 | + case (blocking, msgs) of |
| 110 | + (SingBlocking, m:msgs') -> |
| 111 | + return . SomeMessage $ MsgReply (BlockingReply (m NonEmpty.:| msgs')) |
| 112 | + more |
| 113 | + (SingNonBlocking, _) -> |
| 114 | + return . SomeMessage $ MsgReply (NonBlockingReply msgs) |
| 115 | + more |
| 116 | + (SingBlocking, []) -> fail "codecLocalMsgNotification: MsgReply: empty list not permitted" |
| 117 | + (SingDone, _, _) -> notActiveState stok |
| 118 | + -- |
| 119 | + -- failure |
| 120 | + -- |
| 121 | + (_, _, _) -> |
| 122 | + fail (printf "codecLocalMsgNotification (%s, %s) unexpected key (%d, %d)" |
| 123 | + (show (activeAgency :: ActiveAgency st')) (show stok) key len) |
0 commit comments