-
-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add DcJsonrpcInstance class #2479
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
4f468a4
Add DcJsonrpcInstance class
link2xt 1405ae0
Start using DcJsonrpcInstance
link2xt 3980073
Revert "Start using DcJsonrpcInstance"
link2xt 00fd1a9
Add dcJsonrpcInstance to ApplicationContext
link2xt 5246ee7
Remove getNextEventCPtr
link2xt 39e9ab8
Add JSON-RPC thread
link2xt a7e6f48
Try to send JSON-RPC requests
link2xt e27a901
Generate request IDs
link2xt 070f284
Sending request works
link2xt df1acac
Trying to use StringWriter
link2xt 72f2f89
requestPtr
link2xt 53dc77e
Disable requests for now
link2xt 7d6ff61
Start JSON-RPC parsing
link2xt 5a5d27a
add DcRpc class
adbenitez 1d6d148
fix bug
adbenitez d977d1f
use get()
adbenitez aa1a602
add DcHelp.getRpc()
adbenitez 941627d
move Rpc class to com.b44t.messenger.rpc package
adbenitez b5fc9ab
remove field added while testing
adbenitez e1d0712
Apply suggestions from code review
adbenitez e4beb06
check if future is null
adbenitez 7935513
move SettableFuture to core package
adbenitez a5b1528
tweak comment
adbenitez cbec549
rename RpcError to RpcException
adbenitez 0b85cd2
add getHttpResponse to Rpc
adbenitez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.b44t.messenger; | ||
|
||
public class DcJsonrpcInstance { | ||
|
||
public DcJsonrpcInstance(long jsonrpcInstanceCPtr) { | ||
this.jsonrpcInstanceCPtr = jsonrpcInstanceCPtr; | ||
} | ||
|
||
@Override protected void finalize() throws Throwable { | ||
super.finalize(); | ||
unrefJsonrpcInstanceCPtr(); | ||
jsonrpcInstanceCPtr = 0; | ||
} | ||
|
||
public native void request(String request); | ||
public native String getNextResponse(); | ||
|
||
// working with raw c-data | ||
private long jsonrpcInstanceCPtr; // CAVE: the name is referenced in the JNI | ||
private native void unrefJsonrpcInstanceCPtr(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.b44t.messenger.rpc; | ||
|
||
import android.util.Base64; | ||
|
||
public class HttpResponse { | ||
// base64-encoded response body. | ||
private String blob; | ||
// MIME type, e.g. "text/plain" or "text/html". | ||
private String mimetype; | ||
// Encoding, e.g. "utf-8". | ||
private String encoding; | ||
|
||
public HttpResponse(String blob, String mimetype, String encoding) { | ||
this.blob = blob; | ||
this.mimetype = mimetype; | ||
this.encoding = encoding; | ||
} | ||
|
||
public byte[] getBlob() { | ||
if (blob == null) { | ||
return null; | ||
} | ||
return Base64.decode(blob, Base64.NO_WRAP | Base64.NO_PADDING); | ||
} | ||
|
||
public String getMimetype() { | ||
return mimetype; | ||
} | ||
|
||
public String getEncoding() { | ||
return encoding; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package com.b44t.messenger.rpc; | ||
|
||
import com.b44t.messenger.DcJsonrpcInstance; | ||
import com.b44t.messenger.util.concurrent.SettableFuture; | ||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonSyntaxException; | ||
import com.google.gson.reflect.TypeToken; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
public class Rpc { | ||
private final Map<Integer, SettableFuture<JsonElement>> requestFutures = new ConcurrentHashMap<>(); | ||
private final DcJsonrpcInstance dcJsonrpcInstance; | ||
private int requestId = 0; | ||
private final Gson gson = new GsonBuilder().serializeNulls().create(); | ||
|
||
public Rpc(DcJsonrpcInstance dcJsonrpcInstance) { | ||
this.dcJsonrpcInstance = dcJsonrpcInstance; | ||
} | ||
|
||
private void processResponse() throws JsonSyntaxException { | ||
String jsonResponse = dcJsonrpcInstance.getNextResponse(); | ||
Response response = gson.fromJson(jsonResponse, Response.class); | ||
|
||
if (response.id == 0) { // Got JSON-RPC notification/event, ignore | ||
return; | ||
} | ||
|
||
SettableFuture<JsonElement> future = requestFutures.remove(response.id); | ||
if (future == null) { // Got a response with unknown ID, ignore | ||
return; | ||
} | ||
|
||
if (response.error != null) { | ||
future.setException(new RpcException(response.error.toString())); | ||
} else if (response.result != null) { | ||
future.set(response.result); | ||
} else { | ||
future.setException(new RpcException("Got JSON-RPC response witout result or error: " + jsonResponse)); | ||
} | ||
} | ||
|
||
public void start() { | ||
new Thread(() -> { | ||
while (true) { | ||
try { | ||
processResponse(); | ||
} catch (JsonSyntaxException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
}, "jsonrpcThread").start(); | ||
} | ||
|
||
public SettableFuture<JsonElement> call(String method, Object... params) { | ||
int id; | ||
synchronized (this) { | ||
id = ++requestId; | ||
} | ||
String jsonRequest = gson.toJson(new Request(method, params, id)); | ||
SettableFuture<JsonElement> future = new SettableFuture<>(); | ||
requestFutures.put(id, future); | ||
dcJsonrpcInstance.request(jsonRequest); | ||
return future; | ||
} | ||
|
||
public JsonElement getResult(String method, Object... params) throws RpcException { | ||
try { | ||
return call(method, params).get(); | ||
} catch (ExecutionException e) { | ||
throw (RpcException)e.getCause(); | ||
} catch (InterruptedException e) { | ||
throw new RpcException(e.getMessage()); | ||
} | ||
} | ||
|
||
public int addAccount() throws RpcException { | ||
return gson.fromJson(getResult("add_account"), int.class); | ||
} | ||
|
||
public void startIO() throws RpcException { | ||
getResult("start_io_for_all_accounts"); | ||
} | ||
|
||
public void stopIO() throws RpcException { | ||
getResult("stop_io_for_all_accounts"); | ||
} | ||
|
||
public Map<String, String> getSystemInfo() throws RpcException { | ||
TypeToken<Map<String, String>> mapType = new TypeToken<Map<String, String>>(){}; | ||
return gson.fromJson(getResult("get_system_info"), mapType); | ||
} | ||
|
||
public HttpResponse getHttpResponse(int accountId, String url) throws RpcException { | ||
return gson.fromJson(getResult("get_http_response", accountId, url), HttpResponse.class); | ||
} | ||
|
||
|
||
private static class Request { | ||
public String jsonrpc = "2.0"; | ||
public String method; | ||
public Object[] params; | ||
public int id; | ||
|
||
public Request(String method, Object[] params, int id) { | ||
this.method = method; | ||
this.params = params; | ||
this.id = id; | ||
} | ||
} | ||
|
||
private static class Response { | ||
public int id = 0; | ||
public JsonElement result; | ||
public JsonElement error; | ||
|
||
public Response(int id, JsonElement result, JsonElement error) { | ||
this.id = id; | ||
this.result = result; | ||
this.error = error; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.b44t.messenger.rpc; | ||
|
||
/** | ||
* An exception occurred while processing a request in Delta Chat core. | ||
**/ | ||
public class RpcException extends Exception { | ||
|
||
public RpcException(String message) { | ||
super(message); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...sms/util/concurrent/ListenableFuture.java → ...ger/util/concurrent/ListenableFuture.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...resms/util/concurrent/SettableFuture.java → ...enger/util/concurrent/SettableFuture.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, so we need one more JSON library? jackson-databind doesn't work? Can we use gson instead of jackson-databind, in order to not increase the number of dependencies?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you grep for gson, you will find that we already use it in some part of the code, it seems it is a dependency of some of our dependencies already, I had to explicitly add this because it seems the version used by the dependency was old and didn't have a needed method.
in a follow-up PR we should try to only use one of gson, jackson and org.json (the 3 are used at the moment in existing code besides this PR)