Less message classes, more singletons

This commit is contained in:
Peter Palaga
2020-11-01 01:17:06 +01:00
parent a9e9b7997f
commit 8452440c51
4 changed files with 40 additions and 47 deletions

View File

@@ -35,6 +35,9 @@ public abstract class Message {
static final int KEEP_ALIVE = 4;
static final int STOP = 5;
public static final SimpleMessage KEEP_ALIVE_SINGLETON = new SimpleMessage(Message.KEEP_ALIVE, "KEEP_ALIVE");
public static final SimpleMessage STOP_SINGLETON = new SimpleMessage(Message.STOP, "STOP");
public static Message read(DataInputStream input) throws IOException {
int type = input.read();
if (type == -1) {
@@ -50,9 +53,9 @@ public abstract class Message {
case BUILD_EXCEPTION:
return BuildException.read(input);
case KEEP_ALIVE:
return KeepAliveMessage.SINGLETON;
return SimpleMessage.KEEP_ALIVE_SINGLETON;
case STOP:
return StopMessage.SINGLETON;
return SimpleMessage.STOP_SINGLETON;
}
throw new IllegalStateException("Unexpected message type: " + type);
}
@@ -401,43 +404,30 @@ public abstract class Message {
}
}
public static class KeepAliveMessage extends Message {
public static final KeepAliveMessage SINGLETON = new KeepAliveMessage();
public static class SimpleMessage extends Message {
final int type;
final String mnemonic;
/**
* Use {@link #SINGLETON}
* Use {@link #KEEP_ALIVE_SINGLETON}
*
* @param type
*/
private KeepAliveMessage() {
private SimpleMessage(int type, String mnemonic) {
this.type = type;
this.mnemonic = mnemonic;
}
@Override
public String toString() {
return "KeepAliveMessage{}";
return mnemonic;
}
@Override
public void write(DataOutputStream output) throws IOException {
output.write(KEEP_ALIVE);
output.write(type);
}
}
public static class StopMessage extends Message {
public static final KeepAliveMessage SINGLETON = new KeepAliveMessage();
/**
* Use {@link #SINGLETON}
*/
private StopMessage() {
}
@Override
public String toString() {
return "StopMessage{}";
}
@Override
public void write(DataOutputStream output) throws IOException {
output.write(STOP);
}
}
}

View File

@@ -60,15 +60,16 @@ public class TerminalOutput implements ClientOutput {
private volatile Exception exception;
private volatile boolean closing;
private final CountDownLatch closed = new CountDownLatch(1);
private int linesPerProject = 0;
private boolean displayDone = false;
private final long start;
private String buildStatus;
private String name;
private int totalProjects;
private int doneProjects;
private int maxThreads;
private volatile String name;
private volatile int totalProjects;
private volatile int maxThreads;
private int linesPerProject = 0; // read/written only by the displayLoop
private int doneProjects = 0; // read/written only by the displayLoop
private String buildStatus; // read/written only by the displayLoop
private boolean displayDone = false; // read/written only by the displayLoop
enum EventType {
BUILD_STATUS,
@@ -82,6 +83,7 @@ public class TerminalOutput implements ClientOutput {
}
static class Event {
public static final Event KEEP_ALIVE = new Event(EventType.KEEP_ALIVE, null, null);
public final EventType type;
public final String projectId;
public final String message;
@@ -129,8 +131,12 @@ public class TerminalOutput implements ClientOutput {
public void startBuild(String name, int projects, int cores) {
this.name = name;
this.totalProjects = projects;
this.doneProjects = 0;
this.maxThreads = cores;
try {
queue.put(Event.KEEP_ALIVE);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
public void projectStateChanged(String projectId, String task) {
@@ -181,7 +187,7 @@ public class TerminalOutput implements ClientOutput {
@Override
public void keepAlive() {
try {
queue.put(new Event(EventType.KEEP_ALIVE, null, null));
queue.put(Event.KEEP_ALIVE);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}