Skip to content

Commit

Permalink
fix(network): remove update method
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 Mar 20, 2019
1 parent f845d8e commit a46f107
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 60 deletions.
3 changes: 1 addition & 2 deletions client/react-native/gomobile/core/core.go
Expand Up @@ -22,7 +22,6 @@ var (
rootContext = context.Background()
NotificationDriver = MobileNotification{}.New() // Android / iOS
ElectronNotificationDriver *notification.Driver // Electron Desktop client
networkDriver *network.Network
)

// Setup call it at first native start
Expand Down Expand Up @@ -207,7 +206,7 @@ func daemon(cfg *MobileOptions) error {

var a *account.Account

networkDriver, err = network.New(rootContext, network.WithDefaultMobileOptions())
networkDriver, err := network.New(rootContext, network.WithDefaultMobileOptions())
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion client/react-native/gomobile/core/network.go
Expand Up @@ -12,7 +12,9 @@ func GetNetworkConfig() (string, error) {
defer panicHandler()
waitDaemon(accountName)

cfg := networkDriver.Config()
a, _ := account.Get(rootContext, accountName)

cfg := a.Network().Config()
data, err := json.Marshal(cfg)
if err != nil {
return "", err
Expand Down
30 changes: 23 additions & 7 deletions core/manager/account/p2p.go
Expand Up @@ -3,6 +3,7 @@ package account
import (
"context"

"berty.tech/core/network"
network_config "berty.tech/core/network/config"
"berty.tech/core/pkg/tracing"
"github.com/pkg/errors"
Expand All @@ -13,15 +14,30 @@ func (a *Account) UpdateNetwork(ctx context.Context, opts ...network_config.Opti
defer tracer.Finish()
ctx = tracer.Context()

err := a.network.Close(ctx)
updated, err := network.New(ctx, append(
[]network_config.Option{network.WithConfig(a.network.Config())},
opts...,
)...)

if err != nil {
return err
return errors.New("account failed to update network")
}
if err := a.node.UseNetworkDriver(ctx, updated); err != nil {
return a.node.UseNetworkDriver(ctx, a.network)
}

if err := a.network.Update(ctx, opts...); err != nil {
return errors.New("account failed to update network")
a.node.UseNetworkMetric(ctx, updated.Metric())

err = a.network.Close(ctx)
if err != nil {
logger().Error("last network cannot be closed: " + err.Error())
}
a.metric = a.network.Metric()
a.node.UseNetworkMetric(ctx, a.metric)
return a.node.UseNetworkDriver(ctx, a.network)

a.network = updated
a.metric = updated.Metric()
return nil
}

func (a *Account) Network() network.Driver {
return a.network
}
3 changes: 0 additions & 3 deletions core/network/interface.go
Expand Up @@ -38,9 +38,6 @@ type Driver interface {
// Return current config
Config() *config.Config

// Update update current network with config
Update(context.Context, ...config.Option) error

// Close cleanups things
Close(context.Context) error
}
45 changes: 6 additions & 39 deletions core/network/network.go
Expand Up @@ -2,7 +2,6 @@ package network

import (
"context"
"fmt"
"sync"

"berty.tech/core/entity"
Expand Down Expand Up @@ -42,49 +41,34 @@ func New(ctx context.Context, opts ...config.Option) (*Network, error) {
tracer := tracing.EnterFunc(ctx)
defer tracer.Finish()

net := &Network{}

if err := net.new(ctx, opts...); err != nil {
return nil, err
}
return net, nil
}

func (net *Network) new(ctx context.Context, opts ...config.Option) error {
var err error

ctx, cancel := context.WithCancel(ctx)

new := &Network{
net := &Network{
config: &config.Config{},
updating: &sync.Mutex{},
shutdown: cancel,
cache: NewNoopCache(),
}

if net.updating != nil {
new.updating = net.updating
}

if err := new.config.Apply(ctx, opts...); err != nil {
if err := net.config.Apply(ctx, opts...); err != nil {
cancel()
return err
return nil, err
}

new.host, err = new.config.NewNode(ctx)
net.host, err = net.config.NewNode(ctx)
if err != nil {
cancel()
return err
return nil, err
}

*net = *new

net.init(ctx)

if net.Config().PeerCache {
net.cache = NewPeerCache(net.host)
}
return nil
return net, nil
}

func (net *Network) init(ctx context.Context) {
Expand All @@ -98,23 +82,6 @@ func (net *Network) init(ctx context.Context) {
}
}

// Update create new network and permit to override previous config
func (net *Network) Update(ctx context.Context, opts ...config.Option) error {
net.updating.Lock()
defer net.updating.Unlock()

swap := *net

if err := net.new(ctx, append([]config.Option{WithConfig(net.config)}, opts...)...); err != nil {
return err
}

logger().Debug(fmt.Sprintf("NETWORK ADDR %p %+v, swap: %p %+v", net, net, &swap, swap))
swap.Close(ctx)

return nil
}

func (net *Network) Close(ctx context.Context) error {
tracer := tracing.EnterFunc(ctx)
defer tracer.Finish()
Expand Down
8 changes: 0 additions & 8 deletions core/network/network_test.go
Expand Up @@ -91,14 +91,6 @@ func TestDriver(t *testing.T) {
So(err, ShouldBeNil)
})

Convey("update clients", FailureHalts, func() {
So(homer.Update(ctx, WithClientTestOptions()), ShouldBeNil)

So(lisa.Update(ctx, WithClientTestOptions()), ShouldBeNil)

So(bart.Update(ctx, WithClientTestOptions()), ShouldBeNil)
})

Convey("bootstrap clients", FailureHalts, func() {

bootstrap := []string{}
Expand Down

0 comments on commit a46f107

Please sign in to comment.