Skip to content

Commit 4bcf816

Browse files
authored
Make ConnectionInfoStore non-singleton (#97)
1 parent bd1375b commit 4bcf816

File tree

6 files changed

+33
-16
lines changed

6 files changed

+33
-16
lines changed

mqtt-client/src/main/java/com/gojek/mqtt/client/connectioninfo/ConnectionInfoStore.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.gojek.mqtt.client.connectioninfo
22

3-
internal object ConnectionInfoStore {
3+
internal class ConnectionInfoStore {
44
@Volatile
55
private var state: State = State.UninitialisedState
66

mqtt-client/src/main/java/com/gojek/mqtt/client/event/interceptor/ConnectionInfoInterceptor.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package com.gojek.mqtt.client.event.interceptor
33
import com.gojek.mqtt.client.connectioninfo.ConnectionInfoStore
44
import com.gojek.mqtt.event.MqttEvent
55

6-
internal class ConnectionInfoInterceptor : EventInterceptor {
6+
internal class ConnectionInfoInterceptor(
7+
private val connectionInfoStore: ConnectionInfoStore
8+
) : EventInterceptor {
79
override fun intercept(mqttEvent: MqttEvent): MqttEvent {
8-
mqttEvent.connectionInfo = ConnectionInfoStore.getConnectionInfo()
10+
mqttEvent.connectionInfo = connectionInfoStore.getConnectionInfo()
911
return mqttEvent
1012
}
1113
}

mqtt-client/src/main/java/com/gojek/mqtt/client/event/interceptor/MqttEventHandler.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.gojek.mqtt.client.event.interceptor
22

3+
import com.gojek.mqtt.client.connectioninfo.ConnectionInfoStore
34
import com.gojek.mqtt.event.EventHandler
45
import com.gojek.mqtt.event.MqttEvent
56
import com.gojek.mqtt.utils.MqttUtils
@@ -9,7 +10,8 @@ import java.util.concurrent.ThreadPoolExecutor
910
import java.util.concurrent.TimeUnit.SECONDS
1011

1112
internal class MqttEventHandler(
12-
mqttUtils: MqttUtils
13+
mqttUtils: MqttUtils,
14+
connectionInfoStore: ConnectionInfoStore
1315
) : EventHandler {
1416

1517
private val eventScheduler = ThreadPoolExecutor(
@@ -24,7 +26,7 @@ internal class MqttEventHandler(
2426
private val eventHandlers = CopyOnWriteArrayList<EventHandler>()
2527

2628
init {
27-
interceptorList.add(ConnectionInfoInterceptor())
29+
interceptorList.add(ConnectionInfoInterceptor(connectionInfoStore))
2830
}
2931

3032
override fun onEvent(mqttEvent: MqttEvent) {

mqtt-client/src/main/java/com/gojek/mqtt/client/factory/IAndroidMqttClientFactory.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.content.Context
44
import com.gojek.courier.logging.NoOpLogger
55
import com.gojek.keepalive.KeepAliveFailureHandler
66
import com.gojek.mqtt.client.config.v3.MqttV3Configuration
7+
import com.gojek.mqtt.client.connectioninfo.ConnectionInfoStore
78
import com.gojek.mqtt.client.internal.KeepAliveProvider
89
import com.gojek.mqtt.client.v3.IAndroidMqttClient
910
import com.gojek.mqtt.client.v3.impl.AndroidMqttClient
@@ -21,7 +22,8 @@ internal interface IAndroidMqttClientFactory {
2122
keepAliveProvider: KeepAliveProvider,
2223
keepAliveFailureHandler: KeepAliveFailureHandler,
2324
eventHandler: EventHandler,
24-
pingEventHandler: IPingSenderEvents
25+
pingEventHandler: IPingSenderEvents,
26+
connectionInfoStore: ConnectionInfoStore
2527
): IAndroidMqttClient
2628
fun createAdaptiveAndroidMqttClient(
2729
pingSender: MqttPingSender,
@@ -30,7 +32,8 @@ internal interface IAndroidMqttClientFactory {
3032
networkStateTracker: NetworkStateTracker,
3133
keepAliveProvider: KeepAliveProvider,
3234
keepAliveFailureHandler: KeepAliveFailureHandler,
33-
pingEventHandler: IPingSenderEvents
35+
pingEventHandler: IPingSenderEvents,
36+
connectionInfoStore: ConnectionInfoStore
3437
): IAndroidMqttClient
3538
}
3639

@@ -42,7 +45,8 @@ internal class AndroidMqttClientFactory : IAndroidMqttClientFactory {
4245
keepAliveProvider: KeepAliveProvider,
4346
keepAliveFailureHandler: KeepAliveFailureHandler,
4447
eventHandler: EventHandler,
45-
pingEventHandler: IPingSenderEvents
48+
pingEventHandler: IPingSenderEvents,
49+
connectionInfoStore: ConnectionInfoStore
4650
): IAndroidMqttClient {
4751
val pingSender = mqttConfiguration.pingSender
4852
pingSender.setPingEventHandler(pingEventHandler)
@@ -53,7 +57,8 @@ internal class AndroidMqttClientFactory : IAndroidMqttClientFactory {
5357
mqttPingSender = pingSender,
5458
keepAliveProvider = keepAliveProvider,
5559
keepAliveFailureHandler = keepAliveFailureHandler,
56-
eventHandler = eventHandler
60+
eventHandler = eventHandler,
61+
connectionInfoStore = connectionInfoStore
5762
)
5863
}
5964

@@ -64,7 +69,8 @@ internal class AndroidMqttClientFactory : IAndroidMqttClientFactory {
6469
networkStateTracker: NetworkStateTracker,
6570
keepAliveProvider: KeepAliveProvider,
6671
keepAliveFailureHandler: KeepAliveFailureHandler,
67-
pingEventHandler: IPingSenderEvents
72+
pingEventHandler: IPingSenderEvents,
73+
connectionInfoStore: ConnectionInfoStore
6874
): IAndroidMqttClient {
6975
pingSender.setPingEventHandler(pingEventHandler)
7076
return AndroidMqttClient(
@@ -77,7 +83,8 @@ internal class AndroidMqttClientFactory : IAndroidMqttClientFactory {
7783
isAdaptiveKAConnection = true,
7884
keepAliveProvider = keepAliveProvider,
7985
keepAliveFailureHandler = keepAliveFailureHandler,
80-
eventHandler = NoOpEventHandler()
86+
eventHandler = NoOpEventHandler(),
87+
connectionInfoStore = connectionInfoStore
8188
)
8289
}
8390
}

mqtt-client/src/main/java/com/gojek/mqtt/client/internal/MqttClientInternal.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.gojek.keepalive.OptimalKeepAliveObserver
1010
import com.gojek.keepalive.OptimalKeepAliveProvider
1111
import com.gojek.keepalive.config.AdaptiveKeepAliveConfig as AdaptiveKAConfig
1212
import com.gojek.mqtt.client.config.v3.MqttV3Configuration
13+
import com.gojek.mqtt.client.connectioninfo.ConnectionInfoStore
1314
import com.gojek.mqtt.client.event.interceptor.MqttEventHandler
1415
import com.gojek.mqtt.client.factory.getAndroidMqttClientFactory
1516
import com.gojek.mqtt.client.listener.MessageListener
@@ -43,7 +44,9 @@ internal class MqttClientInternal(
4344
private var keepAliveProvider: KeepAliveProvider = NonAdaptiveKeepAliveProvider()
4445
private var keepAliveFailureHandler: KeepAliveFailureHandler = NoOpKeepAliveFailureHandler()
4546

46-
private val eventHandler = MqttEventHandler(MqttUtils())
47+
private val connectionInfoStore = ConnectionInfoStore()
48+
49+
private val eventHandler = MqttEventHandler(MqttUtils(), connectionInfoStore)
4750

4851
private val optimalKeepAliveObserver = object : OptimalKeepAliveObserver {
4952
override fun onOptimalKeepAliveFound(
@@ -72,7 +75,8 @@ internal class MqttClientInternal(
7275
keepAliveProvider = keepAliveProvider,
7376
keepAliveFailureHandler = keepAliveFailureHandler,
7477
eventHandler = eventHandler,
75-
pingEventHandler = PingEventHandler(eventHandler)
78+
pingEventHandler = PingEventHandler(eventHandler),
79+
connectionInfoStore = connectionInfoStore
7680
)
7781
}
7882

@@ -145,7 +149,8 @@ internal class MqttClientInternal(
145149
adaptiveKeepAliveConfig.upperBoundMinutes * 60
146150
),
147151
keepAliveFailureHandler = NoOpKeepAliveFailureHandler(),
148-
pingEventHandler = AdaptivePingEventHandler(eventHandler)
152+
pingEventHandler = AdaptivePingEventHandler(eventHandler),
153+
connectionInfoStore = connectionInfoStore
149154
)
150155
}
151156
}

mqtt-client/src/main/java/com/gojek/mqtt/client/v3/impl/AndroidMqttClient.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ internal class AndroidMqttClient(
8484
private val isAdaptiveKAConnection: Boolean = false,
8585
private val keepAliveProvider: KeepAliveProvider,
8686
private val eventHandler: EventHandler,
87-
keepAliveFailureHandler: KeepAliveFailureHandler
87+
keepAliveFailureHandler: KeepAliveFailureHandler,
88+
private val connectionInfoStore: ConnectionInfoStore
8889
) : IAndroidMqttClient, IClientSchedulerBridge {
8990

9091
private val runnableScheduler: IRunnableScheduler
@@ -516,7 +517,7 @@ internal class AndroidMqttClient(
516517
}
517518

518519
if (isAdaptiveKAConnection.not()) {
519-
ConnectionInfoStore.updateConnectionInfo(
520+
connectionInfoStore.updateConnectionInfo(
520521
ConnectionInfo(
521522
clientId = mqttConnectOptions.clientId,
522523
username = mqttConnectOptions.username,

0 commit comments

Comments
 (0)