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

Send mobile notification msg in a thread. #1983

Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,35 @@
import bisq.network.NetworkOptionKeys;
import bisq.network.http.HttpClient;

import bisq.common.UserThread;
import bisq.common.app.Version;
import bisq.common.util.Utilities;

import com.google.gson.Gson;

import com.google.inject.Inject;

import javax.inject.Named;

import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;

import org.apache.commons.codec.binary.Hex;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;

import java.util.UUID;
import java.util.function.Consumer;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import org.jetbrains.annotations.NotNull;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

@Slf4j
Expand All @@ -57,6 +68,9 @@ public class MobileNotificationService {
private final MobileMessageEncryption mobileMessageEncryption;
private final MobileNotificationValidator mobileNotificationValidator;
private final HttpClient httpClient;

private final ListeningExecutorService executorService = Utilities.getListeningExecutorService(
"MobileNotificationService", 10, 15, 10 * 60);
@Getter
private final MobileModel mobileModel;

Expand Down Expand Up @@ -114,11 +128,8 @@ public boolean applyKeyAndToken(String keyAndToken) {
preferences.setPhoneKeyAndToken(keyAndToken);
if (!setupConfirmationSent) {
try {
boolean success = sendConfirmationMessage();
if (success)
setupConfirmationSent = true;
else
log.warn("sendConfirmationMessage failed");
sendConfirmationMessage();
setupConfirmationSent = true;
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -129,7 +140,35 @@ public boolean applyKeyAndToken(String keyAndToken) {
}
}

public boolean sendMessage(MobileMessage message, boolean useSound) throws Exception {
/**
*
* @param message The message to send
* @param useSound If a sound should be used on the mobile device.
* @return Returns true if the message was sent. It does not reflect if the sending was successful.
* The result and error handlers carry that information.
* @throws Exception
*/
public boolean sendMessage(MobileMessage message,
boolean useSound) throws Exception {
return sendMessage(message, useSound,
result -> log.debug("sendMessage result=" + result),
throwable -> log.error("sendMessage failed. throwable=" + throwable.toString()));
}

/**
*
* @param message The message to send
* @param useSound If a sound should be used on the mobile device.
* @param resultHandler The result of the send operation (sent on a custom thread)
* @param errorHandler Carries the throwable if an error occurred at sending (sent on a custom thread)
* @return Returns true if the message was sent. It does not reflect if the sending was successful.
* The result and error handlers carry that information.
* @throws Exception
*/
private boolean sendMessage(MobileMessage message,
boolean useSound,
Consumer<String> resultHandler,
Consumer<Throwable> errorHandler) throws Exception {
log.info("Send message: '{}'", message.getMessage());
if (mobileModel.getKey() == null)
return false;
Expand Down Expand Up @@ -180,14 +219,16 @@ public boolean sendMessage(MobileMessage message, boolean useSound) throws Excep
log.info("key = " + mobileModel.getKey());
log.info("iv = " + iv);
log.info("encryptedJson = " + cipher);
return doSendMessage(iv, cipher, useSound);

doSendMessage(iv, cipher, useSound, resultHandler, errorHandler);
return true;
}

public boolean sendEraseMessage() throws Exception {
public void sendEraseMessage() throws Exception {
MobileMessage message = new MobileMessage("",
"",
MobileMessageType.ERASE);
return sendMessage(message, false);
sendMessage(message, false);
}

public void reset() {
Expand All @@ -197,15 +238,19 @@ public void reset() {
}


private boolean sendConfirmationMessage() throws Exception {
private void sendConfirmationMessage() throws Exception {
log.info("sendConfirmationMessage");
MobileMessage message = new MobileMessage("",
"",
MobileMessageType.SETUP_CONFIRMATION);
return sendMessage(message, true);
sendMessage(message, true);
}

private boolean doSendMessage(String iv, String cipher, boolean useSound) throws Exception {
private void doSendMessage(String iv,
String cipher,
boolean useSound,
Consumer<String> resultHandler,
Consumer<Throwable> errorHandler) throws Exception {
String msg;
if (mobileModel.getOs() == null)
throw new RuntimeException("No mobileModel OS set");
Expand Down Expand Up @@ -244,9 +289,26 @@ private boolean doSendMessage(String iv, String cipher, boolean useSound) throws
log.info("Send: isAndroid={}\nuseSound={}\ntokenAsHex={}\nmsgAsHex={}",
isAndroid, useSound, tokenAsHex, msgAsHex);

String result = httpClient.requestWithGET(param, "User-Agent", "bisq/" +
Version.VERSION + ", uid:" + httpClient.getUid());
log.info("result: " + result);
return result.equals(SUCCESS);
String threadName = "sendMobileNotification-" + msgAsHex.substring(0, 5) + "...";
ListenableFuture<String> future = executorService.submit(() -> {
Thread.currentThread().setName(threadName);
String result = httpClient.requestWithGET(param, "User-Agent",
"bisq/" + Version.VERSION + ", uid:" + httpClient.getUid());
log.info("sendMobileNotification result: " + result);
checkArgument(result.equals(SUCCESS), "Result was not 'success'. result=" + result);
return result;
});

Futures.addCallback(future, new FutureCallback<>() {
@Override
public void onSuccess(String result) {
UserThread.execute(() -> resultHandler.accept(result));
}

@Override
public void onFailure(@NotNull Throwable throwable) {
UserThread.execute(() -> errorHandler.accept(throwable));
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ else if (!isFiatCurrency && !isSellOffer)
shortOfferId,
MobileMessageType.MARKET);
try {
boolean success = mobileNotificationService.sendMessage(message);
if (success) {
// In case we have disabled alerts we do not get a success msg back and we do not
boolean wasSent = mobileNotificationService.sendMessage(message);
if (wasSent) {
// In case we have disabled alerts wasSent is false and we do not
// persist the offer
marketAlertFilter.addAlertId(alertId);
user.persist();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,7 @@ private void onNoWebCam() {

private void onErase() {
try {
boolean success = mobileNotificationService.sendEraseMessage();
if (!success)
log.warn("Erase message sending did not succeed");
mobileNotificationService.sendEraseMessage();
reset();
} catch (Exception e) {
new Popup<>().error(e.toString()).show();
Expand Down