Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved performance by instantiating and connecting to MQTT client… #34

Merged
merged 1 commit into from
Jun 28, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions AWSAppSyncClient/AppSyncMQTTClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import Reachability
class AppSyncMQTTClient: MQTTClientDelegate {

var mqttClient = MQTTClient<AnyObject, AnyObject>()
var mqttClients = [MQTTClient<AnyObject, AnyObject>]()
var mqttClientsWithTopics = [MQTTClient<AnyObject, AnyObject>: [String]]()
var mqttClients = Set<MQTTClient<AnyObject, AnyObject>>()
var mqttClientsWithTopics = [MQTTClient<AnyObject, AnyObject>: Set<String>]()
var reachability: Reachability?
var hostURL: String?
var clientId: String?
var topicSubscribersDictionary = [String: [MQTTSubscritionWatcher]]()
var topicQueue = NSMutableSet()
var initialConnection = true
var shouldSubscribe = true
var allowCellularAccess = true
Expand All @@ -38,7 +37,6 @@ class AppSyncMQTTClient: MQTTClientDelegate {
for topic in mqttClientsWithTopics[mqttClient]! {
mqttClient.subscribe(toTopic: topic, qos: 1, extendedCallback: nil)
}
self.topicQueue = NSMutableSet()
} else if status.rawValue >= 3 {
for topic in mqttClientsWithTopics[mqttClient]! {
let subscribers = topicSubscribersDictionary[topic]
Expand Down Expand Up @@ -69,26 +67,26 @@ class AppSyncMQTTClient: MQTTClientDelegate {
client.clientDelegate = nil
client.disconnect()
}
mqttClients = []
mqttClientsWithTopics = [:]
mqttClients.removeAll()
mqttClientsWithTopics.removeAll()

for subscription in subscriptionInfo {
startNewSubscription(subscriptionInfo: subscription)
}
}

func startNewSubscription(subscriptionInfo: AWSSubscriptionInfo) {
var topicQueue = [String]()
let interestedTopics = subscriptionInfo.topics.filter({ topicSubscribersDictionary[$0] != nil })

guard !interestedTopics.isEmpty else {
return
}

let mqttClient = MQTTClient<AnyObject, AnyObject>()
mqttClient.clientDelegate = self
for topic in subscriptionInfo.topics {
if topicSubscribersDictionary[topic] != nil {
// if the client wants subscriptions and is allowed we add it to list of subscribe
topicQueue.append(topic)
}
}
mqttClients.append(mqttClient)
mqttClientsWithTopics[mqttClient] = topicQueue

mqttClients.insert(mqttClient)
mqttClientsWithTopics[mqttClient] = Set(interestedTopics)
mqttClient.connect(withClientId: subscriptionInfo.clientId, toHost: subscriptionInfo.url, statusCallback: nil)
}

Expand All @@ -99,6 +97,12 @@ class AppSyncMQTTClient: MQTTClientDelegate {
topicSubscribersDictionary.filter({ $0.value.isEmpty })
.map({ $0.key })
.forEach(unsubscribeTopic)

for (client, _) in mqttClientsWithTopics.filter({ $0.value.isEmpty }) {
client.disconnect()
mqttClientsWithTopics[client] = nil
mqttClients.remove(client)
}
}


Expand All @@ -123,8 +127,10 @@ class AppSyncMQTTClient: MQTTClientDelegate {
///
/// - Parameter topic: String
private func unsubscribeTopic(topic: String) {
mqttClientsWithTopics.filter({ $0.value.contains(topic) })
.forEach({ $0.key.unsubscribeTopic(topic) })
for (client, _) in mqttClientsWithTopics.filter({ $0.value.contains(topic) }) {
client.unsubscribeTopic(topic)
mqttClientsWithTopics[client]?.remove(topic)
}
}

/// Removes subscriber from the array using id
Expand Down