| 1 | /* |
| 2 | * SPDX-License-Identifier: LGPL-2.1-only |
| 3 | * |
| 4 | * Copyright (C) 2015 EfficiOS Inc. |
| 5 | * Copyright (C) 2015 Alexandre Montplaisir <alexmonthy@efficios.com> |
| 6 | * Copyright (C) 2013 David Goulet <dgoulet@efficios.com> |
| 7 | */ |
| 8 | |
| 9 | package org.lttng.ust.agent.client; |
| 10 | |
| 11 | import java.nio.ByteBuffer; |
| 12 | import java.nio.ByteOrder; |
| 13 | |
| 14 | /** |
| 15 | * Interface for all response messages sent from the Java agent to the sessiond |
| 16 | * daemon. Normally sent after a command coming from the session daemon was |
| 17 | * executed. |
| 18 | * |
| 19 | * @author Alexandre Montplaisir |
| 20 | */ |
| 21 | abstract class LttngAgentResponse { |
| 22 | |
| 23 | private static final int INT_SIZE = 4; |
| 24 | |
| 25 | public static final LttngAgentResponse SUCESS_RESPONSE = new LttngAgentResponse() { |
| 26 | @Override |
| 27 | public ReturnCode getReturnCode() { |
| 28 | return ReturnCode.CODE_SUCCESS_CMD; |
| 29 | } |
| 30 | }; |
| 31 | |
| 32 | public static final LttngAgentResponse FAILURE_RESPONSE = new LttngAgentResponse() { |
| 33 | @Override |
| 34 | public ReturnCode getReturnCode() { |
| 35 | return ReturnCode.CODE_INVALID_CMD; |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | /** |
| 40 | * Return codes used in agent responses, to indicate success or different |
| 41 | * types of failures of the commands. |
| 42 | */ |
| 43 | protected enum ReturnCode { |
| 44 | |
| 45 | CODE_SUCCESS_CMD(1, "success"), |
| 46 | CODE_INVALID_CMD(2, "invalid"), |
| 47 | CODE_UNKNOWN_LOGGER_NAME(3, "unknown logger name"); |
| 48 | |
| 49 | private final int code; |
| 50 | private final String toString; |
| 51 | |
| 52 | private ReturnCode(int c, String str) { |
| 53 | code = c; |
| 54 | toString = str; |
| 55 | } |
| 56 | |
| 57 | public int getCode() { |
| 58 | return code; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Mainly used for debugging. The strings are not sent through the |
| 63 | * socket. |
| 64 | */ |
| 65 | @Override |
| 66 | public String toString() { |
| 67 | return toString; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get the {@link ReturnCode} that goes with this response. It is expected |
| 73 | * by the session daemon, but some commands may require more than this |
| 74 | * in their response. |
| 75 | * |
| 76 | * @return The return code |
| 77 | */ |
| 78 | public abstract ReturnCode getReturnCode(); |
| 79 | |
| 80 | /** |
| 81 | * Gets a byte array of the response so that it may be streamed. |
| 82 | * |
| 83 | * @return The byte array of the response |
| 84 | */ |
| 85 | public byte[] getBytes() { |
| 86 | byte data[] = new byte[INT_SIZE]; |
| 87 | ByteBuffer buf = ByteBuffer.wrap(data); |
| 88 | buf.order(ByteOrder.BIG_ENDIAN); |
| 89 | buf.putInt(getReturnCode().getCode()); |
| 90 | return data; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public String toString() { |
| 95 | return "LttngAgentResponse[" |
| 96 | + "code=" + getReturnCode().getCode() |
| 97 | + ", " + getReturnCode().toString() |
| 98 | + "]"; |
| 99 | } |
| 100 | } |