RemoteLibrary: new isOnline() method
authorNiki Roo <niki@nikiroo.be>
Sat, 2 Dec 2017 14:02:48 +0000 (15:02 +0100)
committerNiki Roo <niki@nikiroo.be>
Sat, 2 Dec 2017 14:02:48 +0000 (15:02 +0100)
src/be/nikiroo/fanfix/library/RemoteLibrary.java
src/be/nikiroo/fanfix/library/RemoteLibraryServer.java

index ea155eb88d14515cf4334dfc38eb1843e0b9f005..e51de45daf1d17aaf5ee294073b7e58ff3de726a 100644 (file)
@@ -224,6 +224,36 @@ public class RemoteLibrary extends BasicLibrary {
                                "Operation not supportorted on remote Libraries");
        }
 
+       /**
+        * Check if this {@link RemoteLibraryServer} is able to connect and identify
+        * to the remote server.
+        * 
+        * @return TRUE if it is online
+        */
+       public boolean isOnline() {
+               final Boolean[] result = new Boolean[1];
+
+               result[0] = false;
+               try {
+                       new ConnectActionClientObject(host, port, true) {
+                               @Override
+                               public void action(Version serverVersion) throws Exception {
+                                       Object rep = send(new Object[] { key, "PING" });
+                                       result[0] = "PONG".equals(rep);
+                               }
+
+                               @Override
+                               protected void onError(Exception e) {
+                                       Instance.getTraceHandler().error(e);
+                               }
+                       }.connect();
+               } catch (Exception e) {
+                       Instance.getTraceHandler().error(e);
+               }
+
+               return result[0];
+       }
+
        @Override
        protected List<MetaData> getMetas(Progress pg) {
                final Progress pgF = pg;
index 40386ec2ce915db26a811fb18329579b88a1c714..b95c50f84f490134a12a8d8592b599a429b99d76 100644 (file)
@@ -25,6 +25,7 @@ import be.nikiroo.utils.serial.server.ServerObject;
  * The key is always a String, the commands are also Strings; the parameters
  * vary depending upon the command.
  * <ul>
+ * <li>[key] PING: will return PONG if the key is accepted</li>
  * <li>[key] GET_METADATA *: will return the metadata of all the stories in the
  * library</li>
  * <li>[key] GET_STORY [luid]: will return the given story if it exists (or NULL
@@ -93,7 +94,9 @@ public class RemoteLibraryServer extends ServerObject {
                        return null;
                }
 
-               if ("GET_METADATA".equals(command)) {
+               if ("PING".equals(command)) {
+                       return "PONG";
+               } else if ("GET_METADATA".equals(command)) {
                        if (args[0].equals("*")) {
                                List<MetaData> metas = Instance.getLibrary().getMetas(
                                                createPgForwarder(action));