Skip to content

Commit

Permalink
fix: few fixes and refactor so Android and iOS package API are identical
Browse files Browse the repository at this point in the history
  • Loading branch information
aeddi committed Dec 1, 2019
1 parent f0f6a52 commit 3388954
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 49 deletions.
14 changes: 6 additions & 8 deletions ios/Example/GomobileIPFS/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,22 @@ import GomobileIPFS

class ViewController: UIViewController {
@IBOutlet weak var PeerID: UILabel!

var ipfs: IPFS? = nil

override func viewDidLoad() {
super.viewDidLoad()

let dirurl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let repoPath = dirurl.appendingPathComponent("repo", isDirectory: true)


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

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

self.PeerID.text = res["ID"] as? String
} catch let error {
print(error)
self.PeerID.text = "Error: \(error.localizedDescription)"
}
}

Expand Down
22 changes: 11 additions & 11 deletions ios/GomobileIPFS/Classes/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ public enum ConfigError: Error {

public class Config {
let goConfig: MobileConfig

public init(_ config: MobileConfig) {
self.goConfig = config
}

public class func defaultConfig() throws -> Config {
var err: NSError?
let config: MobileConfig? = MobileNewDefaultConfig(&err)
if let error = err {
throw ConfigError.runtimeError(error, "failed to create default config")
}

return Config(config!)
}

Expand All @@ -42,7 +42,7 @@ public class Config {

public class func configFromDict(dict: [String: Any]) throws -> Config {
var err: NSError?

let json = try JSONSerialization.data(withJSONObject: dict)
let config: MobileConfig? = MobileNewConfig(json, &err)
if let error = err {
Expand All @@ -55,32 +55,32 @@ public class Config {
public func setKey(key: String, dict: [String: Any]) throws {
let json = try JSONSerialization.data(withJSONObject: dict)
try self.goConfig.setKey(key, raw_value: json)

}

public func getKey(key: String) throws -> [String: Any] {
let rawJson = try self.goConfig.getKey(key)
if let json = try? JSONSerialization.jsonObject(with: rawJson, options: []) {
if let dict = json as? [String: Any] {
return dict
}
}

throw ConfigError.error("json deserialization error")
}

// Helper

// set tcp api
public func setTCPAPIWithPort(_ port: String) {
self.goConfig.setupTCPAPI(port)
}

// set tcp api
public func setupTCPGateway(_ port: String) {
self.goConfig.setupTCPGateway(port)
}

// set unix socket api (sockfile is relative to repo folder)
public func setupUnixSocketAPI(_ sockfile: String) {
self.goConfig.setupUnixSocketAPI(sockfile)
Expand Down
68 changes: 44 additions & 24 deletions ios/GomobileIPFS/Classes/IPFS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,57 @@ public enum IpfsError: CustomNSError {
}
}

let sockName = "api.sock"

public class IPFS: NSObject {
var node: Node? = nil
var shell: MobileShell? = nil
var repo: Repo? = nil

let repoPath: URL
public static let defaultRepoPath = "ipfs/repo"
private static let sockName = "sock" // FIXME: Use sockManager

let absRepoPath: URL

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

// FIXME: Init sockManager with tmp folder

// init ipfs repo with the given path
public init(_ repoPath: URL) throws {
// init repo if needed
if !(try Repo.isInitialized(url: repoPath)) {
if !(try Repo.isInitialized(url: absRepoPath)) {
let config = try Config.defaultConfig()
config.setupUnixSocketAPI(sockName)
try Repo.initialize(url: repoPath, config: config)
config.setupUnixSocketAPI(IPFS.sockName)
try Repo.initialize(url: absRepoPath, config: config)
}

self.repoPath = repoPath
self.absRepoPath = absRepoPath
super.init()
}

public func getRepoPath() -> URL {
return self.absRepoPath
}

public func isStarted() -> Bool {
return self.node != nil
}

public func start() throws {
guard self.node == nil else {
if self.isStarted() {
throw IpfsError.nodeAlreadyStarted
}

var err: NSError?

// open repo
let repo = try Repo(self.repoPath)
let repo = try Repo(self.absRepoPath)

// init node
let node = try Node(repo)

// init shell
let sock = self.repoPath.appendingPathComponent(sockName)
let sock = self.absRepoPath.appendingPathComponent(IPFS.sockName)
if let shell = MobileNewUDSShell(sock.path, &err) {
self.shell = shell
} else {
Expand All @@ -71,8 +85,22 @@ public class IPFS: NSObject {
self.node = node
}

public func shell(_ command: String, b64Body: String) throws -> [String: Any] {
guard node != nil else {
public func stop() throws {
if !self.isStarted() {
throw IpfsError.nodeNotStarted
}

try self.node?.close()
self.node = nil
}

public func restart() throws {
try self.stop()
try self.start()
}

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

Expand All @@ -84,23 +112,15 @@ public class IPFS: NSObject {
guard let rawJson = 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 {
throw IpfsError.runtimeError("failed to deserialize response, empty response")
}

guard let dict = json as? [String: Any] else {
throw IpfsError.runtimeError("failed to convert json to dictionary")
}

return dict
}

public func stop() throws {
guard node != nil else {
throw IpfsError.nodeNotStarted
}

try self.node?.close()
}
}
12 changes: 6 additions & 6 deletions ios/GomobileIPFS/Classes/Repo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public enum RepoError: Error {

public class Repo {
let goRepo: MobileRepo

private let url: URL

public init(_ url: URL) throws {
self.url = url

Expand All @@ -34,26 +34,26 @@ public class Repo {
public static func isInitialized(url: URL) throws -> Bool{
return MobileRepoIsInitialized(url.path)
}

public static func initialize(url: URL, config: Config) throws {
var err: NSError?
var isDirectory: ObjCBool = true
let exist = FileManager.default.fileExists(atPath: url.path, isDirectory: &isDirectory)
if !exist {
try FileManager.default.createDirectory(atPath: url.path, withIntermediateDirectories: true, attributes: nil)
}

MobileInitRepo(url.path, config.goConfig, &err)
if let error = err {
throw RepoError.runtimeError(error, "failed to init repo")
}
}

public func getConfig() throws -> Config {
let goconfig = try self.goRepo.getConfig()
return Config(goconfig)
}

public func setConfig(_ config: Config) throws {
try self.goRepo.setConfig(config.goConfig)
}
Expand Down

0 comments on commit 3388954

Please sign in to comment.