Small refactor of the Java agent's TCP client
[lttng-ust.git] / liblttng-ust-java-agent / java / lttng-ust-agent-common / org / lttng / ust / agent / client / SessiondListLoggersCommand.java
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
new file mode 100644 (file)
index 0000000..cb20ea9
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * 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;
+
+import java.nio.ByteBuffer;
+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.
+ *
+ * @author Alexandre Montplaisir
+ * @author David Goulet
+ */
+class SessiondListLoggersCommand implements ISessiondCommand {
+
+       @Override
+       public ILttngAgentResponse execute(AbstractLttngAgent<?> agent) {
+               final List<String> loggerList = new ArrayList<String>();
+               int dataSize = 0;
+
+               for (String event : agent.listEnabledEvents()) {
+                       loggerList.add(event);
+                       dataSize += event.length() + 1;
+               }
+
+               return new SessiondListLoggersResponse(loggerList, dataSize);
+       }
+
+       private static class SessiondListLoggersResponse implements ILttngAgentResponse {
+
+               private final static int SIZE = 12;
+
+               private final List<String> loggers;
+               private final int dataSize;
+
+               public SessiondListLoggersResponse(List<String> loggers, int dataSize) {
+                       this.loggers = loggers;
+                       this.dataSize = dataSize;
+               }
+
+               @Override
+               public ReturnCode getReturnCode() {
+                       /* This command can't really fail */
+                       return ILttngAgentResponse.SUCESS_RESPONSE.getReturnCode();
+               }
+
+               @Override
+               public byte[] getBytes() {
+                       byte data[] = new byte[SIZE + dataSize];
+                       ByteBuffer buf = ByteBuffer.wrap(data);
+                       buf.order(ByteOrder.BIG_ENDIAN);
+
+                       /* Returned code */
+                       buf.putInt(getReturnCode().getCode());
+                       buf.putInt(dataSize);
+                       buf.putInt(loggers.size());
+
+                       for (String logger : loggers) {
+                               buf.put(logger.getBytes());
+                               /* NULL terminated byte after the logger name. */
+                               buf.put((byte) 0x0);
+                       }
+                       return data;
+               }
+       }
+
+}
This page took 0.023421 seconds and 4 git commands to generate.