Turn ISessiondCommand into an abstract class
authorAlexandre Montplaisir <alexmonthy@efficios.com>
Fri, 8 Jan 2016 20:28:07 +0000 (15:28 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Fri, 12 Feb 2016 22:54:29 +0000 (17:54 -0500)
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 <alexmonthy@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
liblttng-ust-java-agent/java/lttng-ust-agent-common/Makefile.am
liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/ISessiondCommand.java [deleted file]
liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/LttngTcpSessiondClient.java
liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondCommand.java [new file with mode: 0644]
liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondCommandHeader.java
liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondDisableEventCommand.java
liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondEnableEventCommand.java
liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/SessiondListLoggersCommand.java

index 628aedb6c5cee305de35696054801657d6c2a73b..33476ae3082a3d9a1964b2293cab280df039fe70 100644 (file)
@@ -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 (file)
index c1faa15..0000000
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
- * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
- *
- * 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
index 6cf971384260524869026351e868cfd5a4b608c2..2b31889c3019a5ae88864dee2995c42f34086c51 100644 (file)
@@ -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 (file)
index 0000000..ee191da
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2015-2016 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
+ * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
+ *
+ * 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 <code>null</code> 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();
+       }
+}
index 1ce6502b1f5a79c075fa3745329bcd4d015498c7..d341ae673b2f3041ec622483a3f7815c27232a8c 100644 (file)
@@ -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.
index 2956c7e20363f2cd0cbbe73bfc873453f3ff74b1..920a2bf16bfc36b6b727b625c2e964e273e00ffe 100644 (file)
@@ -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;
index 5665a6916baa640954e4f272af0b1f8596253792..5b36ac5d10a006c0e2ed1365a83b628f7dd631fe 100644 (file)
@@ -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
index a8bc15209e8c3e4072d5f6a437d0a8921a2a4812..c1bdaf407007ccf686c5a060ee1e510b0bf5dd49 100644 (file)
@@ -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) {
This page took 0.031219 seconds and 4 git commands to generate.