Introducing Responder and JSONObject to JSPackagerClient

Reviewed By: cwdick

Differential Revision: D4537115

fbshipit-source-id: 61ee03d3e700bbee8b1cdb02bbf31a8491ca7891
This commit is contained in:
Lukas Piatkowski
2017-02-14 09:37:33 -08:00
committed by Facebook Github Bot
parent 47616d84d8
commit e28a12c613
5 changed files with 112 additions and 103 deletions

View File

@@ -10,25 +10,68 @@ package com.facebook.react.packagerconnection;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.Map;
import android.util.JsonReader;
import android.util.JsonToken;
import com.facebook.common.logging.FLog;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okhttp3.ws.WebSocket;
import org.json.JSONObject;
/**
* A client for packager that uses WebSocket connection.
*/
final public class JSPackagerClient implements ReconnectingWebSocket.MessageCallback {
private static final String TAG = JSPackagerClient.class.getSimpleName();
private static final int PROTOCOL_VERSION = 1;
public class Responder {
private Object mId;
public Responder(Object id) {
mId = id;
}
public void respond(String result) {
try {
mWebSocket.sendMessage(RequestBody.create(WebSocket.TEXT, result));
} catch (Exception e) {
FLog.e(TAG, "Responding failed", e);
}
}
public void error(Object error) {
try {
JSONObject message = new JSONObject();
message.put("id", mId);
message.put("error", error);
mWebSocket.sendMessage(RequestBody.create(WebSocket.TEXT, message.toString()));
} catch (Exception e) {
FLog.e(TAG, "Responding with error failed", e);
}
}
}
public interface RequestHandler {
public void onNotification(@Nullable ReconnectingWebSocket.WebSocketSender webSocket);
public void onRequest(@Nullable Object params, Responder responder);
public void onNotification(@Nullable Object params);
}
public static abstract class NotificationOnlyHandler implements RequestHandler {
final public void onRequest(@Nullable Object params, Responder responder) {
responder.error("Request is not supported");
FLog.e(TAG, "Request is not supported");
}
abstract public void onNotification(@Nullable Object params);
}
public static abstract class RequestOnlyHandler implements RequestHandler {
abstract public void onRequest(@Nullable Object params, Responder responder);
final public void onNotification(@Nullable Object params) {
FLog.e(TAG, "Notification is not supported");
}
}
private ReconnectingWebSocket mWebSocket;
@@ -49,47 +92,45 @@ final public class JSPackagerClient implements ReconnectingWebSocket.MessageCall
}
@Override
public void onMessage(@Nullable ReconnectingWebSocket.WebSocketSender webSocket, ResponseBody response) {
public void onMessage(ResponseBody response) {
if (response.contentType() != WebSocket.TEXT) {
FLog.w(TAG, "Websocket received unexpected message with payload of type " + response.contentType());
FLog.w(
TAG,
"Websocket received message with payload of unexpected type " + response.contentType());
return;
}
try {
JsonReader reader = new JsonReader(response.charStream());
JSONObject message = new JSONObject(response.string());
Integer version = null;
String target = null;
String action = null;
int version = message.optInt("version");
String target = message.optString("target");
String action = message.optString("action");
reader.beginObject();
while (reader.hasNext()) {
String field = reader.nextName();
if (JsonToken.NULL == reader.peek()) {
reader.skipValue();
continue;
}
if ("version".equals(field)) {
version = reader.nextInt();
} else if ("target".equals(field)) {
target = reader.nextString();
} else if ("action".equals(field)) {
action = reader.nextString();
}
}
reader.close();
if (version == null || target == null || action == null || version != 1) {
if (version != PROTOCOL_VERSION) {
FLog.e(
TAG,
"Message with incompatible or missing version of protocol received: " + version);
return;
}
if ("bridge".equals(target) && mRequestHandlers.containsKey(action)) {
mRequestHandlers.get(action).onNotification(webSocket);
if (!"bridge".equals(target)) {
return;
}
} catch (IOException e) {
FLog.e(TAG, "Parsing response message from websocket failed", e);
RequestHandler handler = mRequestHandlers.get(action);
if (handler == null) {
FLog.e(TAG, "No request handler for action: " + action);
return;
}
if (!"pokeSamplingProfiler".equals(action)) {
handler.onNotification(null);
} else {
handler.onRequest(null, new Responder("profiler"));
}
} catch (Exception e) {
FLog.e(TAG, "Handling the message failed", e);
} finally {
response.close();
}