SRADA-445: implemented devices -u command for tracing devices.
authorVladislav Eliseev <v.eliseev@samsung.com>
Wed, 18 May 2016 08:50:52 +0000 (11:50 +0300)
committerMaria Guseva <m.guseva@samsung.com>
Fri, 1 Jul 2016 03:58:24 +0000 (06:58 +0300)
Added command option parser in DeviceCommand.
Added getCurrentlyTracingDevices() method in CliInternals.

Change-Id: I5e5fabcfd879a85ce35dc0948197d592c855a688

org.tizen.dynamicanalyzer.cli/src/org/tizen/dynamicanalyzer/cli/CliInternals.java
org.tizen.dynamicanalyzer.cli/src/org/tizen/dynamicanalyzer/cli/commands/DevicesCommand.java

index 4f4a489..12288c2 100644 (file)
@@ -1,6 +1,7 @@
 package org.tizen.dynamicanalyzer.cli;
 
 import java.rmi.ConnectException;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.regex.Matcher;
@@ -320,6 +321,32 @@ public final class CliInternals {
        }
 
        /**
+        * Get list of devices on which tracing is currently running.
+        *
+        * @return list of {@link DeviceInfo} objects
+        * @throws ConnectException in case of connection error
+        */
+       public static List<DeviceInfo> getCurrentlyTracingDevices() throws ConnectException {
+               ProcessManagerMBean pmProxy = getPMProxyInstance();
+
+               String[] tracingDevicesSerials = pmProxy.getTracedDevices();
+               List<DeviceInfo> connectedDevices = getDevices();
+
+               List<DeviceInfo> tracingDevices = new LinkedList<>();
+               for (DeviceInfo devInfo : connectedDevices) {
+                       String conDevSerial = devInfo.getIDevice().getSerialNumber();
+                       for (String devSerial : tracingDevicesSerials) {
+                               if (conDevSerial.equals(devSerial)) {
+                                       tracingDevices.add(devInfo);
+                                       break;
+                               }
+                       }
+               }
+
+               return tracingDevices;
+       }
+
+       /**
         * Start tracing with requested parameters: setup given data as selected in
         * DA and run the StartTraceManager thread
         *
index d10a683..6e90b59 100644 (file)
@@ -1,14 +1,49 @@
 package org.tizen.dynamicanalyzer.cli.commands;
 
+import java.io.IOException;
+import java.rmi.ConnectException;
 import java.util.List;
 
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.DefaultParser;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
 import org.tizen.dynamicanalyzer.cli.CliInternals;
+import org.tizen.dynamicanalyzer.cli.utils.Help;
 import org.tizen.dynamicanalyzer.communicator.DeviceInfo;
+import org.tizen.dynamicanalyzer.util.Logger;
 
 /**
  * Class representing 'devices' command.
  */
 public final class DevicesCommand extends Command {
+
+       /**
+        * Parser for currently tracing devices option.
+        */
+       private static class TracingDevicesOptionParser {
+               private static CommandLineParser parser =  new DefaultParser();
+
+               private static Option listTracingDevices = Option.builder("u")
+                               .desc("show list of devices where tracing is running")
+                               .build();
+
+               private static Options opts = new Options().addOption(listTracingDevices);
+
+               /**
+                * Check whether option present in arguments list.
+                *
+                * @param args arguments list
+                * @return <code>true</code> if option present, <code>false</code> otherwise
+                */
+               public static boolean isOptionPresent(String[] args) throws ParseException {
+                       CommandLine cmdLine = parser.parse(opts, args);
+                       return cmdLine.hasOption(listTracingDevices.getOpt());
+               }
+       }
+
        /**
         * Default constructor.
         */
@@ -16,38 +51,119 @@ public final class DevicesCommand extends Command {
                super("devices", 0, "show list of connected devices and emulators");
        }
 
-       private void printDevices() {
+       @Override
+       public boolean checkArgs(String[] args) {
+               if (args.length == 0) return true;
+               else if (args.length > 1) return false;
+
+               try {
+                       return TracingDevicesOptionParser.isOptionPresent(args);
+               } catch (ParseException e) {
+                       System.out.println("Wrong command arguments: " + e.getMessage());
+                       return false;
+               }
+       }
+
+       @Override
+       public void printHelp() {
+               String message = String.format(helpFormat, name + " " + optsFormat, helpMessage);
+
+               try {
+                       // print help message for command options to System.out
+                       Help.printHelp(message, TracingDevicesOptionParser.opts, System.out);
+               } catch (IOException e) {
+                       Logger.exception(e);
+               }
+       }
+
+       /**
+        * Pretty prints devices list.
+        */
+       private void printDevices(List<DeviceInfo> devInfoList) {
+               final String DEV_NAME_HEADER = "<Device Name>";
+               final String DEV_SERIALNUM_HEADER = "<Serial Number>";
+
+               // find max name length
+               int maxDeviceName = DEV_NAME_HEADER.length();
+               for (DeviceInfo devInfo : devInfoList) {
+                       int len = CliInternals.getDeviceName(devInfo).length();
+                       if (len > maxDeviceName)
+                               maxDeviceName = len;
+               }
+               String format = String.format("  %%-%ds %%s%n", maxDeviceName);
+               System.out.format(format, DEV_NAME_HEADER, DEV_SERIALNUM_HEADER);
+
+               for (DeviceInfo devInfo : devInfoList) {
+                       System.out.format(format, CliInternals.getDeviceName(devInfo),
+                                       CliInternals.getSerialNumber(devInfo));
+               }
+       }
+
+       /**
+        * Process connected devices request.
+        */
+       private ExitCode processConnectedDevices() {
                List<DeviceInfo> devInfoList = CliInternals.getDevices();
+
                if (devInfoList.isEmpty()) {
-                       System.out.println("No devices connected");
-               } else {
-                       final String DEV_NAME_HEADER = "<Device Name>";
-                       final String DEV_SERIALNUM_HEADER = "<Serial Number>";
-
-                       // find max name length
-                       int maxDeviceName = DEV_NAME_HEADER.length();
-                       for (DeviceInfo devInfo : devInfoList) {
-                               int len = CliInternals.getDeviceName(devInfo).length();
-                               if (len > maxDeviceName)
-                                       maxDeviceName = len;
-                       }
-                       String format = String.format("  %%-%ds (%%s)%n", maxDeviceName);
+                       System.out.println("No devices connected.");
+               }       else {
                        System.out.println("List of connected devices:");
-                       System.out.format(format, DEV_NAME_HEADER, DEV_SERIALNUM_HEADER);
 
-                       for (DeviceInfo devInfo : devInfoList) {
-                               System.out.format(format, CliInternals.getDeviceName(devInfo),
-                                               CliInternals.getSerialNumber(devInfo));
-                       }
+                       // Print info about devices
+                       printDevices(devInfoList);
+               }
+
+               return ExitCode.EX_SUCCESS;
+       }
+
+       /**
+        * Process currently tracing devices request.
+        */
+       private ExitCode processCurrentlyTracingDevices() {
+               List<DeviceInfo> devInfoList;
+
+               try {
+                       devInfoList = CliInternals.getCurrentlyTracingDevices();
+               } catch (ConnectException e) {
+                       System.out.println(e.toString());
+                       e.printStackTrace();
+                       return ExitCode.EX_CONNECTION_ERROR;
+               }
+
+               if (devInfoList.isEmpty()) {
+                       System.out.println("No devices currently tracing.");
+               }       else {
+                       System.out.println("List of currently tracing devices:");
+
+                       // Print info about devices
+                       printDevices(devInfoList);
+
+                       System.out.println();
+                       System.out.println("Run \"dacli stop <ip:port|serial>\" to finish tracing on device.");
                }
+
+               return ExitCode.EX_SUCCESS;
        }
 
        @Override
        public ExitCode processCommand(String[] args) {
+
+               boolean tracingDevicesRequested;
+               try {
+                       tracingDevicesRequested = TracingDevicesOptionParser.isOptionPresent(args);
+               } catch (ParseException e) {
+                       System.out.println("Wrong command arguments: " + e.getMessage());
+                       return ExitCode.EX_ARGUMENTS_ERROR;
+               }
+
                // Initialize device connection
                CliInternals.initDevices();
-               // Print info of connected devices
-               printDevices();
-               return ExitCode.EX_SUCCESS;
+
+               if (tracingDevicesRequested) {
+                       return processCurrentlyTracingDevices();
+               } else {
+                       return processConnectedDevices();
+               }
        }
 }