From: Alexandre Montplaisir Date: Fri, 8 Jan 2016 20:28:07 +0000 (-0500) Subject: Turn ISessiondCommand into an abstract class X-Git-Tag: v2.8.0-rc1~40 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=1d193914f13dd604d60df9db680001c9a4af5f9d;p=lttng-ust.git Turn ISessiondCommand into an abstract class This will allow us to define common methods to read strings passed on the socket, which many commands (including upcoming ones) need to do. Signed-off-by: Alexandre Montplaisir Signed-off-by: Mathieu Desnoyers --- diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-common/Makefile.am b/liblttng-ust-java-agent/java/lttng-ust-agent-common/Makefile.am index 628aedb6..33476ae3 100644 --- a/liblttng-ust-java-agent/java/lttng-ust-agent-common/Makefile.am +++ b/liblttng-ust-java-agent/java/lttng-ust-agent-common/Makefile.am @@ -15,7 +15,7 @@ dist_noinst_JAVA = $(pkgpath)/AbstractLttngAgent.java \ $(pkgpath)/ILttngHandler.java \ $(pkgpath)/LTTngAgent.java \ $(pkgpath)/client/ILttngTcpClientListener.java \ - $(pkgpath)/client/ISessiondCommand.java \ + $(pkgpath)/client/SessiondCommand.java \ $(pkgpath)/client/LttngAgentResponse.java \ $(pkgpath)/client/LttngTcpSessiondClient.java \ $(pkgpath)/client/SessiondCommandHeader.java \ diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/ISessiondCommand.java b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/ISessiondCommand.java deleted file mode 100644 index c1faa15f..00000000 --- a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/ISessiondCommand.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir - * Copyright (C) 2013 - David Goulet - * - * This library is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License, version 2.1 only, - * as published by the Free Software Foundation. - * - * This library is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this library; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -package org.lttng.ust.agent.client; - -/** - * Interface to represent all commands sent from the session daemon to the Java - * agent. The agent is then expected to execute the command and provide a - * response. - * - * @author Alexandre Montplaisir - */ -interface ISessiondCommand { - - enum CommandType { - - /** List logger(s). */ - CMD_LIST(1), - /** Enable logger by name. */ - CMD_ENABLE(2), - /** Disable logger by name. */ - CMD_DISABLE(3), - /** Registration done */ - CMD_REG_DONE(4); - - private int code; - - private CommandType(int c) { - code = c; - } - - public int getCommandType() { - return code; - } - } - - /** - * Execute the command handler's action on the specified tracing agent. - * - * @param agent - * The agent on which to execute the command - * @return If the command completed successfully or not - */ - public LttngAgentResponse execute(ILttngTcpClientListener agent); -} \ No newline at end of file diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/LttngTcpSessiondClient.java b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/LttngTcpSessiondClient.java index 6cf97138..2b31889c 100644 --- a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/LttngTcpSessiondClient.java +++ b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/LttngTcpSessiondClient.java @@ -250,7 +250,7 @@ public class LttngTcpSessiondClient implements Runnable { } case CMD_LIST: { - ISessiondCommand listLoggerCmd = new SessiondListLoggersCommand(); + SessiondCommand listLoggerCmd = new SessiondListLoggersCommand(); LttngAgentResponse response = listLoggerCmd.execute(logAgent); responseData = response.getBytes(); break; @@ -262,7 +262,7 @@ public class LttngTcpSessiondClient implements Runnable { responseData = LttngAgentResponse.FAILURE_RESPONSE.getBytes(); break; } - ISessiondCommand enableCmd = new SessiondEnableEventCommand(inputData); + SessiondCommand enableCmd = new SessiondEnableEventCommand(inputData); LttngAgentResponse response = enableCmd.execute(logAgent); responseData = response.getBytes(); break; @@ -274,7 +274,7 @@ public class LttngTcpSessiondClient implements Runnable { responseData = LttngAgentResponse.FAILURE_RESPONSE.getBytes(); break; } - ISessiondCommand disableCmd = new SessiondDisableEventCommand(inputData); + SessiondCommand disableCmd = new SessiondDisableEventCommand(inputData); LttngAgentResponse response = disableCmd.execute(logAgent); responseData = response.getBytes(); break; diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondCommand.java b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondCommand.java new file mode 100644 index 00000000..ee191da1 --- /dev/null +++ b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondCommand.java @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2015-2016 - EfficiOS Inc., Alexandre Montplaisir + * Copyright (C) 2013 - David Goulet + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License, version 2.1 only, + * as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package org.lttng.ust.agent.client; + +import java.nio.ByteBuffer; + +/** + * Base class to represent all commands sent from the session daemon to the Java + * agent. The agent is then expected to execute the command and provide a + * response. + * + * @author Alexandre Montplaisir + */ +abstract class SessiondCommand { + + enum CommandType { + + /** List logger(s). */ + CMD_LIST(1), + /** Enable logger by name. */ + CMD_ENABLE(2), + /** Disable logger by name. */ + CMD_DISABLE(3), + /** Registration done */ + CMD_REG_DONE(4); + + private int code; + + private CommandType(int c) { + code = c; + } + + public int getCommandType() { + return code; + } + } + + /** + * Execute the command handler's action on the specified tracing agent. + * + * @param agent + * The agent on which to execute the command + * @return If the command completed successfully or not + */ + public abstract LttngAgentResponse execute(ILttngTcpClientListener agent); + + /** + * Utility method to read agent-protocol strings passed on the socket. The + * buffer will contain a 32-bit integer representing the length, immediately + * followed by the string itself. + * + * @param buffer + * The ByteBuffer from which to read. It should already be setup + * and positioned where the read should begin. + * @return The string that was read, or null if it was badly + * formatted. + */ + protected static String readNextString(ByteBuffer buffer) { + int length = buffer.getInt(); + if (length < 0) { + /* The string length should be positive */ + return null; + } + if (length == 0) { + /* The string is explicitly an empty string */ + return ""; + } + + byte[] stringBytes = new byte[length]; + buffer.get(stringBytes); + return new String(stringBytes).trim(); + } +} diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondCommandHeader.java b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondCommandHeader.java index 1ce6502b..d341ae67 100644 --- a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondCommandHeader.java +++ b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondCommandHeader.java @@ -21,7 +21,7 @@ package org.lttng.ust.agent.client; import java.nio.ByteBuffer; import java.nio.ByteOrder; -import org.lttng.ust.agent.client.ISessiondCommand.CommandType; +import org.lttng.ust.agent.client.SessiondCommand.CommandType; /** * Header of session daemon commands. diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondDisableEventCommand.java b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondDisableEventCommand.java index 2956c7e2..920a2bf1 100644 --- a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondDisableEventCommand.java +++ b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondDisableEventCommand.java @@ -28,7 +28,7 @@ import java.nio.ByteOrder; * @author Alexandre Montplaisir * @author David Goulet */ -class SessiondDisableEventCommand implements ISessiondCommand { +class SessiondDisableEventCommand extends SessiondCommand { /** Event name to disable from the tracing session */ private final String eventName; diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondEnableEventCommand.java b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondEnableEventCommand.java index 5665a691..5b36ac5d 100644 --- a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondEnableEventCommand.java +++ b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondEnableEventCommand.java @@ -31,7 +31,7 @@ import org.lttng.ust.agent.session.LogLevelSelector; * @author Alexandre Montplaisir * @author David Goulet */ -class SessiondEnableEventCommand implements ISessiondCommand { +class SessiondEnableEventCommand extends SessiondCommand { /** Fixed event name length. Value defined by the lttng agent protocol. */ private static final int EVENT_NAME_LENGTH = 256; @@ -58,35 +58,11 @@ class SessiondEnableEventCommand implements ISessiondCommand { buf.get(eventNameBytes); eventName = new String(eventNameBytes).trim(); - /* - * Read the filter string. The buffer contains the length (number of - * bytes), then the bytes themselves. - * - * The length is represented as an unsigned int, but it should never - * be greater than Integer.MAX_VALUE. - */ - int filterStringLength = buf.getInt(); - if (filterStringLength < 0) { - /* - * The (unsigned) length is above what the sessiond should send. The - * command cannot be processed. - */ - filterString = null; - commandIsValid = false; - return; - } - if (filterStringLength == 0) { - /* There is explicitly no filter string */ - filterString = ""; - commandIsValid = true; - return; - } - - byte[] filterStringBytes = new byte[filterStringLength]; - buf.get(filterStringBytes); - filterString = new String(filterStringBytes).trim(); + /* Read the filter string */ + filterString = readNextString(buf); - commandIsValid = true; + /* The command was invalid if the string could not be read correctly */ + commandIsValid = (filterString != null); } @Override diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondListLoggersCommand.java b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondListLoggersCommand.java index a8bc1520..c1bdaf40 100644 --- a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondListLoggersCommand.java +++ b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondListLoggersCommand.java @@ -29,7 +29,7 @@ import java.util.Collection; * @author Alexandre Montplaisir * @author David Goulet */ -class SessiondListLoggersCommand implements ISessiondCommand { +class SessiondListLoggersCommand extends SessiondCommand { @Override public LttngAgentResponse execute(ILttngTcpClientListener agent) {