Skip to content

Commit

Permalink
Project restructuring. Java code is now for example purposes. Made bi…
Browse files Browse the repository at this point in the history
…ndings folder for all Makefile and code related to langage bindings
  • Loading branch information
jrhea committed Dec 9, 2018
1 parent 73b9525 commit a18dc15
Show file tree
Hide file tree
Showing 18 changed files with 460 additions and 347 deletions.
54 changes: 9 additions & 45 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,52 +1,20 @@
SHELL := /bin/sh
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

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 = //')
JAVA_INCLUDES = -I$(JAVA_HOME)/include/$(OS) -I$(JAVA_HOME)/include
CLASS_PATH = .
vpath %.class $(CLASS_PATH)

DDIR = p2pd
CDIR = p2pc
JDIR = p2pclient/java
DNAME = p2pd
CNAME = p2pc
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 java-daemon java-client

java-daemon: lib$(DNAME).$(EXT)

java-client: lib$(CNAME).$(EXT)

lib%.$(EXT): java-%.o go-%.a
$(CC) $(LFLAGS) -o $(JDIR)/$@ $(JDIR)/$(firstword $^) $(JDIR)/$(lastword $^)

java-%.o: go-%.a java-%.h %.class
$(CC) $(CFLAGS) -c $(JDIR)/java-$*.c $(JAVA_INCLUDES) -o $(JDIR)/$@
all: deps go-daemon go-client go-bindings java-daemon java-client

go-%.a:
go build -o $(JDIR)/$@ -buildmode=c-archive $*/main.go
java-daemon:
cd $(BDIR) && make $@

java-%.h:
cd $(JDIR) && javac -h $(CLASS_PATH) $*.java && mv $*.h $@
java-client:
cd $(BDIR) && make $@

%.class:
cd $(JDIR) && javac $*.java
go-bindings:
cd $(BDIR) && make $@

go-client:
cd $(CDIR) && go install ./...
Expand All @@ -64,9 +32,5 @@ gx:

clean:
gx-go uw
rm -f $(JDIR)/*.o \
&& rm -f $(JDIR)/*.a \
&& rm -f $(JDIR)/*.$(EXT) \
&& rm -f $(JDIR)/*.class \
&& rm -f $(JDIR)/*.h
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
File renamed without changes.
File renamed without changes.
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
3 changes: 1 addition & 2 deletions p2pclient/java/java-p2pc.c → bindings/java/java-p2pc.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <jni.h>
#include "java-p2pc.h"
#include "go-p2pc.h"
#include "../go-p2pc.h"

JNIEXPORT void JNICALL Java_p2pc_startClient (JNIEnv *jenv, jclass jcls, jstring jarg1){
char *arg1 = (char *) 0 ;
Expand Down
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
3 changes: 1 addition & 2 deletions p2pclient/java/java-p2pd.c → bindings/java/java-p2pd.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <jni.h>
#include "java-p2pd.h"
#include "go-p2pd.h"
#include "../go-p2pd.h"

JNIEXPORT void JNICALL Java_p2pd_startDaemon (JNIEnv *jenv, jclass jcls, jstring jarg1){
char *arg1 = (char *) 0 ;
Expand Down
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
135 changes: 5 additions & 130 deletions p2pc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,145 +2,20 @@ package main

import "C"
import (
"flag"
"fmt"
"io"
"log"
"os"
"strings"

peer "github.com/libp2p/go-libp2p-peer"
identify "github.com/libp2p/go-libp2p/p2p/protocol/identify"
multiaddr "github.com/multiformats/go-multiaddr"

"github.com/libp2p/go-libp2p-daemon/p2pclient/go"
)

// ClientConfig defines the configuration options
type ClientConfig struct {
pathd *string
pathc *string
command *string
args []string
}

type Command int

const (
Identify Command = 0
Connect Command = 1
ListenForMessage Command = 2
SendMessage Command = 3
p2pc "github.com/libp2p/go-libp2p-daemon/p2pclient"
)

func (c Command) String() string {
commands := [...]string{
"Identify",
"Connect",
"ListenForMessage",
"SendMessage",
}
return commands[c]
}

func main() {
identify.ClientVersion = "p2pc/0.1"
config := initialize()
start(config)
}

func initialize() ClientConfig {
config := ClientConfig{
pathd: flag.String("pathd", "/tmp/p2pd.sock", "daemon control socket path"),
pathc: flag.String("pathc", "/tmp/p2pc.sock", "client control socket path"),
command: flag.String("command", "Identify", "command to send to the daemon"),
}
flag.Parse()
config.args = flag.Args()
// delete control socket if it already exists
if _, err := os.Stat(*config.pathc); !os.IsNotExist(err) {
err = os.Remove(*config.pathc)
if err != nil {
log.Fatal(err)
}
}
return config
}

func start(config ClientConfig) {

client, err := p2pclient.NewClient(*config.pathd, *config.pathc)
defer os.Remove(*config.pathc)

if err != nil {
log.Fatal(err)
}

switch *config.command {

case Identify.String():
id, addrs, err := client.Identify()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Daemon ID: %s\n", id.Pretty())
fmt.Printf("Peer addresses: %v\n", addrs)

case Connect.String():
id, err := peer.IDB58Decode(config.args[0])
var addrs []multiaddr.Multiaddr
addrs = make([]multiaddr.Multiaddr, len(config.args[1:]))
for i, arg := range config.args[1:] {
addr, _ := multiaddr.NewMultiaddr(arg)
addrs[i] = addr
}
err = client.Connect(id, addrs)
if err != nil {
log.Fatal(err)
}

pi, err := client.FindPeer(id)
fmt.Printf("ID: %s has multiaddr: %v", pi.ID, pi.Addrs)

case ListenForMessage.String():
protos := []string{"/test"}
done := make(chan struct{})
client.NewStreamHandler(protos, func(info *p2pclient.StreamInfo, conn io.ReadWriteCloser) {
defer conn.Close()
buf := make([]byte, 1024)
_, err := conn.Read(buf)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(buf))
done <- struct{}{}
})
select {}

case SendMessage.String():
protos := []string{"/test"}
recipientID, err := peer.IDB58Decode(config.args[0])
_, conn, err := client.NewStream(recipientID, protos)
if err != nil {
log.Fatal(err)
}
_, err = conn.Write([]byte(config.args[1]))
if err != nil {
log.Fatal(err)
}

default:

}
config := p2pc.Initialize()
p2pc.Start(config)
}

//export startClient
func startClient(args *C.char) {
//replace default config options with configs passed from external source
argsGoString := C.GoString(args)
argsArray := strings.Split(argsGoString, "|")
os.Args = argsArray
//call initialize() to get config
config := initialize()
start(config)
config := p2pc.ProcessArgs(&argsGoString)
p2pc.Start(config)
}
File renamed without changes.
Loading

0 comments on commit a18dc15

Please sign in to comment.