FELIX-1882: karaf-client should have the option to retry connection establishment

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@882582 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/karaf/client/src/main/java/org/apache/felix/karaf/client/Main.java b/karaf/client/src/main/java/org/apache/felix/karaf/client/Main.java
index e17434c..414bfd6 100644
--- a/karaf/client/src/main/java/org/apache/felix/karaf/client/Main.java
+++ b/karaf/client/src/main/java/org/apache/felix/karaf/client/Main.java
@@ -26,6 +26,7 @@
 import org.apache.sshd.SshClient;
 import org.apache.sshd.client.channel.ChannelShell;
 import org.apache.sshd.client.future.ConnectFuture;
+import org.apache.sshd.common.RuntimeSshException;
 
 import org.fusesource.jansi.AnsiConsole;
 import org.slf4j.impl.SimpleLogger;
@@ -42,6 +43,8 @@
         String password = "karaf";
         StringBuilder sb = new StringBuilder();
         int level = 1;
+        int retryAttempts = 0;
+        int retryDelay = 2;
 
         for (int i = 0; i < args.length; i++) {
             if (args[i].charAt(0) == '-') {
@@ -55,6 +58,10 @@
                     password = args[++i];
                 } else if (args[i].equals("-v")) {
                     level++;
+                } else if (args[i].equals("-r")) {
+                    retryAttempts = Integer.parseInt(args[++i]);
+                } else if (args[i].equals("-d")) {
+                    retryDelay = Integer.parseInt(args[++i]);
                 } else if (args[i].equals("--help")) {
                     System.out.println("Apache Felix Karaf client");
                     System.out.println("  -a [port]     specify the port to connect to");
@@ -63,6 +70,8 @@
                     System.out.println("  -p [password] specify the password");
                     System.out.println("  --help        shows this help message");
                     System.out.println("  -v            raise verbosity");
+                    System.out.println("  -r [attempts] retry connection establishment (up to attempts times)");
+                    System.out.println("  -d [delay]    intra-retry delay (defaults to 2 seconds)");
                     System.out.println("  [commands]    commands to run");
                     System.out.println("If no commands are specified, the client will be put in an interactive mode");
                     System.exit(0);
@@ -83,9 +92,22 @@
         try {
             client = SshClient.setUpDefaultClient();
             client.start();
-            ConnectFuture future = client.connect(host, port);
-            future.await();
-            ClientSession session = future.getSession();
+            int retries = 0;
+            ClientSession session = null;
+            do {
+                ConnectFuture future = client.connect(host, port);
+                future.await();
+                try { 
+                    session = future.getSession();
+                } catch (RuntimeSshException ex) {
+                    if (retries++ < retryAttempts) {
+                        Thread.sleep(retryDelay * 1000);
+                        System.out.println("retrying (attempt " + retries + ") ...");
+                    } else {
+                        throw ex;
+                    }
+                }
+            } while (session == null);
             session.authPassword(user, password);
             ClientChannel channel;
 			if (sb.length() > 0) {