/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.karaf.cellar.shell;

import org.apache.karaf.cellar.core.Node;
import org.apache.karaf.cellar.utils.ping.Ping;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;

import java.util.Arrays;
import java.util.HashSet;

@Command(scope = "cluster", name = "node-ping", description = "Ping a cluster node.")
public class NodePingCommand extends ClusterCommandSupport {

    private static Long TIMEOUT = 5000L;

    @Argument(index = 0, name = "node", description = "The ID of the node to ping.", required = true, multiValued = false)
    String nodeId;

    @Argument(index = 1, name = "iterations", description = "The number of iterations to perform.", required = false, multiValued = false)
    Integer iterations = 10;

    @Argument(index = 2, name = "interval", description = "The time in millis to wait between iteration.", required = false, multiValued = false)
    Long interval = 1000L;

    @Override
    protected Object doExecute() throws Exception {
        Node node = clusterManager.findNodeById(nodeId);
        if (node == null) {
            System.out.println("Node " + nodeId + " is not found");
            return null;
        }
        System.out.println("Pinging node: " + node.getId());
        for (int i = 1; i <= iterations; i++) {
            Long start = System.currentTimeMillis();
            Ping ping = new Ping(clusterManager.generateId());
            ping.setDestination(new HashSet(Arrays.asList(node)));
            executionContext.execute(ping);
            Long stop = System.currentTimeMillis();
            Long delay = stop - start;
            if (delay > TIMEOUT) {
                System.err.println(String.format("TIMEOUT %s %s %sms", i, node.getId(), delay));
            } else {
                System.out.println(String.format("PING %s %s %sms", i, node.getId(), delay));
            }
            Thread.sleep(interval);
        }
        return null;
    }

}

@@ -1696,17 +1696,25 @@
 rintln(%22
-N
+Cluster n
 ode %22 + 
@@ -1728,20 +1728,21 @@
 + %22 
-is not found
+doesn't exist
 %22);%0A
@@ -1768,32 +1768,33 @@
 null;%0A        %7D%0A
+%0A
         System.o
@@ -1810,20 +1810,11 @@
 n(%22P
-inging node:
+ING
  %22 +
@@ -2218,16 +2218,17 @@
 (delay %3E
+=
  TIMEOUT
@@ -2290,29 +2290,24 @@
 IMEOUT %25s %25s
- %25sms
 %22, i, node.g
@@ -2304,39 +2304,32 @@
  i, node.getId()
-, delay
 ));%0A            
@@ -2391,21 +2391,32 @@
 at(%22
-PING %25s %25s 
+from %25s: req=%25s time=
 %25s
+ 
 ms%22,

Improve cluster:node-ping output