Skip to content

Commit

Permalink
added null terminator to strings. ficed build again
Browse files Browse the repository at this point in the history
  • Loading branch information
jrhea committed Aug 13, 2019
1 parent a6812c4 commit a2c4c9b
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 38 deletions.
29 changes: 18 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,33 @@ 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
java-examples: java-bindings
@echo ""
@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 $@
@echo ""
@echo Building C bindings
cd $(BIND_DIR) && make $@
@echo ""
@echo Building Rust bindings
cd $(CORE_DIR) && make $@

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

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: 3 additions & 2 deletions bindings/c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ SHELL := /bin/sh
include ../../config.mk

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

.PHONY: c-bindings clean

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

clean:
rm -rf $(OUT_DIR)/libmothra.a
2 changes: 1 addition & 1 deletion bindings/java/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ INCLUDES = -I$(JBIND_DIR)/
CFLAGS = -O2 -fPIC
LFLAGS = -shared

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

java-bindings-ingress:
mkdir -p $(CORE_DIR)/target/release
Expand Down
8 changes: 0 additions & 8 deletions bindings/java/mothra-jni-ingress.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,14 @@ static JavaVM *jvm;

JNIEXPORT void JNICALL Java_mothra_Init(JNIEnv* jenv, jclass jcls)
{
printf("Java_mothra_Init: start\n");
jint rs = (*jenv)->GetJavaVM(jenv, &jvm);
printf("Java_mothra_Init: rs = %i\n",rs);
assert (rs == JNI_OK);
printf("Java_mothra_Init: end\n");
}

void receive_gossip(char* message) {

JNIEnv *jenv;
printf("receive_gossip: before attach\n");
jint rs = (*jvm)->AttachCurrentThread(jvm, (void**)&jenv, NULL);
printf("receive_gossip: rs = %i\n",rs);
printf("receive_gossip: after attach\n");
assert (rs == JNI_OK);
printf("jenv: %p\n",jenv);
if(jenv != NULL) {
jclass mothra_class;
jmethodID receivegossip_method;
Expand Down
11 changes: 5 additions & 6 deletions core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ include ../config.mk
JAVA_INCLUDES = -I$(JAVA_HOME)/include/$(OS) -I$(JAVA_HOME)/include
JAVA_LIBS = -L$(JAVA_HOME)/lib/server/ -ljvm

#c-bindings:
# rm -f $(CORE_DIR)/target/release/libmothra.$(EXT)
# cargo build --release
# $(shell mkdir -p $(OUT_DIR))
# mv $(CORE_DIR)/target/release/libmothra.$(EXT) $(OUT_DIR)/libmothra-c.$(EXT)
# rm $(CORE_DIR)/target/release/libmothra*
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
Expand Down
8 changes: 5 additions & 3 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod mothra_api;
extern crate getopts;
use std::sync::mpsc::{channel,Sender,Receiver};
use std::{thread, time};
use std::ffi::CStr;
use std::ffi::{CStr,CString};
use std::os::raw::{c_char,c_int};
use std::io::Cursor;
use std::convert::TryFrom;
Expand Down Expand Up @@ -58,9 +58,11 @@ pub extern fn libp2p_start(args_c_char: *mut *mut c_char, length: c_int) {
loop{
let network_message = rx2.recv().unwrap();
if network_message.command == "GOSSIP".to_string() {
let message = String::from_utf8(network_message.value).unwrap();
let mut message = String::from_utf8(network_message.value).unwrap();
let mut message_c_str = CString::new(message).unwrap(); //adds null terminator
let mut message_ptr = message_c_str.as_ptr();
unsafe {
receive_gossip(message.as_ptr() as *mut i8);
receive_gossip(message_ptr as *mut i8);
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ SHELL := /bin/sh

include ../config.mk

examples:
#cd c && make $@
c-examples:
cd c && make $@

java-examples:
cd java && make $@

clean:
Expand Down
2 changes: 1 addition & 1 deletion examples/c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ INCLUDES:=-I$(CBIND_DIR)

.PHONY : examples clean

examples:
c-examples:
$(CC) example.c $(INCLUDES) -L$(OUT_DIR)/ -lmothra-c -o $(OUT_DIR)/example

clean:
Expand Down
2 changes: 1 addition & 1 deletion examples/java/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ include ../../config.mk
CLASS_PATH = ./
vpath %.class $(CLASS_PATH)

examples: moveFiles mothra.class example.class
java-examples: moveFiles mothra.class example.class
rm -f mothra.java

%.class:
Expand Down

0 comments on commit a2c4c9b

Please sign in to comment.