Skip to content

Commit

Permalink
Merge pull request #17 from ghorbani-m/main
Browse files Browse the repository at this point in the history
feat:add multi-thread to android module.
  • Loading branch information
farhoud authored Jul 7, 2022
2 parents 56ede3d + 4744f03 commit 4dcee08
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 35 deletions.
83 changes: 48 additions & 35 deletions android/src/main/java/land/fx/FulaModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,107 +65,120 @@ public String getName() {

@ReactMethod
public void send(String path, Promise promise) {
try{
ThreadUtils.runOnExecutor(() -> {
try{
String cid = fula.send(path);
promise.resolve(cid);
}
catch(Exception e){
}
catch(Exception e){
promise.reject(e);
}
}
});
}

@ReactMethod
public void encryptSend(String path, Promise promise) {
try{
ThreadUtils.runOnExecutor(() -> {
try{
String res = fula.encryptSend(path);
Log.d(NAME,res);
promise.resolve(res);
}
catch(Exception e){
}
catch(Exception e){
promise.reject(e);
}
}
});
}

@ReactMethod
public void addBox(String boxId, Promise promise) {
Log.d("fulaModule", appDir);
try{
ThreadUtils.runOnExecutor(() -> {
try{
fula.addBox(boxId);
promise.resolve(true);
}
catch(Exception e){
}
catch(Exception e){
promise.reject(e);
}
}
});
}

@ReactMethod
public void receiveFileInfo(String fileId, Promise promise){
try{
ThreadUtils.runOnExecutor(() -> {
try{
byte[] res = fula.receiveFileInfo(fileId);
WritableNativeArray arr = new WritableNativeArray();
for(byte b : res){
arr.pushInt(b);
arr.pushInt(b);
}
promise.resolve(arr);
}catch (Exception e){
}catch (Exception e){
promise.reject(e);
}
}
});
}

@ReactMethod
public void receiveFile(String fileId, String fileName, boolean includeBS64,Promise promise) {
try{
ThreadUtils.runOnExecutor(() -> {
try{
String filePath = storeDirPath + fileName;
fula.receiveFile(fileId, filePath);
Uri uri = Uri.fromFile(new File(filePath));
Log.d(NAME,"File Downloaded");
WritableMap map;
if(includeBS64){
String bs64 = getBase64StringFromFile(filePath);
Log.d(NAME,"File Transform to bs64");
map = makeResponseMap(uri.toString(), bs64);
String bs64 = getBase64StringFromFile(filePath);
Log.d(NAME,"File Transform to bs64");
map = makeResponseMap(uri.toString(), bs64);
}else{
map = makeResponseMap(uri.toString(), "");
map = makeResponseMap(uri.toString(), "");
}
promise.resolve(map);
}catch (Exception e){
}catch (Exception e){
promise.reject(e);
}
}
});
}

@ReactMethod
public void receiveDecryptFile(String fileRef, String fileName, boolean includeBS64,Promise promise) {
try{
ThreadUtils.runOnExecutor(() -> {
try{
String filePath = storeDirPath + fileName;
fula.receiveDecryptFile(fileRef, filePath);
Uri uri = Uri.fromFile(new File(filePath));
Log.d(NAME,"File Downloaded");
WritableMap map;
if(includeBS64){
String bs64 = getBase64StringFromFile(filePath);
Log.d(NAME,"File Transform to bs64");
map = makeResponseMap(uri.toString(), bs64);
String bs64 = getBase64StringFromFile(filePath);
Log.d(NAME,"File Transform to bs64");
map = makeResponseMap(uri.toString(), bs64);
}else{
map = makeResponseMap(uri.toString(), "");
map = makeResponseMap(uri.toString(), "");
}
promise.resolve(map);
}catch (Exception e){
}catch (Exception e){
promise.reject(e);
}
}
});
}

@ReactMethod
public void graphQL(String query, String variableValues, Promise promise) {
try{
ThreadUtils.runOnExecutor(() -> {
try{
byte[] res = fula.graphQL(query, variableValues);
WritableNativeArray arr = new WritableNativeArray();
for(byte b : res){
arr.pushInt(b);
arr.pushInt(b);
}
promise.resolve(arr);
}catch (Exception e){
}catch (Exception e){
promise.reject(e);
}
}
});
}

private static WritableMap makeResponseMap(String uri, String base64) {
Expand Down
42 changes: 42 additions & 0 deletions android/src/main/java/land/fx/ThreadUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package land.fx.fula;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

final class ThreadUtils {
/**
* Thread which will be used to call all Libp2p APIs. They
* they don't run on the calling thread anyway, we are deferring the calls
* to this thread to avoid (potentially) blocking the calling thread.
*/
private static final ExecutorService executor
= Executors.newSingleThreadExecutor();

/**
* Runs the given {@link Runnable} on the executor.
* @param runnable
*/
public static void runOnExecutor(Runnable runnable) {
executor.execute(runnable);
}

/**
* Submits the given {@link Callable} to be run on the executor.
* @param callable
* @return Future.
*/
public static <T> Future<T> submitToExecutor(Callable<T> callable) {
return executor.submit(callable);
}

/**
* Submits the given {@link Runnable} to be run on the executor.
* @param runnable
* @return Future.
*/
public static Future<?> submitToExecutor(Runnable runnable) {
return executor.submit(runnable);
}
}

0 comments on commit 4dcee08

Please sign in to comment.