From 6f130bd6b06a41646458620fc0c00acf96b51897 Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Thu, 31 May 2018 15:33:19 +0200 Subject: [PATCH] Fixes #4023: Correctly recognize running remote server --- src/main/java/org/jabref/JabRefMain.java | 16 ++++++++-------- .../org/jabref/logic/remote/RemoteSetupTest.java | 11 +++++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/jabref/JabRefMain.java b/src/main/java/org/jabref/JabRefMain.java index 4654655e860..4ba71bc68ef 100644 --- a/src/main/java/org/jabref/JabRefMain.java +++ b/src/main/java/org/jabref/JabRefMain.java @@ -131,19 +131,19 @@ private static void start(String[] args) { private static boolean handleMultipleAppInstances(String[] args) { RemotePreferences remotePreferences = Globals.prefs.getRemotePreferences(); if (remotePreferences.useRemoteServer()) { - Globals.REMOTE_LISTENER.open(new JabRefMessageHandler(), remotePreferences.getPort()); - - if (!Globals.REMOTE_LISTENER.isOpen()) { - // we are not alone, there is already a server out there, try to contact already running JabRef: - if (new RemoteClient(remotePreferences.getPort()).sendCommandLineArguments(args)) { - // We have successfully sent our command line options through the socket to another JabRef instance. + // Try to contact already running JabRef + RemoteClient remoteClient = new RemoteClient(remotePreferences.getPort()); + if (remoteClient.ping()) { + // We are not alone, there is already a server out there, send command line arguments to other instance + if (remoteClient.sendCommandLineArguments(args)) { // So we assume it's all taken care of, and quit. LOGGER.info(Localization.lang("Arguments passed on to running JabRef instance. Shutting down.")); return false; } + } else { + // We are alone, so we start the server + Globals.REMOTE_LISTENER.openAndStart(new JabRefMessageHandler(), remotePreferences.getPort()); } - // we are alone, we start the server - Globals.REMOTE_LISTENER.start(); } return true; } diff --git a/src/test/java/org/jabref/logic/remote/RemoteSetupTest.java b/src/test/java/org/jabref/logic/remote/RemoteSetupTest.java index 19c4e350403..366f1a34334 100644 --- a/src/test/java/org/jabref/logic/remote/RemoteSetupTest.java +++ b/src/test/java/org/jabref/logic/remote/RemoteSetupTest.java @@ -129,4 +129,15 @@ void pingReturnsFalseForNoServerListening() throws IOException, InterruptedExcep assertFalse(new RemoteClient(port).ping()); } + + @Test + void pingReturnsTrueWhenServerIsRunning() { + final int port = 34567; + + try (RemoteListenerServerLifecycle server = new RemoteListenerServerLifecycle()) { + server.openAndStart(messageHandler, port); + + assertTrue(new RemoteClient(port).ping()); + } + } }