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