forked from riemann/riemann-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractRiemannClient.java
97 lines (75 loc) · 2.73 KB
/
AbstractRiemannClient.java
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
package com.aphyr.riemann.client;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.List;
import java.util.Arrays;
import com.aphyr.riemann.Proto.Event;
import com.aphyr.riemann.Proto.Query;
import com.aphyr.riemann.Proto.Msg;
public abstract class AbstractRiemannClient {
public static final int DEFAULT_PORT = 5555;
protected final InetSocketAddress server;
public AbstractRiemannClient(final InetSocketAddress server) {
this.server = server;
}
public AbstractRiemannClient(final int port) throws UnknownHostException {
this.server = new InetSocketAddress(InetAddress.getLocalHost(), port);
}
public AbstractRiemannClient() throws UnknownHostException {
this(new InetSocketAddress(InetAddress.getLocalHost(), DEFAULT_PORT));
}
public EventDSL event() {
return new EventDSL(this);
}
// Sends events and checks the server's response. Will throw IOException for
// network failures, ServerError for error responses from Riemann. Returns
// true if events acknowledged.
public Boolean sendEventsWithAck(final Event... events) throws IOException, ServerError {
validate(
sendRecvMessage(
Msg.newBuilder()
.addAllEvents(Arrays.asList(events))
.build()
)
);
return true;
}
// Sends events in fire-and-forget fashion. Doesn't check server response,
// swallows all exceptions silently. No guarantees on delivery.
public void sendEvents(final Event... events) {
try {
sendMaybeRecvMessage(
Msg.newBuilder()
.addAllEvents(Arrays.asList(events))
.build()
);
} catch (IOException e) {
// Fuck it.
}
}
public List<Event> query(String q) throws IOException, ServerError {
Msg m = sendRecvMessage(Msg.newBuilder()
.setQuery(
Query.newBuilder().setString(q).build())
.build());
validate(m);
return Collections.unmodifiableList(m.getEventsList());
}
public abstract void sendMessage(Msg message) throws IOException;
public abstract Msg recvMessage() throws IOException;
public abstract Msg sendRecvMessage(Msg message) throws IOException;
public abstract Msg sendMaybeRecvMessage(Msg message) throws IOException;
public abstract boolean isConnected();
public abstract void connect() throws IOException;
public abstract void disconnect() throws IOException;
// Asserts that the message is OK; if not, throws a ServerError.
public Msg validate(Msg message) throws IOException, ServerError {
if (message.hasOk() && message.getOk() == false) {
throw(new ServerError(message.getError()));
}
return message;
}
}