remove the useless stuff

This commit is contained in:
Leijurv
2018-11-18 11:35:54 -08:00
parent dfb49179c5
commit 9ad35dbf28
13 changed files with 54 additions and 149 deletions
+8 -8
View File
@@ -32,16 +32,16 @@ import java.util.concurrent.LinkedBlockingQueue;
* *
* @author leijurv * @author leijurv
*/ */
public class BufferedConnection<T extends iMessage> implements IConnection<T> { public class BufferedConnection implements IConnection {
private final IConnection wrapped; private final IConnection wrapped;
private final LinkedBlockingQueue<T> queue; private final LinkedBlockingQueue<iMessage> queue;
private volatile transient IOException thrownOnRead; private volatile transient IOException thrownOnRead;
public BufferedConnection(IConnection<T> wrapped) { public BufferedConnection(IConnection wrapped) {
this(wrapped, Integer.MAX_VALUE); // LinkedBlockingQueue accepts this as "no limit" this(wrapped, Integer.MAX_VALUE); // LinkedBlockingQueue accepts this as "no limit"
} }
public BufferedConnection(IConnection<T> wrapped, int maxInternalQueueSize) { public BufferedConnection(IConnection wrapped, int maxInternalQueueSize) {
this.wrapped = wrapped; this.wrapped = wrapped;
this.queue = new LinkedBlockingQueue<>(); this.queue = new LinkedBlockingQueue<>();
this.thrownOnRead = null; this.thrownOnRead = null;
@@ -59,12 +59,12 @@ public class BufferedConnection<T extends iMessage> implements IConnection<T> {
} }
@Override @Override
public void sendMessage(T message) throws IOException { public void sendMessage(iMessage message) throws IOException {
wrapped.sendMessage(message); wrapped.sendMessage(message);
} }
@Override @Override
public T receiveMessage() { public iMessage receiveMessage() {
throw new UnsupportedOperationException("BufferedConnection can only be read from non-blockingly"); throw new UnsupportedOperationException("BufferedConnection can only be read from non-blockingly");
} }
@@ -74,8 +74,8 @@ public class BufferedConnection<T extends iMessage> implements IConnection<T> {
thrownOnRead = new EOFException("Closed"); thrownOnRead = new EOFException("Closed");
} }
public List<T> receiveMessagesNonBlocking() throws IOException { public List<iMessage> receiveMessagesNonBlocking() throws IOException {
ArrayList<T> msgs = new ArrayList<>(); ArrayList<iMessage> msgs = new ArrayList<>();
queue.drainTo(msgs); // preserves order -- first message received will be first in this arraylist queue.drainTo(msgs); // preserves order -- first message received will be first in this arraylist
if (msgs.isEmpty() && thrownOnRead != null) { if (msgs.isEmpty() && thrownOnRead != null) {
IOException up = new IOException("BufferedConnection wrapped", thrownOnRead); IOException up = new IOException("BufferedConnection wrapped", thrownOnRead);
@@ -26,20 +26,20 @@ import java.util.List;
public enum ConstructingDeserializer implements MessageDeserializer { public enum ConstructingDeserializer implements MessageDeserializer {
INSTANCE; INSTANCE;
private final List<Class<? extends SerializableMessage>> MSGS; private final List<Class<? extends iMessage>> MSGS;
ConstructingDeserializer() { ConstructingDeserializer() {
MSGS = new ArrayList<>(); MSGS = new ArrayList<>();
// imagine doing something in reflect but it's actually concise and you don't need to catch 42069 different exceptions. huh. // imagine doing something in reflect but it's actually concise and you don't need to catch 42069 different exceptions. huh.
for (Method m : IMessageListener.class.getDeclaredMethods()) { for (Method m : IMessageListener.class.getDeclaredMethods()) {
if (m.getName().equals("handle")) { if (m.getName().equals("handle")) {
MSGS.add((Class<? extends SerializableMessage>) m.getParameterTypes()[0]); MSGS.add((Class<? extends iMessage>) m.getParameterTypes()[0]);
} }
} }
} }
@Override @Override
public SerializableMessage deserialize(DataInputStream in) throws IOException { public iMessage deserialize(DataInputStream in) throws IOException {
int type = ((int) in.readByte()) & 0xff; int type = ((int) in.readByte()) & 0xff;
try { try {
return MSGS.get(type).getConstructor(DataInputStream.class).newInstance(in); return MSGS.get(type).getConstructor(DataInputStream.class).newInstance(in);
@@ -48,7 +48,7 @@ public enum ConstructingDeserializer implements MessageDeserializer {
} }
} }
public byte getHeader(Class<? extends SerializableMessage> klass) { public byte getHeader(Class<? extends iMessage> klass) {
return (byte) MSGS.indexOf(klass); return (byte) MSGS.indexOf(klass);
} }
} }
@@ -1,55 +0,0 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package comms;
import java.io.IOException;
/**
* This is just a meme idk if this is actually useful
*
* @param <T>
*/
public class FilteredConnection<T extends iMessage> implements IConnection<T> {
private final Class<T> klass;
private final IConnection<? super iMessage> wrapped;
public FilteredConnection(IConnection<? super iMessage> wrapped, Class<T> klass) {
this.klass = klass;
this.wrapped = wrapped;
}
@Override
public void sendMessage(T message) throws IOException {
wrapped.sendMessage(message);
}
@Override
public T receiveMessage() throws IOException {
while (true) {
iMessage msg = wrapped.receiveMessage();
if (klass.isInstance(msg)) {
return klass.cast(msg);
}
}
}
@Override
public void close() {
wrapped.close();
}
}
@@ -1,22 +0,0 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package comms;
public interface HandlableMessage {
void handle(IMessageListener listener);
}
+3 -3
View File
@@ -19,10 +19,10 @@ package comms;
import java.io.IOException; import java.io.IOException;
public interface IConnection<T extends iMessage> { public interface IConnection {
void sendMessage(T message) throws IOException; void sendMessage(iMessage message) throws IOException;
T receiveMessage() throws IOException; iMessage receiveMessage() throws IOException;
void close(); void close();
} }
@@ -21,5 +21,5 @@ import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
public interface MessageDeserializer { public interface MessageDeserializer {
SerializableMessage deserialize(DataInputStream in) throws IOException; iMessage deserialize(DataInputStream in) throws IOException;
} }
+9 -9
View File
@@ -26,8 +26,8 @@ import java.util.concurrent.LinkedBlockingQueue;
* Do you want a socket to localhost without actually making a gross real socket to localhost? * Do you want a socket to localhost without actually making a gross real socket to localhost?
*/ */
public class Pipe<T extends iMessage> { public class Pipe<T extends iMessage> {
private final LinkedBlockingQueue<Optional<T>> AtoB; private final LinkedBlockingQueue<Optional<iMessage>> AtoB;
private final LinkedBlockingQueue<Optional<T>> BtoA; private final LinkedBlockingQueue<Optional<iMessage>> BtoA;
private final PipedConnection A; private final PipedConnection A;
private final PipedConnection B; private final PipedConnection B;
private volatile boolean closed; private volatile boolean closed;
@@ -47,17 +47,17 @@ public class Pipe<T extends iMessage> {
return B; return B;
} }
public class PipedConnection implements IConnection<T> { public class PipedConnection implements IConnection {
private final LinkedBlockingQueue<Optional<T>> in; private final LinkedBlockingQueue<Optional<iMessage>> in;
private final LinkedBlockingQueue<Optional<T>> out; private final LinkedBlockingQueue<Optional<iMessage>> out;
private PipedConnection(LinkedBlockingQueue<Optional<T>> in, LinkedBlockingQueue<Optional<T>> out) { private PipedConnection(LinkedBlockingQueue<Optional<iMessage>> in, LinkedBlockingQueue<Optional<iMessage>> out) {
this.in = in; this.in = in;
this.out = out; this.out = out;
} }
@Override @Override
public void sendMessage(T message) throws IOException { public void sendMessage(iMessage message) throws IOException {
if (closed) { if (closed) {
throw new EOFException("Closed"); throw new EOFException("Closed");
} }
@@ -69,12 +69,12 @@ public class Pipe<T extends iMessage> {
} }
@Override @Override
public T receiveMessage() throws IOException { public iMessage receiveMessage() throws IOException {
if (closed) { if (closed) {
throw new EOFException("Closed"); throw new EOFException("Closed");
} }
try { try {
Optional<T> t = in.take(); Optional<iMessage> t = in.take();
if (!t.isPresent()) { if (!t.isPresent()) {
throw new EOFException("Closed"); throw new EOFException("Closed");
} }
@@ -1,33 +0,0 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package comms;
import java.io.DataOutputStream;
import java.io.IOException;
public interface SerializableMessage extends iMessage {
void write(DataOutputStream out) throws IOException;
default void writeHeader(DataOutputStream out) throws IOException {
out.writeByte(getHeader());
}
default byte getHeader() {
return ConstructingDeserializer.INSTANCE.getHeader(getClass());
}
}
@@ -19,7 +19,7 @@ package comms;
import java.io.*; import java.io.*;
public class SerializedConnection implements IConnection<SerializableMessage> { public class SerializedConnection implements IConnection {
private final DataInputStream in; private final DataInputStream in;
private final DataOutputStream out; private final DataOutputStream out;
private final MessageDeserializer deserializer; private final MessageDeserializer deserializer;
@@ -35,13 +35,13 @@ public class SerializedConnection implements IConnection<SerializableMessage> {
} }
@Override @Override
public void sendMessage(SerializableMessage message) throws IOException { public void sendMessage(iMessage message) throws IOException {
message.writeHeader(out); message.writeHeader(out);
message.write(out); message.write(out);
} }
@Override @Override
public SerializableMessage receiveMessage() throws IOException { public iMessage receiveMessage() throws IOException {
return deserializer.deserialize(in); return deserializer.deserialize(in);
} }
@@ -17,15 +17,14 @@
package comms.downward; package comms.downward;
import comms.HandlableMessage;
import comms.IMessageListener; import comms.IMessageListener;
import comms.SerializableMessage; import comms.iMessage;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
public class MessageChat implements SerializableMessage, HandlableMessage { public class MessageChat implements iMessage {
public final String msg; public final String msg;
+14
View File
@@ -17,6 +17,9 @@
package comms; package comms;
import java.io.DataOutputStream;
import java.io.IOException;
/** /**
* hell yeah * hell yeah
* <p> * <p>
@@ -27,4 +30,15 @@ package comms;
* @author leijurv * @author leijurv
*/ */
public interface iMessage { public interface iMessage {
void write(DataOutputStream out) throws IOException;
default void writeHeader(DataOutputStream out) throws IOException {
out.writeByte(getHeader());
}
default byte getHeader() {
return ConstructingDeserializer.INSTANCE.getHeader(getClass());
}
void handle(IMessageListener listener);
} }
@@ -17,15 +17,14 @@
package comms.upward; package comms.upward;
import comms.HandlableMessage;
import comms.IMessageListener; import comms.IMessageListener;
import comms.SerializableMessage; import comms.iMessage;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
public class MessageStatus implements SerializableMessage, HandlableMessage { public class MessageStatus implements iMessage {
public final double x; public final double x;
public final double y; public final double y;
@@ -21,8 +21,11 @@ import baritone.Baritone;
import baritone.api.event.events.ChatEvent; import baritone.api.event.events.ChatEvent;
import baritone.api.event.events.TickEvent; import baritone.api.event.events.TickEvent;
import baritone.utils.Helper; import baritone.utils.Helper;
import comms.*; import comms.BufferedConnection;
import comms.IConnection;
import comms.IMessageListener;
import comms.downward.MessageChat; import comms.downward.MessageChat;
import comms.iMessage;
import comms.upward.MessageStatus; import comms.upward.MessageStatus;
import java.io.IOException; import java.io.IOException;
@@ -33,7 +36,7 @@ public class ControllerBehavior extends Behavior implements IMessageListener {
super(baritone); super(baritone);
} }
private BufferedConnection<SerializableMessage> conn; private BufferedConnection conn;
@Override @Override
public void onTick(TickEvent event) { public void onTick(TickEvent event) {
@@ -48,15 +51,15 @@ public class ControllerBehavior extends Behavior implements IMessageListener {
return; return;
} }
try { try {
List<SerializableMessage> msgs = conn.receiveMessagesNonBlocking(); List<iMessage> msgs = conn.receiveMessagesNonBlocking();
msgs.forEach(msg -> ((HandlableMessage) msg).handle(this)); msgs.forEach(msg -> msg.handle(this));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
disconnect(); disconnect();
} }
} }
public boolean trySend(SerializableMessage msg) { public boolean trySend(iMessage msg) {
if (conn == null) { if (conn == null) {
return false; return false;
} }