diff --git a/src/org/apollo/net/codec/update/OnDemandRequest.java b/src/org/apollo/net/codec/update/OnDemandRequest.java index 86baf132..d91737a8 100644 --- a/src/org/apollo/net/codec/update/OnDemandRequest.java +++ b/src/org/apollo/net/codec/update/OnDemandRequest.java @@ -11,29 +11,27 @@ public final class OnDemandRequest implements Comparable { /** * An enumeration containing the different request priorities. - * - * @author Graham */ public enum Priority { /** - * High priority - used in-game when data is required immediately but has not yet been received. + * High priority - used when a player is in-game and data is required immediately. */ HIGH(0), /** - * Medium priority - used while loading the 'bare minimum' required to run the game. + * Medium priority - used while loading extra resources when the client is not logged in. */ MEDIUM(1), /** - * Low priority - used when a file is not required urgently. The client login screen says - * "loading extra files.." when low priority loading is being performed. + * Low priority - used when a file is not required urgently (such as when serving the rest of the cache whilst + * the player is in-game). */ LOW(2); /** - * Converts the integer value to a priority. + * Converts the integer value to a Priority. * * @param value The integer value. * @return The priority. @@ -58,7 +56,7 @@ public final class OnDemandRequest implements Comparable { private final int value; /** - * Creates a priority. + * Creates the Priority. * * @param value The integer value. */ @@ -66,6 +64,18 @@ public final class OnDemandRequest implements Comparable { this.value = value; } + /** + * Compares this Priority with the specified other Priority. + *

+ * Used as an ordinal-independent variant of {@link #compareTo}. + * + * @param other The other Priority. + * @return 1 if this Priority is greater than {@code other}, 0 if they are equal, otherwise -1. + */ + public int compareWith(Priority other) { + return Integer.compare(value, other.value); + } + /** * Converts the priority to an integer. * @@ -78,52 +88,44 @@ public final class OnDemandRequest implements Comparable { } /** - * The file descriptor. + * The FileDescriptor. */ - private final FileDescriptor fileDescriptor; + private final FileDescriptor descriptor; /** - * The request priority. + * The request Priority. */ private final Priority priority; /** - * Creates the 'on-demand' request. + * Creates the OnDemandRequest. * - * @param fileDescriptor The file descriptor. - * @param priority The priority. + * @param descriptor The {@link FileDescriptor}. + * @param priority The {@link Priority}. */ - public OnDemandRequest(FileDescriptor fileDescriptor, Priority priority) { - this.fileDescriptor = fileDescriptor; + public OnDemandRequest(FileDescriptor descriptor, Priority priority) { + this.descriptor = descriptor; this.priority = priority; } @Override public int compareTo(OnDemandRequest other) { - int thisPriority = priority.toInteger(); - int otherPriority = other.priority.toInteger(); - - if (thisPriority < otherPriority) { - return 1; - } else if (thisPriority == otherPriority) { - return 0; - } - return -1; + return priority.compareWith(other.priority); } /** - * Gets the file descriptor. + * Gets the {@link FileDescriptor}. * - * @return The file descriptor. + * @return The FileDescriptor. */ public FileDescriptor getFileDescriptor() { - return fileDescriptor; + return descriptor; } /** - * Gets the priority. + * Gets the {@link Priority}. * - * @return The priority. + * @return The Priority. */ public Priority getPriority() { return priority; diff --git a/src/org/apollo/update/ChannelRequest.java b/src/org/apollo/update/ChannelRequest.java index 31f17faa..0656c445 100644 --- a/src/org/apollo/update/ChannelRequest.java +++ b/src/org/apollo/update/ChannelRequest.java @@ -8,7 +8,7 @@ import io.netty.channel.Channel; * @author Graham * @param The type of request. */ -public final class ChannelRequest implements Comparable> { +public class ChannelRequest { /** * The channel. @@ -18,7 +18,7 @@ public final class ChannelRequest implements Comparable> { /** * The request. */ - private final T request; + protected final T request; /** * Creates a new channel request. @@ -31,15 +31,6 @@ public final class ChannelRequest implements Comparable> { this.request = request; } - @SuppressWarnings("unchecked") - @Override - public int compareTo(ChannelRequest o) { - if (request instanceof Comparable && o.request instanceof Comparable) { - return ((Comparable) request).compareTo(o.request); - } - return 0; - } - /** * Gets the channel. * diff --git a/src/org/apollo/update/ComparableChannelRequest.java b/src/org/apollo/update/ComparableChannelRequest.java new file mode 100644 index 00000000..02f90f1d --- /dev/null +++ b/src/org/apollo/update/ComparableChannelRequest.java @@ -0,0 +1,30 @@ +package org.apollo.update; + +import io.netty.channel.Channel; + +/** + * A {@link ChannelRequest} with a {@link Comparable} request type. + * + * @author Major + * + * @param The type of request. + */ +public final class ComparableChannelRequest> extends ChannelRequest implements + Comparable> { + + /** + * Creates the ComparableChannelRequest. + * + * @param channel The {@link Channel} making the request. + * @param request The request. + */ + public ComparableChannelRequest(Channel channel, T request) { + super(channel, request); + } + + @Override + public int compareTo(ComparableChannelRequest o) { + return request.compareTo(o.request); + } + +} \ No newline at end of file diff --git a/src/org/apollo/update/UpdateDispatcher.java b/src/org/apollo/update/UpdateDispatcher.java index b5a27fca..d46dfa7f 100644 --- a/src/org/apollo/update/UpdateDispatcher.java +++ b/src/org/apollo/update/UpdateDispatcher.java @@ -11,7 +11,7 @@ import org.apollo.net.codec.jaggrab.JagGrabRequest; import org.apollo.net.codec.update.OnDemandRequest; /** - * A class which dispatches requests to worker threads. + * Dispatches update requests to worker threads. * * @author Graham */ @@ -22,20 +22,20 @@ public final class UpdateDispatcher { */ private static final int MAXIMUM_QUEUE_SIZE = 1024; + /** + * A queue for pending 'on-demand' requests. + */ + private final BlockingQueue> demand = new PriorityBlockingQueue<>(); + /** * A queue for pending HTTP requests. */ - private final BlockingQueue> httpQueue = new LinkedBlockingQueue<>(); + private final BlockingQueue> http = new LinkedBlockingQueue<>(); /** * A queue for pending JAGGRAB requests. */ - private final BlockingQueue> jagGrabQueue = new LinkedBlockingQueue<>(); - - /** - * A queue for pending 'on-demand' requests. - */ - private final BlockingQueue> onDemandQueue = new PriorityBlockingQueue<>(); + private final BlockingQueue> jaggrab = new LinkedBlockingQueue<>(); /** * Dispatches a HTTP request. @@ -44,10 +44,10 @@ public final class UpdateDispatcher { * @param request The request. */ public void dispatch(Channel channel, HttpRequest request) { - if (httpQueue.size() >= MAXIMUM_QUEUE_SIZE) { + if (http.size() >= MAXIMUM_QUEUE_SIZE) { channel.close(); } - httpQueue.add(new ChannelRequest<>(channel, request)); + http.add(new ChannelRequest<>(channel, request)); } /** @@ -57,10 +57,10 @@ public final class UpdateDispatcher { * @param request The request. */ public void dispatch(Channel channel, JagGrabRequest request) { - if (jagGrabQueue.size() >= MAXIMUM_QUEUE_SIZE) { + if (jaggrab.size() >= MAXIMUM_QUEUE_SIZE) { channel.close(); } - jagGrabQueue.add(new ChannelRequest<>(channel, request)); + jaggrab.add(new ChannelRequest<>(channel, request)); } /** @@ -70,10 +70,10 @@ public final class UpdateDispatcher { * @param request The request. */ public void dispatch(Channel channel, OnDemandRequest request) { - if (onDemandQueue.size() >= MAXIMUM_QUEUE_SIZE) { + if (demand.size() >= MAXIMUM_QUEUE_SIZE) { channel.close(); } - onDemandQueue.add(new ChannelRequest<>(channel, request)); + demand.add(new ComparableChannelRequest<>(channel, request)); } /** @@ -83,7 +83,7 @@ public final class UpdateDispatcher { * @throws InterruptedException If the thread is interrupted. */ ChannelRequest nextHttpRequest() throws InterruptedException { - return httpQueue.take(); + return http.take(); } /** @@ -93,7 +93,7 @@ public final class UpdateDispatcher { * @throws InterruptedException If the thread is interrupted. */ ChannelRequest nextJagGrabRequest() throws InterruptedException { - return jagGrabQueue.take(); + return jaggrab.take(); } /** @@ -103,7 +103,7 @@ public final class UpdateDispatcher { * @throws InterruptedException If the thread is interrupted. */ ChannelRequest nextOnDemandRequest() throws InterruptedException { - return onDemandQueue.take(); + return demand.take(); } } \ No newline at end of file