mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-06 16:51:37 +00:00
Fix poor usage of compareTo.
This commit is contained in:
@@ -11,29 +11,27 @@ public final class OnDemandRequest implements Comparable<OnDemandRequest> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* An enumeration containing the different request priorities.
|
* An enumeration containing the different request priorities.
|
||||||
*
|
|
||||||
* @author Graham
|
|
||||||
*/
|
*/
|
||||||
public enum Priority {
|
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),
|
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),
|
MEDIUM(1),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Low priority - used when a file is not required urgently. The client login screen says
|
* Low priority - used when a file is not required urgently (such as when serving the rest of the cache whilst
|
||||||
* "loading extra files.." when low priority loading is being performed.
|
* the player is in-game).
|
||||||
*/
|
*/
|
||||||
LOW(2);
|
LOW(2);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts the integer value to a priority.
|
* Converts the integer value to a Priority.
|
||||||
*
|
*
|
||||||
* @param value The integer value.
|
* @param value The integer value.
|
||||||
* @return The priority.
|
* @return The priority.
|
||||||
@@ -58,7 +56,7 @@ public final class OnDemandRequest implements Comparable<OnDemandRequest> {
|
|||||||
private final int value;
|
private final int value;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a priority.
|
* Creates the Priority.
|
||||||
*
|
*
|
||||||
* @param value The integer value.
|
* @param value The integer value.
|
||||||
*/
|
*/
|
||||||
@@ -66,6 +64,18 @@ public final class OnDemandRequest implements Comparable<OnDemandRequest> {
|
|||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compares this Priority with the specified other Priority.
|
||||||
|
* <p>
|
||||||
|
* 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.
|
* Converts the priority to an integer.
|
||||||
*
|
*
|
||||||
@@ -78,52 +88,44 @@ public final class OnDemandRequest implements Comparable<OnDemandRequest> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The file descriptor.
|
* The FileDescriptor.
|
||||||
*/
|
*/
|
||||||
private final FileDescriptor fileDescriptor;
|
private final FileDescriptor descriptor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The request priority.
|
* The request Priority.
|
||||||
*/
|
*/
|
||||||
private final Priority priority;
|
private final Priority priority;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the 'on-demand' request.
|
* Creates the OnDemandRequest.
|
||||||
*
|
*
|
||||||
* @param fileDescriptor The file descriptor.
|
* @param descriptor The {@link FileDescriptor}.
|
||||||
* @param priority The priority.
|
* @param priority The {@link Priority}.
|
||||||
*/
|
*/
|
||||||
public OnDemandRequest(FileDescriptor fileDescriptor, Priority priority) {
|
public OnDemandRequest(FileDescriptor descriptor, Priority priority) {
|
||||||
this.fileDescriptor = fileDescriptor;
|
this.descriptor = descriptor;
|
||||||
this.priority = priority;
|
this.priority = priority;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(OnDemandRequest other) {
|
public int compareTo(OnDemandRequest other) {
|
||||||
int thisPriority = priority.toInteger();
|
return priority.compareWith(other.priority);
|
||||||
int otherPriority = other.priority.toInteger();
|
|
||||||
|
|
||||||
if (thisPriority < otherPriority) {
|
|
||||||
return 1;
|
|
||||||
} else if (thisPriority == otherPriority) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the file descriptor.
|
* Gets the {@link FileDescriptor}.
|
||||||
*
|
*
|
||||||
* @return The file descriptor.
|
* @return The FileDescriptor.
|
||||||
*/
|
*/
|
||||||
public FileDescriptor getFileDescriptor() {
|
public FileDescriptor getFileDescriptor() {
|
||||||
return fileDescriptor;
|
return descriptor;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the priority.
|
* Gets the {@link Priority}.
|
||||||
*
|
*
|
||||||
* @return The priority.
|
* @return The Priority.
|
||||||
*/
|
*/
|
||||||
public Priority getPriority() {
|
public Priority getPriority() {
|
||||||
return priority;
|
return priority;
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import io.netty.channel.Channel;
|
|||||||
* @author Graham
|
* @author Graham
|
||||||
* @param <T> The type of request.
|
* @param <T> The type of request.
|
||||||
*/
|
*/
|
||||||
public final class ChannelRequest<T> implements Comparable<ChannelRequest<T>> {
|
public class ChannelRequest<T> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The channel.
|
* The channel.
|
||||||
@@ -18,7 +18,7 @@ public final class ChannelRequest<T> implements Comparable<ChannelRequest<T>> {
|
|||||||
/**
|
/**
|
||||||
* The request.
|
* The request.
|
||||||
*/
|
*/
|
||||||
private final T request;
|
protected final T request;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new channel request.
|
* Creates a new channel request.
|
||||||
@@ -31,15 +31,6 @@ public final class ChannelRequest<T> implements Comparable<ChannelRequest<T>> {
|
|||||||
this.request = request;
|
this.request = request;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
@Override
|
|
||||||
public int compareTo(ChannelRequest<T> o) {
|
|
||||||
if (request instanceof Comparable<?> && o.request instanceof Comparable<?>) {
|
|
||||||
return ((Comparable<T>) request).compareTo(o.request);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the channel.
|
* Gets the channel.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -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 <T> The type of request.
|
||||||
|
*/
|
||||||
|
public final class ComparableChannelRequest<T extends Comparable<T>> extends ChannelRequest<T> implements
|
||||||
|
Comparable<ComparableChannelRequest<T>> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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<T> o) {
|
||||||
|
return request.compareTo(o.request);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -11,7 +11,7 @@ import org.apollo.net.codec.jaggrab.JagGrabRequest;
|
|||||||
import org.apollo.net.codec.update.OnDemandRequest;
|
import org.apollo.net.codec.update.OnDemandRequest;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class which dispatches requests to worker threads.
|
* Dispatches update requests to worker threads.
|
||||||
*
|
*
|
||||||
* @author Graham
|
* @author Graham
|
||||||
*/
|
*/
|
||||||
@@ -22,20 +22,20 @@ public final class UpdateDispatcher {
|
|||||||
*/
|
*/
|
||||||
private static final int MAXIMUM_QUEUE_SIZE = 1024;
|
private static final int MAXIMUM_QUEUE_SIZE = 1024;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A queue for pending 'on-demand' requests.
|
||||||
|
*/
|
||||||
|
private final BlockingQueue<ComparableChannelRequest<OnDemandRequest>> demand = new PriorityBlockingQueue<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A queue for pending HTTP requests.
|
* A queue for pending HTTP requests.
|
||||||
*/
|
*/
|
||||||
private final BlockingQueue<ChannelRequest<HttpRequest>> httpQueue = new LinkedBlockingQueue<>();
|
private final BlockingQueue<ChannelRequest<HttpRequest>> http = new LinkedBlockingQueue<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A queue for pending JAGGRAB requests.
|
* A queue for pending JAGGRAB requests.
|
||||||
*/
|
*/
|
||||||
private final BlockingQueue<ChannelRequest<JagGrabRequest>> jagGrabQueue = new LinkedBlockingQueue<>();
|
private final BlockingQueue<ChannelRequest<JagGrabRequest>> jaggrab = new LinkedBlockingQueue<>();
|
||||||
|
|
||||||
/**
|
|
||||||
* A queue for pending 'on-demand' requests.
|
|
||||||
*/
|
|
||||||
private final BlockingQueue<ChannelRequest<OnDemandRequest>> onDemandQueue = new PriorityBlockingQueue<>();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dispatches a HTTP request.
|
* Dispatches a HTTP request.
|
||||||
@@ -44,10 +44,10 @@ public final class UpdateDispatcher {
|
|||||||
* @param request The request.
|
* @param request The request.
|
||||||
*/
|
*/
|
||||||
public void dispatch(Channel channel, HttpRequest request) {
|
public void dispatch(Channel channel, HttpRequest request) {
|
||||||
if (httpQueue.size() >= MAXIMUM_QUEUE_SIZE) {
|
if (http.size() >= MAXIMUM_QUEUE_SIZE) {
|
||||||
channel.close();
|
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.
|
* @param request The request.
|
||||||
*/
|
*/
|
||||||
public void dispatch(Channel channel, JagGrabRequest request) {
|
public void dispatch(Channel channel, JagGrabRequest request) {
|
||||||
if (jagGrabQueue.size() >= MAXIMUM_QUEUE_SIZE) {
|
if (jaggrab.size() >= MAXIMUM_QUEUE_SIZE) {
|
||||||
channel.close();
|
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.
|
* @param request The request.
|
||||||
*/
|
*/
|
||||||
public void dispatch(Channel channel, OnDemandRequest request) {
|
public void dispatch(Channel channel, OnDemandRequest request) {
|
||||||
if (onDemandQueue.size() >= MAXIMUM_QUEUE_SIZE) {
|
if (demand.size() >= MAXIMUM_QUEUE_SIZE) {
|
||||||
channel.close();
|
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.
|
* @throws InterruptedException If the thread is interrupted.
|
||||||
*/
|
*/
|
||||||
ChannelRequest<HttpRequest> nextHttpRequest() throws InterruptedException {
|
ChannelRequest<HttpRequest> nextHttpRequest() throws InterruptedException {
|
||||||
return httpQueue.take();
|
return http.take();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -93,7 +93,7 @@ public final class UpdateDispatcher {
|
|||||||
* @throws InterruptedException If the thread is interrupted.
|
* @throws InterruptedException If the thread is interrupted.
|
||||||
*/
|
*/
|
||||||
ChannelRequest<JagGrabRequest> nextJagGrabRequest() throws InterruptedException {
|
ChannelRequest<JagGrabRequest> nextJagGrabRequest() throws InterruptedException {
|
||||||
return jagGrabQueue.take();
|
return jaggrab.take();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -103,7 +103,7 @@ public final class UpdateDispatcher {
|
|||||||
* @throws InterruptedException If the thread is interrupted.
|
* @throws InterruptedException If the thread is interrupted.
|
||||||
*/
|
*/
|
||||||
ChannelRequest<OnDemandRequest> nextOnDemandRequest() throws InterruptedException {
|
ChannelRequest<OnDemandRequest> nextOnDemandRequest() throws InterruptedException {
|
||||||
return onDemandQueue.take();
|
return demand.take();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user