2020-02-02 13:44:10 +01:00
|
|
|
package envoy.client.net;
|
2019-12-30 17:18:03 +01:00
|
|
|
|
2020-07-25 15:09:00 +02:00
|
|
|
import java.io.*;
|
2020-01-02 16:11:41 +01:00
|
|
|
import java.net.SocketException;
|
2019-12-30 17:18:03 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2019-12-31 10:57:11 +01:00
|
|
|
import java.util.function.Consumer;
|
2019-12-30 17:18:03 +01:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
2020-03-05 14:27:40 +01:00
|
|
|
import envoy.util.EnvoyLog;
|
2020-01-06 17:37:38 +01:00
|
|
|
import envoy.util.SerializationUtils;
|
2019-12-30 17:18:03 +01:00
|
|
|
|
|
|
|
/**
|
2020-06-09 15:41:01 +02:00
|
|
|
* Receives objects from the server and passes them to processor objects based
|
|
|
|
* on their class.
|
|
|
|
* <p>
|
2019-12-30 17:18:03 +01:00
|
|
|
* Project: <strong>envoy-client</strong><br>
|
|
|
|
* File: <strong>Receiver.java</strong><br>
|
|
|
|
* Created: <strong>30.12.2019</strong><br>
|
2020-01-06 17:37:38 +01:00
|
|
|
*
|
2019-12-30 17:18:03 +01:00
|
|
|
* @author Kai S. K. Engelbart
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2019-12-30 17:18:03 +01:00
|
|
|
*/
|
2020-03-07 19:38:06 +01:00
|
|
|
public class Receiver extends Thread {
|
2019-12-30 17:18:03 +01:00
|
|
|
|
2020-01-02 16:11:41 +01:00
|
|
|
private final InputStream in;
|
|
|
|
private final Map<Class<?>, Consumer<?>> processors = new HashMap<>();
|
2019-12-30 17:18:03 +01:00
|
|
|
|
2020-02-18 07:16:35 +01:00
|
|
|
private static final Logger logger = EnvoyLog.getLogger(Receiver.class);
|
2019-12-30 17:18:03 +01:00
|
|
|
|
2019-12-31 10:57:11 +01:00
|
|
|
/**
|
|
|
|
* Creates an instance of {@link Receiver}.
|
|
|
|
*
|
|
|
|
* @param in the {@link InputStream} to parse objects from
|
2020-06-09 15:41:01 +02:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2019-12-31 10:57:11 +01:00
|
|
|
*/
|
2020-03-07 19:38:06 +01:00
|
|
|
public Receiver(InputStream in) {
|
|
|
|
super("Receiver");
|
|
|
|
this.in = in;
|
|
|
|
}
|
2019-12-30 17:18:03 +01:00
|
|
|
|
2020-06-09 15:41:01 +02:00
|
|
|
/**
|
|
|
|
* Starts the receiver loop. When an object is read, it is passed to the
|
|
|
|
* appropriate processor.
|
2020-06-13 22:36:52 +02:00
|
|
|
*
|
2020-06-09 15:41:01 +02:00
|
|
|
* @since Envoy Client v0.3-alpha
|
|
|
|
*/
|
2019-12-30 17:18:03 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2020-01-06 17:37:38 +01:00
|
|
|
|
2020-07-03 23:37:25 +02:00
|
|
|
while (true) {
|
|
|
|
try {
|
2020-01-06 17:37:38 +01:00
|
|
|
// Read object length
|
2020-06-13 22:36:52 +02:00
|
|
|
final byte[] lenBytes = new byte[4];
|
2020-01-06 17:37:38 +01:00
|
|
|
in.read(lenBytes);
|
2020-06-13 22:36:52 +02:00
|
|
|
final int len = SerializationUtils.bytesToInt(lenBytes, 0);
|
2020-07-03 23:37:25 +02:00
|
|
|
logger.log(Level.FINEST, "Expecting object of length " + len + ".");
|
2020-01-06 17:37:38 +01:00
|
|
|
|
|
|
|
// Read object into byte array
|
2020-07-03 23:37:25 +02:00
|
|
|
final byte[] objBytes = new byte[len];
|
|
|
|
final int bytesRead = in.read(objBytes);
|
|
|
|
logger.log(Level.FINEST, "Read " + bytesRead + " bytes.");
|
|
|
|
|
|
|
|
// Catch LV encoding errors
|
|
|
|
if (len != bytesRead) {
|
|
|
|
logger.log(Level.WARNING,
|
|
|
|
String.format("LV encoding violated: expected %d bytes, received %d bytes. Discarding object...", len, bytesRead));
|
|
|
|
continue;
|
|
|
|
}
|
2019-12-30 17:18:03 +01:00
|
|
|
|
2020-01-06 17:37:38 +01:00
|
|
|
try (ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(objBytes))) {
|
2020-06-13 22:36:52 +02:00
|
|
|
final Object obj = oin.readObject();
|
|
|
|
logger.log(Level.FINE, "Received " + obj);
|
2020-01-06 17:37:38 +01:00
|
|
|
|
|
|
|
// Get appropriate processor
|
|
|
|
@SuppressWarnings("rawtypes")
|
2020-06-13 22:36:52 +02:00
|
|
|
final Consumer processor = processors.get(obj.getClass());
|
2020-06-14 16:03:02 +02:00
|
|
|
if (processor == null)
|
2020-07-09 09:37:31 +02:00
|
|
|
logger.log(Level.WARNING, String.format("The received object has the %s for which no processor is defined.", obj.getClass()));
|
2020-01-06 17:37:38 +01:00
|
|
|
else processor.accept(obj);
|
|
|
|
}
|
2020-07-25 15:09:00 +02:00
|
|
|
} catch (final SocketException | EOFException e) {
|
2020-07-03 23:37:25 +02:00
|
|
|
// Connection probably closed by client.
|
2020-07-25 15:09:00 +02:00
|
|
|
logger.log(Level.FINER, "Exiting receiver...");
|
2020-07-03 23:37:25 +02:00
|
|
|
return;
|
|
|
|
} catch (final Exception e) {
|
|
|
|
logger.log(Level.SEVERE, "Error on receiver thread", e);
|
2019-12-30 17:18:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 10:57:11 +01:00
|
|
|
/**
|
|
|
|
* Adds an object processor to this {@link Receiver}. It will be called once an
|
|
|
|
* object of the accepted class has been received.
|
2020-01-06 17:37:38 +01:00
|
|
|
*
|
2019-12-31 10:57:11 +01:00
|
|
|
* @param processorClass the object class accepted by the processor
|
|
|
|
* @param processor the object processor
|
2020-06-09 15:41:01 +02:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2019-12-31 10:57:11 +01:00
|
|
|
*/
|
|
|
|
public <T> void registerProcessor(Class<T> processorClass, Consumer<T> processor) { processors.put(processorClass, processor); }
|
2020-01-02 16:11:41 +01:00
|
|
|
|
2020-07-09 09:37:31 +02:00
|
|
|
/**
|
|
|
|
* Adds a map of object processors to this {@link Receiver}.
|
|
|
|
*
|
|
|
|
* @param processors the processors to add the processors to add
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
public void registerProcessors(Map<Class<?>, ? extends Consumer<?>> processors) { this.processors.putAll(processors); }
|
|
|
|
|
2020-01-02 16:11:41 +01:00
|
|
|
/**
|
|
|
|
* Removes all object processors registered at this {@link Receiver}.
|
2020-06-13 22:36:52 +02:00
|
|
|
*
|
2020-06-09 15:41:01 +02:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-01-02 16:11:41 +01:00
|
|
|
*/
|
|
|
|
public void removeAllProcessors() { processors.clear(); }
|
2020-03-14 19:59:37 +01:00
|
|
|
}
|