-
Notifications
You must be signed in to change notification settings - Fork 904
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
First basic draft version of a java client library. need feedback #2223
First basic draft version of a java client library. need feedback #2223
Conversation
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.
Just a comment regarding reading from the socket. I'm not sure if this needs to be in the clightning repo, perhaps it could be distributed as a standlone Java library on your github?
while(true) { | ||
int read = this.is.read(buf); | ||
response = response + new String(buf, 0, read, "UTF-8"); | ||
if(read <=0 || response.contains("\n\n" )) { |
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.
in my Haskell implementation I count open and closed curly brackets (ensuring they sum to 0) to determine when to stop reading.
This could fail in an unlikely event that both \n
's appear at the end and start of two separate reads. This would result in the entire program blocking indefinitely on the next read call.
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.
Response is the string that has been appended with the buffer so I see no problem with split \n
On the other side the sum of open and closed brakets might fail as those could be part of the strings in the json data. So those need to be ignored
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.
ah good point on the curly's. I see now that it gets appended before the check so yeah looks like it would be fine. I should probably do something similar... I do find this part a bit strange. It looks like lightning-cli does it by parsing tokens as they come in, and ends the reading when there is no more to parse.
@renepickhardt I'm personally not a fan of using |
* network at http://ln.rene-pickhardt.de | ||
* | ||
* @author Rene Pickhardt | ||
|
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.
nit - extra line break.
public static void main(String[] args) { | ||
// TODO Auto-generated method stub | ||
|
||
JLightningRpc rpc_interface = new JLightningRpc("/tmp/spark-env/ln1/lightning-rpc"); |
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.
I would inject this "/tmp/spark-env/ln1/lightning-rpc"
via command line. Will be available in args
.
package de.renepickhardt.ln; | ||
|
||
/** | ||
* JLightning a small test / example class to demonstrate how to use JLightningRpc |
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.
Maybe move this into a README.md
and add the necessary code snippets that you provide here in the main
method.
@@ -0,0 +1,14 @@ | |||
Draft version of the JLightning rpc interface for c-lightning. | |||
|
|||
Using this client library is is as simple as the following code: |
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.
Even if someone is familiar with java, would be nice to have the actual command to build and launch: e.g.
$ javac <details-to-build>
$ java <details-to-run>
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.
On that note, have you though of using Gradle or Maven to handle building and dependency management? Preferably Gradle
public String delInvoice(String label, Status status) { | ||
HashMap<String, String> payload = new HashMap<String,String> (); | ||
payload.put("label", label); | ||
payload.put("status", status.toString()); |
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.
To handle NullPointerException
here on toString()
, you can use String.valueOf(status)
. In case of null
, it will return the String
"null"
.
payload = new HashMap<String, String>(); | ||
} | ||
|
||
//remove null items from payload |
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.
There's no need to add the keys to a new HashSet
here. You can simply iterate through the payload.keySet()
and and remove an entry if value is null
.
Here's some sample code:
Map<String, String> m = new HashMap<String, String>();
m.put("1", "One");
m.put("2", null);
System.out.println(m);
for(String k:m.keySet()) {
m.remove(k, null);
}
System.out.println(m);
And output is:
{1=One, 2=null}
{1=One}
json.put("method", method); | ||
json.put("params", new JSONObject(payload)); | ||
// FIXME: Use id field to dispatch requests | ||
json.put("id", Integer.toString(this.id++)); |
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.
this.id
isn't thread-safe since it is static
. Use AtomicInteger instead and then call getAndIncrement()
.
Not sure how relevant but in abcore I needed json rpc to talk to Bitcoin core daemon running in the background and I used https://github.com/Polve/bitcoin-rpc-client , maybe it can be adapted? |
@renepickhardt are you planning on moving this library into its own repository? Otherwise I could give it a new home in the |
- Added License Apache 2.0 - Added license and reference to code of the pull request ElementsProject/lightning#2223 by https://github.com/renepickhardt
I have started to create a Java Client Library for c-lightning called
JLightning
which is basically followingpylightning
code.This PR is work in progress. I would like to have some feedback before I continue to finish the work.
null
values.