Skip to content

Commit

Permalink
Merge pull request #2376 from ManfredKarrer/refactor-clock-handlers
Browse files Browse the repository at this point in the history
Refactor: Add handler, make onMissedSecondTick default
  • Loading branch information
ripcurlx authored Feb 6, 2019
2 parents c53612f + dcf40bc commit 0b81095
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 22 deletions.
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

0 comments on commit 0b81095

Please sign in to comment.