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