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; | |
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 | */ | |
93253569 | 21 | abstract class LttngAgentResponse { |
301a3ddb | 22 | |
93253569 | 23 | private static final int INT_SIZE = 4; |
301a3ddb | 24 | |
f35c6aa0 AM |
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 | ||
301a3ddb AM |
39 | /** |
40 | * Return codes used in agent responses, to indicate success or different | |
41 | * types of failures of the commands. | |
42 | */ | |
93253569 | 43 | protected enum ReturnCode { |
301a3ddb | 44 | |
f35c6aa0 AM |
45 | CODE_SUCCESS_CMD(1, "sucess"), |
46 | CODE_INVALID_CMD(2, "invalid"), | |
47 | CODE_UNKNOWN_LOGGER_NAME(3, "unknown logger name"); | |
301a3ddb | 48 | |
f35c6aa0 AM |
49 | private final int code; |
50 | private final String toString; | |
301a3ddb | 51 | |
f35c6aa0 | 52 | private ReturnCode(int c, String str) { |
301a3ddb | 53 | code = c; |
f35c6aa0 | 54 | toString = str; |
301a3ddb AM |
55 | } |
56 | ||
57 | public int getCode() { | |
58 | return code; | |
59 | } | |
f35c6aa0 AM |
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 | } | |
301a3ddb AM |
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 | */ | |
93253569 | 78 | public abstract ReturnCode getReturnCode(); |
301a3ddb AM |
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 | */ | |
93253569 AM |
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 | } | |
301a3ddb | 92 | |
f35c6aa0 AM |
93 | @Override |
94 | public String toString() { | |
95 | return "LttngAgentResponse[" | |
96 | + "code=" + getReturnCode().getCode() | |
97 | + ", " + getReturnCode().toString() | |
98 | + "]"; | |
99 | } | |
301a3ddb | 100 | } |