-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDaemonClient.java
More file actions
180 lines (164 loc) · 7.51 KB
/
DaemonClient.java
File metadata and controls
180 lines (164 loc) · 7.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package io.github.randomcodespace.sonarpredict.cli;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.UnixDomainSocketAddress;
import java.nio.channels.Channels;
import java.nio.channels.SocketChannel;
import java.util.Objects;
import java.util.UUID;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.TextNode;
import io.github.randomcodespace.sonarpredict.protocol.Json;
import io.github.randomcodespace.sonarpredict.protocol.MessageCodec;
import io.github.randomcodespace.sonarpredict.protocol.Method;
import io.github.randomcodespace.sonarpredict.protocol.SocketPaths;
import io.github.randomcodespace.sonarpredict.protocol.WireMessage;
import io.github.randomcodespace.sonarpredict.protocol.dto.AnalyzeRequest;
import io.github.randomcodespace.sonarpredict.protocol.dto.AnalyzeResponse;
import io.github.randomcodespace.sonarpredict.protocol.dto.PingResponse;
import io.github.randomcodespace.sonarpredict.protocol.dto.RuleMetadata;
/**
* RPC client for the analysis daemon over its Unix domain socket.
*
* <p><b>Auto-start.</b> Every call connects a fresh {@link SocketChannel} to
* {@link SocketPaths#socket()}. If the connect fails — no daemon is listening —
* the client asks {@link DaemonLauncher} to start one and retries exactly once.
* The launcher's {@code start()} returns only when the socket is accepting, so
* the retry never races the bind.
*
* <p><b>Framing & correlation.</b> Requests and responses are framed
* {@link WireMessage}s via {@link MessageCodec}. Each request carries a fresh
* {@code id}; the response is checked to carry the same {@code id} and
* {@code method}, so a mismatched frame is caught rather than mis-decoded.
*
* <p><b>Errors.</b> The daemon reports every failure as a response whose
* payload is {@code {"error": "..."}} reusing the request method. The client
* detects that {@code error} field and throws {@link DaemonException}; a bad
* result is never returned silently. Socket and protocol failures also become
* {@link DaemonException}.
*
* <p>One connection per call: the daemon serves a connection serially, and the
* CLI issues calls one at a time, so no pooling is needed in v1.
*/
public final class DaemonClient implements DaemonRpc {
/** JSON field the daemon uses to signal a failure payload. */
private static final String ERROR_FIELD = "error";
private final SocketPaths paths;
private final DaemonLauncher launcher;
/**
* @param paths the daemon socket location
* @param launcher used to auto-start the daemon when it is not running
*/
public DaemonClient(SocketPaths paths, DaemonLauncher launcher) {
this.paths = Objects.requireNonNull(paths, "paths");
this.launcher = Objects.requireNonNull(launcher, "launcher");
}
/** Sends a {@code PING} and returns the daemon's liveness/identity payload. */
@Override
public PingResponse ping() {
return call(Method.PING, null, PingResponse.class);
}
/** Sends an {@code ANALYZE} request and returns its findings. */
@Override
public AnalyzeResponse analyze(AnalyzeRequest request) {
Objects.requireNonNull(request, "request");
return call(Method.ANALYZE, Json.mapper().valueToTree(request), AnalyzeResponse.class);
}
/** Looks up static metadata for one rule key. */
@Override
public RuleMetadata ruleMetadata(String ruleKey) {
Objects.requireNonNull(ruleKey, "ruleKey");
return call(Method.RULE_METADATA, new TextNode(ruleKey), RuleMetadata.class);
}
/**
* Fetches the daemon's whole rule catalog in one round trip — a
* {@code RULE_METADATA} call with a {@code null} payload, which the daemon
* answers with a {@code List<RuleMetadata>}.
*/
@Override
public java.util.List<RuleMetadata> ruleCatalog() {
WireMessage response = exchange(
new WireMessage(newId(), Method.RULE_METADATA, null));
JsonNode body = response.payload();
if (body != null && body.isObject() && body.has(ERROR_FIELD)) {
throw new DaemonException(
"daemon error (RULE_METADATA): " + body.get(ERROR_FIELD).asText());
}
try {
return Json.mapper().convertValue(
body,
Json.mapper().getTypeFactory()
.constructCollectionType(java.util.List.class, RuleMetadata.class));
} catch (IllegalArgumentException e) {
throw new DaemonException(
"could not decode the rule catalog from the daemon response", e);
}
}
/** Sends a {@code SHUTDOWN} request asking the daemon to stop. */
@Override
public void shutdown() {
// The daemon may close the socket as it stops; a clean ack or an EOF
// both mean the request was honoured.
try {
exchange(new WireMessage(newId(), Method.SHUTDOWN, null));
} catch (DaemonException ignored) {
// The daemon tearing the connection down mid-response is expected.
}
}
private <T> T call(Method method, JsonNode payload, Class<T> responseType) {
WireMessage response = exchange(new WireMessage(newId(), method, payload));
JsonNode body = response.payload();
if (body != null && body.isObject() && body.has(ERROR_FIELD)) {
throw new DaemonException(
"daemon error (" + method + "): " + body.get(ERROR_FIELD).asText());
}
try {
return Json.mapper().treeToValue(body, responseType);
} catch (com.fasterxml.jackson.core.JsonProcessingException e) {
throw new DaemonException(
"could not decode " + responseType.getSimpleName()
+ " from the daemon response", e);
}
}
/**
* Sends one request and reads its response, auto-starting the daemon and
* retrying once if no daemon is listening.
*/
private WireMessage exchange(WireMessage request) {
try {
return roundTrip(request);
} catch (IOException firstFailure) {
// No daemon, or a stale socket — start one and retry exactly once.
launcher.start();
try {
return roundTrip(request);
} catch (IOException secondFailure) {
throw new DaemonException(
"daemon RPC failed after auto-start: " + secondFailure.getMessage(),
secondFailure);
}
}
}
private WireMessage roundTrip(WireMessage request) throws IOException {
try (SocketChannel channel =
SocketChannel.open(UnixDomainSocketAddress.of(paths.socket()));
OutputStream out = Channels.newOutputStream(channel);
InputStream in = Channels.newInputStream(channel)) {
MessageCodec.writeMessage(out, request);
WireMessage response = MessageCodec.readMessage(in);
if (!request.id().equals(response.id())) {
throw new DaemonException("response id mismatch: expected "
+ request.id() + ", got " + response.id());
}
if (request.method() != response.method()) {
throw new DaemonException("response method mismatch: expected "
+ request.method() + ", got " + response.method());
}
return response;
}
}
private static String newId() {
return UUID.randomUUID().toString();
}
}