Skip to content
This repository has been archived by the owner on May 24, 2021. It is now read-only.

Commit

Permalink
Daemon remote start
Browse files Browse the repository at this point in the history
  • Loading branch information
jazcarate committed Aug 22, 2020
1 parent 0d94c4c commit b92b3e0
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 27 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"Attoparsec",
"Hspec",
"delim",
"mbls",
"metavar",
"subparser"
]
Expand Down
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
PID_FILE=~/.marble-os.pid


help: ## Print documentation
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

Expand All @@ -20,4 +23,7 @@ run: ## Run de program in dev mode. Pass arguments in ARGS
nix-shell \
--run "cabal new-run marble -- $$ARGS"

.PHONY: serve build test update-deps repl run
kill-daemon: ## [Helper] Kill running daemon
test -s $(PID_FILE) && kill -9 `cat $(PID_FILE)` && rm $(PID_FILE)

.PHONY: serve build test update-deps repl run kill-daemon
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ eg:
This would, in 4 ticks, output `Say something`

### TUI for the daemon
`brics` tui to see all connected "sync" and .mbl before running.
`brics` tui to see all connected "sync" and .mbl before running.
2 changes: 1 addition & 1 deletion sample/calculator.mbl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
--5--37
5--37
91 changes: 68 additions & 23 deletions src/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,69 @@ import Data.Serialize ( Serialize )
import Data.Default ( def )
import qualified Pipes as P
import Pipes ( (<-<) )
import qualified Control.Pipe.Serialize as P
import qualified Control.Pipe.Socket as DP
import qualified Control.Monad as M
import Control.Monad.Trans.Class ( lift )
import Control.Pipe.Serialize ( serializer
, deserializer
)
import qualified Control.Concurrent.Chan as Chan

data Command = Hello MBL
| Start
| TriggerStart
| List
deriving ( Generic, Show )


instance Serialize Command

type Registry = Cursor.ListCursor MBL
type Registry = Cursor.ListCursor (MBL, Chan.Chan MBL)

data Response = Ok
data Response = Started Int
| Start MBL
| Listed [MBL]
deriving ( Generic, Show )

{-
Command | Response
Hello | await.... Start
TriggerStart | Started
List | Listed
-}

instance Serialize Response

handleCommands :: Con.MVar Registry -> Command -> IO Response
handleCommands registryVar command = case command of
Hello mbl -> Con.modifyMVar registryVar
$ \registry -> return $ (Cursor.listCursorAppend mbl registry, Ok)
Start -> error "can't start. Did not program it"
List -> Listed <$> Cursor.rebuildListCursor <$> Con.readMVar registryVar
handleCommands :: Con.MVar Registry -> DP.Handler ()
handleCommands registryVar reader writer =
P.runEffect
$ writer
<-< serializer
<-< commandExecuter
<-< deserializer
<-< reader
where
commandExecuter = M.forever $ do
command <- P.await
case command of
List -> do
registry <- lift $ Con.readMVar registryVar
let mbls = fst <$> Cursor.rebuildListCursor registry
P.yield $ Listed mbls
TriggerStart -> do
registry <- lift $ Con.readMVar registryVar
let n = Cursor.listCursorLength registry
lift $ M.forM_ (Cursor.rebuildListCursor registry) $ \reg ->
Chan.writeChan (snd reg) (fst reg)
P.yield $ Started n
lift $ Con.modifyMVar_ registryVar $ \_ -> pure $ Cursor.emptyListCursor
Hello mbl -> do
newChan <- lift Chan.newChan
let newReg = (mbl, newChan)
lift $ Con.modifyMVar_ registryVar $ \reg ->
pure $ Cursor.listCursorAppend newReg reg
newMbl <- lift $ Chan.readChan newChan
P.yield $ Start newMbl


main :: IO ()
main = do
Expand All @@ -57,28 +94,36 @@ main = do
C.Daemon (C.DaemonConfiguration config' remote) -> do
let port = C.port remote
let host = C.host remote
let options = def { D.daemonPort = port, D.printOnDaemonStarted = False }
state <- Con.newMVar Cursor.emptyListCursor
let options = def { D.daemonPort = port, D.printOnDaemonStarted = False } -- TODO duplicated!
if host == def
then D.ensureDaemonRunning "marble-os" options (handleCommands state)
then do
state <- Con.newMVar Cursor.emptyListCursor
D.ensureDaemonWithHandlerRunning "marble-os"
options
(handleCommands state)
else pure ()
res <- D.runClient (T.unpack $ C.unHost host) port (command)
print (res :: Maybe Response)
where
command :: Command
command = case config' of
C.List -> List
C.Start -> Start
C.Start -> TriggerStart
C.Sync (C.SyncConfiguration config' remote) -> do
let port = C.port remote
let port = C.port remote
let host = (T.unpack $ C.unHost $ C.host remote)
let options = def { D.daemonPort = port, D.printOnDaemonStarted = False } -- TODO duplicated!
if host == def
then do
state <- Con.newMVar Cursor.emptyListCursor
D.ensureDaemonWithHandlerRunning "marble-os"
options
(handleCommands state)
else pure ()
contents <- BS.readFile $ T.unpack $ C.path config'
mbl <- either (fail) pure $ parse config' contents
D.runClientWithHandler (T.unpack $ C.unHost $ C.host remote) port
$ \reader writer -> P.runEffect $ do
writer <-< P.serializer <-< P.yield (Hello mbl)
(M.forever $ P.await >>= \(res :: Maybe Response) ->
lift (print (Just res))
)
<-< P.deserializer
<-< reader
res <- D.runClient host port (Hello mbl)
case res of
Just (Start newMbl) -> interpret config' newMbl
_ -> fail "TODO"

3 changes: 2 additions & 1 deletion src/Configuration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ data RunConfiguration = RunConfiguration
, tick :: D.Duration
, delimiter :: Delimiter }


data SyncConfiguration = SyncConfiguration RunConfiguration Remote

data DaemonConfiguration = DaemonConfiguration DaemonSubConfiguration Remote

data DaemonSubConfiguration = List | Start | Restart
data DaemonSubConfiguration = List | Start

data Remote = Remote { port :: Port , host :: Host }

Expand Down

0 comments on commit b92b3e0

Please sign in to comment.