Skip to content

Commit db362db

Browse files
authored
Merge pull request #17 from kraigmckernan/kmckernan-test-conduit
Allow users to create their own client
2 parents de67fba + a473a69 commit db362db

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/Network/GRPC/MQTT/RemoteClient.hs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
-}
66
{-# LANGUAGE RecordWildCards #-}
77

8-
module Network.GRPC.MQTT.RemoteClient (runRemoteClient) where
8+
module Network.GRPC.MQTT.RemoteClient (runRemoteClient, runRemoteClientWithConnect) where
99

1010
import Relude
1111

@@ -113,10 +113,25 @@ runRemoteClient ::
113113
-- | A map from gRPC method names to functions that can make requests to an appropriate gRPC server
114114
MethodMap ->
115115
IO ()
116-
runRemoteClient logger cfg baseTopic methodMap = do
116+
runRemoteClient = runRemoteClientWithConnect connectMQTT
117+
118+
runRemoteClientWithConnect ::
119+
-- | A function that creates a client for us to use
120+
(MQTTGRPCConfig -> IO MQTTClient) ->
121+
Logger ->
122+
-- | MQTT configuration for connecting to the MQTT broker
123+
MQTTGRPCConfig ->
124+
-- | Base topic which should uniquely identify the device
125+
Topic ->
126+
-- | A map from gRPC method names to functions that can make requests to an appropriate gRPC server
127+
MethodMap ->
128+
IO ()
129+
runRemoteClientWithConnect connect logger cfg baseTopic methodMap = do
117130
sharedSessionMap <- newTVarIO mempty
118131
let gatewayConfig = cfg{_msgCB = gatewayHandler sharedSessionMap}
119-
bracket (connectMQTT gatewayConfig) normalDisconnect $ \gatewayMQTTClient -> do
132+
133+
logInfo logger "Connecting to MQTT Broker"
134+
bracket (connect gatewayConfig) normalDisconnect $ \gatewayMQTTClient -> do
120135
logInfo logger "Connected to MQTT Broker"
121136

122137
handle (logException "MQTT client connection") $ do
@@ -163,7 +178,6 @@ runRemoteClient logger cfg baseTopic methodMap = do
163178
Nothing -> logInfo taggedLogger "Received control message for non-existant session"
164179
Just session -> controlMsgHandler taggedLogger session mqttMessage
165180
_ -> logErr logger $ "Failed to parse topic: " <> unTopic topic
166-
167181
logException :: Text -> SomeException -> IO ()
168182
logException name e =
169183
logErr logger $

0 commit comments

Comments
 (0)