use of de.roderick.weberknecht.WebSocket in project intellij-plugins by JetBrains.
the class VmServiceBase method connect.
/**
* Connect to the VM observatory service via the specified URI
*
* @return an API object for interacting with the VM service (not {@code null}).
*/
public static VmService connect(final String url) throws IOException {
// Validate URL
URI uri;
try {
uri = new URI(url);
} catch (URISyntaxException e) {
throw new IOException("Invalid URL: " + url, e);
}
String wsScheme = uri.getScheme();
if (!"ws".equals(wsScheme) && !"wss".equals(wsScheme)) {
throw new IOException("Unsupported URL scheme: " + wsScheme);
}
// Create web socket and observatory
WebSocket webSocket;
try {
webSocket = new WebSocket(uri);
} catch (WebSocketException e) {
throw new IOException("Failed to create websocket: " + url, e);
}
final VmService vmService = new VmService();
// Setup event handler for forwarding responses
webSocket.setEventHandler(new WebSocketEventHandler() {
@Override
public void onClose() {
Logging.getLogger().logInformation("VM connection closed: " + url);
vmService.connectionClosed();
}
@Override
public void onMessage(WebSocketMessage message) {
Logging.getLogger().logInformation("VM message: " + message.getText());
try {
vmService.processResponse(message.getText());
} catch (Exception e) {
Logging.getLogger().logError(e.getMessage(), e);
}
}
@Override
public void onOpen() {
vmService.connectionOpened();
Logging.getLogger().logInformation("VM connection open: " + url);
}
@Override
public void onPing() {
}
@Override
public void onPong() {
}
});
// Establish WebSocket Connection
try {
webSocket.connect();
} catch (WebSocketException e) {
throw new IOException("Failed to connect: " + url, e);
}
vmService.requestSink = new WebSocketRequestSink(webSocket);
// Check protocol version
final CountDownLatch latch = new CountDownLatch(1);
final String[] errMsg = new String[1];
vmService.getVersion(new VersionConsumer() {
@Override
public void onError(RPCError error) {
String msg = "Failed to determine protocol version: " + error.getCode() + "\n message: " + error.getMessage() + "\n details: " + error.getDetails();
Logging.getLogger().logInformation(msg);
errMsg[0] = msg;
}
@Override
public void received(Version response) {
int major = response.getMajor();
int minor = response.getMinor();
if (major != VmService.versionMajor || minor != VmService.versionMinor) {
if (major == 2 || major == 3) {
Logging.getLogger().logInformation("Difference in protocol version: client=" + VmService.versionMajor + "." + VmService.versionMinor + " vm=" + major + "." + minor);
} else {
String msg = "Incompatible protocol version: client=" + VmService.versionMajor + "." + VmService.versionMinor + " vm=" + major + "." + minor;
Logging.getLogger().logError(msg);
errMsg[0] = msg;
}
}
latch.countDown();
}
});
try {
if (!latch.await(5, TimeUnit.SECONDS)) {
throw new IOException("Failed to determine protocol version");
}
if (errMsg[0] != null) {
throw new IOException(errMsg[0]);
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while waiting for response", e);
}
return vmService;
}
use of de.roderick.weberknecht.WebSocket in project flutter-intellij by flutter.
the class VmOpenSourceLocationListener method connect.
/**
* Connect to the VM observatory service via the specified URI
*
* @return an API object for interacting with the VM service (not {@code null}).
*/
public static VmOpenSourceLocationListener connect(@NotNull final String url) throws IOException {
// Validate URL
final URI uri;
try {
uri = new URI(url);
} catch (URISyntaxException e) {
throw new IOException("Invalid URL: " + url, e);
}
String wsScheme = uri.getScheme();
if (!"ws".equals(wsScheme) && !"wss".equals(wsScheme)) {
throw new IOException("Unsupported URL scheme: " + wsScheme);
}
// Create web socket and observatory
final WebSocket webSocket;
try {
webSocket = new WebSocket(uri);
} catch (WebSocketException e) {
throw new IOException("Failed to create websocket: " + url, e);
}
final VmOpenSourceLocationListener listener = new VmOpenSourceLocationListener(new MessageSender() {
@Override
public void sendMessage(JsonObject message) {
try {
webSocket.send(message.toString());
} catch (WebSocketException e) {
LOG.warn(e);
}
}
@Override
public void close() {
try {
webSocket.close();
} catch (WebSocketException e) {
LOG.warn(e);
}
}
});
webSocket.setEventHandler(new WebSocketEventHandler() {
final JsonParser parser = new JsonParser();
@Override
public void onClose() {
// ignore
}
@Override
public void onMessage(WebSocketMessage message) {
listener.onMessage(parser.parse(message.getText()).getAsJsonObject());
}
@Override
public void onOpen() {
listener.onOpen();
}
@Override
public void onPing() {
// ignore
}
@Override
public void onPong() {
// ignore
}
});
// Establish WebSocket Connection
try {
webSocket.connect();
} catch (WebSocketException e) {
throw new IOException("Failed to connect: " + url, e);
}
return listener;
}
use of de.roderick.weberknecht.WebSocket in project flutter-intellij by flutter.
the class VmServiceBase method connect.
/**
* Connect to the VM observatory service via the specified URI
*
* @return an API object for interacting with the VM service (not {@code null}).
*/
public static VmService connect(final String url) throws IOException {
// Validate URL
URI uri;
try {
uri = new URI(url);
} catch (URISyntaxException e) {
throw new IOException("Invalid URL: " + url, e);
}
String wsScheme = uri.getScheme();
if (!"ws".equals(wsScheme) && !"wss".equals(wsScheme)) {
throw new IOException("Unsupported URL scheme: " + wsScheme);
}
// Create web socket and observatory
WebSocket webSocket;
try {
webSocket = new WebSocket(uri);
} catch (WebSocketException e) {
throw new IOException("Failed to create websocket: " + url, e);
}
final VmService vmService = new VmService();
// Setup event handler for forwarding responses
webSocket.setEventHandler(new WebSocketEventHandler() {
@Override
public void onClose() {
Logging.getLogger().logInformation("VM connection closed: " + url);
vmService.connectionClosed();
}
@Override
public void onMessage(WebSocketMessage message) {
Logging.getLogger().logInformation("VM message: " + message.getText());
try {
vmService.processMessage(message.getText());
} catch (Exception e) {
Logging.getLogger().logError(e.getMessage(), e);
}
}
@Override
public void onOpen() {
vmService.connectionOpened();
Logging.getLogger().logInformation("VM connection open: " + url);
}
@Override
public void onPing() {
}
@Override
public void onPong() {
}
});
// noinspection TryWithIdenticalCatches
try {
webSocket.connect();
} catch (WebSocketException e) {
throw new IOException("Failed to connect: " + url, e);
} catch (ArrayIndexOutOfBoundsException e) {
// (de.roderick.weberknecht.WebSocket.connect, WebSocket.java:126).
throw new IOException("Failed to connect: " + url, e);
}
vmService.requestSink = new WebSocketRequestSink(webSocket);
// Check protocol version
final CountDownLatch latch = new CountDownLatch(1);
final String[] errMsg = new String[1];
vmService.getVersion(new VersionConsumer() {
@Override
public void onError(RPCError error) {
String msg = "Failed to determine protocol version: " + error.getCode() + "\n message: " + error.getMessage() + "\n details: " + error.getDetails();
Logging.getLogger().logInformation(msg);
errMsg[0] = msg;
}
@Override
public void received(Version version) {
vmService.runtimeVersion = version;
latch.countDown();
}
});
try {
if (!latch.await(5, TimeUnit.SECONDS)) {
throw new IOException("Failed to determine protocol version");
}
if (errMsg[0] != null) {
throw new IOException(errMsg[0]);
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while waiting for response", e);
}
return vmService;
}
Aggregations