Skip to content

Commit

Permalink
[GR-25722] Refactoring to make inspector session opening more clear.
Browse files Browse the repository at this point in the history
PullRequest: graal/7012
  • Loading branch information
entlicher committed Aug 21, 2020
2 parents 0c38901 + 2076e52 commit 8c730a3
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public void run() {
connectionWatcher = sessionInfo.getConnectionWatcher();
contextId = sessionInfo.getId();
inspectorContext = sessionInfo.getInspectorContext();
inspect.setMessageListener(this);
inspect.open(this);
Context context = Context.newBuilder().engine(engine).allowAllAccess(true).build();
initialized.release();
Source source = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void onOpen(ServerHandshake sh) {
executionContext.logMessage("CLIENT ws connection opened at ", getURI());
iss = InspectServerSession.create(executionContext, debugBreak, connectionWatcher);
connectionWatcher.notifyOpen();
iss.setMessageListener(new MessageEndpoint() {
iss.open(new MessageEndpoint() {
@Override
public void sendText(String message) {
executionContext.logMessage("SERVER: ", message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ private static final class Server {
}
if (serverEndpoint == null) {
InspectorServer server;
interceptor.close(token);
interceptor.resetSessionEndpoint(); // A new endpoint is going to be opened
server = InspectorServer.get(socketAddress, token, pathContainingToken, executionContext, debugBreak, secure, keyStoreOptions, connectionWatcher, iss);
String wsAddress = server.getWSAddress(token);
wss = server;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private Object connect() {
}
InspectorExecutionContext execContext = contextSupplier.get();
iss = InspectServerSession.create(execContext, false, new ConnectionWatcher());
iss.setJSONMessageListener(getListeners());
iss.open(getListeners());
execContext.setSynchronous(true);
// Enable the Runtime by default
iss.sendCommand(new Command("{\"id\":0,\"method\":\"Runtime.enable\"}"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ public void sendClose() throws IOException {
}
}

boolean isClosed() {
return processThread == null;
}

// For tests only
public DebuggerDomain getDebugger() {
return debugger;
Expand Down Expand Up @@ -141,21 +145,32 @@ public void dispose() {
}
}

public synchronized void setMessageListener(MessageEndpoint messageListener) {
synchronized void clearMessageEndpoint() {
this.messageEndpoint = null;
}

/**
* Open the communication with a {@link MessageEndpoint}.
*/
public synchronized void open(MessageEndpoint messageListener) {
assert messageListener != null : "Message listener must not be null";
this.messageEndpoint = messageListener;
if (messageListener != null && processThread == null) {
EventHandler eh = new EventHandlerImpl();
runtime.setEventHandler(eh);
debugger.setEventHandler(eh);
profiler.setEventHandler(eh);
processThread = new CommandProcessThread();
processThread.start();
}
startUp();
}

public synchronized void setJSONMessageListener(JSONMessageListener messageListener) {
/**
* Open the communication with a {@link JSONMessageListener}.
*/
public synchronized void open(JSONMessageListener messageListener) {
assert messageListener != null : "Message listener must not be null";
assert this.jsonMessageListener == null : "A message listener was set already.";
this.jsonMessageListener = messageListener;
if (messageListener != null && processThread == null) {
startUp();
}

private void startUp() {
assert Thread.holdsLock(this);
if (processThread == null) {
EventHandler eh = new EventHandlerImpl();
runtime.setEventHandler(eh);
debugger.setEventHandler(eh);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ private class InspectWebSocket extends WebSocket {
public void onOpen() {
iss.context.logMessage("CLIENT web socket connection opened.", "");
connectionWatcher.notifyOpen();
iss.setMessageListener(new MessageEndpoint() {
iss.open(new MessageEndpoint() {
@Override
public void sendText(String message) throws IOException {
iss.context.logMessage("SERVER: ", message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,22 @@ public WSInterceptorServer(int port, Token token, InspectServerSession iss, Conn
this.token = token;
this.connectionWatcher = connectionWatcher;
this.iss = iss;
iss.setMessageListener(this);
iss.open(this);
}

public void resetSessionEndpoint() {
iss.clearMessageEndpoint();
}

public void newSession(InspectServerSession newIss) {
this.iss.setMessageListener(null);
assert this.iss.isClosed();
this.iss = newIss;
this.iss.setMessageListener(this);
this.iss.open(this);
}

public void opened(MessageEndpoint endpoint) {
this.inspectEndpoint = endpoint;
iss.setMessageListener(this);
this.iss.open(endpoint);
this.connectionWatcher.notifyOpen();
}

Expand All @@ -71,7 +75,7 @@ public int getPort() {

@Override
public void close(Token tokenToClose) throws IOException {
iss.setMessageListener(null);
iss.dispose();
if (inspectEndpoint != null) {
if (tokenToClose.equals(this.token)) {
inspectEndpoint.sendClose();
Expand Down

0 comments on commit 8c730a3

Please sign in to comment.