Skip to content

Commit

Permalink
fix(ios): enable tcp shell on ios simulator only
Browse files Browse the repository at this point in the history
  • Loading branch information
gfanton authored and aeddi committed Dec 1, 2019
1 parent 3e4f7ef commit a9c7cb9
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ synchronized public void start()

try {
node = Ipfs.newNode(repo);
node.serveOnUDS(absSockPath);
node.serveUnixSocketAPI(absSockPath);
} catch (Exception e) {
throw new NodeStartException("Node start failed", e);
}
Expand Down
3 changes: 1 addition & 2 deletions go/bind/ipfs/bind_config.go → go/bind/ipfs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io/ioutil"

mobile_config "github.com/berty/gomobile-ipfs/go/bind/ipfs/config"
ipfs_config "github.com/ipfs/go-ipfs-config"
ipfs_common "github.com/ipfs/go-ipfs/repo/common"
)
Expand All @@ -21,7 +20,7 @@ func NewConfig(raw_json []byte) (cfg *Config, err error) {
}

func NewDefaultConfig() (*Config, error) {
cfg, err := mobile_config.InitConfig(ioutil.Discard, 2048)
cfg, err := initConfig(ioutil.Discard, 2048)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions go/bind/ipfs/config/init.go → go/bind/ipfs/init.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package ipfs

import (
"encoding/base64"
Expand All @@ -12,7 +12,7 @@ import (
libp2p_peer "github.com/libp2p/go-libp2p-core/peer"
)

func InitConfig(out io.Writer, nBitsForKeypair int) (*ipfs_config.Config, error) {
func initConfig(out io.Writer, nBitsForKeypair int) (*ipfs_config.Config, error) {
identity, err := identityConfig(out, nBitsForKeypair)
if err != nil {
return nil, err
Expand Down
21 changes: 16 additions & 5 deletions go/bind/ipfs/bind_node.go → go/bind/ipfs/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package ipfs

import (
"context"
"fmt"
"log"

mobile_host "github.com/berty/gomobile-ipfs/go/pkg/host"
Expand All @@ -19,19 +20,29 @@ import (
)

type Node struct {
*mobile_node.IpfsMobile
ipfsMobile *mobile_node.IpfsMobile
}

func (n *Node) Close() error {
return n.IpfsMobile.Close()
return n.ipfsMobile.Close()
}

func (n *Node) ServeUnixSocketAPI(sockpath string) error {
return n.IpfsMobile.Serve("/unix/" + sockpath)
return n.ipfsMobile.Serve("/unix/" + sockpath)
}

func (n *Node) ServeTCPAPI(port string) error {
return n.IpfsMobile.Serve("/ip4/127.0.0.1/tcp/" + port)
// Serve API on the given port and return the current listening maddr
func (n *Node) ServeTCPAPI(port string) (string, error) {
if err := n.ipfsMobile.Serve("/ip4/127.0.0.1/tcp/" + port); err != nil {
return "", err
}

// get the last maddr added
if addrs := n.ipfsMobile.GetAPIAddrs(); len(addrs) > 0 {
return addrs[len(addrs)-1], nil
}

return "", fmt.Errorf("unable to serve api, no listener registered")
}

func NewNode(r *Repo) (*Node, error) {
Expand Down
61 changes: 33 additions & 28 deletions go/bind/ipfs/bind_test.go → go/bind/ipfs/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"testing"

ipfs_config "github.com/ipfs/go-ipfs-config"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr-net"

. "github.com/smartystreets/goconvey/convey"
)
Expand All @@ -33,9 +31,12 @@ func TestMobile(t *testing.T) {
var (
testCfg *Config
testRepo *Repo
testNode Node
testNode *Node
testID *ipfs_config.Identity

sockpath string
tcpaddr string

err error
)

Expand All @@ -57,6 +58,26 @@ func TestMobile(t *testing.T) {
}()

Convey("test config", t, FailureHalts, func() {
Convey("test sockmanager", FailureHalts, func() {
var sm *SockManager

wrongpath := strings.Repeat("a", 110)
sm, err = NewSockManager(wrongpath)
So(err, ShouldNotBeNil)
So(sm, ShouldBeNil)

sm, err = NewSockManager(tmpdir)
So(err, ShouldBeNil)
So(sm, ShouldNotBeNil)

for i := 0; i < 100; i++ {
sockpath, err = sm.NewSockPath()
So(err, ShouldBeNil)
So(sockpath, ShouldNotBeEmpty)
So(len(sockpath), ShouldBeLessThan, 104)
}
})

Convey("test get/set config", FailureHalts, func() {
var cfg *Config
var val []byte
Expand Down Expand Up @@ -136,11 +157,7 @@ func TestMobile(t *testing.T) {
ok = RepoIsInitialized(tmpdir)
So(ok, ShouldBeFalse)

testCfg.SetupTCPAPI("0")
testCfg.SetupUnixSocketAPI("api.sock")

testCfg.SetupTCPGateway("0")
testCfg.SetupUnixSocketGateway("gateway.sock")
testCfg.SetupUnixSocketAPI(sockpath)

// init repo
err = InitRepo(tmpdir, testCfg)
Expand All @@ -164,10 +181,14 @@ func TestMobile(t *testing.T) {
Convey("test node", FailureHalts, func() {
testNode, err = NewNode(testRepo)
So(err, ShouldBeNil)

tcpaddr, err = testNode.ServeTCPAPI("0")
So(err, ShouldBeNil)
So(tcpaddr, ShouldNotBeEmpty)
})

Convey("test Unix Soscket shell", FailureHalts, func() {
socketaddr := "/unix/" + tmpdir + "/api.sock"
socketaddr := "/unix/" + sockpath
shell, err := NewShell(socketaddr)
So(err, ShouldBeNil)

Expand All @@ -177,7 +198,7 @@ func TestMobile(t *testing.T) {
So(err, ShouldBeNil)

api := struct {
Addrs []string `json:"Value"`
Addrs string `json:"Value"`
}{}

err = json.Unmarshal(res, &api)
Expand All @@ -186,26 +207,10 @@ func TestMobile(t *testing.T) {
})

Convey("test TCP shell", FailureHalts, func() {
addrs := strings.Split(testNode.GetApiAddrs(), ",")
So(len(addrs), ShouldBeGreaterThan, 0)
apiaddr := ""
for _, addr := range addrs {
maddr, err := ma.NewMultiaddr(addr)
So(err, ShouldBeNil)

naddr, err := manet.ToNetAddr(maddr)
So(err, ShouldBeNil)
if naddr.Network() == "tcp" {
apiaddr = addr
}
}

So(apiaddr, ShouldNotBeEmpty)

shell, err := NewShell(apiaddr)
shell, err := NewShell(tcpaddr)
So(err, ShouldBeNil)
out, err := shell.Request("id", nil)

out, err := shell.Request("id", nil)
So(err, ShouldBeNil)

id := struct {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
19 changes: 19 additions & 0 deletions go/pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ func (im *IpfsMobile) Close() error {
return err
}

// GetApiAddrs return current api listeners (separate with a comma)
func (im *IpfsMobile) GetAPIAddrs() (addrs []string) {
im.muListeners.Lock()

addrs = make([]string, len(im.listeners))
for i, l := range im.listeners {
a, err := manet.FromNetAddr(l.Addr())
if err == nil {
addrs[i] = a.String()
} else {
log.Printf("unable to get multiaddr from `%s`: %s", l.Addr().String(), err)
}

}
im.muListeners.Unlock()

return addrs
}

func NewNode(ctx context.Context, repo *MobileRepo, mcfg *host.MobileConfig) (*IpfsMobile, error) {
cfg, err := repo.Config()
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion go/pkg/node/node_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
manet "github.com/multiformats/go-multiaddr-net"
)

// @TODO: return multiaddr served
func (im *IpfsMobile) Serve(maddrs ...string) error {
var err error

Expand Down Expand Up @@ -60,7 +61,8 @@ func (im *IpfsMobile) Serve(maddrs ...string) error {
l := manet.NetListener(ml)
go func(l net.Listener) {
if err := ipfs_corehttp.Serve(im.IpfsNode, l, opts...); err != nil {
log.Printf("serve error: %s", err)
l.Close()
log.Printf("Serve on `%s` failed: %s", maddr.String(), err)
}
}(l)

Expand Down
Empty file removed ios/GomobileIPFS/Classes/.gitkeep
Empty file.
5 changes: 3 additions & 2 deletions ios/GomobileIPFS/Classes/IPFS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ public class IPFS: NSObject {
// serve api
var err: NSError?

// init shell
#if targetEnvironment(simulator) // fallback on tcp on simulator
try node.serve(onTCPPort: "4555")
let maddr: String = try node.serve(onTCPPort: "0")
// init shell
if let shell = IpfsNewTCPShell("4555", &err) {
if let shell = IpfsNewShell(maddr, &err) {
self.shell = shell
} else {
throw IpfsError.runtimeError("unable to get shell")
Expand Down
20 changes: 14 additions & 6 deletions ios/GomobileIPFS/Classes/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public enum NodeError: Error {

public class Node {
let node: IpfsNode

public init(_ repo: Repo) throws {
var err: NSError?

if let node = IpfsNewNode(repo.goRepo, &err) {
self.node = node
} else if let error = err {
Expand All @@ -31,12 +31,20 @@ public class Node {
public func close() throws {
try self.node.close()
}

public func serve(onUDS: String) throws {
try self.node.serveUnixSocketAPI(onUDS)
}

public func serve(onTCPPort: String) throws {
try self.node.serveTCPAPI(onTCPPort)

// return the multiaddr from listener
public func serve(onTCPPort: String) throws -> String {
var err: NSError?

let maddr = self.node.serveTCPAPI(onTCPPort, error: &err)
if let error = err {
throw NodeError.runtimeError(error, "unable to serve api")
}

return maddr
}
}

0 comments on commit a9c7cb9

Please sign in to comment.