message handlers return synchronously

This commit is contained in:
woodser 2022-11-26 17:24:55 +00:00
parent a266fab6ec
commit 7062fa9e79
2 changed files with 199 additions and 209 deletions

View File

@ -227,6 +227,7 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
@Override @Override
public void onDirectMessage(DecryptedMessageWithPubKey message, NodeAddress peer) { public void onDirectMessage(DecryptedMessageWithPubKey message, NodeAddress peer) {
NetworkEnvelope networkEnvelope = message.getNetworkEnvelope(); NetworkEnvelope networkEnvelope = message.getNetworkEnvelope();
new Thread(() -> {
if (networkEnvelope instanceof InitTradeRequest) { if (networkEnvelope instanceof InitTradeRequest) {
handleInitTradeRequest((InitTradeRequest) networkEnvelope, peer); handleInitTradeRequest((InitTradeRequest) networkEnvelope, peer);
} else if (networkEnvelope instanceof InitMultisigRequest) { } else if (networkEnvelope instanceof InitMultisigRequest) {
@ -240,6 +241,7 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
} else if (networkEnvelope instanceof DepositResponse) { } else if (networkEnvelope instanceof DepositResponse) {
handleDepositResponse((DepositResponse) networkEnvelope, peer); handleDepositResponse((DepositResponse) networkEnvelope, peer);
} }
}).start();
} }

View File

@ -104,17 +104,16 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
protected void onTradeMessage(TradeMessage message, NodeAddress peerNodeAddress) { protected void onTradeMessage(TradeMessage message, NodeAddress peerNodeAddress) {
log.info("Received {} as TradeMessage from {} with tradeId {} and uid {}", message.getClass().getSimpleName(), peerNodeAddress, message.getTradeId(), message.getUid()); log.info("Received {} as TradeMessage from {} with tradeId {} and uid {}", message.getClass().getSimpleName(), peerNodeAddress, message.getTradeId(), message.getUid());
if (message instanceof DepositsConfirmedMessage) { handle(message, peerNodeAddress);
handle((DepositsConfirmedMessage) message, peerNodeAddress);
} else if (message instanceof PaymentSentMessage) {
handle((PaymentSentMessage) message, peerNodeAddress);
} else if (message instanceof PaymentReceivedMessage) {
handle((PaymentReceivedMessage) message, peerNodeAddress);
}
} }
protected void onMailboxMessage(TradeMessage message, NodeAddress peerNodeAddress) { protected void onMailboxMessage(TradeMessage message, NodeAddress peerNodeAddress) {
log.info("Received {} as MailboxMessage from {} with tradeId {} and uid {}", message.getClass().getSimpleName(), peerNodeAddress, message.getTradeId(), message.getUid()); log.info("Received {} as MailboxMessage from {} with tradeId {} and uid {}", message.getClass().getSimpleName(), peerNodeAddress, message.getTradeId(), message.getUid());
handle(message, peerNodeAddress);
}
private void handle(TradeMessage message, NodeAddress peerNodeAddress) {
new Thread(() -> {
if (message instanceof DepositsConfirmedMessage) { if (message instanceof DepositsConfirmedMessage) {
handle((DepositsConfirmedMessage) message, peerNodeAddress); handle((DepositsConfirmedMessage) message, peerNodeAddress);
} else if (message instanceof PaymentSentMessage) { } else if (message instanceof PaymentSentMessage) {
@ -122,6 +121,7 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
} else if (message instanceof PaymentReceivedMessage) { } else if (message instanceof PaymentReceivedMessage) {
handle((PaymentReceivedMessage) message, peerNodeAddress); handle((PaymentReceivedMessage) message, peerNodeAddress);
} }
}).start();
} }
@Override @Override
@ -242,7 +242,9 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
// handle trade events // handle trade events
EasyBind.subscribe(trade.stateProperty(), state -> { EasyBind.subscribe(trade.stateProperty(), state -> {
if (state == Trade.State.DEPOSIT_TXS_CONFIRMED_IN_BLOCKCHAIN) sendDepositsConfirmedMessage(); if (state == Trade.State.DEPOSIT_TXS_CONFIRMED_IN_BLOCKCHAIN) {
new Thread(() -> sendDepositsConfirmedMessage()).start();
}
}); });
// initialize trade // initialize trade
@ -256,7 +258,6 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
public void handleInitMultisigRequest(InitMultisigRequest request, NodeAddress sender) { public void handleInitMultisigRequest(InitMultisigRequest request, NodeAddress sender) {
System.out.println(getClass().getSimpleName() + ".handleInitMultisigRequest()"); System.out.println(getClass().getSimpleName() + ".handleInitMultisigRequest()");
new Thread(() -> {
synchronized (trade) { synchronized (trade) {
latchTrade(); latchTrade();
Validator.checkTradeId(processModel.getOfferId(), request); Validator.checkTradeId(processModel.getOfferId(), request);
@ -279,12 +280,10 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
.executeTasks(true); .executeTasks(true);
awaitTradeLatch(); awaitTradeLatch();
} }
}).start();
} }
public void handleSignContractRequest(SignContractRequest message, NodeAddress sender) { public void handleSignContractRequest(SignContractRequest message, NodeAddress sender) {
System.out.println(getClass().getSimpleName() + ".handleSignContractRequest() " + trade.getId()); System.out.println(getClass().getSimpleName() + ".handleSignContractRequest() " + trade.getId());
new Thread(() -> {
synchronized (trade) { synchronized (trade) {
Validator.checkTradeId(processModel.getOfferId(), message); Validator.checkTradeId(processModel.getOfferId(), message);
if (trade.getState() == Trade.State.MULTISIG_COMPLETED || trade.getState() == Trade.State.CONTRACT_SIGNATURE_REQUESTED) { if (trade.getState() == Trade.State.MULTISIG_COMPLETED || trade.getState() == Trade.State.CONTRACT_SIGNATURE_REQUESTED) {
@ -315,12 +314,10 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
}); });
} }
} }
}).start();
} }
public void handleSignContractResponse(SignContractResponse message, NodeAddress sender) { public void handleSignContractResponse(SignContractResponse message, NodeAddress sender) {
System.out.println(getClass().getSimpleName() + ".handleSignContractResponse() " + trade.getId()); System.out.println(getClass().getSimpleName() + ".handleSignContractResponse() " + trade.getId());
new Thread(() -> {
synchronized (trade) { synchronized (trade) {
Validator.checkTradeId(processModel.getOfferId(), message); Validator.checkTradeId(processModel.getOfferId(), message);
if (trade.getState() == Trade.State.CONTRACT_SIGNED) { if (trade.getState() == Trade.State.CONTRACT_SIGNED) {
@ -352,12 +349,10 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
}); });
} }
} }
}).start();
} }
public void handleDepositResponse(DepositResponse response, NodeAddress sender) { public void handleDepositResponse(DepositResponse response, NodeAddress sender) {
System.out.println(getClass().getSimpleName() + ".handleDepositResponse()"); System.out.println(getClass().getSimpleName() + ".handleDepositResponse()");
new Thread(() -> {
synchronized (trade) { synchronized (trade) {
latchTrade(); latchTrade();
Validator.checkTradeId(processModel.getOfferId(), response); Validator.checkTradeId(processModel.getOfferId(), response);
@ -382,12 +377,10 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
.executeTasks(true); .executeTasks(true);
awaitTradeLatch(); awaitTradeLatch();
} }
}).start();
} }
public void handle(DepositsConfirmedMessage response, NodeAddress sender) { public void handle(DepositsConfirmedMessage response, NodeAddress sender) {
System.out.println(getClass().getSimpleName() + ".handle(DepositsConfirmedMessage)"); System.out.println(getClass().getSimpleName() + ".handle(DepositsConfirmedMessage)");
new Thread(() -> {
synchronized (trade) { synchronized (trade) {
latchTrade(); latchTrade();
expect(new Condition(trade) expect(new Condition(trade)
@ -404,7 +397,6 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
.executeTasks(); .executeTasks();
awaitTradeLatch(); awaitTradeLatch();
} }
}).start();
} }
// received by seller and arbitrator // received by seller and arbitrator
@ -414,7 +406,6 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
log.warn("Ignoring PaymentSentMessage since not seller or arbitrator"); log.warn("Ignoring PaymentSentMessage since not seller or arbitrator");
return; return;
} }
new Thread(() -> {
// We are more tolerant with expected phase and allow also DEPOSITS_PUBLISHED as it can be the case // We are more tolerant with expected phase and allow also DEPOSITS_PUBLISHED as it can be the case
// that the wallet is still syncing and so the DEPOSITS_CONFIRMED state to yet triggered when we received // that the wallet is still syncing and so the DEPOSITS_CONFIRMED state to yet triggered when we received
// a mailbox message with PaymentSentMessage. // a mailbox message with PaymentSentMessage.
@ -450,7 +441,6 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
.executeTasks(true); .executeTasks(true);
awaitTradeLatch(); awaitTradeLatch();
} }
}).start();
} }
// received by buyer and arbitrator // received by buyer and arbitrator
@ -761,7 +751,6 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
} }
private void sendDepositsConfirmedMessage() { private void sendDepositsConfirmedMessage() {
new Thread(() -> {
synchronized (trade) { synchronized (trade) {
latchTrade(); latchTrade();
expect(new Condition(trade)) expect(new Condition(trade))
@ -776,6 +765,5 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
.executeTasks(true); .executeTasks(true);
awaitTradeLatch(); awaitTradeLatch();
} }
}).start();
} }
} }