Introduce a new client listener interface for the Java agent
authorAlexandre Montplaisir <alexmonthy@efficios.com>
Sat, 29 Aug 2015 02:41:04 +0000 (22:41 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 22 Oct 2015 20:54:54 +0000 (16:54 -0400)
Decouple the TCP client from the implementation of the LTTng Java agent.
Instead of using AbstractLttngAgent directly, the TCP client (and the
command subclasses) can deal with the new ILttngTcpClientListener
interface. The agent will implement this interface.

This will also allow easier testing of the TCP client and its protocol,
since test classess can now implement their own listener and verify the
contents of each command.

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/AbstractLttngAgent.java
liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/ILttngAgent.java
liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/ILttngTcpClientListener.java [new file with mode: 0644]
liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/ISessiondCommand.java
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/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 2258fdc0c347dcd90b40cc2e6b8d346c8eaa9a49..bbf98f643bb8d88bf29ba196f273060e0d94223a 100644 (file)
@@ -14,8 +14,9 @@ dist_noinst_JAVA = $(pkgpath)/AbstractLttngAgent.java \
                                   $(pkgpath)/ILttngAgent.java \
                                   $(pkgpath)/ILttngHandler.java \
                                   $(pkgpath)/LTTngAgent.java \
-                                  $(pkgpath)/client/LttngAgentResponse.java \
+                                  $(pkgpath)/client/ILttngTcpClientListener.java \
                                   $(pkgpath)/client/ISessiondCommand.java \
+                                  $(pkgpath)/client/LttngAgentResponse.java \
                                   $(pkgpath)/client/LttngTcpSessiondClient.java \
                                   $(pkgpath)/client/SessiondCommandHeader.java \
                                   $(pkgpath)/client/SessiondDisableEventCommand.java \
index 64b33b6c596d96a5a80dfb04b9d064c46a541fdc..c3fc339bc628b09234e1ed41962f2197fb221b8f 100644 (file)
@@ -28,6 +28,7 @@ import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentSkipListMap;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import org.lttng.ust.agent.client.ILttngTcpClientListener;
 import org.lttng.ust.agent.client.LttngTcpSessiondClient;
 
 /**
@@ -37,7 +38,8 @@ import org.lttng.ust.agent.client.LttngTcpSessiondClient;
  * @param <T>
  *            The type of logging handler that should register to this agent
  */
-public abstract class AbstractLttngAgent<T extends ILttngHandler> implements ILttngAgent<T> {
+public abstract class AbstractLttngAgent<T extends ILttngHandler>
+               implements ILttngAgent<T>, ILttngTcpClientListener {
 
        private static final String WILDCARD = "*";
        private static final int INIT_TIMEOUT = 3; /* Seconds */
@@ -134,14 +136,14 @@ public abstract class AbstractLttngAgent<T extends ILttngHandler> implements ILt
                }
                String rootClientThreadName = "Root sessiond client started by agent: " + this.getClass().getSimpleName();
 
-               rootSessiondClient = new LttngTcpSessiondClient(this, true);
+               rootSessiondClient = new LttngTcpSessiondClient(this, getDomain().value(), true);
                rootSessiondClientThread = new Thread(rootSessiondClient, rootClientThreadName);
                rootSessiondClientThread.setDaemon(true);
                rootSessiondClientThread.start();
 
                String userClientThreadName = "User sessiond client started by agent: " + this.getClass().getSimpleName();
 
-               userSessiondClient = new LttngTcpSessiondClient(this, false);
+               userSessiondClient = new LttngTcpSessiondClient(this, getDomain().value(), false);
                userSessiondClientThread = new Thread(userSessiondClient, userClientThreadName);
                userSessiondClientThread.setDaemon(true);
                userSessiondClientThread.start();
@@ -186,15 +188,7 @@ public abstract class AbstractLttngAgent<T extends ILttngHandler> implements ILt
 
        }
 
-       /**
-        * Callback for the TCP clients to notify the agent that a request for
-        * enabling an event was sent from the session daemon.
-        *
-        * @param eventName
-        *            The name of the event that was requested to be enabled.
-        * @return Since we do not track individual sessions, right now this command
-        *         cannot fail. It will always return true.
-        */
+       @Override
        public boolean eventEnabled(String eventName) {
                if (eventName.equals(WILDCARD)) {
                        enabledWildcards.incrementAndGet();
@@ -210,15 +204,7 @@ public abstract class AbstractLttngAgent<T extends ILttngHandler> implements ILt
                return incrementEventCount(eventName, enabledEvents);
        }
 
-       /**
-        * Callback for the TCP clients to notify the agent that a request for
-        * disabling an event was sent from the session daemon.
-        *
-        * @param eventName
-        *            The name of the event that was requested to be disabled.
-        * @return True if the command completed successfully, false if we should
-        *         report an error (event was not enabled, etc.)
-        */
+       @Override
        public boolean eventDisabled(String eventName) {
                if (eventName.equals(WILDCARD)) {
                        int newCount = enabledWildcards.decrementAndGet();
@@ -239,6 +225,20 @@ public abstract class AbstractLttngAgent<T extends ILttngHandler> implements ILt
                return decrementEventCount(eventName, enabledEvents);
        }
 
+       @Override
+       public Iterable<String> listEnabledEvents() {
+               List<String> events = new LinkedList<String>();
+
+               if (enabledWildcards.get() > 0) {
+                       events.add(WILDCARD);
+               }
+               for (String prefix : enabledEventPrefixes.keySet()) {
+                       events.add(new String(prefix + WILDCARD));
+               }
+               events.addAll(enabledEvents.keySet());
+               return events;
+       }
+
        @Override
        public boolean isEventEnabled(String eventName) {
                /* If at least one session enabled the "*" wildcard, send the event */
@@ -260,20 +260,6 @@ public abstract class AbstractLttngAgent<T extends ILttngHandler> implements ILt
                return false;
        }
 
-       @Override
-       public Iterable<String> listEnabledEvents() {
-               List<String> events = new LinkedList<String>();
-
-               if (enabledWildcards.get() > 0) {
-                       events.add(WILDCARD);
-               }
-               for (String prefix : enabledEventPrefixes.keySet()) {
-                       events.add(new String(prefix + WILDCARD));
-               }
-               events.addAll(enabledEvents.keySet());
-               return events;
-       }
-
        private static boolean incrementEventCount(String eventName, Map<String, Integer> eventMap) {
                synchronized (eventMap) {
                        Integer count = eventMap.get(eventName);
index 044bdf07c3adebb3f5085a1430d6f0d057fb0c05..fd20a9ebc1d8246b67f5b0f7027a5288494b4579 100644 (file)
@@ -86,19 +86,11 @@ public interface ILttngAgent<T extends ILttngHandler> {
 
        /**
         * Query if a given event is currently enabled in a current tracing session,
-        * meaning it should be sent to UST. May be quicker than listing all events
-        * via {@link #listEnabledEvents()}.
+        * meaning it should be sent to UST.
         *
         * @param eventName
         *            The name of the event to check.
         * @return True if the event is currently enabled, false if it is not.
         */
        boolean isEventEnabled(String eventName);
-
-       /**
-        * List the all events currently enabled in the current tracing sessions.
-        *
-        * @return The list of enabled events
-        */
-       Iterable<String> listEnabledEvents();
 }
diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/ILttngTcpClientListener.java b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/ILttngTcpClientListener.java
new file mode 100644 (file)
index 0000000..662b998
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@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;
+
+/**
+ * TCP client listener interface.
+ *
+ * This interface contains callbacks that are called when the TCP client
+ * receives commands from the session daemon. These callbacks will define what
+ * do to with each command.
+ *
+ * @author Alexandre Montplaisir
+ */
+public interface ILttngTcpClientListener {
+
+       /**
+        * Callback for the TCP client to notify the listener agent that a request
+        * for enabling an event was sent from the session daemon.
+        *
+        * @param eventName
+        *            The name of the event that was requested to be enabled.
+        * @return Since we do not track individual sessions, right now this command
+        *         cannot fail. It will always return true.
+        */
+       boolean eventEnabled(String eventName);
+
+       /**
+        * Callback for the TCP client to notify the listener agent that a request
+        * for disabling an event was sent from the session daemon.
+        *
+        * @param eventName
+        *            The name of the event that was requested to be disabled.
+        * @return True if the command completed successfully, false if we should
+        *         report an error (event was not enabled, etc.)
+        */
+       boolean eventDisabled(String eventName);
+
+       /**
+        * List the all events currently enabled in the current tracing sessions.
+        *
+        * @return The list of enabled events
+        */
+       Iterable<String> listEnabledEvents();
+}
index c7d4faf8c38a5c0848c7ac2784505ff6c04455e2..c1faa15f1a74e6b25cdf92b95531eb9af92acc21 100644 (file)
@@ -18,8 +18,6 @@
 
 package org.lttng.ust.agent.client;
 
-import org.lttng.ust.agent.AbstractLttngAgent;
-
 /**
  * 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
@@ -58,5 +56,5 @@ interface ISessiondCommand {
         *            The agent on which to execute the command
         * @return If the command completed successfully or not
         */
-       public LttngAgentResponse execute(AbstractLttngAgent<?> agent);
+       public LttngAgentResponse execute(ILttngTcpClientListener agent);
 }
\ No newline at end of file
index 8fb0f5a5b81db230d218b5ac040174f00c9b231d..d8788d320c416a9b9538bed603f7a9b45a8ca4cf 100644 (file)
@@ -31,8 +31,6 @@ import java.nio.ByteOrder;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
-import org.lttng.ust.agent.AbstractLttngAgent;
-
 /**
  * Client for agents to connect to a local session daemon, using a TCP socket.
  *
@@ -56,21 +54,26 @@ public class LttngTcpSessiondClient implements Runnable {
        private DataInputStream inFromSessiond;
        private DataOutputStream outToSessiond;
 
-       private final AbstractLttngAgent<?> logAgent;
+       private final ILttngTcpClientListener logAgent;
+       private final int domainValue;
        private final boolean isRoot;
 
-
        /**
         * Constructor
         *
         * @param logAgent
-        *            The logging agent this client will operate on.
+        *            The listener this client will operate on, typically an LTTng
+        *            agent.
+        * @param domainValue
+        *            The integer to send to the session daemon representing the
+        *            tracing domain to handle.
         * @param isRoot
         *            True if this client should connect to the root session daemon,
         *            false if it should connect to the user one.
         */
-       public LttngTcpSessiondClient(AbstractLttngAgent<?> logAgent, boolean isRoot) {
+       public LttngTcpSessiondClient(ILttngTcpClientListener logAgent, int domainValue, boolean isRoot) {
                this.logAgent = logAgent;
+               this.domainValue = domainValue;
                this.isRoot = isRoot;
        }
 
@@ -206,7 +209,7 @@ public class LttngTcpSessiondClient implements Runnable {
                ByteBuffer buf = ByteBuffer.wrap(data);
                String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
 
-               buf.putInt(logAgent.getDomain().value());
+               buf.putInt(domainValue);
                buf.putInt(Integer.parseInt(pid));
                buf.putInt(protocolMajorVersion);
                buf.putInt(protocolMinorVersion);
index ee9d519f65a8fd699a07623974a0fc54041dabde..03cd9de6fb48cf94bf7ef35a3f2da373319940c7 100644 (file)
@@ -21,8 +21,6 @@ package org.lttng.ust.agent.client;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 
-import org.lttng.ust.agent.AbstractLttngAgent;
-
 /**
  * Session daemon command indicating to the Java agent that some events were
  * disabled in the tracing session.
@@ -45,7 +43,7 @@ class SessiondDisableEventCommand implements ISessiondCommand {
        }
 
        @Override
-       public LttngAgentResponse execute(AbstractLttngAgent<?> agent) {
+       public LttngAgentResponse execute(ILttngTcpClientListener agent) {
                boolean success = agent.eventDisabled(this.eventName);
                return (success ? LttngAgentResponse.SUCESS_RESPONSE : DISABLE_EVENT_FAILURE_RESPONSE);
        }
index bb20ac5784454ac6170b22913f1e096e3c379fe3..82204eb19b895af2d22eca200169769f40d59114 100644 (file)
@@ -21,8 +21,6 @@ package org.lttng.ust.agent.client;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 
-import org.lttng.ust.agent.AbstractLttngAgent;
-
 /**
  * Session daemon command indicating to the Java agent that some events were
  * enabled in the tracing session.
@@ -51,7 +49,7 @@ class SessiondEnableEventCommand implements ISessiondCommand {
        }
 
        @Override
-       public LttngAgentResponse execute(AbstractLttngAgent<?> agent) {
+       public LttngAgentResponse execute(ILttngTcpClientListener agent) {
                boolean success = agent.eventEnabled(this.eventName);
                return (success ? LttngAgentResponse.SUCESS_RESPONSE : LttngAgentResponse.FAILURE_RESPONSE);
        }
index f5ad9c50443f6ec805ced0191e2456600693e148..c06eaaad95e09db12565d841a7b75c88ada37194 100644 (file)
@@ -23,8 +23,6 @@ import java.nio.ByteOrder;
 import java.util.ArrayList;
 import java.util.List;
 
-import org.lttng.ust.agent.AbstractLttngAgent;
-
 /**
  * Session daemon command asking the Java agent to list its registered loggers,
  * which corresponds to event names in the tracing session.
@@ -35,7 +33,7 @@ import org.lttng.ust.agent.AbstractLttngAgent;
 class SessiondListLoggersCommand implements ISessiondCommand {
 
        @Override
-       public LttngAgentResponse execute(AbstractLttngAgent<?> agent) {
+       public LttngAgentResponse execute(ILttngTcpClientListener agent) {
                final List<String> loggerList = new ArrayList<String>();
                int dataSize = 0;
 
This page took 0.030662 seconds and 5 git commands to generate.