2014-04-05 18:30:42 +00:00
|
|
|
{- git-remote-daemon data types.
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2014 Joey Hess <id@joeyh.name>
|
2014-04-05 18:30:42 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
|
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
|
|
|
|
module RemoteDaemon.Types where
|
|
|
|
|
2014-04-09 18:10:29 +00:00
|
|
|
import Common
|
2014-04-06 23:06:03 +00:00
|
|
|
import qualified Annex
|
2014-04-05 18:30:42 +00:00
|
|
|
import qualified Git.Types as Git
|
|
|
|
import qualified Utility.SimpleProtocol as Proto
|
|
|
|
|
2014-04-09 18:10:29 +00:00
|
|
|
import Network.URI
|
2014-04-06 23:06:03 +00:00
|
|
|
import Control.Concurrent
|
2015-01-15 19:37:48 +00:00
|
|
|
import Control.Concurrent.STM
|
2014-04-06 23:06:03 +00:00
|
|
|
|
2014-04-09 18:10:29 +00:00
|
|
|
-- The URI of a remote is used to uniquely identify it (names change..)
|
|
|
|
newtype RemoteURI = RemoteURI URI
|
2014-10-09 19:09:26 +00:00
|
|
|
deriving (Show)
|
2014-04-09 18:10:29 +00:00
|
|
|
|
2014-04-06 23:06:03 +00:00
|
|
|
-- A Transport for a particular git remote consumes some messages
|
|
|
|
-- from a Chan, and emits others to another Chan.
|
2015-01-15 19:37:48 +00:00
|
|
|
type Transport = RemoteRepo -> RemoteURI -> TransportHandle -> TChan Consumed -> TChan Emitted -> IO ()
|
2014-04-08 17:41:36 +00:00
|
|
|
|
|
|
|
type RemoteRepo = Git.Repo
|
|
|
|
type LocalRepo = Git.Repo
|
|
|
|
|
|
|
|
-- All Transports share a single AnnexState MVar
|
2014-04-20 19:45:14 +00:00
|
|
|
--
|
|
|
|
-- Different TransportHandles may have different versions of the LocalRepo.
|
|
|
|
-- (For example, the ssh transport modifies it to enable ssh connection
|
|
|
|
-- caching.)
|
2014-04-08 17:41:36 +00:00
|
|
|
data TransportHandle = TransportHandle LocalRepo (MVar Annex.AnnexState)
|
2014-04-06 23:06:03 +00:00
|
|
|
|
2014-04-05 18:30:42 +00:00
|
|
|
-- Messages that the daemon emits.
|
|
|
|
data Emitted
|
2014-04-09 18:10:29 +00:00
|
|
|
= CONNECTED RemoteURI
|
|
|
|
| DISCONNECTED RemoteURI
|
|
|
|
| SYNCING RemoteURI
|
|
|
|
| DONESYNCING RemoteURI Bool
|
|
|
|
| WARNING RemoteURI String
|
2014-04-20 19:30:39 +00:00
|
|
|
deriving (Show)
|
2014-04-05 18:30:42 +00:00
|
|
|
|
|
|
|
-- Messages that the deamon consumes.
|
|
|
|
data Consumed
|
|
|
|
= PAUSE
|
2014-04-12 20:32:59 +00:00
|
|
|
| LOSTNET
|
2014-04-05 18:30:42 +00:00
|
|
|
| RESUME
|
2014-04-06 23:06:03 +00:00
|
|
|
| CHANGED RefList
|
2014-04-05 18:30:42 +00:00
|
|
|
| RELOAD
|
2014-04-06 23:06:03 +00:00
|
|
|
| STOP
|
2014-04-20 19:30:39 +00:00
|
|
|
deriving (Show)
|
2014-04-05 18:30:42 +00:00
|
|
|
|
2014-04-06 23:06:03 +00:00
|
|
|
type RefList = [Git.Ref]
|
2014-04-05 18:30:42 +00:00
|
|
|
|
|
|
|
instance Proto.Sendable Emitted where
|
2014-04-05 20:04:37 +00:00
|
|
|
formatMessage (CONNECTED remote) =
|
|
|
|
["CONNECTED", Proto.serialize remote]
|
|
|
|
formatMessage (DISCONNECTED remote) =
|
|
|
|
["DISCONNECTED", Proto.serialize remote]
|
2014-04-06 23:06:03 +00:00
|
|
|
formatMessage (SYNCING remote) =
|
|
|
|
["SYNCING", Proto.serialize remote]
|
2014-04-09 18:10:29 +00:00
|
|
|
formatMessage (DONESYNCING remote status) =
|
|
|
|
["DONESYNCING", Proto.serialize remote, Proto.serialize status]
|
|
|
|
formatMessage (WARNING remote message) =
|
|
|
|
["WARNING", Proto.serialize remote, Proto.serialize message]
|
2014-04-05 18:30:42 +00:00
|
|
|
|
|
|
|
instance Proto.Sendable Consumed where
|
|
|
|
formatMessage PAUSE = ["PAUSE"]
|
2014-04-12 20:32:59 +00:00
|
|
|
formatMessage LOSTNET = ["LOSTNET"]
|
2014-04-05 18:30:42 +00:00
|
|
|
formatMessage RESUME = ["RESUME"]
|
2014-04-06 23:06:03 +00:00
|
|
|
formatMessage (CHANGED refs) =["CHANGED", Proto.serialize refs]
|
2014-04-05 18:30:42 +00:00
|
|
|
formatMessage RELOAD = ["RELOAD"]
|
2014-04-06 23:06:03 +00:00
|
|
|
formatMessage STOP = ["STOP"]
|
2014-04-05 18:30:42 +00:00
|
|
|
|
|
|
|
instance Proto.Receivable Emitted where
|
2014-04-05 20:04:37 +00:00
|
|
|
parseCommand "CONNECTED" = Proto.parse1 CONNECTED
|
|
|
|
parseCommand "DISCONNECTED" = Proto.parse1 DISCONNECTED
|
2014-04-06 23:06:03 +00:00
|
|
|
parseCommand "SYNCING" = Proto.parse1 SYNCING
|
|
|
|
parseCommand "DONESYNCING" = Proto.parse2 DONESYNCING
|
2014-04-09 18:10:29 +00:00
|
|
|
parseCommand "WARNING" = Proto.parse2 WARNING
|
2014-04-05 18:30:42 +00:00
|
|
|
parseCommand _ = Proto.parseFail
|
|
|
|
|
|
|
|
instance Proto.Receivable Consumed where
|
|
|
|
parseCommand "PAUSE" = Proto.parse0 PAUSE
|
2014-04-12 20:32:59 +00:00
|
|
|
parseCommand "LOSTNET" = Proto.parse0 LOSTNET
|
2014-04-05 18:30:42 +00:00
|
|
|
parseCommand "RESUME" = Proto.parse0 RESUME
|
2014-04-06 23:06:03 +00:00
|
|
|
parseCommand "CHANGED" = Proto.parse1 CHANGED
|
2014-04-05 18:30:42 +00:00
|
|
|
parseCommand "RELOAD" = Proto.parse0 RELOAD
|
2014-04-06 23:06:03 +00:00
|
|
|
parseCommand "STOP" = Proto.parse0 STOP
|
2014-04-05 18:30:42 +00:00
|
|
|
parseCommand _ = Proto.parseFail
|
|
|
|
|
2014-04-09 18:10:29 +00:00
|
|
|
instance Proto.Serializable RemoteURI where
|
|
|
|
serialize (RemoteURI u) = show u
|
|
|
|
deserialize = RemoteURI <$$> parseURI
|
|
|
|
|
2014-04-05 18:30:42 +00:00
|
|
|
instance Proto.Serializable [Char] where
|
|
|
|
serialize = id
|
|
|
|
deserialize = Just
|
|
|
|
|
2014-04-06 23:06:03 +00:00
|
|
|
instance Proto.Serializable RefList where
|
2014-04-05 18:30:42 +00:00
|
|
|
serialize = unwords . map Git.fromRef
|
|
|
|
deserialize = Just . map Git.Ref . words
|
2014-04-06 23:06:03 +00:00
|
|
|
|
|
|
|
instance Proto.Serializable Bool where
|
|
|
|
serialize False = "0"
|
|
|
|
serialize True = "1"
|
|
|
|
|
|
|
|
deserialize "0" = Just False
|
|
|
|
deserialize "1" = Just True
|
|
|
|
deserialize _ = Nothing
|