Skip to content

Commit

Permalink
feat(chunk): first implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Godefroy Ponsinet <godefroy.ponsinet@outlook.com>
  • Loading branch information
90dy committed Jan 30, 2019
1 parent 7981142 commit b2cbee9
Show file tree
Hide file tree
Showing 27 changed files with 1,331 additions and 216 deletions.
Expand Up @@ -23,12 +23,20 @@ public CoreModule(ReactApplicationContext reactContext) {
super(reactContext);
this.filesDir = reactContext.getFilesDir().getAbsolutePath();

String storagePath = reactContext.getFilesDir().getAbsolutePath();
try {
Core.getDeviceInfo().setStoragePath(storagePath);
} catch (Exception error) {
logger.format(Level.ERROR, this.getName(), error.getMessage());
}
this.notificationDriver.setNative(new NotificationNative());

// TODO: Get rid of this and make a proper react-native module that extends ReactContextBaseJavaModule
// See https://facebook.github.io/react-native/docs/native-modules-android
Object activityAndContextGetter = actGetter(reactContext);

BleManager.setReactGetter(activityAndContextGetter, reactContext);
this.notificationDriver.setNative(new NotificationNative());

}

private Object actGetter(final ReactApplicationContext reactContext) {
Expand All @@ -51,7 +59,7 @@ public String getName() {
@ReactMethod
public void listAccounts(Promise promise) {
try {
String data = Core.listAccounts(this.filesDir);
String data = Core.listAccounts();
promise.resolve(data);
} catch (Exception err) {
this.logger.format(Level.ERROR, this.getName(), "Unable to list accounts: %s", err);
Expand All @@ -62,7 +70,7 @@ public void listAccounts(Promise promise) {
@ReactMethod
public void initialize(Promise promise) {
try {
Core.initialize(this.logger, this.filesDir);
Core.initialize(this.logger, Core.getDeviceInfo().getStoragePath());
promise.resolve(null);
} catch (Exception err) {
this.logger.format(Level.ERROR, this.getName(), "Unable to init core: %s", err);
Expand All @@ -76,7 +84,6 @@ public void start(String nickname, Promise promise) {
try {
core.MobileOptions coreOptions = new core.MobileOptions()
.withNickname(nickname)
.withDatastorePath(this.filesDir)
.withLoggerDriver(this.logger);

Core.start(coreOptions);
Expand Down Expand Up @@ -112,7 +119,7 @@ public void throwException() throws Exception {
@ReactMethod
public void dropDatabase(Promise promise) {
try {
Core.dropDatabase(this.filesDir);
Core.dropDatabase();
promise.resolve(null);
} catch (Exception err) {
this.logger.format(Level.ERROR, this.getName(), "Unable to drop database: %s", err);
Expand Down
16 changes: 9 additions & 7 deletions client/react-native/gomobile/core/core.go
Expand Up @@ -22,6 +22,8 @@ var (
NotificationDriver = MobileNotification{}.New()
)

// Setup call it at first native start

func logger() *zap.Logger {
return zap.L().Named(defaultLoggerName)
}
Expand Down Expand Up @@ -81,18 +83,18 @@ func Initialize(loggerNative NativeLogger, datastorePath string) error {
return nil
}

func ListAccounts(datastorePath string) (string, error) {
func ListAccounts() (string, error) {
defer panicHandler()

accounts, err := account.List(rootContext, datastorePath)
accounts, err := account.List(rootContext)
if err != nil {
return "", err
}
logger().Debug("ListAccounts", zap.Strings("acccounts", accounts))
return strings.Join(accounts, ":"), nil
}

func initOrRestoreAppState(datastorePath string) error {
func initOrRestoreAppState() error {
initialJSONNetConf, err := json.Marshal(initialNetConf)
if err != nil {
return err
Expand All @@ -105,7 +107,7 @@ func initOrRestoreAppState(datastorePath string) error {
LocalGRPC: initiallocalGRPC,
}

appState, err := account.OpenStateDB(datastorePath+"/berty.state.db", initialState)
appState, err := account.OpenStateDB("./berty.state.db", initialState)
if err != nil {
return errors.Wrap(err, "state DB init failed")
}
Expand Down Expand Up @@ -133,7 +135,7 @@ func Start(cfg *MobileOptions) error {
return nil
}

if err := initOrRestoreAppState(cfg.datastorePath); err != nil {
if err := initOrRestoreAppState(); err != nil {
return errors.Wrap(err, "app init/restore state failed")
}

Expand All @@ -154,7 +156,7 @@ func Restart() error {
return nil
}

func DropDatabase(datastorePath string) error {
func DropDatabase() error {
defer panicHandler()

currentAccount, err := account.Get(rootContext, accountName)
Expand Down Expand Up @@ -218,7 +220,7 @@ func daemon(cfg *MobileOptions) error {
account.WithPassphrase("secure"),
account.WithNotificationDriver(NotificationDriver),
account.WithDatabase(&account.DatabaseOptions{
Path: cfg.datastorePath,
Path: ".",
Drop: false,
}),
account.WithP2PNetwork(netConf),
Expand Down
15 changes: 15 additions & 0 deletions client/react-native/gomobile/core/deviceinfo.go
@@ -0,0 +1,15 @@
package core

import "berty.tech/core/pkg/deviceinfo"

type DeviceInfoPkg struct{}

var DeviceInfo = &DeviceInfoPkg{}

func (*DeviceInfoPkg) GetStoragePath() string {
return deviceinfo.GetStoragePath()
}

func (*DeviceInfoPkg) SetStoragePath(path string) error {
return deviceinfo.SetStoragePath(path)
}
54 changes: 39 additions & 15 deletions client/react-native/gomobile/core/notification.go
@@ -1,11 +1,15 @@
package core

import (
"fmt"
"encoding/base64"
"encoding/json"
"sync"

"berty.tech/core/chunk"
"berty.tech/core/pkg/errorcodes"
"berty.tech/core/pkg/notification"
"berty.tech/core/push"
"github.com/pkg/errors"
"go.uber.org/zap"
)

Expand All @@ -20,28 +24,48 @@ type NativeNotificationDriver interface {

type MobileNotification struct {
Native NativeNotificationDriver
subscribers []chan *notification.Payload
subscribers []chan []byte
subscribersMutex sync.Mutex
tokenSubscribers []chan *notification.Token
tokenSubscribersMutex sync.Mutex
}

func (n MobileNotification) New() *MobileNotification {
return &MobileNotification{
subscribers: []chan *notification.Payload{},
m := &MobileNotification{
subscribers: []chan []byte{},
tokenSubscribers: []chan *notification.Token{},
}
return m
}

func (n *MobileNotification) Receive(data string) {
logger().Debug(fmt.Sprintf("receive notification: %+v", data))
n.subscribersMutex.Lock()
for i := range n.subscribers {
n.subscribers[i] <- &notification.Payload{
Body: data,
}
payload := push.Payload{}
if err := json.Unmarshal([]byte(data), &payload); err != nil {
logger().Error(errorcodes.ErrNodePushNotifSub.Wrap(err).Error())
return
}

b64Chunk := payload.Chunk
if b64Chunk == "" {
logger().Error(errorcodes.ErrNotificationReceive.Wrap(errors.New("chunk is missing")).Error())
return
}

bytesChunk, err := base64.StdEncoding.DecodeString(string(b64Chunk))
if err != nil {
logger().Error(errorcodes.ErrNotificationReceive.Wrap(err).Error())
return
}

c := &chunk.Chunk{}
if err := c.Unmarshal(bytesChunk); err != nil {
logger().Error(errorcodes.ErrNotificationReceive.Wrap(err).Error())
return
}

if err := chunk.Publish(c); err != nil {
logger().Error(errorcodes.ErrNotificationReceive.Wrap(err).Error())
}
n.subscribersMutex.Unlock()
}

func (n *MobileNotification) ReceiveAPNSToken(token []byte) {
Expand All @@ -66,18 +90,18 @@ func (n *MobileNotification) ReceiveToken(token *notification.Token) {
n.tokenSubscribers[i] <- token
}
n.tokenSubscribersMutex.Unlock()

}

func (n *MobileNotification) Subscribe() chan *notification.Payload {
sub := make(chan *notification.Payload, 1)
func (n *MobileNotification) Subscribe() chan []byte {
// let chunk manager send reconstructed data
sub := chunk.Subscribe()
n.subscribersMutex.Lock()
n.subscribers = append(n.subscribers, sub)
n.subscribersMutex.Unlock()
return sub
}

func (n *MobileNotification) Unsubscribe(sub chan *notification.Payload) {
func (n *MobileNotification) Unsubscribe(sub chan []byte) {
n.subscribersMutex.Lock()
for i := range n.subscribers {
if sub == n.subscribers[i] {
Expand Down
10 changes: 2 additions & 8 deletions client/react-native/gomobile/core/options.go
@@ -1,14 +1,8 @@
package core

type MobileOptions struct {
logger NativeLogger
datastorePath string
nickname string
}

func (cfg *MobileOptions) WithDatastorePath(path string) *MobileOptions {
cfg.datastorePath = path
return cfg
logger NativeLogger
nickname string
}

func (cfg *MobileOptions) WithLoggerDriver(logger NativeLogger) *MobileOptions {
Expand Down
2 changes: 1 addition & 1 deletion client/react-native/gomobile/go.mod
Expand Up @@ -5,7 +5,7 @@ require (
github.com/libp2p/go-buffer-pool v0.1.2-0.20181009094743-058210c5a0d0 // indirect
github.com/pkg/errors v0.8.0
go.uber.org/zap v1.9.1
golang.org/x/mobile v0.0.0-20181026062114-a27dd33d354d // indirect
golang.org/x/mobile v0.0.0-20190127143845-a42111704963 // indirect
)

replace berty.tech/core v0.0.0 => ../../../core
5 changes: 5 additions & 0 deletions client/react-native/gomobile/go.sum
Expand Up @@ -565,7 +565,12 @@ golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85/go.mod h1:6SG95UA2DQfeDnf
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9 h1:mKdxBk7AujPs8kU4m80U72y/zjbZ3UcXC7dClwKbUI0=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/mobile v0.0.0-20181026062114-a27dd33d354d h1:DuZZDdMFwDrzmycNhCaWSve7Vh+BIrjm7ttgb4fD3Os=
golang.org/x/mobile v0.0.0-20181026062114-a27dd33d354d/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mobile v0.0.0-20190125201028-feefccb6c1c9 h1:guhfyRQZ6kHMDmc247p+xD6JPKdNknxn8CTyEYg59k8=
golang.org/x/mobile v0.0.0-20190125201028-feefccb6c1c9/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mobile v0.0.0-20190127143845-a42111704963 h1:2HSxAhImj2OpXsNjXSqfnv1xtqeCpDjwPB3o1DnQqKM=
golang.org/x/mobile v0.0.0-20190127143845-a42111704963/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/net v0.0.0-20180524181706-dfa909b99c79/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180816102801-aaf60122140d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
8 changes: 7 additions & 1 deletion client/react-native/ios/modules/core/CoreModule.swift
Expand Up @@ -22,6 +22,12 @@ class CoreModule: NSObject {
override init() {
super.init()
Core.notificationDriver()?.native = Notification()
let storagePath = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first?.path
do {
try Core.deviceInfo().setStoragePath(storagePath)
} catch let error as NSError {
logger.format("unable to set storage path", level: .error, error.userInfo.description)
}
}

func getFilesDir() throws -> String {
Expand All @@ -40,7 +46,7 @@ class CoreModule: NSObject {
var err: NSError?

do {
CoreInitialize(logger, try self.getFilesDir(), &err)
CoreInitialize(logger, Core.storagePath(), &err)
if let error = err {
throw error
}
Expand Down
1 change: 1 addition & 0 deletions core/api/p2p/envelope.proto
Expand Up @@ -13,3 +13,4 @@ message Envelope {
keypair.Signature signature = 3;
string source = 4;
}

0 comments on commit b2cbee9

Please sign in to comment.