Skip to content

Commit

Permalink
feat: implement Android IPFS class
Browse files Browse the repository at this point in the history
  • Loading branch information
aeddi committed Dec 1, 2019
1 parent 9d82641 commit aef1689
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 110 deletions.
7 changes: 7 additions & 0 deletions android/.idea/dictionaries/aeddi.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@

import org.json.JSONObject;

import ipfs.gomobile.android.Bridge;
import ipfs.gomobile.android.IPFS;

public class MainActivity extends AppCompatActivity {
static private final String TAG = "IPFS_Mobile_Example";
private Bridge bridge;
private IPFS bridge;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

bridge = new Bridge(getApplicationContext());
bridge = new IPFS(getApplicationContext());

new AsyncTask<Void, Void, String>() {
@Override
Expand Down
106 changes: 0 additions & 106 deletions android/bridge/src/main/java/ipfs/gomobile/android/Bridge.java

This file was deleted.

151 changes: 151 additions & 0 deletions android/bridge/src/main/java/ipfs/gomobile/android/IPFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package ipfs.gomobile.android;

import android.content.Context;
import android.util.Base64;
import android.util.Log;

import androidx.annotation.NonNull;

import java.io.File;

import mobile.Mobile;
import mobile.Config;
import mobile.Repo;
import mobile.Node;
import mobile.Shell;

public final class IPFS {
// Default paths
private static final String defaultRepoPath = "/ipfs/repo";
private static final String apiSockFilename = "api.sock";

// Go IPFS objects
private final Repo repo;
private Node node;
private Shell shell;

public IPFS(@NonNull Context context)
throws ConfigCreationException, RepoInitException {
this(context, defaultRepoPath);
}

public IPFS(@NonNull Context context, @NonNull String repoPath)
throws ConfigCreationException, RepoInitException {
String absRepoPath = context.getFilesDir().getAbsolutePath() + repoPath;

if (!Mobile.repoIsInitialized(absRepoPath)) {
Config config;

try {
config = Mobile.newDefaultConfig();
config.setupUnixSocketAPI(apiSockFilename);
} catch (Exception e) {
throw new ConfigCreationException("Config creation failed", e);
}

final File repoDir = new File(absRepoPath);
if (!repoDir.exists()) {
if (!repoDir.mkdirs()) {
throw new RepoInitException("Repo directory creation failed");
}
}
try {
Mobile.initRepo(absRepoPath, config);
} catch (Exception e) {
throw new RepoInitException("Repo initialisation failed", e);
}
}

try {
repo = Mobile.openRepo(absRepoPath);
} catch (Exception e) {
throw new RepoInitException("Repo opening failed", e);
}
}

synchronized public void start() throws NodeStartException, ShellInitException {
if (node != null) {
throw new NodeStartException("Node already started");
}

try {
node = Mobile.newNode(repo);
} catch (Exception e) {
throw new NodeStartException("Node start failed", e);
}

try {
shell = Mobile.newUDSShell(apiSockFilename);
} catch (Exception e) {
throw new ShellInitException("Shell init failed", e);
}
}

synchronized public void stop() throws NodeStopException {
if (node == null) {
throw new NodeStopException("Node not started yet");
}

try {
node.close();
node = null;
} catch (Exception e) {
throw new NodeStopException("Node stop failed", e);
}
}

synchronized public void restart()
throws NodeStartException, ShellInitException, NodeStopException {
stop();
start();
}

synchronized public String shellRequest(String command, String b64Body)
throws ShellRequestException {
if (node == null) {
throw new ShellRequestException("Shell request failed: node isn't started");
}

try {
byte[] req = new byte[0];

if (b64Body.length() > 0) {
req = Base64.decode(b64Body, Base64.DEFAULT);
}

byte[] res = shell.request(command, req);

return Base64.encodeToString(res, Base64.DEFAULT);
} catch (Exception err) {
throw new ShellRequestException("Shell request failed", err);
}
}

public class ConfigCreationException extends Exception {
public ConfigCreationException(String message, Throwable err) { super(message, err); }
}

public class ShellInitException extends Exception {
public ShellInitException(String message, Throwable err) { super(message, err); }
}

public class ShellRequestException extends Exception {
public ShellRequestException(String message) { super(message); }
public ShellRequestException(String message, Throwable err) { super(message, err); }
}

public class RepoInitException extends Exception {
public RepoInitException(String message) { super(message); }
public RepoInitException(String message, Throwable err) { super(message, err); }
}

public class NodeStartException extends Exception {
public NodeStartException(String message) { super(message); }
public NodeStartException(String message, Throwable err) { super(message, err); }
}

public class NodeStopException extends Exception {
public NodeStopException(String message) { super(message); }
public NodeStopException(String message, Throwable err) { super(message, err); }
}
}
2 changes: 1 addition & 1 deletion go/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (r *Repo) GetRootPath() string {
return r.path
}

func (r *Repo) SetConfig(c Config) error {
func (r *Repo) SetConfig(c *Config) error {
return r.irepo.SetConfig(c.getConfig())
}

Expand Down

0 comments on commit aef1689

Please sign in to comment.