From 0f6b050b89d7c4690fad9f3d0f2637abe86f3191 Mon Sep 17 00:00:00 2001 From: Guilhem Fanton Date: Wed, 9 Sep 2020 15:39:57 +0200 Subject: [PATCH] feat: Add enable pubsub/namesys extra opt Signed-off-by: Guilhem Fanton --- .../main/java/ipfs/gomobile/android/IPFS.java | 35 ++++++++++++++++++- go/bind/core/node.go | 8 ++--- go/bind/core/repo.go | 29 ++++++++++----- go/pkg/node/node.go | 4 +++ go/pkg/node/repo.go | 6 ++-- ios/Bridge/GomobileIPFS/Sources/IPFS.swift | 16 +++++++++ 6 files changed, 80 insertions(+), 18 deletions(-) diff --git a/android/bridge/src/main/java/ipfs/gomobile/android/IPFS.java b/android/bridge/src/main/java/ipfs/gomobile/android/IPFS.java index 5a5d09a0..59492a65 100644 --- a/android/bridge/src/main/java/ipfs/gomobile/android/IPFS.java +++ b/android/bridge/src/main/java/ipfs/gomobile/android/IPFS.java @@ -197,6 +197,36 @@ synchronized public void restart() throws NodeStopException { try { start(); } catch(NodeStartException ignore) { /* Should never happen */ } } + /** + * Instantiate the ipfs node with the experimental pubsub feature enabled. + * + * @throws ExtraOptionException If enable the extra option failed + * @see Experimental features of IPFS + */ + synchronized public void enablePubsubExperiment() throws ExtraOptionException { + try { + openRepoIfClosed(); + repo.enablePubsubExperiment(); + } catch (Exception e) { + throw new ExtraOptionException("Enable pubsub experiment failed", e); + } + } + + /** + * Enable IPNS record distribution through pubsub; enables pubsub. + * + * @throws ExtraOptionException If enable the extra option failed + * @see Experimental features of IPFS + */ + synchronized public void enableNamesysPubsub() throws ExtraOptionException { + try { + openRepoIfClosed(); + repo.enableNamesysPubsub(); + } catch (Exception e) { + throw new ExtraOptionException("Enable namesys pubsub failed", e); + } + } + /** * Gets the IPFS instance config as a JSON. * @@ -332,8 +362,11 @@ synchronized private void openRepoIfClosed() throws RepoOpenException { } } - // Exceptions + public static class ExtraOptionException extends Exception { + ExtraOptionException(String message, Throwable err) { super(message, err); } + } + public static class ConfigCreationException extends Exception { ConfigCreationException(String message, Throwable err) { super(message, err); } } diff --git a/go/bind/core/node.go b/go/bind/core/node.go index 6130acd8..a0c38690 100644 --- a/go/bind/core/node.go +++ b/go/bind/core/node.go @@ -92,15 +92,11 @@ func (n *Node) ServeMultiaddr(smaddr string) (string, error) { func NewNode(r *Repo) (*Node, error) { ctx := context.Background() - if _, err := loadPlugins(r.path); err != nil { + if _, err := loadPlugins(r.mr.Path); err != nil { return nil, err } - repo := &mobile_node.MobileRepo{ - Repo: r.irepo, - Path: r.path, - } - mnode, err := mobile_node.NewNode(ctx, repo, &mobile_host.MobileConfig{}) + mnode, err := mobile_node.NewNode(ctx, r.mr, &mobile_host.MobileConfig{}) if err != nil { return nil, err } diff --git a/go/bind/core/repo.go b/go/bind/core/repo.go index 58613c08..03bf9e88 100644 --- a/go/bind/core/repo.go +++ b/go/bind/core/repo.go @@ -3,6 +3,7 @@ package core import ( "path/filepath" + "github.com/ipfs-shipyard/gomobile-ipfs/go/pkg/node" ipfs_loader "github.com/ipfs/go-ipfs/plugin/loader" ipfs_repo "github.com/ipfs/go-ipfs/repo" ipfs_fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo" @@ -11,8 +12,7 @@ import ( var plugins *ipfs_loader.PluginLoader type Repo struct { - irepo ipfs_repo.Repo - path string + mr *node.MobileRepo } func RepoIsInitialized(path string) bool { @@ -37,19 +37,32 @@ func OpenRepo(path string) (*Repo, error) { return nil, err } - return &Repo{irepo, path}, nil + mRepo := &node.MobileRepo{ + Repo: irepo, + Path: path, + } + + return &Repo{mRepo}, nil +} + +func (r *Repo) EnablePubsubExperiment() { + r.mr.EnablePubsubExperiment = true +} + +func (r *Repo) EnableNamesysPubsub() { + r.mr.EnableNamesysPubsub = true } func (r *Repo) GetRootPath() string { - return r.path + return r.mr.Path } func (r *Repo) SetConfig(c *Config) error { - return r.irepo.SetConfig(c.getConfig()) + return r.mr.Repo.SetConfig(c.getConfig()) } func (r *Repo) GetConfig() (*Config, error) { - cfg, err := r.irepo.Config() + cfg, err := r.mr.Repo.Config() if err != nil { return nil, err } @@ -58,11 +71,11 @@ func (r *Repo) GetConfig() (*Config, error) { } func (r *Repo) Close() error { - return r.irepo.Close() + return r.mr.Close() } func (r *Repo) getRepo() ipfs_repo.Repo { - return r.irepo + return r.mr } func loadPlugins(repoPath string) (*ipfs_loader.PluginLoader, error) { diff --git a/go/pkg/node/node.go b/go/pkg/node/node.go index 82578ec0..f5fa51bf 100644 --- a/go/pkg/node/node.go +++ b/go/pkg/node/node.go @@ -30,6 +30,10 @@ func NewNode(ctx context.Context, repo *MobileRepo, mcfg *host.MobileConfig) (*I NilRepo: false, Repo: repo, Host: host.NewMobileHostOption(mcfg), + ExtraOpts: map[string]bool{ + "pubsub": repo.EnablePubsubExperiment, + "ipnsps": repo.EnableNamesysPubsub, + }, } // create ipfs node diff --git a/go/pkg/node/repo.go b/go/pkg/node/repo.go index faa37df4..8fe62c97 100644 --- a/go/pkg/node/repo.go +++ b/go/pkg/node/repo.go @@ -9,8 +9,8 @@ var _ ipfs_repo.Repo = (*MobileRepo)(nil) type MobileRepo struct { ipfs_repo.Repo Path string -} -func NewMobileRepo(repo ipfs_repo.Repo, path string) *MobileRepo { - return &MobileRepo{repo, path} + // extra config + EnablePubsubExperiment bool + EnableNamesysPubsub bool } diff --git a/ios/Bridge/GomobileIPFS/Sources/IPFS.swift b/ios/Bridge/GomobileIPFS/Sources/IPFS.swift index b827cbd1..56d0d60f 100644 --- a/ios/Bridge/GomobileIPFS/Sources/IPFS.swift +++ b/ios/Bridge/GomobileIPFS/Sources/IPFS.swift @@ -128,6 +128,22 @@ public class IPFS { try self.start() } + /// Instantiate the ipfs node with the experimental pubsub feature enabled. + /// - Throws: + /// - `RepoError`: If the opening of the repo failed + public func enablePubsubExperiment() throws { + try openRepoIfClosed() + self.repo!.goRepo.enablePubsubExperiment() + } + + /// Enable IPNS record distribution through pubsub; enables pubsub. + /// - Throws: + /// - `RepoError`: If the opening of the repo failed + public func enableNamesysPubsub() throws { + try openRepoIfClosed() + self.repo!.goRepo.enableNamesysPubsub() + } + /// Gets the IPFS instance config as a dict /// - Throws: /// - `RepoError`: If the opening of the repo or the getting of its config failed