Skip to content

Commit

Permalink
Merge pull request #1 from jrhea/feat-create_vm
Browse files Browse the repository at this point in the history
jni working for ingress
  • Loading branch information
jrhea authored Aug 13, 2019
2 parents aa172eb + a2c4c9b commit 2267f37
Show file tree
Hide file tree
Showing 23 changed files with 204 additions and 72 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
**/.DS_Store
*.code-workspace
*.vscode
jni.h
39 changes: 27 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,49 @@ SHELL := /bin/sh

include config.mk

.PHONY:=all examples bindings c-bindings java-bindings rust clean
.PHONY:=c java c-bindings java-bindings c-examples java-examples clean

all: examples bindings
c-mash: clean c

mash: clean all
java-mash: clean java

examples: bindings
c: c-examples

java: java-examples

c-examples: c-bindings
@echo ""
@echo Building examples
cd $(EXAMPLES_DIR) && make $@

bindings: c-bindings java-bindings

c-bindings: rust
java-examples: java-bindings
@echo ""
@echo Building C bindings
cd $(BIND_DIR) && make $@
@echo Building examples
cd $(EXAMPLES_DIR) && make $@

c-bindings:
@echo ""
@echo Building C bindings
cd $(BIND_DIR) && make $@
@echo ""
@echo Building Rust bindings
cd $(CORE_DIR) && make $@

java-bindings: rust
java-bindings: java-bindings-ingress java-bindings-egress

java-bindings-ingress:
@echo ""
@echo Building Java bindings
cd $(BIND_DIR) && make $@

rust:
@echo ""
@echo Building Rust bindings
cd $(CORE_DIR) && make $@

java-bindings-egress:
@echo ""
@echo Building Java bindings
cd $(BIND_DIR) && make $@

clean:
cd $(CORE_DIR) && make $@
cd $(BIND_DIR) && make $@
Expand Down
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,23 @@ Building is easy. First, clone the repo:

```

#### Build for C

Next cd into the project's root dir and build:

```sh

> make c

```

#### Build for Java

Next cd into the project's root dir and build:

```sh

> make all
> make java

```

Expand All @@ -79,7 +91,7 @@ Here is a screenshot of the sample app in action:
![demo](./resources/demo.jpeg)


#### Run sample app (C)
#### Run Sample App (C)

The sample app demonstrates two clients using Disv5 to find each other and the use of GossipSub to send messages back and forth.

Expand Down Expand Up @@ -109,7 +121,7 @@ In a second terminal run:

```

#### Run sample app (Java)
#### Run Sample App (Java)

The sample app demonstrates two clients using Disv5 to find each other and the use of GossipSub to send messages back and forth.

Expand Down
5 changes: 4 additions & 1 deletion bindings/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ SHELL := /bin/sh

include ../config.mk

java-bindings:
java-bindings-ingress:
cd $(JBIND_DIR) && make $@

java-bindings-egress:
cd $(JBIND_DIR) && make $@

c-bindings:
Expand Down
15 changes: 11 additions & 4 deletions bindings/c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ SHELL := /bin/sh

include ../../config.mk

.PHONY : examples clean
CFLAGS:=-O2 -fPIC
LFLAGS = -shared
INCLUDES = -I$(CBIND_DIR)/
TARGET:=$(CORE_DIR)/target/release/libmothra-ingress.$(EXT)

c-bindings:
@echo C bindings are header only files
.PHONY: c-bindings clean

c-bindings:
mkdir -p $(CORE_DIR)/target/release
rm -f $(TARGET)
$(CC) $(INCLUDES) $(CBIND_DIR)/mothra.c $(CFLAGS) $(LFLAGS) -o $(TARGET)

clean:
rm -rf $(OUT_DIR)/libmothra.$(EXT)
rm -rf $(OUT_DIR)/libmothra.a
6 changes: 6 additions & 0 deletions bindings/c/mothra.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>
#include "mothra.h"

void receive_gossip(char* message) {
printf("C: received this message from another peer - %s\n",message);
}
9 changes: 6 additions & 3 deletions bindings/c/mothra.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
extern "C" {
#endif

void libp2p_start(char**, int length);
void libp2p_send_gossip(char*);
extern void libp2p_start(char**, int length);
extern void libp2p_send_gossip(char*);

void receive_gossip(char*);


#ifdef __cplusplus
}
#endif

#endif // _MOTHRA_JNI_H_
#endif // _MOTHRA_H_
16 changes: 11 additions & 5 deletions bindings/java/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@ SHELL := /bin/sh
include ../../config.mk

JAVA_INCLUDES = -I$(JAVA_HOME)/include/$(OS) -I$(JAVA_HOME)/include
JAVA_LIBS = -L$(JAVA_HOME)/lib/server -ljvm
INCLUDES = -I$(JBIND_DIR)/
CFLAGS = -O2 -fPIC
LFLAGS = -shared
TARGET:=$(OUT_DIR)/libmothra-java.$(EXT)

.PHONY : java-bindings clean
.PHONY : java-bindings-ingress java-bindings-egress clean

java-bindings:
java-bindings-ingress:
mkdir -p $(CORE_DIR)/target/release
rm -f $(CORE_DIR)/target/release/*.*
$(CC) $(JBIND_DIR)/mothra-jni-ingress.c $(INCLUDES) $(JAVA_INCLUDES) $(JAVA_LIBS) $(CFLAGS) $(LFLAGS) -o $(CORE_DIR)/target/release/libmothra-ingress.$(EXT)

java-bindings-egress:
$(shell mkdir -p $(OUT_DIR))
$(CC) *.c $(JAVA_INCLUDES) -L$(CORE_DIR)/target/release -lmothra $(CFLAGS) $(LFLAGS) -o $(TARGET)
$(CC) $(JBIND_DIR)/mothra-jni-egress.c $(INCLUDES) -L$(OUT_DIR) -lmothra-java $(JAVA_INCLUDES) $(JAVA_LIBS) $(CFLAGS) $(LFLAGS) -o $(OUT_DIR)/libmothra-egress.$(EXT)

clean:
rm -rf $(OUT_DIR)/mothra*
rm -rf $(OUT_DIR)/mothra*
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "mothra-jni.h"
#include "../c/mothra.h"
#include <assert.h>
#include "mothra-jni-egress.h"
#include "mothra-jni-ingress.h"

JNIEXPORT void JNICALL Java_mothra_Start (JNIEnv *jenv, jclass jcls, jobjectArray jargs){
int length = (*jenv)->GetArrayLength(jenv, jargs);
Expand Down
19 changes: 19 additions & 0 deletions bindings/java/mothra-jni-egress.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <jni.h>
#ifndef _MOTHRA_JNI_EGRESS_H_
#define _MOTHRA_JNI_EGRESS_H_

#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT void JNICALL Java_mothra_Start(JNIEnv *, jclass, jobjectArray);
JNIEXPORT void JNICALL Java_mothra_SendGossip(JNIEnv *, jclass, jstring);

extern void libp2p_start(char**, int);
extern void libp2p_send_gossip(char*);

#ifdef __cplusplus
}
#endif

#endif // _MOTHRA_JNI_EGRESS_H_
44 changes: 44 additions & 0 deletions bindings/java/mothra-jni-ingress.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "mothra-jni-ingress.h"

static JavaVM *jvm;

JNIEXPORT void JNICALL Java_mothra_Init(JNIEnv* jenv, jclass jcls)
{
jint rs = (*jenv)->GetJavaVM(jenv, &jvm);
assert (rs == JNI_OK);
}

void receive_gossip(char* message) {
JNIEnv *jenv;
jint rs = (*jvm)->AttachCurrentThread(jvm, (void**)&jenv, NULL);
assert (rs == JNI_OK);
if(jenv != NULL) {
jclass mothra_class;
jmethodID receivegossip_method;
jstring jmessage;
mothra_class = (*jenv)->FindClass(jenv, "mothra");
if(!mothra_class){
detach(jenv);
}
jmessage = (*jenv)->NewStringUTF(jenv, message);
if(!jmessage){
detach(jenv);
}
receivegossip_method = (*jenv)->GetStaticMethodID(jenv, mothra_class, "ReceiveGossip", "(Ljava/lang/String;)V");
if(!receivegossip_method){
detach(jenv);
}
(*jenv)->CallStaticVoidMethod(jenv, mothra_class, receivegossip_method, jmessage);
}
}

void detach(JNIEnv* jenv){
if((*jenv)->ExceptionOccurred(jenv)) {
(*jenv)->ExceptionDescribe(jenv);
}
(*jvm)->DetachCurrentThread(jvm);
}
19 changes: 19 additions & 0 deletions bindings/java/mothra-jni-ingress.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <jni.h>
#ifndef _MOTHRA_JNI_INGRESS_H_
#define _MOTHRA_JNI_INGRESS_H_

#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT void JNICALL Java_mothra_Init(JNIEnv*,jclass);

void receive_gossip(char*);

void detach(JNIEnv* );

#ifdef __cplusplus
}
#endif

#endif // _MOTHRA_JNI_INGRESS_H_
16 changes: 0 additions & 16 deletions bindings/java/mothra-jni.h

This file was deleted.

7 changes: 6 additions & 1 deletion examples/java/mothra.java → bindings/java/mothra.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@

public class mothra {
public static final String NAME = "mothra-java";
public static final String NAME = "mothra-egress";
public static native void Init();
public static native void Start(String[] args);
public static native void SendGossip(String message);
public static void ReceiveGossip(String message){
System.out.println("Java: received this message from another peer - " + message);
}
static {
try {
System.loadLibrary ( NAME ) ;
Expand Down
1 change: 0 additions & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ version = "0.0.1"
authors = ["Jonny Rhea <jonny.rhea@consensys.net>"]
edition = "2018"
build = "build.rs"
links = "libp2p_receive_gossip"
include = [
"**/*.rs",
"Cargo.toml"
Expand Down
21 changes: 13 additions & 8 deletions core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ SHELL := /bin/sh

include ../config.mk

rust: c
rm -f $(CORE_DIR)/target/release/*.$(EXT)
cargo build --release
cp $(CORE_DIR)/target/release/*.$(EXT) $(OUT_DIR)/
JAVA_INCLUDES = -I$(JAVA_HOME)/include/$(OS) -I$(JAVA_HOME)/include
JAVA_LIBS = -L$(JAVA_HOME)/lib/server/ -ljvm

c:
mkdir -p $(CORE_DIR)/target/release
rm -f $(CORE_DIR)/target/release/liblibp2p_receive_gossip.a
$(CC) -c $(CORE_DIR)/src/c/libp2p_receive_gossip.c -o $(CORE_DIR)/target/release/liblibp2p_receive_gossip.a
c-bindings:
cargo build --release
$(shell mkdir -p $(OUT_DIR))
mv $(CORE_DIR)/target/release/libmothra.$(EXT) $(CORE_DIR)/target/release/libmothra-c.$(EXT)
cp $(CORE_DIR)/target/release/*.* $(OUT_DIR)/

java-bindings-ingress:
#RUSTFLAGS="-C link-args=-Wl,-rpath,/Library/Java/JavaVirtualMachines/openjdk-11.0.1.jdk/Contents/Home/lib/server" cargo build --release
cargo build --release
$(shell mkdir -p $(OUT_DIR))
mv $(CORE_DIR)/target/release/libmothra.$(EXT) $(CORE_DIR)/target/release/libmothra-java.$(EXT)
cp $(CORE_DIR)/target/release/*.* $(OUT_DIR)/
clean:
cargo clean
2 changes: 1 addition & 1 deletion core/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ fn main()
let project_dir = env::var("CARGO_MANIFEST_DIR").unwrap();

println!("cargo:rustc-link-search={}/target/release/", project_dir); // the "-L" flag
println!("cargo:rustc-link-lib=libp2p_receive_gossip"); // the "-l" flag
println!("cargo:rustc-link-lib=mothra-ingress"); // the "-l" flag
}
5 changes: 0 additions & 5 deletions core/src/c/libp2p_receive_gossip.c

This file was deleted.

Loading

0 comments on commit 2267f37

Please sign in to comment.