forked from Swofty-Developments/HypixelSkyBlock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerOutboundMessage.java
More file actions
148 lines (126 loc) · 5.93 KB
/
ServerOutboundMessage.java
File metadata and controls
148 lines (126 loc) · 5.93 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
package net.swofty.proxyapi.redis;
import net.swofty.commons.ServiceType;
import net.swofty.commons.impl.ServiceProxyRequest;
import net.swofty.commons.protocol.ProtocolObject;
import net.swofty.commons.proxy.ToProxyChannels;
import net.swofty.redisapi.api.ChannelRegistry;
import net.swofty.redisapi.api.RedisAPI;
import org.json.JSONObject;
import org.tinylog.Logger;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;
public class ServerOutboundMessage {
private static final Map<UUID, Consumer<String>> redisMessageListeners = new HashMap<>();
public static final Map<String, ProtocolObject> protocolObjects = new HashMap<>();
public static void registerServerToProxy(ToProxyChannels channel) {
RedisAPI.getInstance().registerChannel(channel.getChannelName(), (event) -> {
String messageWithoutFilter = event.message.substring(event.message.indexOf(";") + 1);
String[] split = messageWithoutFilter.split("}=-=-=\\{");
UUID uuid = UUID.fromString(split[0]);
redisMessageListeners.get(uuid).accept(split[1]);
redisMessageListeners.remove(uuid);
});
}
public static void sendMessageToProxy(ToProxyChannels channel, JSONObject message, Consumer<JSONObject> response) {
UUID uuid = UUID.randomUUID();
UUID filterID = UUID.fromString(RedisAPI.getInstance().getFilterId());
Consumer<String> consumer = (s) -> {
response.accept(new JSONObject(s));
};
redisMessageListeners.put(uuid, consumer);
RedisAPI.getInstance().publishMessage("proxy",
ChannelRegistry.getFromName(channel.getChannelName()),
message.toString() + "}=-=-={" + uuid + "}=-=-={" + filterID);
}
public static void registerFromProtocolObject(ProtocolObject object) {
String requestTypeName = getRequestTypeName(object);
protocolObjects.put(requestTypeName, object);
RedisAPI.getInstance().registerChannel(object.channel(), (event) -> {
String messageWithoutFilter = event.message.substring(event.message.indexOf(";") + 1);
String[] split = messageWithoutFilter.split("}=-=---=\\{");
UUID uuid = UUID.fromString(split[0]);
String message;
if (split.length != 1) {
message = split[1];
} else message = "";
try {
redisMessageListeners.get(uuid).accept(message);
redisMessageListeners.remove(uuid);
} catch (Exception e) {
Logger.error("Failed to handle message from " + uuid + ": " + e.getMessage());
}
});
}
public static void sendMessageToService(ServiceType service,
ProtocolObject specification,
Object rawMessage,
Consumer<String> response) {
UUID requestId = UUID.randomUUID();
UUID toCallback = null;
try {
toCallback = UUID.fromString(RedisAPI.getInstance().getFilterId());
} catch (Exception e) {
return;
}
redisMessageListeners.put(requestId, response);
String message = specification.translateToString(rawMessage);
RedisAPI.getInstance().publishMessage(service.name(),
ChannelRegistry.getFromName(specification.channel()),
new ServiceProxyRequest(requestId, toCallback.toString(),
specification.channel(), message).toJSON().toString());
}
/**
* Fire-and-forget: send to a service and do not wait for or register a response.
*/
public static void sendMessageToServiceFireAndForget(ServiceType service,
ProtocolObject specification,
Object rawMessage) {
UUID requestId = UUID.randomUUID();
String callback = null;
try {
callback = RedisAPI.getInstance().getFilterId();
} catch (Exception ignored) {
}
String message = specification.translateToString(rawMessage);
RedisAPI.getInstance().publishMessage(
service.name(),
ChannelRegistry.getFromName(specification.channel()),
new ServiceProxyRequest(
requestId,
callback != null ? callback : "proxy",
specification.channel(),
message
).toJSON().toString()
);
}
/**
* Fire-and-forget broadcast to all service types.
*/
public static void sendMessageToAllServicesFireAndForget(ProtocolObject specification,
Object rawMessage) {
for (ServiceType serviceType : ServiceType.values()) {
sendMessageToServiceFireAndForget(serviceType, specification, rawMessage);
}
}
private static String getRequestTypeName(ProtocolObject<?, ?> protocolObject) {
Class<?> clazz = protocolObject.getClass();
Type genericSuperclass = clazz.getGenericSuperclass();
if (genericSuperclass instanceof ParameterizedType paramType) {
Type[] typeArguments = paramType.getActualTypeArguments();
if (typeArguments.length > 0) {
Type firstTypeArg = typeArguments[0];
if (firstTypeArg instanceof Class) {
return ((Class<?>) firstTypeArg).getSimpleName();
} else {
// Handle cases where T might be another generic type
return firstTypeArg.getTypeName();
}
}
}
throw new IllegalArgumentException("Could not determine the type T for the given ProtocolObject");
}
}