Commit d0bae58d04b8b2160c8f336ceac82f652d21843f
1 parent
e9fae278
Fix incorrect silence check in PluginChannels
Showing
1 changed file
with
232 additions
and
232 deletions
java/common/com/mumfrey/liteloader/core/PluginChannels.java
1 | -package com.mumfrey.liteloader.core; | ||
2 | - | ||
3 | -import io.netty.buffer.Unpooled; | ||
4 | - | ||
5 | -import java.util.Collections; | ||
6 | -import java.util.HashMap; | ||
7 | -import java.util.HashSet; | ||
8 | -import java.util.LinkedList; | ||
9 | -import java.util.List; | ||
10 | -import java.util.Map; | ||
11 | -import java.util.Set; | ||
12 | - | ||
13 | -import net.minecraft.network.INetHandler; | ||
14 | -import net.minecraft.network.PacketBuffer; | ||
15 | - | ||
16 | -import com.google.common.base.Charsets; | ||
17 | -import com.mumfrey.liteloader.api.InterfaceProvider; | ||
18 | -import com.mumfrey.liteloader.interfaces.FastIterableDeque; | ||
19 | -import com.mumfrey.liteloader.util.log.LiteLoaderLogger; | ||
20 | - | ||
21 | -/** | ||
22 | - * Manages plugin channel connections and subscriptions for LiteLoader | ||
23 | - * | ||
24 | - * @author Adam Mummery-Smith | ||
25 | - */ | ||
26 | -public abstract class PluginChannels<L extends CommonPluginChannelListener> implements InterfaceProvider | ||
27 | -{ | ||
28 | - // reserved channel consts | ||
29 | - protected static final String CHANNEL_REGISTER = "REGISTER"; | ||
30 | - protected static final String CHANNEL_UNREGISTER = "UNREGISTER"; | ||
31 | - | ||
32 | - /** | ||
33 | - * Number of faults for a specific listener before a warning is generated | ||
34 | - */ | ||
35 | - protected static final int WARN_FAULT_THRESHOLD = 1000; | ||
36 | - | ||
37 | - /** | ||
38 | - * Mapping of plugin channel names to listeners | ||
39 | - */ | ||
40 | - protected final HashMap<String, LinkedList<L>> pluginChannels = new HashMap<String, LinkedList<L>>(); | ||
41 | - | ||
42 | - /** | ||
43 | - * List of mods which implement PluginChannelListener interface | ||
44 | - */ | ||
45 | - protected final FastIterableDeque<L> pluginChannelListeners; | ||
46 | - | ||
47 | - /** | ||
48 | - * Plugin channels that we know the server supports | ||
49 | - */ | ||
50 | - protected final Set<String> remotePluginChannels = new HashSet<String>(); | ||
51 | - | ||
52 | - /** | ||
53 | - * Keep track of faulting listeners so that we can periodically log a message if a listener is throwing LOTS of exceptions | ||
54 | - */ | ||
55 | - protected final Map<L, Integer> faultingPluginChannelListeners = new HashMap<L, Integer>(); | ||
56 | - | ||
57 | - /** | ||
58 | - * Package private | ||
59 | - */ | ||
60 | - PluginChannels() | ||
61 | - { | ||
62 | - this.pluginChannelListeners = this.createHandlerList(); | ||
63 | - } | ||
64 | - | ||
65 | - /** | ||
66 | - * Spawn the handler list instance for this channel manager | ||
67 | - */ | ||
68 | - protected abstract FastIterableDeque<L> createHandlerList(); | ||
69 | - | ||
70 | - /** | ||
71 | - * Get the current set of registered client-side channels | ||
72 | - */ | ||
73 | - public Set<String> getLocalChannels() | ||
74 | - { | ||
75 | - return Collections.unmodifiableSet(this.pluginChannels.keySet()); | ||
76 | - } | ||
77 | - | ||
78 | - /** | ||
79 | - * Get the current set of registered server channels | ||
80 | - */ | ||
81 | - public Set<String> getRemoteChannels() | ||
82 | - { | ||
83 | - return Collections.unmodifiableSet(this.remotePluginChannels); | ||
84 | - } | ||
85 | - | ||
86 | - /** | ||
87 | - * Check whether a server plugin channel is registered | ||
88 | - * | ||
89 | - * @param channel | ||
90 | - * @return true if the channel is registered at the server side | ||
91 | - */ | ||
92 | - public boolean isRemoteChannelRegistered(String channel) | ||
93 | - { | ||
94 | - return this.remotePluginChannels.contains(channel); | ||
95 | - } | ||
96 | - | ||
97 | - /** | ||
98 | - * @param pluginChannelListener | ||
99 | - */ | ||
100 | - protected void addPluginChannelListener(L pluginChannelListener) | ||
101 | - { | ||
102 | - this.pluginChannelListeners.add(pluginChannelListener); | ||
103 | - } | ||
104 | - | ||
105 | - /** | ||
106 | - * Connecting to a new server, clear plugin channels | ||
107 | - * | ||
108 | - * @param netHandler | ||
109 | - */ | ||
110 | - protected void clearPluginChannels(INetHandler netHandler) | ||
111 | - { | ||
112 | - this.pluginChannels.clear(); | ||
113 | - this.remotePluginChannels.clear(); | ||
114 | - this.faultingPluginChannelListeners.clear(); | ||
115 | - } | ||
116 | - | ||
117 | - /** | ||
118 | - * @param data | ||
119 | - */ | ||
120 | - protected void onRegisterPacketReceived(PacketBuffer data) | ||
121 | - { | ||
122 | - try | ||
123 | - { | ||
124 | - byte[] bytes = new byte[data.readableBytes()]; | ||
125 | - data.readBytes(bytes); | ||
126 | - String channels = new String(bytes, Charsets.UTF_8); | ||
127 | - for (String channel : channels.split("\u0000")) | ||
128 | - { | ||
129 | - this.remotePluginChannels.add(channel); | ||
130 | - } | ||
131 | - } | ||
132 | - catch (Exception ex) | ||
133 | - { | ||
134 | - LiteLoaderLogger.warning(ex, "Error decoding REGISTER packet from remote host %s", ex.getClass().getSimpleName()); | ||
135 | - } | ||
136 | - } | ||
137 | - | ||
138 | - /** | ||
139 | - * | ||
140 | - */ | ||
141 | - protected PacketBuffer getRegistrationData() | ||
142 | - { | ||
143 | - // If any mods have registered channels, send the REGISTER packet | ||
144 | - if (this.pluginChannels.keySet().size() > 0) | ||
145 | - { | ||
146 | - StringBuilder channelList = new StringBuilder(); | ||
147 | - boolean separator = false; | ||
148 | - | ||
149 | - for (String channel : this.pluginChannels.keySet()) | ||
150 | - { | ||
151 | - if (separator) channelList.append("\u0000"); | ||
152 | - channelList.append(channel); | ||
153 | - separator = true; | ||
154 | - } | ||
155 | - | ||
156 | - PacketBuffer buffer = new PacketBuffer(Unpooled.buffer()); | ||
157 | - buffer.writeBytes(channelList.toString().getBytes(Charsets.UTF_8)); | ||
158 | - return buffer; | ||
159 | - } | ||
160 | - | ||
161 | - return null; | ||
162 | - } | ||
163 | - | ||
164 | - /** | ||
165 | - * Adds plugin channels for the specified listener to the local channels | ||
166 | - * collection | ||
167 | - * | ||
168 | - * @param pluginChannelListener | ||
169 | - */ | ||
170 | - protected void addPluginChannelsFor(L pluginChannelListener) | ||
171 | - { | ||
172 | - List<String> channels = pluginChannelListener.getChannels(); | ||
173 | - | ||
174 | - if (channels != null) | ||
175 | - { | ||
176 | - for (String channel : channels) | ||
177 | - { | ||
178 | - if (channel.length() > 16 || channel.toUpperCase().equals(CHANNEL_REGISTER) || channel.toUpperCase().equals(CHANNEL_UNREGISTER)) | ||
179 | - continue; | ||
180 | - | ||
181 | - if (!this.pluginChannels.containsKey(channel)) | ||
182 | - { | ||
183 | - this.pluginChannels.put(channel, new LinkedList<L>()); | ||
184 | - } | ||
185 | - | ||
186 | - this.pluginChannels.get(channel).add(pluginChannelListener); | ||
187 | - } | ||
188 | - } | ||
189 | - } | ||
190 | - | ||
191 | - /** | ||
192 | - * Policy for dispatching plugin channel packets | ||
193 | - * | ||
194 | - * @author Adam Mummery-Smith | ||
195 | - */ | ||
196 | - public enum ChannelPolicy | ||
197 | - { | ||
198 | - /** | ||
199 | - * Dispatch the message, throw an exception if the channel is not registered | ||
200 | - */ | ||
201 | - DISPATCH, | ||
202 | - | ||
203 | - /** | ||
204 | - * Dispatch the message, return false if the channel is not registered | ||
205 | - */ | ||
206 | - DISPATCH_IF_REGISTERED, | ||
207 | - | ||
208 | - /** | ||
209 | - * Dispatch the message | ||
210 | - */ | ||
211 | - DISPATCH_ALWAYS; | ||
212 | - | ||
213 | - /** | ||
214 | - * True if this policy allows outbound traffic on the specified channel | ||
215 | - * | ||
216 | - * @param channel | ||
217 | - */ | ||
218 | - public boolean allows(PluginChannels<?> channels, String channel) | ||
219 | - { | ||
220 | - if (this == ChannelPolicy.DISPATCH_ALWAYS) return true; | ||
221 | - return channels.isRemoteChannelRegistered(channel); | ||
222 | - } | ||
223 | - | ||
224 | - /** | ||
225 | - * True if this policy does not throw an exception for unregistered outbound channels | ||
226 | - */ | ||
227 | - public boolean isSilent() | ||
228 | - { | ||
229 | - return (this != ChannelPolicy.DISPATCH_IF_REGISTERED); | ||
230 | - } | ||
231 | - } | ||
232 | -} | 1 | +package com.mumfrey.liteloader.core; |
2 | + | ||
3 | +import io.netty.buffer.Unpooled; | ||
4 | + | ||
5 | +import java.util.Collections; | ||
6 | +import java.util.HashMap; | ||
7 | +import java.util.HashSet; | ||
8 | +import java.util.LinkedList; | ||
9 | +import java.util.List; | ||
10 | +import java.util.Map; | ||
11 | +import java.util.Set; | ||
12 | + | ||
13 | +import net.minecraft.network.INetHandler; | ||
14 | +import net.minecraft.network.PacketBuffer; | ||
15 | + | ||
16 | +import com.google.common.base.Charsets; | ||
17 | +import com.mumfrey.liteloader.api.InterfaceProvider; | ||
18 | +import com.mumfrey.liteloader.interfaces.FastIterableDeque; | ||
19 | +import com.mumfrey.liteloader.util.log.LiteLoaderLogger; | ||
20 | + | ||
21 | +/** | ||
22 | + * Manages plugin channel connections and subscriptions for LiteLoader | ||
23 | + * | ||
24 | + * @author Adam Mummery-Smith | ||
25 | + */ | ||
26 | +public abstract class PluginChannels<L extends CommonPluginChannelListener> implements InterfaceProvider | ||
27 | +{ | ||
28 | + // reserved channel consts | ||
29 | + protected static final String CHANNEL_REGISTER = "REGISTER"; | ||
30 | + protected static final String CHANNEL_UNREGISTER = "UNREGISTER"; | ||
31 | + | ||
32 | + /** | ||
33 | + * Number of faults for a specific listener before a warning is generated | ||
34 | + */ | ||
35 | + protected static final int WARN_FAULT_THRESHOLD = 1000; | ||
36 | + | ||
37 | + /** | ||
38 | + * Mapping of plugin channel names to listeners | ||
39 | + */ | ||
40 | + protected final HashMap<String, LinkedList<L>> pluginChannels = new HashMap<String, LinkedList<L>>(); | ||
41 | + | ||
42 | + /** | ||
43 | + * List of mods which implement PluginChannelListener interface | ||
44 | + */ | ||
45 | + protected final FastIterableDeque<L> pluginChannelListeners; | ||
46 | + | ||
47 | + /** | ||
48 | + * Plugin channels that we know the server supports | ||
49 | + */ | ||
50 | + protected final Set<String> remotePluginChannels = new HashSet<String>(); | ||
51 | + | ||
52 | + /** | ||
53 | + * Keep track of faulting listeners so that we can periodically log a message if a listener is throwing LOTS of exceptions | ||
54 | + */ | ||
55 | + protected final Map<L, Integer> faultingPluginChannelListeners = new HashMap<L, Integer>(); | ||
56 | + | ||
57 | + /** | ||
58 | + * Package private | ||
59 | + */ | ||
60 | + PluginChannels() | ||
61 | + { | ||
62 | + this.pluginChannelListeners = this.createHandlerList(); | ||
63 | + } | ||
64 | + | ||
65 | + /** | ||
66 | + * Spawn the handler list instance for this channel manager | ||
67 | + */ | ||
68 | + protected abstract FastIterableDeque<L> createHandlerList(); | ||
69 | + | ||
70 | + /** | ||
71 | + * Get the current set of registered client-side channels | ||
72 | + */ | ||
73 | + public Set<String> getLocalChannels() | ||
74 | + { | ||
75 | + return Collections.unmodifiableSet(this.pluginChannels.keySet()); | ||
76 | + } | ||
77 | + | ||
78 | + /** | ||
79 | + * Get the current set of registered server channels | ||
80 | + */ | ||
81 | + public Set<String> getRemoteChannels() | ||
82 | + { | ||
83 | + return Collections.unmodifiableSet(this.remotePluginChannels); | ||
84 | + } | ||
85 | + | ||
86 | + /** | ||
87 | + * Check whether a server plugin channel is registered | ||
88 | + * | ||
89 | + * @param channel | ||
90 | + * @return true if the channel is registered at the server side | ||
91 | + */ | ||
92 | + public boolean isRemoteChannelRegistered(String channel) | ||
93 | + { | ||
94 | + return this.remotePluginChannels.contains(channel); | ||
95 | + } | ||
96 | + | ||
97 | + /** | ||
98 | + * @param pluginChannelListener | ||
99 | + */ | ||
100 | + protected void addPluginChannelListener(L pluginChannelListener) | ||
101 | + { | ||
102 | + this.pluginChannelListeners.add(pluginChannelListener); | ||
103 | + } | ||
104 | + | ||
105 | + /** | ||
106 | + * Connecting to a new server, clear plugin channels | ||
107 | + * | ||
108 | + * @param netHandler | ||
109 | + */ | ||
110 | + protected void clearPluginChannels(INetHandler netHandler) | ||
111 | + { | ||
112 | + this.pluginChannels.clear(); | ||
113 | + this.remotePluginChannels.clear(); | ||
114 | + this.faultingPluginChannelListeners.clear(); | ||
115 | + } | ||
116 | + | ||
117 | + /** | ||
118 | + * @param data | ||
119 | + */ | ||
120 | + protected void onRegisterPacketReceived(PacketBuffer data) | ||
121 | + { | ||
122 | + try | ||
123 | + { | ||
124 | + byte[] bytes = new byte[data.readableBytes()]; | ||
125 | + data.readBytes(bytes); | ||
126 | + String channels = new String(bytes, Charsets.UTF_8); | ||
127 | + for (String channel : channels.split("\u0000")) | ||
128 | + { | ||
129 | + this.remotePluginChannels.add(channel); | ||
130 | + } | ||
131 | + } | ||
132 | + catch (Exception ex) | ||
133 | + { | ||
134 | + LiteLoaderLogger.warning(ex, "Error decoding REGISTER packet from remote host %s", ex.getClass().getSimpleName()); | ||
135 | + } | ||
136 | + } | ||
137 | + | ||
138 | + /** | ||
139 | + * | ||
140 | + */ | ||
141 | + protected PacketBuffer getRegistrationData() | ||
142 | + { | ||
143 | + // If any mods have registered channels, send the REGISTER packet | ||
144 | + if (this.pluginChannels.keySet().size() > 0) | ||
145 | + { | ||
146 | + StringBuilder channelList = new StringBuilder(); | ||
147 | + boolean separator = false; | ||
148 | + | ||
149 | + for (String channel : this.pluginChannels.keySet()) | ||
150 | + { | ||
151 | + if (separator) channelList.append("\u0000"); | ||
152 | + channelList.append(channel); | ||
153 | + separator = true; | ||
154 | + } | ||
155 | + | ||
156 | + PacketBuffer buffer = new PacketBuffer(Unpooled.buffer()); | ||
157 | + buffer.writeBytes(channelList.toString().getBytes(Charsets.UTF_8)); | ||
158 | + return buffer; | ||
159 | + } | ||
160 | + | ||
161 | + return null; | ||
162 | + } | ||
163 | + | ||
164 | + /** | ||
165 | + * Adds plugin channels for the specified listener to the local channels | ||
166 | + * collection | ||
167 | + * | ||
168 | + * @param pluginChannelListener | ||
169 | + */ | ||
170 | + protected void addPluginChannelsFor(L pluginChannelListener) | ||
171 | + { | ||
172 | + List<String> channels = pluginChannelListener.getChannels(); | ||
173 | + | ||
174 | + if (channels != null) | ||
175 | + { | ||
176 | + for (String channel : channels) | ||
177 | + { | ||
178 | + if (channel.length() > 16 || channel.toUpperCase().equals(CHANNEL_REGISTER) || channel.toUpperCase().equals(CHANNEL_UNREGISTER)) | ||
179 | + continue; | ||
180 | + | ||
181 | + if (!this.pluginChannels.containsKey(channel)) | ||
182 | + { | ||
183 | + this.pluginChannels.put(channel, new LinkedList<L>()); | ||
184 | + } | ||
185 | + | ||
186 | + this.pluginChannels.get(channel).add(pluginChannelListener); | ||
187 | + } | ||
188 | + } | ||
189 | + } | ||
190 | + | ||
191 | + /** | ||
192 | + * Policy for dispatching plugin channel packets | ||
193 | + * | ||
194 | + * @author Adam Mummery-Smith | ||
195 | + */ | ||
196 | + public enum ChannelPolicy | ||
197 | + { | ||
198 | + /** | ||
199 | + * Dispatch the message, throw an exception if the channel is not registered | ||
200 | + */ | ||
201 | + DISPATCH, | ||
202 | + | ||
203 | + /** | ||
204 | + * Dispatch the message, return false if the channel is not registered | ||
205 | + */ | ||
206 | + DISPATCH_IF_REGISTERED, | ||
207 | + | ||
208 | + /** | ||
209 | + * Dispatch the message | ||
210 | + */ | ||
211 | + DISPATCH_ALWAYS; | ||
212 | + | ||
213 | + /** | ||
214 | + * True if this policy allows outbound traffic on the specified channel | ||
215 | + * | ||
216 | + * @param channel | ||
217 | + */ | ||
218 | + public boolean allows(PluginChannels<?> channels, String channel) | ||
219 | + { | ||
220 | + if (this == ChannelPolicy.DISPATCH_ALWAYS) return true; | ||
221 | + return channels.isRemoteChannelRegistered(channel); | ||
222 | + } | ||
223 | + | ||
224 | + /** | ||
225 | + * True if this policy does not throw an exception for unregistered outbound channels | ||
226 | + */ | ||
227 | + public boolean isSilent() | ||
228 | + { | ||
229 | + return (this != ChannelPolicy.DISPATCH); | ||
230 | + } | ||
231 | + } | ||
232 | +} |