diff --git a/ios/Example/GomobileIPFS/ViewController.swift b/ios/Example/GomobileIPFS/ViewController.swift index a8b5aae0..956322a1 100644 --- a/ios/Example/GomobileIPFS/ViewController.swift +++ b/ios/Example/GomobileIPFS/ViewController.swift @@ -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)" } } diff --git a/ios/GomobileIPFS/Classes/Config.swift b/ios/GomobileIPFS/Classes/Config.swift index 6fd4c60a..0a01f30d 100644 --- a/ios/GomobileIPFS/Classes/Config.swift +++ b/ios/GomobileIPFS/Classes/Config.swift @@ -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!) } @@ -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 { @@ -55,9 +55,9 @@ 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: []) { @@ -65,22 +65,22 @@ public class Config { 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) diff --git a/ios/GomobileIPFS/Classes/IPFS.swift b/ios/GomobileIPFS/Classes/IPFS.swift index 93604d23..2865b91b 100644 --- a/ios/GomobileIPFS/Classes/IPFS.swift +++ b/ios/GomobileIPFS/Classes/IPFS.swift @@ -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 { @@ -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 } @@ -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() - } } diff --git a/ios/GomobileIPFS/Classes/Repo.swift b/ios/GomobileIPFS/Classes/Repo.swift index bb77b0be..bd867b0c 100644 --- a/ios/GomobileIPFS/Classes/Repo.swift +++ b/ios/GomobileIPFS/Classes/Repo.swift @@ -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 @@ -34,7 +34,7 @@ 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 @@ -42,18 +42,18 @@ public class Repo { 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) }