Commit | Line | Data |
---|---|---|
d60dfbe4 | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-only |
d60dfbe4 | 3 | * |
c0c0989a MJ |
4 | * Copyright (C) 2015-2016 EfficiOS Inc. |
5 | * Copyright (C) 2015-2016 Alexandre Montplaisir <alexmonthy@efficios.com> | |
6 | * Copyright (C) 2013 David Goulet <dgoulet@efficios.com> | |
d60dfbe4 AM |
7 | */ |
8 | ||
9 | package org.lttng.ust.agent.client; | |
10 | ||
1d193914 | 11 | import java.nio.ByteBuffer; |
22191ffd | 12 | import java.nio.charset.Charset; |
1d193914 | 13 | |
301a3ddb | 14 | /** |
1d193914 | 15 | * Base class to represent all commands sent from the session daemon to the Java |
301a3ddb AM |
16 | * agent. The agent is then expected to execute the command and provide a |
17 | * response. | |
18 | * | |
19 | * @author Alexandre Montplaisir | |
20 | */ | |
1d193914 | 21 | abstract class SessiondCommand { |
d60dfbe4 | 22 | |
22191ffd AM |
23 | /** |
24 | * Encoding that should be used for the strings in the sessiond agent | |
25 | * protocol on the socket. | |
26 | */ | |
27 | protected static final Charset SESSIOND_PROTOCOL_CHARSET = Charset.forName("UTF-8"); | |
28 | ||
301a3ddb | 29 | enum CommandType { |
d60dfbe4 AM |
30 | /** List logger(s). */ |
31 | CMD_LIST(1), | |
32 | /** Enable logger by name. */ | |
8ab5c06b | 33 | CMD_EVENT_ENABLE(2), |
d60dfbe4 | 34 | /** Disable logger by name. */ |
8ab5c06b | 35 | CMD_EVENT_DISABLE(3), |
d60dfbe4 | 36 | /** Registration done */ |
8ab5c06b AM |
37 | CMD_REG_DONE(4), |
38 | /** Enable application context */ | |
39 | CMD_APP_CTX_ENABLE(5), | |
40 | /** Disable application context */ | |
41 | CMD_APP_CTX_DISABLE(6); | |
d60dfbe4 AM |
42 | |
43 | private int code; | |
44 | ||
301a3ddb | 45 | private CommandType(int c) { |
d60dfbe4 AM |
46 | code = c; |
47 | } | |
48 | ||
301a3ddb | 49 | public int getCommandType() { |
d60dfbe4 AM |
50 | return code; |
51 | } | |
52 | } | |
53 | ||
54 | /** | |
301a3ddb | 55 | * Execute the command handler's action on the specified tracing agent. |
d60dfbe4 | 56 | * |
301a3ddb AM |
57 | * @param agent |
58 | * The agent on which to execute the command | |
59 | * @return If the command completed successfully or not | |
d60dfbe4 | 60 | */ |
1d193914 AM |
61 | public abstract LttngAgentResponse execute(ILttngTcpClientListener agent); |
62 | ||
63 | /** | |
64 | * Utility method to read agent-protocol strings passed on the socket. The | |
65 | * buffer will contain a 32-bit integer representing the length, immediately | |
66 | * followed by the string itself. | |
67 | * | |
68 | * @param buffer | |
69 | * The ByteBuffer from which to read. It should already be setup | |
70 | * and positioned where the read should begin. | |
71 | * @return The string that was read, or <code>null</code> if it was badly | |
72 | * formatted. | |
73 | */ | |
74 | protected static String readNextString(ByteBuffer buffer) { | |
8c207906 AM |
75 | int nbBytes = buffer.getInt(); |
76 | if (nbBytes < 0) { | |
1d193914 AM |
77 | /* The string length should be positive */ |
78 | return null; | |
79 | } | |
8c207906 | 80 | if (nbBytes == 0) { |
1d193914 AM |
81 | /* The string is explicitly an empty string */ |
82 | return ""; | |
83 | } | |
84 | ||
8c207906 | 85 | byte[] stringBytes = new byte[nbBytes]; |
1d193914 | 86 | buffer.get(stringBytes); |
22191ffd | 87 | return new String(stringBytes, SESSIOND_PROTOCOL_CHARSET).trim(); |
1d193914 AM |
88 | } |
89 | } |