Skip to content

Commit

Permalink
refactor: replaced conditional with polymorphism applied in handle Op…
Browse files Browse the repository at this point in the history
… both Client and Server side
  • Loading branch information
matheusbus committed Nov 28, 2024
1 parent 080895b commit 6d20b7c
Showing 1 changed file with 63 additions and 46 deletions.
109 changes: 63 additions & 46 deletions cli/src/main/java/hudson/cli/PlainCLIProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,78 @@ class PlainCLIProtocol {
/** One-byte operation to send to the other side. */
private enum Op {
/** UTF-8 command name or argument. */
ARG(true),
ARG(true) {
@Override
void execute(DataInputStream dis, EitherSide side) throws IOException {
((ServerSide) side).onArg(dis.readUTF());
}
},
/** UTF-8 locale identifier. */
LOCALE(true),
LOCALE(true) {
@Override
void execute(DataInputStream dis, EitherSide side) throws IOException {
((ServerSide) side).onLocale(dis.readUTF());
}
},
/** UTF-8 client encoding. */
ENCODING(true),
ENCODING(true) {
@Override
void execute(DataInputStream dis, EitherSide side) throws IOException {
((ServerSide) side).onEncoding(dis.readUTF());
}
},
/** Start running command. */
START(true),
START(true) {
@Override
void execute(DataInputStream dis, EitherSide side) throws IOException {
((ServerSide) side).onStart();
}
},
/** Exit code, as int. */
EXIT(false),
EXIT(false) {
@Override
void execute(DataInputStream dis, EitherSide side) throws IOException {
((ClientSide) side).onExit(dis.readInt());
}
},
/** Chunk of stdin, as int length followed by bytes. */
STDIN(true),
STDIN(true) {
@Override
void execute(DataInputStream dis, EitherSide side) throws IOException {
((ServerSide) side).onStdin(dis.readAllBytes());
}
},
/** EOF on stdin. */
END_STDIN(true),
END_STDIN(true) {
@Override
void execute(DataInputStream dis, EitherSide side) throws IOException {
((ServerSide) side).onEndStdin();
}
},
/** Chunk of stdout. */
STDOUT(false),
STDOUT(false) {
@Override
void execute(DataInputStream dis, EitherSide side) throws IOException {
((ClientSide) side).onStdout(dis.readAllBytes());
}
},
/** Chunk of stderr. */
STDERR(false);
STDERR(false) {
@Override
void execute(DataInputStream dis, EitherSide side) throws IOException {
((ClientSide) side).onStderr(dis.readAllBytes());
}
};

/** True if sent from the client to the server; false if sent from the server to the client. */
final boolean clientSide;

Op(boolean clientSide) {
this.clientSide = clientSide;
}

abstract void execute(DataInputStream dis, EitherSide side) throws IOException;

void validate(boolean isClient) throws ProtocolException {
if (this.clientSide != isClient) {
throw new ProtocolException("Operation not allowed on this side: " + this);
Expand Down Expand Up @@ -279,29 +327,9 @@ abstract static class ServerSide extends EitherSide {

@Override
protected final boolean handle(Op op, DataInputStream dis) throws IOException {
op.validate(false);
switch (op) {
case ARG:
onArg(dis.readUTF());
return true;
case LOCALE:
onLocale(dis.readUTF());
return true;
case ENCODING:
onEncoding(dis.readUTF());
return true;
case START:
onStart();
return true;
case STDIN:
onStdin(dis.readAllBytes());
return true;
case END_STDIN:
onEndStdin();
return true;
default:
return false;
}
op.validate(true);
op.execute(dis, this);
return true;
}

protected abstract void onArg(String text);
Expand Down Expand Up @@ -338,20 +366,9 @@ abstract static class ClientSide extends EitherSide {

@Override
protected boolean handle(Op op, DataInputStream dis) throws IOException {
op.validate(true);
switch (op) {
case EXIT:
onExit(dis.readInt());
return true;
case STDOUT:
onStdout(dis.readAllBytes());
return true;
case STDERR:
onStderr(dis.readAllBytes());
return true;
default:
return false;
}
op.validate(false);
op.execute(dis, this);
return true;
}

protected abstract void onExit(int code);
Expand Down

0 comments on commit 6d20b7c

Please sign in to comment.