Skip to content

Commit

Permalink
fix(desktop): update desktop bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
gfanton committed Apr 17, 2019
1 parent c0ab03d commit 7216b0a
Show file tree
Hide file tree
Showing 12 changed files with 345 additions and 186 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Expand Up @@ -99,7 +99,7 @@ jobs:

generate:
docker:
- image: bertychat/protoc:v8
- image: bertychat/protoc:v9
working_directory: /go/src/berty.tech
environment:
GOPATH: /go
Expand Down
16 changes: 10 additions & 6 deletions client/react-native/common/bridge/service/daemon/daemon.pb.json
Expand Up @@ -26,7 +26,7 @@
"responseType": "GetPortResponse"
},
"Initialize": {
"requestType": "Void",
"requestType": "Config",
"responseType": "Void"
},
"IsBotRunning": {
Expand All @@ -50,7 +50,7 @@
"responseType": "Void"
},
"Start": {
"requestType": "Config",
"requestType": "StartRequest",
"responseType": "Void"
},
"StartBot": {
Expand Down Expand Up @@ -82,6 +82,14 @@
"Void": {
"fields": {}
},
"StartRequest": {
"fields": {
"nickname": {
"type": "string",
"id": 1
}
}
},
"NetworkConfig": {
"fields": {
"json": {
Expand Down Expand Up @@ -205,10 +213,6 @@
"SwarmKeyPath": {
"type": "string",
"id": 24
},
"nickname": {
"type": "string",
"id": 25
}
}
},
Expand Down
12 changes: 6 additions & 6 deletions client/react-native/common/components/Screens/Accounts/Auth.js
Expand Up @@ -81,12 +81,12 @@ class Auth extends PureComponent {
return test
}

init = async () => {
init = async (config) => {
const { t, bridge } = this.props

this.setState({ loading: true, message: t('core.initializing') })
try {
await bridge.initialize({})
await bridge.initialize(config)
} catch (err) {
console.warn('initialize', err)
}
Expand All @@ -105,14 +105,14 @@ class Auth extends PureComponent {
}
}

start = async config => {
start = async nickname => {
const { t, bridge } = this.props

this.setState({ loading: true, message: t('daemon.initializing') })
try {
await bridge.start(config)
await bridge.start({ nickname })
} catch (error) {
throw error
console.warn(error)
}
}

Expand All @@ -133,7 +133,7 @@ class Auth extends PureComponent {
nickname = list[0]
}

// await this.start(nickname) // @FIXME: implement this later
await this.start(nickname) // @FIXME: implement this later
const context = await this.getRelayContext()
getAvailableUpdate(context).then(update => {
this.props.updateContext.setState(update)
Expand Down
2 changes: 1 addition & 1 deletion client/react-native/desktop/coreinterface/core.go
Expand Up @@ -127,7 +127,7 @@ func Start(nickname string) (interface{}, error) {
cfg = cfg.WithNickname(nickname)
cfg = cfg.WithLoggerDriver(&CliLogger{})

return nil, core.Start(cfg)
return nil, fmt.Errorf("cannot start client")
}

func Restart() (interface{}, error) {
Expand Down
29 changes: 15 additions & 14 deletions client/react-native/desktop/daemon.go
Expand Up @@ -13,26 +13,26 @@ import (
)

type DaemonDesktop struct {
daemon *daemon.Daemon
bridge *daemon.Daemon

server *grpc.Server
conn *grpc.ClientConn
}

func NewDaemonDesktop() (*DaemonDesktop, error) {
daemon := daemon.New()
bridge := daemon.New()

iogrpc := helper.NewIOGrpc()
dialer := iogrpc.NewDialer()
listener := iogrpc.NewListener()
listener := iogrpc.Listener()

dialOpts := append([]grpc.DialOption{
grpc.WithInsecure(),
grpc.WithDialer(icdialer),
grpc.WithDialer(dialer),
})

gs := grpc.NewServer()
daemon.RegisterDaemonServer(gs, daemon)
daemon.RegisterDaemonServer(gs, bridge)

conn, err := grpc.Dial("", dialOpts...)
if err != nil {
Expand All @@ -45,12 +45,12 @@ func NewDaemonDesktop() (*DaemonDesktop, error) {
}
}()

return &NativeBridge{
daemon: daemon,
return &DaemonDesktop{
bridge: bridge,

server: gs,
conn: conn,
}
}, nil
}

func stringPayload(message json.RawMessage) (string, error) {
Expand All @@ -63,13 +63,14 @@ func stringPayload(message json.RawMessage) (string, error) {
return payload, nil
}

func (d *DaemonDesktop) Start(ctx context.Context, cfg *daemon.Config) error {
return d.daemon.Start(ctx, cfg)
func (d *DaemonDesktop) Initialize(ctx context.Context, req *daemon.Config) error {
_, err := d.bridge.Initialize(ctx, req)
return err
}

func (d *DaemonDesktop) handleMessages(_ *astilectron.Window, m bootstrap.MessageIn) (interface{}, error) {
method := m.name
encoded, err := stringPayload(m.payload)
method := m.Name
encoded, err := stringPayload(m.Payload)
if err != nil {
return nil, err
}
Expand All @@ -81,13 +82,13 @@ func (d *DaemonDesktop) handleMessages(_ *astilectron.Window, m bootstrap.Messag
return &res, nil
}

func (n *DaemonDesktop) Invoke(method string, msgIn string) (string, error) {
func (d *DaemonDesktop) Invoke(method string, msgIn string) (string, error) {
in, err := helper.NewLazyMessage().FromBase64(msgIn)
if err != nil {
return "", err
}

out := helper.NewLazyMessage()
err := n.conn.Invoke(context.TODO(), method, in, out, helper.GrpcCallWithLazyCodec())
err = d.conn.Invoke(context.TODO(), method, in, out, helper.GrpcCallWithLazyCodec())
return out.Base64(), err
}
2 changes: 1 addition & 1 deletion client/react-native/desktop/go.mod
Expand Up @@ -12,10 +12,10 @@ require (
github.com/asticode/go-astilog v1.0.0
github.com/asticode/go-bindata v0.0.0-20151023091102-a0ff2567cfb7 // indirect
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
github.com/pkg/errors v0.8.1
github.com/sam-kamerer/go-plister v0.0.0-20190202124357-57f251aa88ff // indirect
github.com/shibukawa/configdir v0.0.0-20170330084843-e180dbdc8da0
go.uber.org/zap v1.9.1
google.golang.org/grpc v1.19.0
)

replace berty.tech/core v0.0.0 => ../../../core
Expand Down
46 changes: 35 additions & 11 deletions client/react-native/desktop/main.go
@@ -1,13 +1,16 @@
package main

import (
"context"
"errors"
"flag"
"fmt"
"os"

"berty.tech/client/react-native/desktop/coreinterface"
"berty.tech/core/daemon"
network_config "berty.tech/core/network/config"
"berty.tech/core/pkg/deviceinfo"
"berty.tech/core/pkg/logmanager"
"go.uber.org/zap"

Expand Down Expand Up @@ -40,11 +43,19 @@ func getStorageDir() (string, error) {
}

func main() {
storagePath, err := getStorageDir()
if err != nil {
panic(err)
}

storagePath, error := getStorageDir()
if err = deviceinfo.SetStoragePath(storagePath); err != nil {
panic(err)
}

fmt.Println(storagePath)
sqlConfig := &daemon.SQLConfig{
Path: opts.sql.path,
Key: opts.sql.key,
Path: fmt.Sprintf("%s/%s", storagePath, "berty.state.db"),
Key: "s3cur3",
}

config := &daemon.Config{
Expand All @@ -56,30 +67,28 @@ func main() {
InitOnly: false,
WithBot: false,
Notification: true,
ApnsCerts: nil,
ApnsDevVoipCerts: nil,
FcmAPIKeys: nil,
PrivateKeyFile: nil,
ApnsCerts: []string{},
ApnsDevVoipCerts: []string{},
FcmAPIKeys: []string{},
PrivateKeyFile: "",
PeerCache: true,
Identity: "",
Bootstrap: network_config.DefaultBootstrap,
NoP2P: false,
BindP2P: []string{},
TransportP2P: []string{},
Hop: false,
Hop: true,
Ble: true,
Mdns: true,
DhtServer: true,
PrivateNetwork: true,
SwarmKeyPath: true,
Nickname: "berty-desktop",
SwarmKeyPath: "",
}

// Init
flag.Parse()

t := true

logman, err := logmanager.New(logmanager.Opts{
RingSize: 10 * 1024 * 1024,
LogLevel: "debug",
Expand All @@ -94,16 +103,31 @@ func main() {

zap.L().Debug("Berty desktop client started")
astilog.SetDefaultLogger()

homepageUrl := "index.html"
if homepage != nil {
homepageUrl = *homepage
}

startRequest := &daemon.StartRequest{
Nickname: "daemon-desktop",
}

d, err := NewDaemonDesktop()
if err != nil {
panic(err)
}

if err := d.Initialize(context.Background(), config); err != nil {
panic(err)
}

if _, err := d.bridge.Start(context.Background(), startRequest); err != nil {
panic(err)
}

zap.L().Debug("Berty desktop client started")

// Run bootstrap
logger().Debug(fmt.Sprintf("Running app built at %s", BuiltAt))
if err := bootstrap.Run(bootstrap.Options{
Expand Down
11 changes: 9 additions & 2 deletions core/cmd/berty/daemon.go
Expand Up @@ -202,7 +202,10 @@ func runDaemon(opts *daemonOptions) error {
DhtServer: opts.dhtServer,
PrivateNetwork: opts.PrivateNetwork,
SwarmKeyPath: opts.SwarmKeyPath,
Nickname: opts.nickname,
}

startRequest := &daemon.StartRequest{
Nickname: opts.nickname,
}

dlogger := zap.L().Named("daemon.grpc")
Expand Down Expand Up @@ -233,7 +236,11 @@ func runDaemon(opts *daemonOptions) error {
d.Notification = notification.NewDesktopNotification()
}

if _, err := d.Start(context.Background(), config); err != nil {
if _, err := d.Initialize(context.Background(), config); err != nil {
return err
}

if _, err := d.Start(context.Background(), startRequest); err != nil {
return err
}

Expand Down
30 changes: 13 additions & 17 deletions core/daemon/daemon.go
Expand Up @@ -69,28 +69,33 @@ func getLocalIP() (string, error) {
return localIP, nil
}

func (d *Daemon) Start(ctx context.Context, cfg *Config) (*Void, error) {
// @TODO: implem this
func (d *Daemon) Initialize(ctx context.Context, cfg *Config) (*Void, error) {
d.config = cfg
return &Void{}, nil
}

func (d *Daemon) Start(ctx context.Context, req *StartRequest) (*Void, error) {
var err error

if cfg == nil {
return &Void{}, fmt.Errorf("core empty configuration")
if d.config == nil || d.config.SqlOpts == nil {
return &Void{}, errors.New("no config/SqlPath set, initialize first")
}

currentAccount, _ := account.Get(d.rootContext, cfg.Nickname)
currentAccount, _ := account.Get(d.rootContext, req.Nickname)
if currentAccount != nil {
// daemon already started, no errors to return
return &Void{}, fmt.Errorf("daemon already started")
}

d.accountName = cfg.Nickname
d.config = cfg
d.accountName = req.Nickname

initialState := account.StateDB{
BotMode: initialBotMode,
LocalGRPC: initiallocalGRPC,
}

d.appConfig, err = account.OpenStateDB(cfg.SqlOpts.Path, initialState)
d.appConfig, err = account.OpenStateDB(d.config.SqlOpts.Path, initialState)
if err != nil {
return &Void{}, errors.Wrap(err, "state DB init failed")
}
Expand All @@ -103,7 +108,7 @@ func (d *Daemon) Start(ctx context.Context, cfg *Config) (*Void, error) {
var cctx context.Context
cctx, d.cancel = context.WithCancel(d.rootContext)

return &Void{}, d.daemon(cctx, cfg)
return &Void{}, d.daemon(cctx, d.config, req.Nickname)
}

func (d *Daemon) DropDatabase(ctx context.Context, v *Void) (*Void, error) {
Expand Down Expand Up @@ -173,15 +178,6 @@ func (d *Daemon) GetPort(context.Context, *Void) (*GetPortResponse, error) {
}, nil
}

// @TODO: implem this
func (d *Daemon) Initialize(context.Context, *Void) (*Void, error) {
// if err := setupLogger("debug", datastorePath, d.Logger); err != nil {
// return err
// }

return &Void{}, fmt.Errorf("not implemented")
}

func (d *Daemon) IsBotRunning(context.Context, *Void) (*Void, error) {
// currentAccount, _ := account.Get(d.rootContext, d.accountName)

Expand Down

0 comments on commit 7216b0a

Please sign in to comment.