Skip to content

Instantly share code, notes, and snippets.

@mmitou
Last active December 29, 2015 09:49
Show Gist options
  • Select an option

  • Save mmitou/7652896 to your computer and use it in GitHub Desktop.

Select an option

Save mmitou/7652896 to your computer and use it in GitHub Desktop.
DBusのサンプル
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Concurrent
import Control.Monad (forever, when)
import Data.IORef
import Data.Int
import Data.ByteString.Char8
import System.Exit
import DBus
import DBus.Client
incCounter :: IORef Int -> IO Int32
incCounter counterRef = do
y <- atomicModifyIORef counterRef (\x -> (x+1, x+1))
return $ fromIntegral y
hello :: IORef Int -> IO ByteString
hello counterRef = do
x <- readIORef counterRef
return $ pack ("hello" ++ show x)
incThread :: IORef Int -> IO ()
incThread counterRef = forever $ do
atomicModifyIORef counterRef (\x -> (x+1, x+1))
threadDelay (1000000)
isEven :: IORef Int -> IO Bool
isEven counterRef = readIORef counterRef >>= return . even
echo :: IORef Int -> String -> IO String
echo counterRef input = do
x <- readIORef counterRef
return $ input ++ " " ++ show x
main :: IO ()
main = do
counterRef <- newIORef (0 :: Int)
client <- connectSession
requestResult <- requestName client "com.worksap.ccms" []
when (requestResult /= NamePrimaryOwner) $ do
-- putStrLn "Another service owns the \"com.worksap.ccms\" bus name"
exitFailure
forkIO $ incThread counterRef
export client "/com/worksap/ccms/counter"
[ autoMethod "com.worksap.ccms.counter" "Increment" (incCounter counterRef)
, autoMethod "com.worksap.ccms.counter" "Hello" (hello counterRef)
, autoMethod "com.worksap.ccms.counter" "IsEven" (isEven counterRef)
, autoMethod "com.worksap.ccms.counter" "Echo" (echo counterRef)
]
forever $ threadDelay 50000
return ()
@mmitou

mmitou commented Nov 26, 2013

Copy link
Copy Markdown
Author

dbus-send --session --dest=com.worksap.ccms --print-reply /com/worksap/ccms/counter com.worksap.ccms.counter.Hello

@mmitou

mmitou commented Nov 26, 2013

Copy link
Copy Markdown
Author

dbus-send --session --dest=com.worksap.ccms --print-reply /com/worksap/ccms/counter com.worksap.ccms.counter.Echo string:hohogege

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment