-
Notifications
You must be signed in to change notification settings - Fork 1
/
Socket.java
103 lines (89 loc) · 2.96 KB
/
Socket.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
98
99
100
101
102
103
package controllers;
import java.util.List;
import models.Component;
import models.Components;
import models.EventBus;
import models.SystemInfo;
import models.datatypes.DataType;
import models.datatypes.DataTypes;
import models.datatypes.Log;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.ObjectNode;
import play.libs.Json;
import ws.wamplay.annotations.URIPrefix;
import ws.wamplay.annotations.onSubscribe;
import ws.wamplay.callbacks.PubSubCallback;
import ws.wamplay.controllers.WAMPlayContoller;
import ws.wamplay.controllers.WAMPlayServer;
import ws.wamplay.models.WAMPlayClient;
import ws.wamplay.models.messages.Event;
import com.google.common.eventbus.Subscribe;
@URIPrefix("pheme.")
public class Socket extends WAMPlayContoller {
public Socket() {
this.addTopic("components");
this.addTopic("system");
// Get in on the awesome event bus action.
EventBus.subscribe(this);
}
@Subscribe
public void componentListener(final Component component) {
addComponentTopic(component);
publishComponents();
}
@Subscribe
public void systemInfoListener(final SystemInfo info) {
WAMPlayServer.publish(getTopic("system"), Json.toJson(info.getInfo()));
}
private void addComponentTopic(final Component component) {
final String componentTopic = getTopic(component.id);
if (!WAMPlayServer.isTopic(componentTopic)) {
WAMPlayServer.addTopic(componentTopic);
WAMPlayServer.addTopic(componentTopic, new PubSubCallback() {
@Override
protected boolean onSubscribe(String sessionID) {
// Send all the old data to the new client right away.
WAMPlayClient client = WAMPlayServer.getClient(sessionID);
List<DataType> datas = DataTypes.getAllFor(component);
for (DataType data : datas) {
client.send(new Event(componentTopic, Json.toJson(data))
.toJson());
}
return true;
}
});
}
}
private void publishComponents() {
ObjectNode result = Json.newObject();
List<Component> computers = Component.getAll(Components.COMPUTER);
List<Component> jobs = Component.getAll(Components.JOB);
result.put("computerCount", computers.size());
result.put("jobCount", jobs.size());
result.put("jobs", Json.toJson(jobs));
result.put("computers", Json.toJson(computers));
WAMPlayServer.publish(getTopic("components"), result);
}
@Subscribe
public void dataListener(DataType data) {
JsonNode d = Json.toJson(data);
if (data.getDataType() == DataTypes.LOG) {
WAMPlayServer.publish(getTopic("logs"), d);
}
WAMPlayServer.publish(getTopic(data.getComponent().id), d);
}
@onSubscribe("logs")
public boolean logsSubscribe(String sessionID) {
String topic = getTopic("logs");
// Send all the old logs to the new client right away.
WAMPlayClient client = WAMPlayServer.getClient(sessionID);
List<Log> datas = Log.getAll();
for (DataType data : datas) {
client.send(new Event(topic, Json.toJson(data)).toJson());
}
return true;
}
private String getTopic(String id) {
return "pheme." + id;
}
}