net-mqtt-rpc 0.1.0.0 → 0.1.1.0
raw patch · 3 files changed
+19/−11 lines, 3 filesdep +exceptionsPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
Dependencies added: exceptions
API changes (from Hackage documentation)
- Network.MQTT.RPC: call :: MQTTClient -> Topic -> ByteString -> IO ByteString
+ Network.MQTT.RPC: call :: (MonadCatch m, MonadThrow m, MonadIO m) => MQTTClient -> Topic -> ByteString -> m ByteString
Files
- app/Main.hs +5/−1
- net-mqtt-rpc.cabal +4/−2
- src/Network/MQTT/RPC.hs +10/−8
app/Main.hs view
@@ -4,6 +4,7 @@ module Main where +import Control.Monad (when) import qualified Data.ByteString.Lazy as BL import Data.Maybe (fromJust) import Network.MQTT.Client@@ -12,13 +13,14 @@ import Options.Applicative (Parser, execParser, fullDesc, help, helper, info, long, maybeReader, option, progDesc, short, showDefault, strOption,- value, (<**>))+ switch, value, (<**>)) import System.IO (stdout) data Options = Options { optUri :: URI , optTopic :: Topic , optMsg :: BL.ByteString+ , optNL :: Bool } options :: Parser Options@@ -26,11 +28,13 @@ <$> option (maybeReader parseURI) (long "mqtt-uri" <> short 'u' <> showDefault <> value (fromJust $ parseURI "mqtt://localhost/") <> help "mqtt broker URI") <*> strOption (long "mqtt-topic" <> short 't' <> showDefault <> value "tmp/rpctest" <> help "MQTT request topic") <*> strOption (long "mqtt-msg" <> short 'm' <> showDefault <> value "" <> help "Request Message")+ <*> switch (long "newline" <> help "add a newline after outputting the message") run :: Options -> IO () run Options{..} = do mc <- connectURI mqttConfig{_protocol=Protocol50} optUri BL.hPut stdout =<< call mc optTopic optMsg+ when optNL $ putStrLn "" main :: IO () main = run =<< execParser opts
net-mqtt-rpc.cabal view
@@ -4,10 +4,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: 2c985a6c1130dcb54d621da343ccbfb4aefad30a6fab33e6bd43c33f66915674+-- hash: da1d3fe3c6516d04fb3e2f8b30e4a1b7d45d7533d80c5e4aea13b9cf4a2d4b3e name: net-mqtt-rpc-version: 0.1.0.0+version: 0.1.1.0 synopsis: Make RPC calls via an MQTT broker. description: Please see the README on GitHub at <https://github.com/dustin/net-mqtt-rpc#readme> category: Network@@ -39,6 +39,7 @@ build-depends: base >=4.7 && <5 , bytestring+ , exceptions , net-mqtt >=0.6.2.0 && <0.7.0.0 , random , stm@@ -57,6 +58,7 @@ build-depends: base >=4.7 && <5 , bytestring+ , exceptions , net-mqtt >=0.6.2.0 && <0.7.0.0 , net-mqtt-rpc , network-uri
src/Network/MQTT/RPC.hs view
@@ -5,11 +5,13 @@ module Network.MQTT.RPC (call) where -import Control.Concurrent.STM (atomically, newTChanIO,- readTChan, writeTChan)-import qualified Control.Exception as E+import Control.Concurrent.STM (atomically, newTChanIO, readTChan,+ writeTChan)+import Control.Monad (when)+import Control.Monad.Catch (MonadCatch (..), MonadThrow (..),+ bracket, throwM)+import Control.Monad.IO.Class (MonadIO (..)) import qualified Data.ByteString.Lazy as BL-import Control.Monad (when) import Data.Text (Text) import qualified Data.Text.Encoding as TE import qualified Data.UUID as UUID@@ -27,14 +29,14 @@ -- Note that this client provides no timeouts or retries. MQTT will -- guarantee the request message is delivered to the broker, but if -- there's nothing to pick it up, there may never be a response.-call :: MQTTClient -> Topic -> BL.ByteString -> IO BL.ByteString-call mc topic req = do+call :: (MonadCatch m, MonadThrow m, MonadIO m) => MQTTClient -> Topic -> BL.ByteString -> m BL.ByteString+call mc topic req = liftIO do r <- newTChanIO corr <- BL.fromStrict . UUID.toASCIIBytes <$> randomIO subid <- BL.fromStrict . ("$rpc/" <>) . UUID.toASCIIBytes <$> randomIO go corr subid r - where go theID theTopic r = E.bracket reg unreg rt+ where go theID theTopic r = bracket reg unreg rt where reg = do atomically $ registerCorrelated mc theID (SimpleCallback cb) subscribe mc [(blToText theTopic, subOptions)] mempty@@ -48,5 +50,5 @@ PropResponseTopic theTopic] atomically do connd <- isConnectedSTM mc- when (not connd) $ E.throw (MQTTException "disconnected")+ when (not connd) $ throwM (MQTTException "disconnected") readTChan r