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

Refactor: Add handler, make onMissedSecondTick default #2376

Merged
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
21 changes: 17 additions & 4 deletions common/src/main/java/bisq/common/Clock.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,24 @@
import java.util.List;
import java.util.concurrent.TimeUnit;

import lombok.extern.slf4j.Slf4j;

// Helps configure listener objects that are run by the `UserThread` each second
// and can do per second, per minute and delayed second actions.
@Slf4j
public class Clock {
public static final int IDLE_TOLERANCE = 20000;
public static final int IDLE_TOLERANCE_MS = 20000;

public interface Listener {
void onSecondTick();

void onMinuteTick();

void onMissedSecondTick(long missed);
default void onMissedSecondTick(long missedMs) {
}

default void onAwakeFromStandby(long missedMs) {
}
}

private Timer timer;
Expand All @@ -55,9 +62,15 @@ public void start() {

long currentTimeMillis = System.currentTimeMillis();
long diff = currentTimeMillis - lastSecondTick;
if (diff > 1000)
listeners.forEach(listener -> listener.onMissedSecondTick(diff - 1000));
if (diff > 1000) {
long missedMs = diff - 1000;
listeners.forEach(listener -> listener.onMissedSecondTick(missedMs));

if (missedMs > Clock.IDLE_TOLERANCE_MS) {
log.info("We have been in standby mode for {} sec", missedMs / 1000);
listeners.forEach(listener -> listener.onAwakeFromStandby(missedMs));
}
}
lastSecondTick = currentTimeMillis;
}, 1, TimeUnit.SECONDS);
}
Expand Down
4 changes: 0 additions & 4 deletions core/src/main/java/bisq/core/trade/TradeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -636,10 +636,6 @@ public void onSecondTick() {
public void onMinuteTick() {
updateTradePeriodState();
}

@Override
public void onMissedSecondTick(long missed) {
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,6 @@ public void onSecondTick() {
public void onMinuteTick() {
updateTimeLeft();
}

@Override
public void onMissedSecondTick(long missed) {
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ public void onSecondTick() {
@Override
public void onMinuteTick() {
}

@Override
public void onMissedSecondTick(long missed) {
}
};
clock.addListener(listener);
onLastActivityChanged(statistic.getLastActivityTimestamp());
Expand Down
10 changes: 4 additions & 6 deletions p2p/src/main/java/bisq/network/p2p/peers/PeerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,10 @@ public void onMinuteTick() {
}

@Override
public void onMissedSecondTick(long missed) {
if (missed > Clock.IDLE_TOLERANCE) {
log.info("We have been in standby mode for {} sec", missed / 1000);
stopped = false;
listeners.stream().forEach(Listener::onAwakeFromStandby);
}
public void onAwakeFromStandby(long missedMs) {
// TODO is "stopped = false;" correct?
stopped = false;
listeners.forEach(Listener::onAwakeFromStandby);
}
};
clock.addListener(listener);
Expand Down