-
Notifications
You must be signed in to change notification settings - Fork 36
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
shared object - ref impl with java/jni #28
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a14606b
- added build option for an .so and generation of jni source for java…
jrhea 9543b11
-split out client into a sep exe called p2pc
jrhea 8a90bc7
- added c scaffolding code for java client. added sep targets for cli…
jrhea 7b2356f
- build now works on linux
jrhea 73b9525
using /bin/sh in Makefile and removed printf in favor of Debugf in da…
jrhea a18dc15
Project restructuring. Java code is now for example purposes. Made …
jrhea aa4e06c
fixed package references to p2pclient in tests
jrhea 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
*.a | ||
*.jnilib | ||
*.o | ||
*.class | ||
|
||
# Test binary, build with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
#misc | ||
p2pclient/java/libp2pd.h | ||
p2pclient/java/p2pd.h |
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 |
---|---|---|
@@ -1,10 +1,36 @@ | ||
bin: deps | ||
go install ./... | ||
SHELL := /bin/sh | ||
|
||
gx: | ||
go get github.com/whyrusleeping/gx | ||
go get github.com/whyrusleeping/gx-go | ||
include config.mk | ||
|
||
.PHONY : all java-daemon java-client go-client go-daemon deps gx clean | ||
.DEFAULT_GOAL : go-daemon | ||
|
||
all: deps go-daemon go-client go-bindings java-daemon java-client | ||
|
||
java-daemon: | ||
cd $(BDIR) && make $@ | ||
|
||
java-client: | ||
cd $(BDIR) && make $@ | ||
|
||
go-bindings: | ||
cd $(BDIR) && make $@ | ||
|
||
go-client: | ||
cd $(CDIR) && go install ./... | ||
|
||
go-daemon: | ||
cd $(DDIR) && go install ./... | ||
|
||
deps: gx | ||
gx --verbose install --global | ||
gx-go rewrite | ||
|
||
gx: | ||
go get github.com/whyrusleeping/gx | ||
go get github.com/whyrusleeping/gx-go | ||
|
||
clean: | ||
gx-go uw | ||
cd $(BDIR) && make $@ | ||
|
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,46 @@ | ||
SHELL := /bin/sh | ||
|
||
include ../config.mk | ||
|
||
CC = gcc | ||
CFLAGS = -O2 -fPIC | ||
LFLAGS = $(OS_LFLAGS) -shared | ||
|
||
JAVA_HOME = $(shell java -XshowSettings:properties -version 2>&1 > /dev/null | grep 'java.home' | sed 's/\s*java.home = //' | sed 's/\/jre//') | ||
JAVA_INCLUDES = -I$(JAVA_HOME)/include/$(OS) -I$(JAVA_HOME)/include | ||
CLASS_PATH = . | ||
vpath %.class $(CLASS_PATH) | ||
|
||
DNAME = p2pd | ||
CNAME = p2pc | ||
|
||
.PHONY : java-daemon java-client go-bindings clean | ||
|
||
java-daemon: lib$(DNAME).$(EXT) $(DNAME).class | ||
|
||
java-client: lib$(CNAME).$(EXT) $(CNAME).class | ||
|
||
go-bindings: java-$(DNAME).o java-$(CNAME).o go-p2p.a | ||
$(CC) $(LFLAGS) -o libp2p.$(EXT) $^ | ||
|
||
lib%.$(EXT): java-%.o go-%.a | ||
$(CC) $(LFLAGS) -o $@ $^ | ||
|
||
java-%.o: go-%.a | ||
$(CC) $(CFLAGS) -c java/java-$*.c $(JAVA_INCLUDES) -o $@ | ||
|
||
go-p2p.a: | ||
go build -o $@ -buildmode=c-archive main.go | ||
|
||
go-%.a: | ||
go build -o $@ -buildmode=c-archive ../$*/main.go | ||
|
||
%.class: | ||
cd java/examples && javac $*.java && mv $@ ../../$@ | ||
|
||
clean: | ||
rm -f *.o \ | ||
&& rm -f *.a \ | ||
&& rm -f *.$(EXT) \ | ||
&& rm -f *.class \ | ||
&& rm -f *.h |
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 @@ | ||
public class p2pc { | ||
private static final String NAME = "p2pc"; | ||
public static native void startClient(String arg1); | ||
static { | ||
try { | ||
|
||
System.loadLibrary ( NAME ) ; | ||
|
||
} catch (UnsatisfiedLinkError e) { | ||
System.err.println("Native code library failed to load.\n" + e); | ||
System.exit(1); | ||
} | ||
} | ||
public static void main(String[] args) { | ||
String parsedArgs = NAME; | ||
if( args.length > 0 ){ | ||
parsedArgs += "|" + String.join("|", args); | ||
} | ||
startClient(parsedArgs); | ||
} | ||
} |
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,22 @@ | ||
public class p2pd { | ||
private static final String NAME = "p2pd"; | ||
public static native void startDaemon(String arg1); | ||
public static native void stopDaemon(); | ||
static { | ||
try { | ||
|
||
System.loadLibrary ( NAME ) ; | ||
|
||
} catch (UnsatisfiedLinkError e) { | ||
System.err.println("Native code library failed to load.\n" + e); | ||
System.exit(1); | ||
} | ||
} | ||
public static void main(String[] args) { | ||
String parsedArgs = NAME; | ||
if( args.length > 0 ){ | ||
parsedArgs += "|" + String.join("|", args); | ||
} | ||
startDaemon(parsedArgs); | ||
} | ||
} |
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,9 @@ | ||
#!/bin/sh | ||
|
||
tmux new-session -d -s foo 'p2pd' | ||
tmux split-window -v -t 0 'cd ../../ && java p2pd --sock=/tmp/p2pd2.sock' | ||
tmux split-window -h 'sleep 1 && cd ../../ && java p2pc --pathc=/tmp/p2c2.sock --pathd=/tmp/p2pd2.sock --command=ListenForMessage' | ||
tmux split-window -v -t 1 '/bin/bash' | ||
tmux select-layout tile | ||
tmux rename-window 'the dude abides' | ||
tmux attach-session -d |
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,15 @@ | ||
#include "java-p2pc.h" | ||
#include "../go-p2pc.h" | ||
|
||
JNIEXPORT void JNICALL Java_p2pc_startClient (JNIEnv *jenv, jclass jcls, jstring jarg1){ | ||
char *arg1 = (char *) 0 ; | ||
(void)jenv; | ||
(void)jcls; | ||
arg1 = 0; | ||
if (jarg1) { | ||
arg1 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg1, 0); | ||
if (!arg1) return ; | ||
} | ||
startClient(arg1); | ||
if (arg1) (*jenv)->ReleaseStringUTFChars(jenv, jarg1, (const char *)arg1); | ||
} |
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,14 @@ | ||
#include <jni.h> | ||
|
||
#ifndef _Included_p2pc | ||
#define _Included_p2pc | ||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
JNIEXPORT void JNICALL Java_p2pc_startClient (JNIEnv *, jclass, jstring); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
#endif |
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,19 @@ | ||
#include "java-p2pd.h" | ||
#include "../go-p2pd.h" | ||
|
||
JNIEXPORT void JNICALL Java_p2pd_startDaemon (JNIEnv *jenv, jclass jcls, jstring jarg1){ | ||
char *arg1 = (char *) 0 ; | ||
(void)jenv; | ||
(void)jcls; | ||
arg1 = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
if (jarg1) { | ||
arg1 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg1, 0); | ||
if (!arg1) return ; | ||
} | ||
startDaemon(arg1); | ||
if (arg1) (*jenv)->ReleaseStringUTFChars(jenv, jarg1, (const char *)arg1); | ||
} | ||
|
||
JNIEXPORT void JNICALL Java_p2pd_stopDaemon (JNIEnv *jenv, jclass jcls){ | ||
stopDaemon(); | ||
} |
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,16 @@ | ||
#include <jni.h> | ||
|
||
#ifndef _Included_p2pd | ||
#define _Included_p2pd | ||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
JNIEXPORT void JNICALL Java_p2pd_startDaemon (JNIEnv *, jclass, jstring); | ||
|
||
JNIEXPORT void JNICALL Java_p2pd_stopDaemon (JNIEnv *, jclass); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
#endif |
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,29 @@ | ||
package main | ||
|
||
import "C" | ||
import ( | ||
p2pd "github.com/libp2p/go-libp2p-daemon" | ||
p2pc "github.com/libp2p/go-libp2p-daemon/p2pclient" | ||
) | ||
|
||
func main() { | ||
} | ||
|
||
//export startClient | ||
func startClient(args *C.char) { | ||
argsGoString := C.GoString(args) | ||
config := p2pc.ProcessArgs(&argsGoString) | ||
p2pc.Start(config) | ||
} | ||
|
||
//export startDaemon | ||
func startDaemon(args *C.char) { | ||
argsGoString := C.GoString(args) | ||
config := p2pd.ProcessArgs(&argsGoString) | ||
p2pd.Start(config) | ||
} | ||
|
||
//export stopDaemon | ||
func stopDaemon() { | ||
p2pd.Stop() | ||
} |
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,13 @@ | ||
OS = $(shell uname -s | tr '[:upper:]' '[:lower:]') | ||
|
||
ifeq ($(OS), linux) | ||
EXT = so | ||
OS_LFLAGS = | ||
else ifeq ($(OS), darwin) | ||
EXT = dylib | ||
OS_LFLAGS = -mmacosx-version-min=$(shell defaults read loginwindow SystemVersionStampAsString) -framework CoreFoundation -framework Security | ||
endif | ||
|
||
DDIR = p2pd | ||
CDIR = p2pc | ||
BDIR = bindings |
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 |
---|---|---|
|
@@ -4,7 +4,10 @@ import ( | |
"context" | ||
"fmt" | ||
"net" | ||
"os" | ||
"os/signal" | ||
"sync" | ||
"syscall" | ||
|
||
logging "github.com/ipfs/go-log" | ||
libp2p "github.com/libp2p/go-libp2p" | ||
|
@@ -54,6 +57,15 @@ func NewDaemon(ctx context.Context, path string, opts ...libp2p.Option) (*Daemon | |
|
||
go d.listen() | ||
|
||
sigc := make(chan os.Signal, 1) | ||
signal.Notify(sigc, os.Interrupt, syscall.SIGTERM) | ||
jrhea marked this conversation as resolved.
Show resolved
Hide resolved
|
||
go func(ln net.Listener, c chan os.Signal) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh this is great! |
||
sig := <-c | ||
log.Debugf("Caught signal %s: shutting down.\n", sig) | ||
ln.Close() | ||
os.Exit(0) | ||
}(d.listener, sigc) | ||
|
||
return d, nil | ||
} | ||
|
||
|
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 main | ||
|
||
import "C" | ||
import ( | ||
identify "github.com/libp2p/go-libp2p/p2p/protocol/identify" | ||
|
||
p2pc "github.com/libp2p/go-libp2p-daemon/p2pclient" | ||
) | ||
|
||
func main() { | ||
identify.ClientVersion = "p2pc/0.1" | ||
config := p2pc.Initialize() | ||
p2pc.Start(config) | ||
} | ||
|
||
//export startClient | ||
func startClient(args *C.char) { | ||
jrhea marked this conversation as resolved.
Show resolved
Hide resolved
|
||
argsGoString := C.GoString(args) | ||
config := p2pc.ProcessArgs(&argsGoString) | ||
p2pc.Start(config) | ||
} |
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.
not really sure i understand what's going on here haha. could the preceding lines of this block not be simplified to