Skip to content

Commit

Permalink
feat(devtools): removed err return of getNetworkConfig on both iOS, A…
Browse files Browse the repository at this point in the history
…ndroid and RN
  • Loading branch information
aeddi committed Nov 9, 2018
1 parent 7771872 commit a05dd6f
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 79 deletions.
@@ -1,2 +1,2 @@
#Wed Nov 07 15:32:05 CET 2018
#Fri Nov 09 15:06:42 CET 2018
connection.project.dir=
@@ -1,2 +1,2 @@
#Wed Nov 07 15:32:06 CET 2018
#Fri Nov 09 15:06:42 CET 2018
connection.project.dir=..
Expand Up @@ -102,13 +102,7 @@ public void getPort(Promise promise) {

@ReactMethod
public void getNetworkConfig(Promise promise) {
try {
String config = Core.getNetworkConfig();
promise.resolve(config);
} catch (Exception err) {
this.logger.format(Level.ERROR, this.getName(), "Unable to get network config: %s", err);
promise.reject(err);
}
promise.resolve(Core.getNetworkConfig());
}

@ReactMethod
Expand Down
Expand Up @@ -25,7 +25,7 @@ export default class Network extends PureComponent {
default_trans: false,
bluetooth_trans: false,
default_bootstrap: false,
bootstrap: [],
custom_bootstrap: [],
mdns: false,
relay: false,
}
Expand All @@ -38,7 +38,7 @@ export default class Network extends PureComponent {
(this.state.default_trans !== this.currentConfig.DefaultTransport ||
this.state.bluetooth_trans !== this.currentConfig.BluetoothTransport ||
this.state.default_bootstrap !== this.currentConfig.DefaultBootstrap ||
this.state.bootstrap !== this.currentConfig.Bootstrap ||
this.state.custom_bootstrap !== this.currentConfig.CustomBootstrap ||
this.state.mdns !== this.currentConfig.MDNS ||
this.state.relay !== this.currentConfig.Relay)
) {
Expand All @@ -58,7 +58,7 @@ export default class Network extends PureComponent {
DefaultTransport: this.state.default_trans,
BluetoothTransport: this.state.bluetooth_trans,
DefaultBootstrap: this.state.default_bootstrap,
Bootstrap: this.state.bootstrap,
CustomBootstrap: this.state.custom_bootstrap,
MDNS: this.state.mdns,
Relay: this.state.relay,
}
Expand All @@ -76,22 +76,18 @@ export default class Network extends PureComponent {
}

getCurrentConfig = async () => {
try {
let json = await NativeModules.CoreModule.getNetworkConfig()
this.currentConfig = JSON.parse(json)
let json = await NativeModules.CoreModule.getNetworkConfig()
this.currentConfig = JSON.parse(json)

this.setState({
loaded: true,
default_trans: this.currentConfig.DefaultTransport,
bluetooth_trans: this.currentConfig.BluetoothTransport,
default_bootstrap: this.currentConfig.DefaultBootstrap,
bootstrap: this.currentConfig.Bootstrap,
mdns: this.currentConfig.MDNS,
relay: this.currentConfig.Relay,
})
} catch (err) {
console.error(err)
}
this.setState({
loaded: true,
default_trans: this.currentConfig.DefaultTransport,
bluetooth_trans: this.currentConfig.BluetoothTransport,
default_bootstrap: this.currentConfig.DefaultBootstrap,
custom_bootstrap: this.currentConfig.CustomBootstrap,
mdns: this.currentConfig.MDNS,
relay: this.currentConfig.Relay,
})
}

componentDidMount () {
Expand Down
26 changes: 26 additions & 0 deletions client/react-native/gomobile/core/config.go
@@ -0,0 +1,26 @@
package core

// This file contain the configuration by default

// Initial configs will be used to set default settings in state DB
// (when the app has never been launched before)
var (
initialNetConf = networkConfig{
DefaultTransport: true,
BluetoothTransport: false,
DefaultBootstrap: true,
CustomBootstrap: []string{},
MDNS: false,
Relay: false,
}

initialBotMode = false
)

// Default configs will be used to set default settings but not saved in state DB
const (
defaultBind = "/ip4/0.0.0.0/tcp/0"
defaultBLEBind = "/ble/00000000-0000-0000-0000-000000000000"
defaultMetrics = true
defaultIdentity = ""
)
78 changes: 41 additions & 37 deletions client/react-native/gomobile/core/network.go
Expand Up @@ -2,7 +2,7 @@ package core

import (
"encoding/json"
"errors"
"github.com/pkg/errors"

account "berty.tech/core/manager/account"
"berty.tech/core/network/p2p"
Expand All @@ -12,70 +12,74 @@ type networkConfig struct {
DefaultTransport bool
BluetoothTransport bool
DefaultBootstrap bool
Bootstrap []string
CustomBootstrap []string
MDNS bool
Relay bool
}

var currentNetworkConfig = networkConfig{
DefaultTransport: true,
BluetoothTransport: false,
DefaultBootstrap: true,
Bootstrap: []string{},
MDNS: false,
Relay: false,
}
func createNetworkConfig() (*account.P2PNetworkOptions, error) {
var (
netConf networkConfig
bind []string
transport []string
bootstrap []string
)

func createNetworkConfig() *account.P2PNetworkOptions {
var transport []string
if err := json.Unmarshal([]byte(appConfig.JSONNetConf), &netConf); err != nil {
return nil, errors.Wrap(err, "JSONNetConf unmarshal failed")
}

if currentNetworkConfig.DefaultTransport {
if netConf.DefaultTransport {
transport = append(transport, "default")
bind = append(bind, defaultBind)
}
if currentNetworkConfig.BluetoothTransport {
if netConf.BluetoothTransport {
transport = append(transport, "ble")
bind = append(bind, defaultBLEBind)
}
if currentNetworkConfig.DefaultBootstrap {
currentNetworkConfig.Bootstrap = append(currentNetworkConfig.Bootstrap, p2p.DefaultBootstrap...)

if netConf.DefaultBootstrap {
bootstrap = append(bootstrap, p2p.DefaultBootstrap...)
}
bootstrap = append(bootstrap, netConf.CustomBootstrap...)

return &account.P2PNetworkOptions{
Bind: []string{"/ip4/0.0.0.0/tcp/0", "/ble/00000000-0000-0000-0000-000000000000"},
Bind: bind,
Transport: transport,
Bootstrap: currentNetworkConfig.Bootstrap,
MDNS: currentNetworkConfig.MDNS,
Relay: currentNetworkConfig.Relay,
Metrics: true,
Identity: "",
}
Bootstrap: bootstrap,
MDNS: netConf.MDNS,
Relay: netConf.Relay,
Metrics: defaultMetrics,
Identity: defaultIdentity,
}, nil
}

func GetNetworkConfig() (string, error) {
json, err := json.Marshal(currentNetworkConfig)
if err != nil {
return "", err
}

return string(json), nil
func GetNetworkConfig() string {
return string(appConfig.JSONNetConf)
}

func UpdateNetworkConfig(jsonConf string) error {
waitDaemon()
currentAccount, _ := account.Get(accountName)
if currentAccount == nil {
return errors.New("account not running")
}

var newNetworkConfig networkConfig

if err := json.Unmarshal([]byte(jsonConf), &newNetworkConfig); err != nil {
return err
}

currentNetworkConfig = newNetworkConfig

if err := currentAccount.UpdateP2PNetwork(createNetworkConfig()); err != nil {
netConf, err := createNetworkConfig()
if err != nil {
return err
}
if err := currentAccount.UpdateP2PNetwork(netConf); err != nil {
return err
}

appConfig.JSONNetConf = jsonConf
appConfig.StartCounter++
if err := appConfig.Save(); err != nil {
return errors.Wrap(err, "state DB save failed")
}

return nil
}
23 changes: 8 additions & 15 deletions client/react-native/ios/modules/core/CoreModule.swift
Expand Up @@ -66,7 +66,7 @@ class CoreModule: NSObject {
resolve(nil)
} catch let error as NSError {
logger.format("unable to start core: %@", level: .Error, error.userInfo.description)
reject("\(String(describing: error.code))", error.userInfo.description, error)
reject("\(String(describing: error.code))", error.localizedDescription, error)
}
}

Expand All @@ -81,7 +81,7 @@ class CoreModule: NSObject {
resolve(nil)
} catch let error as NSError {
logger.format("unable to restart core: %@", level: .Error, error.userInfo.description)
reject("\(String(describing: error.code))", error.userInfo.description, error)
reject("\(String(describing: error.code))", error.localizedDescription, error)
}
}

Expand All @@ -97,7 +97,7 @@ class CoreModule: NSObject {
resolve(nil)
} catch let error as NSError {
logger.format("unable to drop database: %@", level: .Error, error.userInfo.description)
reject("\(String(describing: error.code))", error.userInfo.description, error)
reject("\(String(describing: error.code))", error.localizedDescription, error)
}
}

Expand All @@ -108,20 +108,13 @@ class CoreModule: NSObject {
CoreGetPort(&port, &err)
if let error = err {
logger.format("unable to get port: ", level: .Error, error.userInfo.description)
reject("\(String(describing: error.code))", error.userInfo.description, error)
reject("\(String(describing: error.code))", error.localizedDescription, error)
}
resolve(port)
}

@objc func getNetworkConfig(_ resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
var err: NSError?

let config: String = CoreGetNetworkConfig(&err)
if let error = err {
logger.format("get network config error: ", level: .Error, error.userInfo.description)
reject("\(String(describing: error.code))", error.userInfo.description, error)
}
resolve(config)
resolve(CoreGetNetworkConfig())
}

@objc func updateNetworkConfig(_ config: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
Expand All @@ -130,7 +123,7 @@ class CoreModule: NSObject {
CoreUpdateNetworkConfig(config, &err)
if let error = err {
logger.format("update network config error: ", level: .Error, error.userInfo.description)
reject("\(String(describing: error.code))", error.userInfo.description, error)
reject("\(String(describing: error.code))", error.localizedDescription, error)
}
resolve(nil)
}
Expand All @@ -145,7 +138,7 @@ class CoreModule: NSObject {
CoreStartBot(&err)
if let error = err {
logger.format("start bot error: ", level: .Error, error.userInfo.description)
reject("\(String(describing: error.code))", error.userInfo.description, error)
reject("\(String(describing: error.code))", error.localizedDescription, error)
}
resolve(nil)
}
Expand All @@ -156,7 +149,7 @@ class CoreModule: NSObject {
CoreStopBot(&err)
if let error = err {
logger.format("stop bot error: ", level: .Error, error.userInfo.description)
reject("\(String(describing: error.code))", error.userInfo.description, error)
reject("\(String(describing: error.code))", error.localizedDescription, error)
}
resolve(nil)
}
Expand Down

0 comments on commit a05dd6f

Please sign in to comment.