Skip to content

Commit

Permalink
Remove the superfluous Serializer interface and its implemetation
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga committed Oct 25, 2020
1 parent ada4b27 commit a71033c
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 304 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class DaemonClientConnection implements Closeable {

private final static Logger LOG = LoggerFactory.getLogger(DaemonClientConnection.class);

private final DaemonConnection<Message> connection;
private final DaemonConnection connection;
private final DaemonInfo daemon;
private final StaleAddressDetector staleAddressDetector;
private final boolean newDaemon;
Expand All @@ -53,7 +53,7 @@ public class DaemonClientConnection implements Closeable {
private final AtomicBoolean running = new AtomicBoolean(true);
private final AtomicReference<Exception> exception = new AtomicReference<>();

public DaemonClientConnection(DaemonConnection<Message> connection, DaemonInfo daemon,
public DaemonClientConnection(DaemonConnection connection, DaemonInfo daemon,
StaleAddressDetector staleAddressDetector, boolean newDaemon, int maxKeepAliveMs) {
this.connection = connection;
this.daemon = daemon;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@
import org.jboss.fuse.mvnd.common.DaemonStopEvent;
import org.jboss.fuse.mvnd.common.Environment;
import org.jboss.fuse.mvnd.common.MavenDaemon;
import org.jboss.fuse.mvnd.common.Message;
import org.jboss.fuse.mvnd.common.Os;
import org.jboss.fuse.mvnd.common.Serializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -71,15 +69,12 @@ public class DaemonConnector {

private final DaemonRegistry registry;
private final ClientLayout layout;
private final Serializer<Message> serializer;
private final BuildProperties buildProperties;

public DaemonConnector(ClientLayout layout, DaemonRegistry registry, BuildProperties buildProperties,
Serializer<Message> serializer) {
public DaemonConnector(ClientLayout layout, DaemonRegistry registry, BuildProperties buildProperties) {
this.layout = layout;
this.registry = registry;
this.buildProperties = buildProperties;
this.serializer = serializer;
}

public DaemonClientConnection maybeConnect(DaemonCompatibilitySpec constraint) {
Expand Down Expand Up @@ -331,7 +326,7 @@ private DaemonClientConnection connectToDaemon(DaemonInfo daemon,
LOGGER.debug("Connecting to Daemon");
try {
int maxKeepAliveMs = layout.getKeepAliveMs() * layout.getMaxLostKeepAlive();
DaemonConnection<Message> connection = connect(daemon.getAddress());
DaemonConnection connection = connect(daemon.getAddress());
return new DaemonClientConnection(connection, daemon, staleAddressDetector, newDaemon, maxKeepAliveMs);
} catch (DaemonException.ConnectException e) {
staleAddressDetector.maybeStaleAddress(e);
Expand Down Expand Up @@ -360,7 +355,7 @@ public boolean maybeStaleAddress(Exception failure) {
}
}

public DaemonConnection<Message> connect(int port) throws DaemonException.ConnectException {
public DaemonConnection connect(int port) throws DaemonException.ConnectException {
InetSocketAddress address = new InetSocketAddress(InetAddress.getLoopbackAddress(), port);
try {
LOGGER.debug("Trying to connect to address {}.", address);
Expand All @@ -372,7 +367,7 @@ public DaemonConnection<Message> connect(int port) throws DaemonException.Connec
throw new DaemonException.ConnectException(String.format("Socket connected to itself on %s.", address));
}
LOGGER.debug("Connected to address {}.", socket.getRemoteSocketAddress());
return new DaemonConnection<>(socketChannel, serializer);
return new DaemonConnection(socketChannel);
} catch (DaemonException.ConnectException e) {
throw e;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.jboss.fuse.mvnd.common.Message.BuildException;
import org.jboss.fuse.mvnd.common.Message.BuildMessage;
import org.jboss.fuse.mvnd.common.Message.KeepAliveMessage;
import org.jboss.fuse.mvnd.common.Message.MessageSerializer;
import org.jboss.fuse.mvnd.common.OsUtils;
import org.jboss.fuse.mvnd.common.logging.ClientOutput;
import org.jboss.fuse.mvnd.common.logging.TerminalOutput;
Expand Down Expand Up @@ -184,7 +183,7 @@ public ExecutionResult execute(ClientOutput output, List<String> argv) {
args.add("-Dmaven.repo.local=" + localMavenRepository.toString());
}

final DaemonConnector connector = new DaemonConnector(layout, registry, buildProperties, new MessageSerializer());
final DaemonConnector connector = new DaemonConnector(layout, registry, buildProperties);
List<String> opts = new ArrayList<>();
try (DaemonClientConnection daemon = connector.connect(new DaemonCompatibilitySpec(javaHome, opts),
s -> output.accept(null, s))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,24 @@
* https://github.com/gradle/gradle/blob/v5.6.2/subprojects/messaging/src/main/java/org/gradle/internal/remote/internal/inet/SocketConnection.java
*
*/
public class DaemonConnection<T> implements AutoCloseable {
public class DaemonConnection implements AutoCloseable {

private static final Logger LOGGER = LoggerFactory.getLogger(DaemonConnection.class);

private final SocketChannel socket;
private final Serializer<T> serializer;
private final DataInputStream input;
private final DataOutputStream output;
private final InetSocketAddress localAddress;
private final InetSocketAddress remoteAddress;
private final DataInputStream instr;
private final DataOutputStream outstr;

public DaemonConnection(SocketChannel socket, Serializer<T> serializer) {
public DaemonConnection(SocketChannel socket) {
this.socket = socket;
this.serializer = serializer;
try {
// NOTE: we use non-blocking IO as there is no reliable way when using blocking IO to shutdown reads while
// keeping writes active. For example, Socket.shutdownInput() does not work on Windows.
socket.configureBlocking(false);
outstr = new DataOutputStream(new SocketOutputStream(socket));
instr = new DataInputStream(new SocketInputStream(socket));
this.output = new DataOutputStream(new SocketOutputStream(socket));
this.input = new DataInputStream(new SocketInputStream(socket));
} catch (IOException e) {
throw new DaemonException.InterruptedException(e);
}
Expand All @@ -72,15 +70,15 @@ public String toString() {
return "socket connection from " + localAddress + " to " + remoteAddress;
}

public T receive() throws DaemonException.MessageIOException {
public Message receive() throws DaemonException.MessageIOException {
try {
return serializer.read(instr);
return Message.read(input);
} catch (EOFException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Discarding EOFException: {}", e.toString());
}
return null;
} catch (ClassNotFoundException | IOException e) {
} catch (IOException e) {
throw new DaemonException.RecoverableMessageIOException(
String.format("Could not read message from '%s'.", remoteAddress), e);
} catch (Throwable e) {
Expand Down Expand Up @@ -109,11 +107,11 @@ private static boolean isEndOfStream(Exception e) {
return false;
}

public void dispatch(T message) throws DaemonException.MessageIOException {
public void dispatch(Message message) throws DaemonException.MessageIOException {
try {
serializer.write(outstr, message);
outstr.flush();
} catch (ClassNotFoundException | IOException e) {
message.write(output);
output.flush();
} catch (IOException e) {
throw new DaemonException.RecoverableMessageIOException(
String.format("Could not write message %s to '%s'.", message, remoteAddress), e);
} catch (Throwable e) {
Expand All @@ -124,15 +122,15 @@ public void dispatch(T message) throws DaemonException.MessageIOException {

public void flush() throws DaemonException.MessageIOException {
try {
outstr.flush();
output.flush();
} catch (Throwable e) {
throw new DaemonException.MessageIOException(String.format("Could not write '%s'.", remoteAddress), e);
}
}

public void close() {
Throwable failure = null;
List<Closeable> elements = Arrays.asList(this::flush, instr, outstr, socket);
List<Closeable> elements = Arrays.asList(this::flush, input, output, socket);
for (Closeable element : elements) {
try {
element.close();
Expand Down
Loading

0 comments on commit a71033c

Please sign in to comment.