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.
*/
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();
+ }
}
}