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,65 +519,105 @@ public class NodeCenterClientController implements NodeCenterConn {
queryNCRepoDOI(json, result); queryNCRepoDOI(json, result);
} }
@Action(async = true)
public void receiveProject(JsonObject args, final ResultCallback rc) {
LOGGER.debug("position----7");
String fileName = args.get("fileName").getAsString(); class ReceiveFileThread extends Thread {
boolean isAppend = args.get("isAppend").getAsBoolean(); private final Map<String, FileOutputStream> fileMap = new HashMap<>();
boolean isDone = args.get("isDone").getAsBoolean();
boolean isPrivate = args.get("isPrivate").getAsBoolean();
LOGGER.debug( ReceiveFileThread() {
String.format("isAppend=%b isDone=%b isPrivate=%b", isAppend, isDone, isPrivate)); super();
String path = GlobalConf.instance.publicCompiledDir; this.start();
if (isPrivate && args.has("pubKey")) {
path = GlobalConf.instance.privateCompiledDir + "/" + args.get("pubKey").getAsString();
} }
File dir = new File(path); public void run() {
if (!dir.exists()) { for (; ; ) {
LOGGER.debug("mkdir " + dir.getAbsoluteFile() + ": " + dir.mkdirs()); if (receiveQueue.size() > 0) {
} try {
FileOutputStream fout = null; JsonObject jo = receiveQueue.poll();
if (!isAppend) { receiveProject(jo);
try { } catch (Exception e) {
fout = new FileOutputStream(new File(dir, fileName)); e.printStackTrace();
fileMap.put(fileName, fout); }
} catch (FileNotFoundException e) { } else {
e.printStackTrace(); synchronized (ReceiveFileThread.this) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} }
} else {
fout = fileMap.get(fileName);
} }
if (isDone) {
if (fout != null) public void receiveProject(JsonObject args) {
String fileName = args.get("fileName").getAsString();
boolean isAppend = args.get("isAppend").getAsBoolean();
boolean isDone = args.get("isDone").getAsBoolean();
boolean isPrivate = args.get("isPrivate").getAsBoolean();
LOGGER.debug(
String.format("isAppend=%b isDone=%b isPrivate=%b", isAppend, isDone, isPrivate));
String path = GlobalConf.instance.publicCompiledDir;
if (isPrivate && args.has("pubKey")) {
path = GlobalConf.instance.privateCompiledDir + "/" + args.get("pubKey").getAsString();
}
File dir = new File(path);
if (!dir.exists()) {
LOGGER.debug("mkdir " + dir.getAbsoluteFile() + ": " + dir.mkdirs());
}
FileOutputStream fout = null;
if (!isAppend) {
try { try {
fout.close(); fout = new FileOutputStream(new File(dir, fileName));
fileMap.remove(fileName); fileMap.put(fileName, fout);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} else {
fout = fileMap.get(fileName);
}
if (isDone) {
if (fout != null)
try {
fout.close();
fileMap.remove(fileName);
} catch (IOException e) {
e.printStackTrace();
}
LOGGER.debug("receive finish.");
Map<String, String> req = new HashMap<>();
req.put("action", "onReceiveProject");
req.put("requestID", args.get("requestID").getAsString());
req.put("nodeID", nodeID);
req.put("progress", "100");
NetworkManager.instance.sendToNodeCenter(JsonUtil.toJson(req));
} else {
String data = args.get("data").getAsString();
byte[] byteData = ByteUtil.decodeBASE64(data);
try {
if (null != fout && null != byteData) {
fout.write(byteData);
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
LOGGER.debug("receive finish.");
Map<String, String> req = new HashMap<>();
req.put("action", "onReceiveProject");
req.put("requestID", args.get("requestID").getAsString());
req.put("nodeID", nodeID);
req.put("progress", "100");
rc.onResult(JsonUtil.toJson(req));
} else {
String data = args.get("data").getAsString();
byte[] byteData = ByteUtil.decodeBASE64(data);
try {
if (null != fout && null != byteData) {
fout.write(byteData);
}
} catch (IOException e) {
e.printStackTrace();
} }
} }
} }
@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");
String projectName = json.get("projectName").getAsString(); String projectName = json.get("projectName").getAsString();