Commit | Line | Data |
---|---|---|
301a3ddb AM |
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; | |
68a1ef73 | 23 | import java.util.Collection; |
301a3ddb | 24 | |
301a3ddb AM |
25 | /** |
26 | * Session daemon command asking the Java agent to list its registered loggers, | |
27 | * which corresponds to event names in the tracing session. | |
28 | * | |
29 | * @author Alexandre Montplaisir | |
30 | * @author David Goulet | |
31 | */ | |
1d193914 | 32 | class SessiondListLoggersCommand extends SessiondCommand { |
301a3ddb AM |
33 | |
34 | @Override | |
3165c2f5 | 35 | public LttngAgentResponse execute(ILttngTcpClientListener agent) { |
68a1ef73 | 36 | final Collection<String> loggerList = agent.listAvailableEvents(); |
8c207906 | 37 | return new SessiondListLoggersResponse(loggerList); |
301a3ddb AM |
38 | } |
39 | ||
93253569 | 40 | private static class SessiondListLoggersResponse extends LttngAgentResponse { |
301a3ddb AM |
41 | |
42 | private final static int SIZE = 12; | |
43 | ||
68a1ef73 | 44 | private final Collection<String> loggers; |
301a3ddb | 45 | |
8c207906 | 46 | public SessiondListLoggersResponse(Collection<String> loggers) { |
301a3ddb | 47 | this.loggers = loggers; |
301a3ddb AM |
48 | } |
49 | ||
50 | @Override | |
51 | public ReturnCode getReturnCode() { | |
52 | /* This command can't really fail */ | |
93253569 | 53 | return ReturnCode.CODE_SUCCESS_CMD; |
301a3ddb AM |
54 | } |
55 | ||
56 | @Override | |
57 | public byte[] getBytes() { | |
8c207906 AM |
58 | /* |
59 | * Compute the data size, which is the number of bytes of each | |
60 | * encoded string, +1 per string for the \0 | |
61 | */ | |
62 | int dataSize = 0; | |
63 | for (String logger : loggers) { | |
64 | dataSize += logger.getBytes(SESSIOND_PROTOCOL_CHARSET).length + 1; | |
65 | } | |
66 | ||
67 | /* Prepare the buffer */ | |
301a3ddb AM |
68 | byte data[] = new byte[SIZE + dataSize]; |
69 | ByteBuffer buf = ByteBuffer.wrap(data); | |
70 | buf.order(ByteOrder.BIG_ENDIAN); | |
71 | ||
8c207906 | 72 | /* Write the header section of the response */ |
301a3ddb AM |
73 | buf.putInt(getReturnCode().getCode()); |
74 | buf.putInt(dataSize); | |
75 | buf.putInt(loggers.size()); | |
76 | ||
8c207906 | 77 | /* Write the payload */ |
301a3ddb | 78 | for (String logger : loggers) { |
22191ffd | 79 | buf.put(logger.getBytes(SESSIOND_PROTOCOL_CHARSET)); |
301a3ddb AM |
80 | /* NULL terminated byte after the logger name. */ |
81 | buf.put((byte) 0x0); | |
82 | } | |
83 | return data; | |
84 | } | |
85 | } | |
86 | ||
87 | } |