From ae17acfc910c6513e7b70b036153df9d695f462f Mon Sep 17 00:00:00 2001 From: Vladislav Eliseev Date: Wed, 18 May 2016 11:50:52 +0300 Subject: [PATCH] SRADA-445: implemented devices -u command for tracing devices. Added command option parser in DeviceCommand. Added getCurrentlyTracingDevices() method in CliInternals. Change-Id: I5e5fabcfd879a85ce35dc0948197d592c855a688 --- .../tizen/dynamicanalyzer/cli/CliInternals.java | 27 ++++ .../cli/commands/DevicesCommand.java | 160 ++++++++++++++++++--- 2 files changed, 165 insertions(+), 22 deletions(-) diff --git a/org.tizen.dynamicanalyzer.cli/src/org/tizen/dynamicanalyzer/cli/CliInternals.java b/org.tizen.dynamicanalyzer.cli/src/org/tizen/dynamicanalyzer/cli/CliInternals.java index 4f4a489..12288c2 100644 --- a/org.tizen.dynamicanalyzer.cli/src/org/tizen/dynamicanalyzer/cli/CliInternals.java +++ b/org.tizen.dynamicanalyzer.cli/src/org/tizen/dynamicanalyzer/cli/CliInternals.java @@ -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 getCurrentlyTracingDevices() throws ConnectException { + ProcessManagerMBean pmProxy = getPMProxyInstance(); + + String[] tracingDevicesSerials = pmProxy.getTracedDevices(); + List connectedDevices = getDevices(); + + List 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 * diff --git a/org.tizen.dynamicanalyzer.cli/src/org/tizen/dynamicanalyzer/cli/commands/DevicesCommand.java b/org.tizen.dynamicanalyzer.cli/src/org/tizen/dynamicanalyzer/cli/commands/DevicesCommand.java index d10a683..6e90b59 100644 --- a/org.tizen.dynamicanalyzer.cli/src/org/tizen/dynamicanalyzer/cli/commands/DevicesCommand.java +++ b/org.tizen.dynamicanalyzer.cli/src/org/tizen/dynamicanalyzer/cli/commands/DevicesCommand.java @@ -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 true if option present, false 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 devInfoList) { + final String DEV_NAME_HEADER = ""; + final String DEV_SERIALNUM_HEADER = ""; + + // 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 devInfoList = CliInternals.getDevices(); + if (devInfoList.isEmpty()) { - System.out.println("No devices connected"); - } else { - final String DEV_NAME_HEADER = ""; - final String DEV_SERIALNUM_HEADER = ""; - - // 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 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 \" 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(); + } } } -- 2.7.4