example updated
authorMax Voit <max.voit+gtdv@with-eyes.net>
Thu, 18 Jun 2015 14:54:57 +0000 (16:54 +0200)
committerMax Voit <max.voit+gtdv@with-eyes.net>
Thu, 18 Jun 2015 14:54:57 +0000 (16:54 +0200)
test/HelloWorldMaster.hs
test/HelloWorldSlave.hs [new file with mode: 0644]

index e27c451..2f40b31 100644 (file)
@@ -5,10 +5,11 @@
 import           Control.Monad.Reader
 import           Control.Monad.State
 import           Data.Acid
-import           Data.Acid.MasterCentered
+import           Data.Acid.Centered
 import           Data.SafeCopy
 import           Data.Typeable
 import           System.Environment
+import           System.Exit (exitSuccess)
 
 ------------------------------------------------------
 -- The Haskell structure that we want to encapsulate
@@ -33,9 +34,19 @@ $(makeAcidic ''HelloWorldState ['writeState, 'queryState])
 
 main :: IO ()
 main = do acid <- openMasterState 3333 (HelloWorldState "Hello world")
-          args <- getArgs
-          if null args
-             then do string <- query acid QueryState
-                     putStrLn $ "The state is: " ++ string
-             else do update acid (WriteState (unwords args))
-                     putStrLn "The state has been modified!"
+          putStrLn "Possible commands: x for exit; q for query; uString for update;"
+          forever $ do
+              input <- getLine
+              let cmd = head input
+              case cmd of
+                    'x' -> do
+                        putStrLn "Bye!"
+                        closeAcidState acid
+                        exitSuccess
+                    'q' -> do
+                        string <- query acid QueryState
+                        putStrLn $ "The state is: " ++ string
+                    'u' -> do
+                        update acid (WriteState (tail input))
+                        putStrLn "The state has been modified!"
+                    _   -> putStrLn $ "Unknown command " ++ input
diff --git a/test/HelloWorldSlave.hs b/test/HelloWorldSlave.hs
new file mode 100644 (file)
index 0000000..c0a30df
--- /dev/null
@@ -0,0 +1,52 @@
+{-# LANGUAGE DeriveDataTypeable #-}
+{-# LANGUAGE TemplateHaskell    #-}
+{-# LANGUAGE TypeFamilies       #-}
+
+import           Control.Monad.Reader
+import           Control.Monad.State
+import           Data.Acid
+import           Data.Acid.Centered
+import           Data.SafeCopy
+import           Data.Typeable
+import           System.Environment
+import           System.Exit (exitSuccess)
+
+------------------------------------------------------
+-- The Haskell structure that we want to encapsulate
+
+data HelloWorldState = HelloWorldState String
+    deriving (Show, Typeable)
+
+$(deriveSafeCopy 0 'base ''HelloWorldState)
+
+------------------------------------------------------
+-- The transaction we will execute over the state.
+
+writeState :: String -> Update HelloWorldState ()
+writeState newValue
+    = put (HelloWorldState newValue)
+
+queryState :: Query HelloWorldState String
+queryState = do HelloWorldState string <- ask
+                return string
+
+$(makeAcidic ''HelloWorldState ['writeState, 'queryState])
+
+main :: IO ()
+main = do acid <- enslaveState "localhost"  3333 (HelloWorldState "Hello world")
+          putStrLn "Possible commands: x for exit; q for query; uString for update;"
+          forever $ do
+              input <- getLine
+              let cmd = head input
+              case cmd of
+                    'x' -> do
+                        putStrLn "Bye!"
+                        closeAcidState acid
+                        exitSuccess
+                    'q' -> do
+                        string <- query acid QueryState
+                        putStrLn $ "The state is: " ++ string
+                    'u'  -> do
+                        update acid (WriteState (tail input))
+                        putStrLn "The state has been modified!"
+                    _   -> putStrLn $ "Unknown command " ++ input