Introduce a new client listener interface for the Java agent
[lttng-ust.git] / liblttng-ust-java-agent / java / lttng-ust-agent-common / org / lttng / ust / agent / client / SessiondListLoggersCommand.java
1 /*
2 * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
3 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
4 *
5 * This library is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License, version 2.1 only,
7 * as published by the Free Software Foundation.
8 *
9 * This library is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12 * for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 package org.lttng.ust.agent.client;
20
21 import java.nio.ByteBuffer;
22 import java.nio.ByteOrder;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 /**
27 * Session daemon command asking the Java agent to list its registered loggers,
28 * which corresponds to event names in the tracing session.
29 *
30 * @author Alexandre Montplaisir
31 * @author David Goulet
32 */
33 class SessiondListLoggersCommand implements ISessiondCommand {
34
35 @Override
36 public LttngAgentResponse execute(ILttngTcpClientListener agent) {
37 final List<String> loggerList = new ArrayList<String>();
38 int dataSize = 0;
39
40 for (String event : agent.listEnabledEvents()) {
41 loggerList.add(event);
42 dataSize += event.length() + 1;
43 }
44
45 return new SessiondListLoggersResponse(loggerList, dataSize);
46 }
47
48 private static class SessiondListLoggersResponse extends LttngAgentResponse {
49
50 private final static int SIZE = 12;
51
52 private final List<String> loggers;
53 private final int dataSize;
54
55 public SessiondListLoggersResponse(List<String> loggers, int dataSize) {
56 this.loggers = loggers;
57 this.dataSize = dataSize;
58 }
59
60 @Override
61 public ReturnCode getReturnCode() {
62 /* This command can't really fail */
63 return ReturnCode.CODE_SUCCESS_CMD;
64 }
65
66 @Override
67 public byte[] getBytes() {
68 byte data[] = new byte[SIZE + dataSize];
69 ByteBuffer buf = ByteBuffer.wrap(data);
70 buf.order(ByteOrder.BIG_ENDIAN);
71
72 /* Returned code */
73 buf.putInt(getReturnCode().getCode());
74 buf.putInt(dataSize);
75 buf.putInt(loggers.size());
76
77 for (String logger : loggers) {
78 buf.put(logger.getBytes());
79 /* NULL terminated byte after the logger name. */
80 buf.put((byte) 0x0);
81 }
82 return data;
83 }
84 }
85
86 }
This page took 0.031884 seconds and 5 git commands to generate.