Skip to content

Commit

Permalink
fix: refactor swift API + few fixes on socket manager
Browse files Browse the repository at this point in the history
  • Loading branch information
aeddi committed Dec 1, 2019
1 parent eef3564 commit efd89fb
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 27 deletions.
2 changes: 1 addition & 1 deletion go/bind/ipfs/bind_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Node interface {
GetApiAddrs() string

// Serve api on the given unix socket path
Serve(sockpath string) error
ServeOnUDS(sockpath string) error
}

func NewNode(r *Repo) (Node, error) {
Expand Down
9 changes: 5 additions & 4 deletions go/bind/ipfs/bind_sockmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,22 @@ func NewSockManager(path string) (*SockManager, error) {
}

func (sm *SockManager) NewSockPath() (string, error) {
sm.muCounter.Lock()
if sm.counter == math.MaxUint32 {
// FIXME: do something smarter knowing that a socket may have been
// TODO: do something smarter knowing that a socket may have been
// removed in the meantime
return "", errors.New("max number of socket exceeded")
}

sm.muCounter.Lock()
sm.counter++
sockFilename := fmt.Sprintf(paddingFormatStr, strconv.FormatUint(uint64(sm.counter), 36))
sm.counter++
sm.muCounter.Unlock()

sockPath := filepath.Join(sm.sockDirPath, sockFilename)
_, err := os.Stat(sockPath)
if os.IsNotExist(err) {
return sockPath, nil
} else if err == nil {
return "", errors.New("sock already exists: " + sockPath)
}

return "", errors.Wrap(err, "can't create new sock")
Expand Down
2 changes: 1 addition & 1 deletion go/pkg/node/node_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
manet "github.com/multiformats/go-multiaddr-net"
)

func (im *IpfsMobile) Serve(sockpath string) error {
func (im *IpfsMobile) ServeOnUDS(sockpath string) error {
var cfg *ipfs_config.Config
var err error

Expand Down
4 changes: 1 addition & 3 deletions ios/Example/GomobileIPFS/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ import GomobileIPFS
class ViewController: UIViewController {
@IBOutlet weak var PeerID: UILabel!

var ipfs: IPFS? = nil

override func viewDidLoad() {
super.viewDidLoad()

do {
let ipfs = try IPFS()
try ipfs.start()

let res = try ipfs.shellRequest("id", b64Body: "")
let res = try ipfs.commandToDict("id")

self.PeerID.text = res["ID"] as? String
} catch let error {
Expand Down
34 changes: 18 additions & 16 deletions ios/GomobileIPFS/Classes/IPFS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,28 @@ public enum IpfsError: CustomNSError {
public class IPFS: NSObject {
public static let defaultRepoPath = "ipfs/repo"

static let sockManager: SockManager = nil // FIXME: Use sockManager
static var sockManager: SockManager? = nil

var node: Node? = nil
var shell: IpfsShell? = nil
var repo: Repo? = nil

let absRepoURL: URL
let absSockPath: String

// init ipfs repo with the default or given path
public init(_ repoPath: String = defaultRepoPath) throws {
let absUserUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
self.absRepoURL = absUserUrl.appendingPathComponent(repoPath, isDirectory: true)

// setup sockmanager if needed
if self.sockManager == nil {
// init sockmanager singleton if needed
if IPFS.sockManager == nil {
let absTmpURL = FileManager.default.compatTemporaryDirectory
self.sockManager = try SockManager(self.absTmpURL)
IPFS.sockManager = try SockManager(absTmpURL)
}

self.absSockPath = try IPFS.sockManager!.newSockPath()

// init repo if needed
if !(try Repo.isInitialized(url: absRepoURL)) {
let config = try Config.defaultConfig()
Expand Down Expand Up @@ -84,12 +87,10 @@ public class IPFS: NSObject {
let node = try Node(repo)

// serve api
let sockpath = try self.sockManager.newSockPath()
print("sockpath", sockpath)
try node.serve(sockpath: sockpath)
try node.serveOnUDS(sockpath: self.absSockPath)

// init shell
if let shell = IpfsNewUDSShell(sockpath, &err) {
if let shell = IpfsNewUDSShell(self.absSockPath, &err) {
self.shell = shell
} else {
throw IpfsError.runtimeError("unable to get shell")
Expand Down Expand Up @@ -117,21 +118,22 @@ public class IPFS: NSObject {
try self.start()
}

public func shellRequest(_ command: String, b64Body: String) throws -> [String: Any] {
public func command(_ command: String, body: Data? = nil) throws -> Data {
if !self.isStarted() {
throw IpfsError.nodeNotStarted
}

var body: Data? = nil
if b64Body.count > 0 {
body = Data(base64Encoded: b64Body, options: .ignoreUnknownCharacters)
}

guard let rawJson = try self.shell?.request(command, body: body) else {
guard let raw = try self.shell?.request(command, body: body) else {
throw IpfsError.runtimeError("failed to fetch shell, empty response")
}

guard let json = try? JSONSerialization.jsonObject(with: rawJson, options: []) else {
return raw
}

public func commandToDict(_ command: String, body: Data? = nil) throws -> [String: Any] {
let raw = try self.command(command, body: body)

guard let json = try? JSONSerialization.jsonObject(with: raw, options: []) else {
throw IpfsError.runtimeError("failed to deserialize response, empty response")
}

Expand Down
4 changes: 2 additions & 2 deletions ios/GomobileIPFS/Classes/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class Node {
try self.node.close()
}

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

0 comments on commit efd89fb

Please sign in to comment.