Skip to content

Commit

Permalink
implement node_accessor
Browse files Browse the repository at this point in the history
Signed-off-by: Yuji Ito <llamerada.jp@gmail.com>
  • Loading branch information
llamerada-jp committed Apr 7, 2024
1 parent d2173d8 commit cd210d4
Show file tree
Hide file tree
Showing 29 changed files with 4,252 additions and 95 deletions.
30 changes: 30 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
BasedOnStyle: "Google"
AccessModifierOffset: -1
AlignAfterOpenBracket: "AlwaysBreak"
AlignConsecutiveAssignments: true
AllowShortFunctionsOnASingleLine: "None"
AllowShortLambdasOnASingleLine: "Empty"
AlwaysBreakAfterReturnType: "None"
AlwaysBreakTemplateDeclarations: true
BreakConstructorInitializersBeforeComma: true
BreakConstructorInitializers: "AfterColon"
PenaltyReturnTypeOnItsOwnLine: 120
ColumnLimit: 120
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
FixNamespaceComments: true
IncludeBlocks: "Regroup"
IncludeCategories:
- Regex: '^<.*\.h>'
Priority: 1
- Regex: "^<.*"
Priority: 2
- Regex: ".*"
Priority: 3
IndentPPDirectives: "AfterHash"
PointerAlignment: "Left"
SortIncludes: true
SpaceAfterTemplateKeyword: false
SpacesBeforeTrailingComments: 2
UseTab: "Never"
3 changes: 2 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ jobs:
timeout-minutes: 60
strategy:
matrix:
os: [ubuntu-22.04, macos-latest]
# removing macos-latest since libwebrtc is not available on macos
os: [ubuntu-22.04]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v5
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/_obj
/.vscode
/bin
/include
/local
/localhost.crt
/localhost.key
/node_modules
Expand All @@ -11,4 +13,4 @@
/test/dist/tests/*.wasm
/test/dist/wasm_exec.js
/test/test.wasm
/test/wasm_exec.js
/test/wasm_exec.js
43 changes: 40 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,54 @@ SHELL := /bin/bash -o pipefail
PROTOC_VERSION := 25.2
PROTOC_GEN_GO_VERSION := $(shell awk '/google.golang.org\/protobuf/ {print substr($$2, 2)}' go.mod)

# Paths
ROOT_PATH := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
OUTPUT_PATH := $(ROOT_PATH)/output
LOCAL_ENV_PATH := $(ROOT_PATH)/local
WORK_PATH := /tmp/work

# Vserions
# https://github.com/llamerada-jp/libwebrtc
LIBWEBRTC_URL := "https://github.com/llamerada-jp/libwebrtc/releases/download/m119/libwebrtc-119-linux-amd64.tar.gz"
# https://github.com/kazuho/picojson
PICOJSON_VERSION := 1.3.0

# Commands
CURL := curl -sSLf
SUDO := sudo

BINDIR := $(shell pwd)/bin
PROTOC := PATH=$(BINDIR) $(BINDIR)/protoc

all: seed build-js
all: seed build-lib build-js

seed: internal/proto/colonio.pb.go $(wildcard ***.go)
go build -o output/seed ./cmd/seed
go build -o $(OUTPUT_PATH)/seed ./cmd/seed

internal/proto/colonio.pb.go: colonio.proto
$(PROTOC) --go_out=module=github.com/llamerada-jp/colonio:. $<

test/dist/wasm_exec.js: $(shell go env GOROOT)/misc/wasm/wasm_exec.js
cp $< $@

INCLUDE_FLAGS := -I$(LOCAL_ENV_PATH)/include -I$(LOCAL_ENV_PATH)/include/third_party/abseil-cpp
WEBRTC_DEFS := -DWEBRTC_LINUX=1 -DWEBRTC_POSIX=1
CXX_FLAGS := -std=c++17 -fvisibility=hidden -fvisibility-inlines-hidden -Wall $(INCLUDE_FLAGS) $(WEBRTC_DEFS)
build-lib: $(wildcard internal/c/*.cpp) $(wildcard internal/c/*.h) $(wildcard internal/c/*.hpp)
mkdir -p $(WORK_PATH)
$(CXX) -c -o $(WORK_PATH)/webrtc_config.o $(CXX_FLAGS) internal/c/webrtc_config.cpp
$(CXX) -c -o $(WORK_PATH)/webrtc_link.o $(CXX_FLAGS) internal/c/webrtc_link.cpp
rm -f $(OUTPUT_PATH)/libcolonio.a
ar rcs $(OUTPUT_PATH)/libcolonio.a $(WORK_PATH)/webrtc_config.o $(WORK_PATH)/webrtc_link.o

.PHONY: format-code
format-code: internal/proto/colonio.pb.go
go fmt ./...

export COLONIO_TEST_CERT := $(shell pwd)/localhost.crt
export COLONIO_TEST_KEY := $(shell pwd)/localhost.key
.PHONY: test
test: build-test generate-cert test/dist/wasm_exec.js
test: build-lib build-test generate-cert test/dist/wasm_exec.js
go test -v -count=1 ./config/...
go test -v -count=1 ./seed/...
go test -v -count=1 ./internal/...
Expand All @@ -51,13 +73,28 @@ build-js: $(wildcard src/*.ts)

.PHONY: setup
setup:
mkdir -p $(LOCAL_ENV_PATH) $(WORK_PATH)
# tools for typescript
npm install
# protoc
$(CURL) -o protoc.zip https://github.com/protocolbuffers/protobuf/releases/download/v$(PROTOC_VERSION)/protoc-$(PROTOC_VERSION)-linux-x86_64.zip
unzip -o protoc.zip bin/protoc 'include/*'
rm -f protoc.zip
GOBIN=$(BINDIR) go install google.golang.org/protobuf/cmd/protoc-gen-go@v$(PROTOC_GEN_GO_VERSION)
# libwebrtc
cd $(WORK_PATH) \
&& curl -LOS $(LIBWEBRTC_URL) \
&& if [ $(shell uname -s) = "Linux" ]; then \
tar -zx -C $(shell realpath $(LOCAL_ENV_PATH)) -f $(shell basename $(LIBWEBRTC_URL)); \
elif [ $(shell uname -s) = "Darwin" ]; then \
unzip -o -d $(LOCAL_ENV_PATH) $(shell basename $(LIBWEBRTC_URL)); \
fi
# picojson
cd $(WORK_PATH) \
&& $(RM) -r picojson \
&& git clone --depth=1 --branch v$(PICOJSON_VERSION) https://github.com/kazuho/picojson.git \
&& cd picojson \
&& cp picojson.h $(LOCAL_ENV_PATH)/include/

.PHONY: clean
clean:
Expand Down
50 changes: 13 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,32 @@ The purpose of the majority of engineers is to realize their own service, and th
It is interesting but not essence of work to think and implement difficult algorithms.
The purpose of Colonio is to make it more versatile and to make it easy for everyone to use distributed algorithms that are easy to use.

## More information
## Requirement to build

- The status of this project is experimental.
- Please see [website](https://www.colonio.dev/) to get more information.
- [libwebrtc](https://github.com/llamerada-jp/libwebrtc) is a depending library to use WebRTC on native environment.
- Go 1.21+
- gcc 11+
- npm

## How to build the node library

### Build for C/C++ and JavaScript (WebAssembly)
### Pre build library

```console
// for linux using docker
$ make build

// for mac or for linux without docker
$ make setup
$ make build-native build-wasm
```

There is an output file below.

- `output/libcolonio.a`: static link library for C/C++
- `output/lib/*`: depending shared library for C/C++
- `output/colonio.*`: wasm library for JavaScript

### Run test
Colonio using WebRTC via cgo. So you need to build native library before run colonio.
These are not required if you use colonio via WASM.

```console
$ make build WITH_TEST=ON
$ make build-seed
$ make test [CTEST_ARGS=--verbose]
$ make build-lib
```

Flags for build script are below.
Following libraries are required to build the application using colonio.

| option | values | default | description |
| --------------- | ------------------ | --------- | ------------------------------------------ |
| `BUILD_TYPE` | `Release`, `Debug` | `Release` | build type option used as CMAKE_BUILD_TYPE |
| `WITH_COVERAGE` | `ON`, `OFF` | `OFF` | output coverage when run test programs |
| `WITH_GPROF` | `ON`, `OFF` | `OFF` | enable build option for gprof |
| `WITH_SAMPLE` | `ON`, `OFF` | `OFF` | build sample programs |
| `WITH_TEST` | `ON`, `OFF` | `OFF` | build test programs |
- output/libcolonio.a
- local/lib/libwebrtc.a

## How to build a C/C++ program using colonio
### Run test

```console
$ g++ -I<path to colonio>/src \
-L<path to libcolonio> -L<path to colonio>/output \
-lcolonio -lwebrtc -lprotobuf -lpthread -lssl \
<your source code>
$ make test
```

### Seed usage
Expand Down
31 changes: 20 additions & 11 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ type CoordSystem2D struct {
YMax *float64 `json:"yMax,omitempty"`
}

// IceServer is a configuration for ICE server. It is used to establish a WebRTC connection.
type IceServer struct {
Urls []string `json:"urls,omitempty"`
Username *string `json:"username,omitempty"`
Credential *string `json:"credential,omitempty"`
// URLs is a list of URLs of the ICE server.
URLs []string `json:"urls,omitempty"`
// Username is a username for the ICE server.
Username string `json:"username,omitempty"`
// Credential is a credential for the ICE server.
Credential string `json:"credential,omitempty"`
}

type Routing struct {
Expand Down Expand Up @@ -79,13 +83,18 @@ type Cluster struct {
// PollingTimeout is used to determine the timeout of the polling.
// `polling` request for the seed will be returned after the timeout if there is no packet to relay.
// The value should be less than `sessionTimeout`.
PollingTimeout time.Duration `json:"-"`
NodeAccessor *NodeAccessor `json:"nodeAccessor,omitempty"`
CoordSystem2d *CoordSystem2D `json:"coordSystem2D,omitempty"`
IceServers []IceServer `json:"iceServers,omitempty"`
Routing Routing `json:"routing"`
Kvs *Kvs `json:"kvs,omitempty"`
Spread *Spread `json:"spread,omitempty"`
PollingTimeout time.Duration `json:"-"`

// IceServers is a list of ICE servers.
// In Colonio, multiple ICE servers can be used to establish a WebRTC connection.
// Each entry contains the URLs of the ICE server, the username, and the credential.
IceServers []IceServer `json:"iceServers,omitempty"`

NodeAccessor *NodeAccessor `json:"nodeAccessor,omitempty"`
CoordSystem2d *CoordSystem2D `json:"coordSystem2D,omitempty"`
Routing Routing `json:"routing"`
Kvs *Kvs `json:"kvs,omitempty"`
Spread *Spread `json:"spread,omitempty"`
}

func (c *Cluster) MarshalJSON() ([]byte, error) {
Expand Down Expand Up @@ -131,7 +140,7 @@ func (c *Cluster) UnmarshalJSON(b []byte) error {

func (c *Cluster) Validate() error {
if c.IceServers == nil || len(c.IceServers) == 0 {
return errors.New("config value of `node.iceServers` required")
return errors.New("config value of `iceServers` required")
}

if c.CoordSystem2d == nil && c.Spread != nil {
Expand Down
39 changes: 39 additions & 0 deletions internal/c/webrtc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2017- Yuji Ito <llamerada.jp@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

void webrtc_config_init();
unsigned int webrtc_config_new(const char* ice, int ice_len);
void webrtc_config_destruct(unsigned int id);

void webrtc_link_get_error_message(const char** message, int* message_len);
void webrtc_link_init(
void (*update_state_cb)(unsigned int, int), void (*update_ice_cb)(unsigned int, const void*, int),
void (*receive_data_cb)(unsigned int, const void*, int), void (*error_cb)(unsigned int, const char*, int));
unsigned int webrtc_link_new(unsigned int config_id, int create_data_channel);
int webrtc_link_disconnect(unsigned int id);
int webrtc_link_get_local_sdp(unsigned int id, const char** sdp, int* sdp_len);
int webrtc_link_set_remote_sdp(unsigned int id, const char* sdp, int sdp_len);
int webrtc_link_update_ice(unsigned int id, const char* ice, int ice_len);
int webrtc_link_send(unsigned int id, const void* data, int data_len);

#ifdef __cplusplus
}
#endif
Loading

0 comments on commit cd210d4

Please sign in to comment.