summaryrefslogtreecommitdiff
path: root/iserv/src/Remote/Message.hs
blob: f1745301bab79d04300405bf28548bd75da347d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{-# LANGUAGE GADTs, StandaloneDeriving, ExistentialQuantification #-}

module Remote.Message
  ( SlaveMessage(..)
  , SlaveMsg(..)
  , putSlaveMessage
  , getSlaveMessage )
where

import GHC.Fingerprint (Fingerprint)
import Data.Binary
import Data.ByteString (ByteString)

-- | A @SlaveMessage a@ is message from the iserv process on the
-- target, requesting something from the Proxy of with result type @a@.
data SlaveMessage a where
  -- sends either a new file, or nothing if the file is acceptable.
  Have     :: FilePath -> Fingerprint -> SlaveMessage (Maybe ByteString)
  Missing  :: FilePath -> SlaveMessage ByteString
  Done     :: SlaveMessage ()

deriving instance Show (SlaveMessage a)

putSlaveMessage :: SlaveMessage a -> Put
putSlaveMessage m = case m of
  Have path sha  -> putWord8 0 >> put path >> put sha
  Missing path   -> putWord8 1 >> put path
  Done           -> putWord8 2

data SlaveMsg = forall a . (Binary a, Show a) => SlaveMsg (SlaveMessage a)

getSlaveMessage :: Get SlaveMsg
getSlaveMessage = do
  b <- getWord8
  case b of
    0 -> SlaveMsg <$> (Have   <$> get <*> get)
    1 -> SlaveMsg <$> Missing <$> get
    2 -> return (SlaveMsg Done)