Skip to content
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
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .gitignore
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
36 changes: 31 additions & 5 deletions Makefile
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 $@

46 changes: 46 additions & 0 deletions bindings/Makefile
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
21 changes: 21 additions & 0 deletions bindings/java/examples/p2pc.java
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);
}
}
22 changes: 22 additions & 0 deletions bindings/java/examples/p2pd.java
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);
}
}
9 changes: 9 additions & 0 deletions bindings/java/examples/peerDemo.sh
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
15 changes: 15 additions & 0 deletions bindings/java/java-p2pc.c
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;
Copy link
Contributor

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

char *arg1 = NULL;

if (jarg1) {
arg1 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg1, 0);
if (!arg1) return ;
}
startClient(arg1);
if (arg1) (*jenv)->ReleaseStringUTFChars(jenv, jarg1, (const char *)arg1);
}
14 changes: 14 additions & 0 deletions bindings/java/java-p2pc.h
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
19 changes: 19 additions & 0 deletions bindings/java/java-p2pd.c
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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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();
}
16 changes: 16 additions & 0 deletions bindings/java/java-p2pd.h
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
29 changes: 29 additions & 0 deletions bindings/main.go
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()
}
13 changes: 13 additions & 0 deletions config.mk
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
12 changes: 12 additions & 0 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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
}

Expand Down
21 changes: 21 additions & 0 deletions p2pc/main.go
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)
}
Loading