optimize receive file

This commit is contained in:
CaiHQ 2021-12-29 11:52:24 +08:00
parent bc4eb8b828
commit 9f30c938da
2 changed files with 92 additions and 53 deletions

View File

@ -3,10 +3,7 @@ package org.bdware.server.action;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.bdware.sc.ContractClient; import org.bdware.sc.*;
import org.bdware.sc.ContractManager;
import org.bdware.sc.ContractMeta;
import org.bdware.sc.ContractStatusEnum;
import org.bdware.sc.bean.Contract; import org.bdware.sc.bean.Contract;
import org.bdware.sc.bean.ContractExecType; import org.bdware.sc.bean.ContractExecType;
import org.bdware.sc.conn.ResultCallback; import org.bdware.sc.conn.ResultCallback;
@ -179,8 +176,9 @@ public class TemporyTestAction {
} }
@Action(async = true) @Action(async = true)
public void reconnectAll(JsonObject args, ResultCallback resultCallback) { public void reconnectPort(JsonObject args, ResultCallback resultCallback) {
ContractManager.instance.reconnectContractProcess(); ContractPort.PortVisitor reconnectVisitor = CMActions.manager.statusRecorder.getVisitor();
reconnectVisitor.visit(args.get("port").getAsInt());
String data = ContractManager.instance.listContractsWithOwner( String data = ContractManager.instance.listContractsWithOwner(
args.get("owner").getAsString(), null, 0); args.get("owner").getAsString(), null, 0);
JsonObject ret = new JsonObject(); JsonObject ret = new JsonObject();

View File

@ -40,7 +40,6 @@ public class NodeCenterClientController implements NodeCenterConn {
private static final Logger LOGGER = LogManager.getLogger(NodeCenterClientController.class); private static final Logger LOGGER = LogManager.getLogger(NodeCenterClientController.class);
public static SyncResult sync = new SyncResult(); public static SyncResult sync = new SyncResult();
private static boolean startCheck = false; private static boolean startCheck = false;
private final Map<String, FileOutputStream> fileMap;
private final NetNeighbors neighbors; private final NetNeighbors neighbors;
public Map<String, ResultCallback> distributeReqMap = new ConcurrentHashMap<>(); public Map<String, ResultCallback> distributeReqMap = new ConcurrentHashMap<>();
// public NodeCenterClientController cmClientController; // public NodeCenterClientController cmClientController;
@ -48,9 +47,11 @@ public class NodeCenterClientController implements NodeCenterConn {
NodeCenterClientHandler handler; NodeCenterClientHandler handler;
// 合约contractIDmaster的公钥 // 合约contractIDmaster的公钥
Map<String, String> contractID2PubKey = new ConcurrentHashMap<>(); Map<String, String> contractID2PubKey = new ConcurrentHashMap<>();
Deque<JsonObject> receiveQueue = new ArrayDeque<>();
ReceiveFileThread receiveFileThread = new ReceiveFileThread();
public NodeCenterClientController(String nodeID) { public NodeCenterClientController(String nodeID) {
this.fileMap = new HashMap<>();
this.nodeID = nodeID; this.nodeID = nodeID;
this.neighbors = new NetNeighbors(); this.neighbors = new NetNeighbors();
} }
@ -518,22 +519,49 @@ public class NodeCenterClientController implements NodeCenterConn {
queryNCRepoDOI(json, result); queryNCRepoDOI(json, result);
} }
@Action(async = true)
public void receiveProject(JsonObject args, final ResultCallback rc) { class ReceiveFileThread extends Thread {
LOGGER.debug("position----7"); private final Map<String, FileOutputStream> fileMap = new HashMap<>();
ReceiveFileThread() {
super();
this.start();
}
public void run() {
for (; ; ) {
if (receiveQueue.size() > 0) {
try {
JsonObject jo = receiveQueue.poll();
receiveProject(jo);
} catch (Exception e) {
e.printStackTrace();
}
} else {
synchronized (ReceiveFileThread.this) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public void receiveProject(JsonObject args) {
String fileName = args.get("fileName").getAsString(); String fileName = args.get("fileName").getAsString();
boolean isAppend = args.get("isAppend").getAsBoolean(); boolean isAppend = args.get("isAppend").getAsBoolean();
boolean isDone = args.get("isDone").getAsBoolean(); boolean isDone = args.get("isDone").getAsBoolean();
boolean isPrivate = args.get("isPrivate").getAsBoolean(); boolean isPrivate = args.get("isPrivate").getAsBoolean();
LOGGER.debug( LOGGER.debug(
String.format("isAppend=%b isDone=%b isPrivate=%b", isAppend, isDone, isPrivate)); String.format("isAppend=%b isDone=%b isPrivate=%b", isAppend, isDone, isPrivate));
String path = GlobalConf.instance.publicCompiledDir; String path = GlobalConf.instance.publicCompiledDir;
if (isPrivate && args.has("pubKey")) { if (isPrivate && args.has("pubKey")) {
path = GlobalConf.instance.privateCompiledDir + "/" + args.get("pubKey").getAsString(); path = GlobalConf.instance.privateCompiledDir + "/" + args.get("pubKey").getAsString();
} }
File dir = new File(path); File dir = new File(path);
if (!dir.exists()) { if (!dir.exists()) {
LOGGER.debug("mkdir " + dir.getAbsoluteFile() + ": " + dir.mkdirs()); LOGGER.debug("mkdir " + dir.getAbsoluteFile() + ": " + dir.mkdirs());
@ -563,7 +591,7 @@ public class NodeCenterClientController implements NodeCenterConn {
req.put("requestID", args.get("requestID").getAsString()); req.put("requestID", args.get("requestID").getAsString());
req.put("nodeID", nodeID); req.put("nodeID", nodeID);
req.put("progress", "100"); req.put("progress", "100");
rc.onResult(JsonUtil.toJson(req)); NetworkManager.instance.sendToNodeCenter(JsonUtil.toJson(req));
} else { } else {
String data = args.get("data").getAsString(); String data = args.get("data").getAsString();
byte[] byteData = ByteUtil.decodeBASE64(data); byte[] byteData = ByteUtil.decodeBASE64(data);
@ -576,6 +604,19 @@ public class NodeCenterClientController implements NodeCenterConn {
} }
} }
} }
}
@Action(async = false)
public synchronized void receiveProject(JsonObject args, final ResultCallback rc) {
try {
receiveQueue.add(args);
synchronized (receiveFileThread) {
receiveFileThread.notify();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void queryNCRepoDOI(JsonObject json, ResultCallback result) { public void queryNCRepoDOI(JsonObject json, ResultCallback result) {
LOGGER.debug("sendProject: position ---- 3"); LOGGER.debug("sendProject: position ---- 3");