Skip to content

Commit

Permalink
Merge pull request #4407 from chimp1984/use-sound-loop-on-osx
Browse files Browse the repository at this point in the history
Revert usage of caffeinate on OSX
  • Loading branch information
ripcurlx committed Aug 19, 2020
2 parents 287abd2 + 47c4ed7 commit 943d1c3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions core/src/main/java/bisq/core/app/AvoidStandbyModeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

/**
* Prevents that Bisq gets hibernated from the OS. On OSX there is a tool called caffeinate but it seems it does not
* provide the behaviour we need, thus we use the trick to play a almost silent sound file in a loop. This keeps the
* application active even if the OS has moved to hibernate. Hibernating Bisq would cause network degradations and other
* resource limitations which would lead to offers not published or if a taker takes an offer that the trade process is
* at risk to fail due too slow response time.
*/
@Slf4j
@Singleton
public class AvoidStandbyModeService {
Expand Down Expand Up @@ -88,7 +95,7 @@ public void init() {

private void start() {
isStopped = false;
if (Utilities.isLinux() || Utilities.isOSX()) {
if (Utilities.isLinux()) {
startInhibitor();
} else {
new Thread(this::playSilentAudioFile, "AvoidStandbyModeService-thread").start();
Expand Down Expand Up @@ -125,7 +132,6 @@ private void startInhibitor() {

private void stopInhibitor() {
try {
// Cannot toggle off osx caffeinate, but it will shutdown with bisq.
if (Utilities.isLinux()) {
if (!isStopped) {
Objects.requireNonNull(stopLinuxInhibitorCountdownLatch).await();
Expand Down Expand Up @@ -167,7 +173,9 @@ private void playSilentAudioFile() {

private File getSoundFile() throws IOException, ResourceNotFoundException {
File soundFile = new File(config.appDataDir, "prevent-app-nap-silent-sound.aiff");
if (!soundFile.exists()) {
// We replaced the old file which was 42 MB with a smaller file of 0.8 MB. To enforce replacement we check for
// the size...
if (!soundFile.exists() || soundFile.length() > 42000000) {
FileUtil.resourceToFile("prevent-app-nap-silent-sound.aiff", soundFile);
}
return soundFile;
Expand Down Expand Up @@ -196,7 +204,7 @@ private Optional<String[]> inhibitCommand() {
? new String[]{cmd, "--app-id", "Bisq", "--inhibit", "suspend", "--reason", "Avoid Standby", "--inhibit-only"}
: new String[]{cmd, "--who", "Bisq", "--what", "sleep", "--why", "Avoid Standby", "--mode", "block", "tail", "-f", "/dev/null"};
} else {
params = Utilities.isOSX() ? new String[]{cmd, "-w", "" + ProcessHandle.current().pid()} : null;
params = null;
}
} else {
params = null; // fall back to silent audio file player
Expand Down Expand Up @@ -235,15 +243,11 @@ private Optional<ProcessHandle> runningInhibitorProcess() {
new ArrayList<>() {{
add(gnomeSessionInhibitPathSpec.get()); // On linux, preferred inhibitor is gnome-session-inhibit,
add(systemdInhibitPathSpec.get()); // then fall back to systemd-inhibit if it is installed.
add(caffeinatePathSec.get()); // On OSX, caffeinate should be installed.
}};

private final Supplier<Optional<String>> gnomeSessionInhibitPathSpec = () ->
cmdPath.apply(new String[]{"/usr/bin/gnome-session-inhibit", "/bin/gnome-session-inhibit"});

private final Supplier<Optional<String>> systemdInhibitPathSpec = () ->
cmdPath.apply(new String[]{"/usr/bin/systemd-inhibit", "/bin/systemd-inhibit"});

private final Supplier<Optional<String>> caffeinatePathSec = () ->
cmdPath.apply(new String[]{"/usr/bin/caffeinate"});
}
Binary file modified core/src/main/resources/prevent-app-nap-silent-sound.aiff
Binary file not shown.

0 comments on commit 943d1c3

Please sign in to comment.