git.iko.soy/distributed-process
on master help sign in
Actually, Static constructor should not be exposed
commit13ac8e7badece55bc84dfde3971a74ff66b5a02d authorEdsko de Vries <edsko@well-typed.com> dateThu 2 Aug 2012, 12:28 (14 years ago) parent8550105
6 files changed+83−41
distributed-process-azure/distributed-process-azure.cabal+6−2 distributed-process-azure/src/Control/Distributed/Process/Backend/Azure.hs+42−21 distributed-process-azure/src/Control/Distributed/Process/Backend/Azure/CommandLine.hs+10−7 distributed-process-azure/src/Control/Distributed/Process/Backend/Azure/GenericMain.hs+23−9 distributed-process/ChangeLog+1−1 distributed-process/src/Control/Distributed/Process.hs+1−1
distributed-process-azure/distributed-process-azure.cabal+6−2
@@ -29,10 +29,14 @@ Library
2929 distributed-process >= 0.2 && < 0.3,
3030 binary >= 0.5 && < 0.6,
3131 network-transport-tcp >= 0.2 && < 0.3,
32− optparse-applicative >= 0.2 && < 0.4
32+ optparse-applicative >= 0.2 && < 0.4,
33+ transformers >= 0.3 && < 0.4
3334 Exposed-modules: Control.Distributed.Process.Backend.Azure,
3435 Control.Distributed.Process.Backend.Azure.GenericMain
35− Extensions: ViewPatterns
36+ Extensions: ViewPatterns,
37+ RankNTypes,
38+ ExistentialQuantification,
39+ ScopedTypeVariables
3640 ghc-options: -Wall
3741 HS-Source-Dirs: src
3842
distributed-process-azure/src/Control/Distributed/Process/Backend/Azure.hs+42−21
@@ -1,3 +1,4 @@
1+{-# LANGUAGE TemplateHaskell #-}
12module Control.Distributed.Process.Backend.Azure
23 ( -- * Initialization
34 Backend(..)
@@ -13,13 +14,14 @@ import System.Environment (getEnv)
1314import System.FilePath ((</>), takeFileName)
1415import System.Environment.Executable (getExecutablePath)
1516import System.Posix.Types (Fd)
16−import Data.Binary (encode)
17+import Data.Binary (encode, decode)
1718import Data.Digest.Pure.MD5 (md5, MD5Digest)
18−import qualified Data.ByteString.Lazy as BSL (readFile)
19−import qualified Data.ByteString.Lazy.Char8 as BSLC (putStr)
19+import qualified Data.ByteString.Lazy as BSL (readFile, putStr)
20+import Data.Typeable (Typeable)
2021import Control.Applicative ((<$>))
2122import Control.Monad (void)
2223import Control.Exception (catches, Handler(Handler))
24+import Control.Monad.IO.Class (liftIO)
2325
2426-- Azure
2527import Network.Azure.ServiceManagement
@@ -61,7 +63,23 @@ import qualified Network.SSH.Client.LibSSH2.ByteString.Lazy as SSHBS
6163import Control.Distributed.Process
6264 ( Closure
6365 , Process
66+ , Static
6467 )
68+import Control.Distributed.Process.Closure
69+ ( remotable
70+ , mkClosure
71+ , cpBind
72+ , SerializableDict(SerializableDict)
73+ )
74+import Control.Distributed.Process.Serializable (Serializable)
75+
76+encodeToStdout :: Serializable a => a -> Process ()
77+encodeToStdout = liftIO . BSL.putStr . encode
78+
79+encodeToStdoutDict :: SerializableDict a -> a -> Process ()
80+encodeToStdoutDict SerializableDict = encodeToStdout
81+
82+remotable ['encodeToStdoutDict]
6583
6684-- | Azure backend
6785data Backend = Backend {
@@ -71,8 +89,8 @@ data Backend = Backend {
7189 , copyToVM :: VirtualMachine -> IO ()
7290 -- | Check the MD5 hash of the remote executable
7391 , checkMD5 :: VirtualMachine -> IO Bool
74− -- | @runOnVM vm port p@ starts a CH node on port 'port' and runs 'p'
75− , runOnVM :: VirtualMachine -> String -> Closure (Process ()) -> IO ()
92+ -- | @runOnVM vm port p bg@ starts a CH node on port 'port' and runs 'p'
93+ , callOnVM :: forall a. Serializable a => Static (SerializableDict a) -> VirtualMachine -> String -> Closure (Process a) -> IO a
7694 }
7795
7896data AzureParameters = AzureParameters {
@@ -120,7 +138,7 @@ initializeBackend params = do
120138 cloudServices = Azure.cloudServices setup
121139 , copyToVM = apiCopyToVM params
122140 , checkMD5 = apiCheckMD5 params
123− , runOnVM = apiRunOnVM params
141+ , callOnVM = apiCallOnVM params
124142 }
125143
126144-- | Start a CH node on the given virtual machine
@@ -129,21 +147,24 @@ apiCopyToVM params vm =
129147 void . withSSH2 params vm $ \fd s -> catchSshError s $
130148 SSH.scpSendFile fd s 0o700 (azureSshLocalPath params) (azureSshRemotePath params)
131149
132−-- | Start the executable on the remote machine
133−apiRunOnVM :: AzureParameters -> VirtualMachine -> String -> Closure (Process ()) -> IO ()
134−apiRunOnVM params vm port proc =
135− void . withSSH2 params vm $ \fd s -> do
136− let exe = "PATH=. " ++ azureSshRemotePath params
137− ++ " onvm run "
138− ++ " --host " ++ vmIpAddress vm
139− ++ " --port " ++ port
140− ++ " 2>&1"
141− (_, r) <- SSH.withChannel (SSH.openChannelSession s) id fd s $ \ch -> do
142− SSH.retryIfNeeded fd s $ SSH.channelExecute ch exe
143− SSHBS.writeChannel fd ch (encode proc)
144− SSH.channelSendEOF ch
145− SSHBS.readAllChannel fd ch
146− BSLC.putStr r
150+-- | Call a process on a VM
151+apiCallOnVM :: Serializable a => AzureParameters -> Static (SerializableDict a) -> VirtualMachine -> String -> Closure (Process a) -> IO a
152+apiCallOnVM params dict vm port proc =
153+ withSSH2 params vm $ \fd s -> do
154+ let exe = "PATH=. " ++ azureSshRemotePath params
155+ ++ " onvm run "
156+ ++ " --host " ++ vmIpAddress vm
157+ ++ " --port " ++ port
158+ ++ " 2>&1"
159+ (_, r) <- SSH.withChannel (SSH.openChannelSession s) id fd s $ \ch -> do
160+ SSH.retryIfNeeded fd s $ SSH.channelExecute ch exe
161+ SSHBS.writeChannel fd ch (encode proc)
162+ SSH.channelSendEOF ch
163+ SSHBS.readAllChannel fd ch
164+ return (decode r)
165+ where
166+ proc' :: Closure (Process ())
167+ proc' = proc `cpBind` undefined
147168
148169-- | Check the MD5 hash of the executable on the remote machine
149170apiCheckMD5 :: AzureParameters -> VirtualMachine -> IO Bool
distributed-process-azure/src/Control/Distributed/Process/Backend/Azure/CommandLine.hs+10−7
@@ -1,16 +1,19 @@
11{-# LANGUAGE TemplateHaskell #-}
22
33import Control.Monad.IO.Class (liftIO)
4−import Control.Distributed.Process (Process)
5−import Control.Distributed.Process.Closure (remotable, mkClosure)
6−import Control.Distributed.Process.Backend.Azure.GenericMain (genericMain)
4+import Control.Distributed.Process (Process, ProcessId, getSelfPid)
5+import Control.Distributed.Process.Closure (remotable, mkClosure, sdictProcessId)
6+import Control.Distributed.Process.Backend.Azure.GenericMain
7+ ( genericMain
8+ , ProcessPair(..)
9+ )
710
8−cprint :: String -> Process ()
9−cprint = liftIO . putStrLn
11+getPid :: () -> Process ProcessId
12+getPid () = getSelfPid
1013
11−remotable ['cprint]
14+remotable ['getPid]
1215
1316main = genericMain __remoteTable $ \cmd ->
1417 case cmd of
15− "hello" -> return $ $(mkClosure 'cprint) "Hi world!"
18+ "hello" -> return $ ProcessPair ($(mkClosure 'getPid) ()) print sdictProcessId
1619 _ -> error "unknown command"
distributed-process-azure/src/Control/Distributed/Process/Backend/Azure/GenericMain.hs+23−9
@@ -1,5 +1,8 @@
11-- | Generic main
2−module Control.Distributed.Process.Backend.Azure.GenericMain (genericMain) where
2+module Control.Distributed.Process.Backend.Azure.GenericMain
3+ ( genericMain
4+ , ProcessPair(..)
5+ ) where
36
47import Prelude hiding (catch)
58import System.Exit (exitSuccess, exitFailure)
@@ -20,7 +23,7 @@ import Control.Distributed.Process.Backend.Azure
2023 , cloudServices
2124 , CloudService(cloudServiceName, cloudServiceVMs)
2225 , VirtualMachine(vmName)
23− , Backend(copyToVM, checkMD5, runOnVM)
26+ , Backend(copyToVM, checkMD5, callOnVM)
2427 )
2528import qualified Network.SSH.Client.LibSSH2.Foreign as SSH
2629 ( initialize
@@ -49,16 +52,25 @@ import Control.Distributed.Process
4952 , Closure
5053 , Process
5154 , unClosure
55+ , Static
5256 )
5357import Control.Distributed.Process.Node (newLocalNode, runProcess, initRemoteTable)
58+import Control.Distributed.Process.Serializable (Serializable)
59+import Control.Distributed.Process.Closure (SerializableDict)
5460import Network.Transport.TCP (createTransport, defaultTCPParameters)
5561
5662--------------------------------------------------------------------------------
5763-- Main --
5864--------------------------------------------------------------------------------
5965
60−genericMain :: (RemoteTable -> RemoteTable) -- ^ Standard CH remote table
61− -> (String -> IO (Closure (Process ()))) -- ^ Closures to support in 'run'
66+data ProcessPair b = forall a. Serializable a => ProcessPair {
67+ ppairRemote :: Closure (Process a)
68+ , ppairLocal :: a -> IO b
69+ , ppairDict :: Static (SerializableDict a)
70+ }
71+
72+genericMain :: (RemoteTable -> RemoteTable) -- ^ Standard CH remote table
73+ -> (String -> IO (ProcessPair ())) -- ^ Closures to support in 'run'
6274 -> IO ()
6375genericMain remoteTable cmds = do
6476 _ <- SSH.initialize True
@@ -90,13 +102,15 @@ genericMain remoteTable cmds = do
90102 then exitSuccess
91103 else exitFailure
92104 RunOn {} -> do
93− closure <- cmds (closureId cmd)
94− params <- azureParameters (azureOptions cmd) (Just (sshOptions cmd))
95− backend <- initializeBackend params
96− css <- cloudServices backend
105+ procPair <- cmds (closureId cmd)
106+ params <- azureParameters (azureOptions cmd) (Just (sshOptions cmd))
107+ backend <- initializeBackend params
108+ css <- cloudServices backend
97109 forM_ (findTarget (target cmd) css) $ \vm -> do
98110 putStr (vmName vm ++ ": ") >> hFlush stdout
99− runOnVM backend vm (remotePort cmd) closure
111+ case procPair of
112+ ProcessPair rProc lProc dict ->
113+ callOnVM backend dict vm (remotePort cmd) rProc >>= lProc
100114 OnVmCommand (vmCmd@OnVmRun {}) -> do
101115 onVmRun (remoteTable initRemoteTable) (onVmIP vmCmd) (onVmPort vmCmd)
102116 SSH.exit
distributed-process/ChangeLog+1−1
@@ -1,6 +1,6 @@
112012-08-02 Edsko de Vries <edsko@well-typed.com> 0.2.2.1
22
3−* Expose the constructors of Closure and Static
3+* Expose the constructors of Closure
44* Improved docs
55
662012-07-31 Edsko de Vries <edsko@well-typed.com> 0.2.2.0
distributed-process/src/Control/Distributed/Process.hs+1−1
@@ -64,7 +64,7 @@ module Control.Distributed.Process
6464 , DiedReason(..)
6565 -- * Closures
6666 , Closure(..)
67− , Static(..)
67+ , Static
6868 , unClosure
6969 , RemoteTable
7070 -- * Logging