Skip to content

Commit

Permalink
fix(node): bootstrap peers on new node
Browse files Browse the repository at this point in the history
  • Loading branch information
gfanton authored and aeddi committed Dec 1, 2019
1 parent 308b79e commit 9f9cdef
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
19 changes: 8 additions & 11 deletions example/react-native/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import {
} from 'react-native';


const asyncTimeout = (cb, time) =>
new Promise(res => setTimeout(cb, time))
const asyncTimeout = time =>
new Promise(res => setTimeout(res, time))

class App extends Component {
state = {
Expand All @@ -28,18 +28,17 @@ class App extends Component {
}

checkGateway = async () => {
try {
const res = await fetch('http://127.0.0.1:5001/webui')
let res = null
for (;;) {
res = await fetch('http://127.0.0.1:5001/webui')
if (res.ok) {
console.warn(res.url)
this.setState({
loading: false,
url: res.url,
})
return
}
} catch (err) {
console.warn(err)
return asyncTimeout(this.checkGateway, 2000)
await asyncTimeout(2000)
}
}

Expand All @@ -61,7 +60,6 @@ class App extends Component {
.then(id => console.info('peerID:', id))
.catch(err => console.error(err))
.then(this.checkGateway)
.catch(err => console.Warn(err))
}

render() {
Expand All @@ -78,8 +76,7 @@ class App extends Component {
<WebView
source={{uri: this.state.url}}
originWhitelist={['*']}
onError={err => console.warn(err)}
onMessage={msg => console.log(msg)}
onError={err => console.warn('webview error:', err)}
/>
</Fragment>
);
Expand Down
14 changes: 13 additions & 1 deletion mobile.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ package mobile
import (
"context"
"io/ioutil"
"log"

host "github.com/berty/gomobile-ipfs/host"
node "github.com/berty/gomobile-ipfs/node"

ipfs_bs "github.com/ipfs/go-ipfs/core/bootstrap"
ipfs_fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
)

Expand All @@ -28,7 +31,16 @@ func NewNode(r *Repo) (Node, error) {

ctx := context.Background()
repo := r.getRepo()
return node.NewNode(ctx, repo, &host.MobileConfig{})
node, err := node.NewNode(ctx, repo, &host.MobileConfig{})
if err != nil {
return nil, err
}

if err := node.IpfsNode.Bootstrap(ipfs_bs.DefaultBootstrapConfig); err != nil {
log.Printf("unable to start node: `%s`", err)
}

return node, nil
}

func NewConfig(raw_json []byte) (cfg *Config, err error) {
Expand Down
8 changes: 4 additions & 4 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import (

type IpfsMobile struct {
lapi []manet.Listener
ipfsNode *ipfs_core.IpfsNode
IpfsNode *ipfs_core.IpfsNode
}

func (im *IpfsMobile) Close() error {
for _, l := range im.lapi {
_ = l.Close()
}

return im.ipfsNode.Close()
return im.IpfsNode.Close()
}

func NewNode(ctx context.Context, repo ipfs_repo.Repo, mcfg *host.MobileConfig) (*IpfsMobile, error) {
Expand Down Expand Up @@ -90,14 +90,14 @@ func NewNode(ctx context.Context, repo ipfs_repo.Repo, mcfg *host.MobileConfig)
l := manet.NetListener(ml)
go func(l net.Listener) {
if err := ipfs_corehttp.Serve(inode, l, opts...); err != nil {
log.Printf("serve error: %s", err)
log.Printf("serve error: `%s`", err)
}
}(l)

}

return &IpfsMobile{
lapi: lapi,
ipfsNode: inode,
IpfsNode: inode,
}, nil
}

0 comments on commit 9f9cdef

Please sign in to comment.