Commit | Line | Data |
---|---|---|
301a3ddb | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-only |
301a3ddb | 3 | * |
c0c0989a MJ |
4 | * Copyright (C) 2015 EfficiOS Inc. |
5 | * Copyright (C) 2015 Alexandre Montplaisir <alexmonthy@efficios.com> | |
6 | * Copyright (C) 2013 David Goulet <dgoulet@efficios.com> | |
301a3ddb AM |
7 | */ |
8 | ||
9 | package org.lttng.ust.agent.client; | |
10 | ||
11 | import java.nio.ByteBuffer; | |
12 | import java.nio.ByteOrder; | |
68a1ef73 | 13 | import java.util.Collection; |
301a3ddb | 14 | |
301a3ddb AM |
15 | /** |
16 | * Session daemon command asking the Java agent to list its registered loggers, | |
17 | * which corresponds to event names in the tracing session. | |
18 | * | |
19 | * @author Alexandre Montplaisir | |
20 | * @author David Goulet | |
21 | */ | |
1d193914 | 22 | class SessiondListLoggersCommand extends SessiondCommand { |
301a3ddb AM |
23 | |
24 | @Override | |
3165c2f5 | 25 | public LttngAgentResponse execute(ILttngTcpClientListener agent) { |
68a1ef73 | 26 | final Collection<String> loggerList = agent.listAvailableEvents(); |
8c207906 | 27 | return new SessiondListLoggersResponse(loggerList); |
301a3ddb AM |
28 | } |
29 | ||
93253569 | 30 | private static class SessiondListLoggersResponse extends LttngAgentResponse { |
301a3ddb AM |
31 | |
32 | private final static int SIZE = 12; | |
33 | ||
68a1ef73 | 34 | private final Collection<String> loggers; |
301a3ddb | 35 | |
8c207906 | 36 | public SessiondListLoggersResponse(Collection<String> loggers) { |
301a3ddb | 37 | this.loggers = loggers; |
301a3ddb AM |
38 | } |
39 | ||
40 | @Override | |
41 | public ReturnCode getReturnCode() { | |
42 | /* This command can't really fail */ | |
93253569 | 43 | return ReturnCode.CODE_SUCCESS_CMD; |
301a3ddb AM |
44 | } |
45 | ||
46 | @Override | |
47 | public byte[] getBytes() { | |
8c207906 AM |
48 | /* |
49 | * Compute the data size, which is the number of bytes of each | |
50 | * encoded string, +1 per string for the \0 | |
51 | */ | |
52 | int dataSize = 0; | |
53 | for (String logger : loggers) { | |
54 | dataSize += logger.getBytes(SESSIOND_PROTOCOL_CHARSET).length + 1; | |
55 | } | |
56 | ||
57 | /* Prepare the buffer */ | |
301a3ddb AM |
58 | byte data[] = new byte[SIZE + dataSize]; |
59 | ByteBuffer buf = ByteBuffer.wrap(data); | |
60 | buf.order(ByteOrder.BIG_ENDIAN); | |
61 | ||
8c207906 | 62 | /* Write the header section of the response */ |
301a3ddb AM |
63 | buf.putInt(getReturnCode().getCode()); |
64 | buf.putInt(dataSize); | |
65 | buf.putInt(loggers.size()); | |
66 | ||
8c207906 | 67 | /* Write the payload */ |
301a3ddb | 68 | for (String logger : loggers) { |
22191ffd | 69 | buf.put(logger.getBytes(SESSIOND_PROTOCOL_CHARSET)); |
301a3ddb AM |
70 | /* NULL terminated byte after the logger name. */ |
71 | buf.put((byte) 0x0); | |
72 | } | |
73 | return data; | |
74 | } | |
75 | } | |
76 | ||
77 | } |