Skip to content

Commit

Permalink
improve test message handling
Browse files Browse the repository at this point in the history
- always qos 1 and no retain flag
- send to presence topic, if set
  • Loading branch information
ostrya committed Oct 24, 2020
1 parent 1e2845c commit 6ff055d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@
import static org.ostrya.presencepublisher.ui.preference.connection.RetainFlagPreference.RETAIN_FLAG;
import static org.ostrya.presencepublisher.ui.preference.connection.UseTlsPreference.USE_TLS;
import static org.ostrya.presencepublisher.ui.preference.connection.UsernamePreference.USERNAME;
import static org.ostrya.presencepublisher.ui.preference.schedule.PresenceTopicPreference.PRESENCE_TOPIC;

public class MqttService {
private static final String TAG = "MqttService";

private static final byte[] TEST_PAYLOAD = {0x74, 0x65, 0x73, 0x74};

private final AndroidSslSocketFactoryFactory factory;
private final SharedPreferences sharedPreferences;
private final SharedPreferences securePreferences;
Expand All @@ -40,6 +43,31 @@ public MqttService(Context context) {
securePreferences = SecurePreferencesHelper.getSecurePreferences(applicationContext);
}

public void sendTestMessage() throws MqttException {
HyperLog.i(TAG, "Sending test message to server");
boolean tls = sharedPreferences.getBoolean(USE_TLS, false);
String clientCertAlias = sharedPreferences.getString(CLIENT_CERTIFICATE, null);
String login = sharedPreferences.getString(USERNAME, "");
String password = securePreferences.getString(PASSWORD, "");
String topic = sharedPreferences.getString(PRESENCE_TOPIC, "test");

try (MqttClient mqttClient = new MqttClient(getMqttUrl(tls), Settings.Secure.ANDROID_ID, new MemoryPersistence())) {
MqttConnectOptions options = new MqttConnectOptions();
options.setConnectionTimeout(5);
if (!login.isEmpty() && !password.isEmpty()) {
options.setUserName(login);
options.setPassword(password.toCharArray());
}
if (tls) {
options.setSocketFactory(factory.getSslSocketFactory(clientCertAlias));
}
mqttClient.connect(options);
mqttClient.publish(topic, TEST_PAYLOAD, 1, false);
mqttClient.disconnect(5);
}
HyperLog.i(TAG, "Sending messages was successful");
}

public void sendMessages(List<Message> messages) throws MqttException {
HyperLog.i(TAG, "Sending " + messages.size() + " messages to server");
boolean tls = sharedPreferences.getBoolean(USE_TLS, false);
Expand All @@ -49,24 +77,21 @@ public void sendMessages(List<Message> messages) throws MqttException {
int qos = getQosFromString(sharedPreferences.getString(QOS_VALUE, null));
boolean retain = sharedPreferences.getBoolean(RETAIN_FLAG, false);

MqttClient mqttClient = new MqttClient(getMqttUrl(tls), Settings.Secure.ANDROID_ID, new MemoryPersistence());
MqttConnectOptions options = new MqttConnectOptions();
options.setConnectionTimeout(5);
if (!login.isEmpty() && !password.isEmpty()) {
options.setUserName(login);
options.setPassword(password.toCharArray());
}
if (tls) {
options.setSocketFactory(factory.getSslSocketFactory(clientCertAlias));
}
try {
try (MqttClient mqttClient = new MqttClient(getMqttUrl(tls), Settings.Secure.ANDROID_ID, new MemoryPersistence())) {
MqttConnectOptions options = new MqttConnectOptions();
options.setConnectionTimeout(5);
if (!login.isEmpty() && !password.isEmpty()) {
options.setUserName(login);
options.setPassword(password.toCharArray());
}
if (tls) {
options.setSocketFactory(factory.getSslSocketFactory(clientCertAlias));
}
mqttClient.connect(options);
for (Message message : messages) {
mqttClient.publish(message.getTopic(), message.getContent().getBytes(Charset.forName("UTF-8")), qos, retain);
}
} finally {
mqttClient.disconnect(5);
mqttClient.close(true);
}
HyperLog.i(TAG, "Sending messages was successful");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@
import org.ostrya.presencepublisher.R;
import org.ostrya.presencepublisher.mqtt.MqttService;

import java.util.Collections;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import static org.ostrya.presencepublisher.message.Message.messageForTopic;

public class CheckConnectionDialogFragment extends DialogFragment {
private static final String TAG = "CheckConnectionDialogFragment";

Expand Down Expand Up @@ -67,7 +64,7 @@ private ConnectionTestWorker(AlertDialog alertDialog) {
public void run() {
String message;
try {
mqttService.sendMessages(Collections.singletonList(messageForTopic("test").withContent("test")));
mqttService.sendTestMessage();
message = getResources().getString(R.string.dialog_check_connection_summary_success);
} catch (MqttException | RuntimeException e) {
HyperLog.w(TAG, "Error while sending message", e);
Expand Down

0 comments on commit 6ff055d

Please sign in to comment.