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