-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIncrementPlugin.java
More file actions
67 lines (55 loc) · 2.34 KB
/
IncrementPlugin.java
File metadata and controls
67 lines (55 loc) · 2.34 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
package com.example.plugin;
import com.google.auto.service.AutoService;
import io.deephaven.engine.table.Table;
import io.deephaven.engine.util.TableTools;
import io.deephaven.plugin.type.ObjectCommunicationException;
import io.deephaven.plugin.type.ObjectType;
import io.deephaven.plugin.type.ObjectTypeBase;
import org.json.JSONObject;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import static com.example.plugin.IncrementPluginClient.*;
@AutoService(ObjectType.class)
public class IncrementPlugin extends ObjectTypeBase {
@Override
public String name() {
return PLUGIN_NAME;
}
@Override
public boolean isType(final Object object) {
return object instanceof IncrementPlugin;
}
@Override
public MessageStream compatibleClientConnection(final Object object, final MessageStream connection)
throws ObjectCommunicationException {
// must send an empty payload when the client first connects
connection.onData(ByteBuffer.allocate(0));
return new ServerMessageStream(connection);
}
private class ServerMessageStream implements MessageStream {
private final MessageStream connection;
public ServerMessageStream(final MessageStream connection) {
this.connection = connection;
}
@Override
public void onData(ByteBuffer payload, Object[] references) throws ObjectCommunicationException {
// Parses the payload as a JSON string, returns a table with the incremented value.
final String jsonString = StandardCharsets.UTF_8.decode(payload).toString();
JSONObject jsonObject = new JSONObject(jsonString);
if (!jsonObject.has(VALUE_KEY)) {
System.out.println("IncrementPlugin: Received JSON does not contain '" + VALUE_KEY + "' key. Ignoring payload.");
return;
}
int receivedInteger = jsonObject.getInt(VALUE_KEY);
int resultInteger = receivedInteger + 1;
final Table resultTable = TableTools.newTable(TableTools.intCol(TABLE_COL, resultInteger));
// Send a reference to the table to the client
payload.flip();
connection.onData(payload, resultTable);
}
@Override
public void onClose() {
connection.onClose();
}
}
}