auto prune

This commit is contained in:
CaiHQ 2023-12-15 16:41:27 +08:00
parent 22532666dd
commit 540a4232d0
9 changed files with 547 additions and 648 deletions

View File

@ -4,6 +4,7 @@ plugins {
} }
mainClassName = 'org.bdware.server.CMHttpServer' mainClassName = 'org.bdware.server.CMHttpServer'
apply from: '../spotless.gradle'
application { application {

View File

@ -57,9 +57,8 @@ public class CMHttpServer {
private static final Logger LOGGER = LogManager.getLogger(CMHttpServer.class); private static final Logger LOGGER = LogManager.getLogger(CMHttpServer.class);
private static final String CONFIG_PATH = "cmconfig.json"; private static final String CONFIG_PATH = "cmconfig.json";
public static EventLoopGroup workerGroup = new NioEventLoopGroup(); public static EventLoopGroup workerGroup = new NioEventLoopGroup();
public static MultiIndexTimeRocksDBUtil nodeLogDB = public static MultiIndexTimeRocksDBUtil nodeLogDB = new MultiIndexTimeRocksDBUtil(
new MultiIndexTimeRocksDBUtil( "./ContractManagerDB", CMTables.LocalNodeLogDB.toString());
"./ContractManagerDB", CMTables.LocalNodeLogDB.toString());
public static URLClassLoader pluginLoader; public static URLClassLoader pluginLoader;
private static SslContext sslContext = null; private static SslContext sslContext = null;
private static CMDConf cmdConf; private static CMDConf cmdConf;
@ -89,23 +88,21 @@ public class CMHttpServer {
GlobalConf.initDOAConfig(doaConf); GlobalConf.initDOAConfig(doaConf);
if (cmdConf.withBdledgerServer) { if (cmdConf.withBdledgerServer) {
ContractManager.threadPool.execute( ContractManager.threadPool
() -> NetworkManager.instance.initP2P(cmdConf.servicePort + 4)); .execute(() -> NetworkManager.instance.initP2P(cmdConf.servicePort + 4));
} }
// 可自动运行bdledger可执行文件也可在shell脚步中运行和停止 // 可自动运行bdledger可执行文件也可在shell脚步中运行和停止
if (!cmdConf.withBdledgerClient.isEmpty()) { if (!cmdConf.withBdledgerClient.isEmpty()) {
ContractManager.scheduledThreadPool.schedule( ContractManager.scheduledThreadPool.schedule(() -> {
() -> { File ledgerClient = new File(cmdConf.withBdledgerClient);
File ledgerClient = new File(cmdConf.withBdledgerClient); LOGGER.debug("canRead=" + ledgerClient.canRead() + " path="
LOGGER.debug("canRead=" + ledgerClient.canRead() + + ledgerClient.getAbsolutePath());
" path=" + ledgerClient.getAbsolutePath()); try {
try { Runtime.getRuntime().exec(ledgerClient.getAbsolutePath());
Runtime.getRuntime().exec(ledgerClient.getAbsolutePath()); } catch (IOException e) {
} catch (IOException e) { LOGGER.warn("start bdledger client failed: " + e.getMessage());
LOGGER.warn("start bdledger client failed: " + e.getMessage()); }
} }, 1, TimeUnit.SECONDS);
},
1, TimeUnit.SECONDS);
} }
if (cmdConf.enableEventPersistence) { if (cmdConf.enableEventPersistence) {
ContractManager.eventPersistenceEnabled = true; ContractManager.eventPersistenceEnabled = true;
@ -115,15 +112,12 @@ public class CMHttpServer {
String[] filePaths = cmdConf.enableSsl.split(":"); String[] filePaths = cmdConf.enableSsl.split(":");
File chainedFile = new File(filePaths[0]), keyFile = new File(filePaths[1]); File chainedFile = new File(filePaths[0]), keyFile = new File(filePaths[1]);
if (chainedFile.exists() && keyFile.exists()) { if (chainedFile.exists() && keyFile.exists()) {
sslContext = sslContext = SslContextBuilder.forServer(chainedFile, keyFile)
SslContextBuilder.forServer(chainedFile, keyFile) .ciphers(null,
.ciphers( (ciphers, defaultCiphers, supportedCiphers) -> defaultCiphers
null, .stream().filter(x -> null != x && !x.contains("RC4"))
(ciphers, defaultCiphers, supportedCiphers) -> .toArray(String[]::new))
defaultCiphers.stream() .build();
.filter(x -> null != x && !x.contains("RC4"))
.toArray(String[]::new))
.build();
LOGGER.info("openssl isAvailable:" + OpenSsl.isAvailable()); LOGGER.info("openssl isAvailable:" + OpenSsl.isAvailable());
} }
} catch (Exception e) { } catch (Exception e) {
@ -143,7 +137,8 @@ public class CMHttpServer {
// plugins // plugins
CMHttpHandler.wsPluginActions = parseStrAsList(cmdConf.wsPluginActions); CMHttpHandler.wsPluginActions = parseStrAsList(cmdConf.wsPluginActions);
TCPClientFrameHandler.clientToAgentPlugins = parseStrAsList(cmdConf.clientToAgentPlugins); TCPClientFrameHandler.clientToAgentPlugins = parseStrAsList(cmdConf.clientToAgentPlugins);
NodeCenterClientHandler.clientToClusterPlugins = parseStrAsList(cmdConf.clientToClusterPlugins); NodeCenterClientHandler.clientToClusterPlugins =
parseStrAsList(cmdConf.clientToClusterPlugins);
org.bdware.units.tcp.TCPClientFrameHandler.tcpPlugins = parseStrAsList(cmdConf.tcpPlugins); org.bdware.units.tcp.TCPClientFrameHandler.tcpPlugins = parseStrAsList(cmdConf.tcpPlugins);
if (!StringUtil.isNullOrEmpty(cmdConf.debug)) { if (!StringUtil.isNullOrEmpty(cmdConf.debug)) {
try { try {
@ -188,7 +183,7 @@ public class CMHttpServer {
private static String[] parseStrAsList(String str) { private static String[] parseStrAsList(String str) {
if (str == null) { if (str == null) {
return new String[]{}; return new String[] {};
} }
return str.split(","); return str.split(",");
} }
@ -196,7 +191,7 @@ public class CMHttpServer {
private static void addDirToPath(String s) { private static void addDirToPath(String s) {
try { try {
LOGGER.info("add to path: " + s); LOGGER.info("add to path: " + s);
// Field field = ClassLoader.class.getDeclaredField("sys_paths"); // Field field = ClassLoader.class.getDeclaredField("sys_paths");
Field field = ClassLoader.class.getDeclaredField("usr_paths"); Field field = ClassLoader.class.getDeclaredField("usr_paths");
field.setAccessible(true); field.setAccessible(true);
String[] path = (String[]) field.get(null); String[] path = (String[]) field.get(null);
@ -246,19 +241,15 @@ public class CMHttpServer {
try { try {
BufferedReader br = new BufferedReader(new FileReader(keyFile)); BufferedReader br = new BufferedReader(new FileReader(keyFile));
String pubKey = br.readLine(); String pubKey = br.readLine();
String nowManager = String nowManager = KeyValueDBUtil.instance.getValue(CMTables.ConfigDB.toString(),
KeyValueDBUtil.instance.getValue( "__NodeManager__");
CMTables.ConfigDB.toString(), "__NodeManager__");
// manager.key is used when node manager isn' set // manager.key is used when node manager isn' set
if (null == nowManager || nowManager.isEmpty()) { if (null == nowManager || nowManager.isEmpty()) {
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(CMTables.ConfigDB.toString(),
CMTables.ConfigDB.toString(), "__NodeManager__", pubKey); "__NodeManager__", pubKey);
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(CMTables.NodeRole.toString(), pubKey,
CMTables.NodeRole.toString(), pubKey,
"NodeManager,ContractProvider,ContractUser,ContractInstanceManager"); "NodeManager,ContractProvider,ContractUser,ContractInstanceManager");
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(CMTables.NodeTime.toString(), pubKey,
CMTables.NodeTime.toString(),
pubKey,
Long.toString(new Date().getTime())); Long.toString(new Date().getTime()));
LOGGER.info("set node manager from manager.key"); LOGGER.info("set node manager from manager.key");
} }
@ -266,7 +257,7 @@ public class CMHttpServer {
} }
} }
GlobalConf.initIpPort(cmdConf.ip + ":" + cmdConf.servicePort); GlobalConf.initIpPort(cmdConf.ip + ":" + cmdConf.servicePort);
//GlobalConf.initMasterAddress(cmdConf.ip + ":" + (cmdConf.servicePort + 1)); // GlobalConf.initMasterAddress(cmdConf.ip + ":" + (cmdConf.servicePort + 1));
start(cmdConf.servicePort); start(cmdConf.servicePort);
} }
@ -274,7 +265,7 @@ public class CMHttpServer {
LOGGER.info("start server at:" + port); LOGGER.info("start server at:" + port);
LOGGER.debug("dir:" + new File("./").getAbsolutePath()); LOGGER.debug("dir:" + new File("./").getAbsolutePath());
new CMHttpServer(port).start(); new CMHttpServer(port).start();
//never reach here!! // never reach here!!
} }
/** /**
@ -285,9 +276,9 @@ public class CMHttpServer {
// EpollEventLoopGroup // EpollEventLoopGroup
// EpollServerSocketChannel // EpollServerSocketChannel
// ContractManager.reconnectPort = (port - 18000) * 30 + 1630; // ContractManager.reconnectPort = (port - 18000) * 30 + 1630;
// if (ContractManager.reconnectPort < 0) ContractManager.reconnectPort = 1630; // if (ContractManager.reconnectPort < 0) ContractManager.reconnectPort = 1630;
File[] pluginJar = new File("./pluginLib/") File[] pluginJar =
.listFiles(pathname -> pathname.getName().endsWith(".jar")); new File("./pluginLib/").listFiles(pathname -> pathname.getName().endsWith(".jar"));
URL[] urls; URL[] urls;
if (pluginJar != null && pluginJar.length > 0) { if (pluginJar != null && pluginJar.length > 0) {
urls = new URL[pluginJar.length]; urls = new URL[pluginJar.length];
@ -300,7 +291,7 @@ public class CMHttpServer {
} }
} }
} else { } else {
urls = new URL[]{}; urls = new URL[] {};
} }
pluginLoader = new URLClassLoader(urls, CMHttpServer.class.getClassLoader()); pluginLoader = new URLClassLoader(urls, CMHttpServer.class.getClassLoader());
if (port >= 18000 && port < 18100) { if (port >= 18000 && port < 18100) {
@ -316,28 +307,20 @@ public class CMHttpServer {
try { try {
ServerBootstrap b1 = new ServerBootstrap(); ServerBootstrap b1 = new ServerBootstrap();
b1.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); b1.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
b1.group(bossGroup, workerGroup) b1.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
.channel(NioServerSocketChannel.class) .localAddress(port).childHandler(new ChannelInitializer<SocketChannel>() {
.localAddress(port) @Override
.childHandler( protected void initChannel(SocketChannel arg0) {
new ChannelInitializer<SocketChannel>() { if (sslContext != null) {
@Override arg0.pipeline().addLast(new OptionalSslHandler(sslContext));
protected void initChannel(SocketChannel arg0) { }
if (sslContext != null) { arg0.pipeline().addLast(trafficSharp).addLast(new HttpServerCodec())
arg0.pipeline().addLast(new OptionalSslHandler(sslContext)); .addLast(new HttpObjectAggregator(10 * 1024 * 1024))
} .addLast(new WebSocketServerProtocolHandler(PATH, null, true))
arg0.pipeline() .addLast(new ChunkedWriteHandler()).addLast(serverHandler)
.addLast(trafficSharp) .addLast(new ContractManagerFrameHandler());
.addLast(new HttpServerCodec()) }
.addLast(new HttpObjectAggregator(10 * 1024 * 1024)) });
.addLast(
new WebSocketServerProtocolHandler(
PATH, null, true))
.addLast(new ChunkedWriteHandler())
.addLast(serverHandler)
.addLast(new ContractManagerFrameHandler());
}
});
final Channel ch = b1.bind(port).sync().channel(); final Channel ch = b1.bind(port).sync().channel();
LOGGER.debug("[CMHttpServer] listen master port at:" + port); LOGGER.debug("[CMHttpServer] listen master port at:" + port);
new HTTPServer(port + 3); new HTTPServer(port + 3);
@ -358,37 +341,35 @@ public class CMHttpServer {
private void loadStartContractConfiguration() { private void loadStartContractConfiguration() {
if (cmdConf.startContract != null && cmdConf.startContract.size() > 0) { if (cmdConf.startContract != null && cmdConf.startContract.size() > 0) {
ContractManager.scheduledThreadPool.schedule( ContractManager.scheduledThreadPool.schedule(() -> {
() -> { for (JsonElement je : cmdConf.startContract) {
for (JsonElement je : cmdConf.startContract) { try {
try { JsonObject jo = je.getAsJsonObject();
JsonObject jo = je.getAsJsonObject(); if (!jo.has("path"))
if (!jo.has("path")) continue;
continue; if (!jo.has("owner"))
if (!jo.has("owner")) jo.addProperty("owner", UserManagerAction.getNodeManager());
jo.addProperty("owner", UserManagerAction.getNodeManager()); if (jo.has("killBeforeStart")) {
if (jo.has("killBeforeStart")) { ContractManager.instance
ContractManager.instance.stopContract(jo.get("killBeforeStart").getAsString()); .stopContract(jo.get("killBeforeStart").getAsString());
}
if (jo.get("path").getAsString().startsWith("@")) {
jo.addProperty("bcoId", jo.get("path").getAsString().substring(1));
ContractRepositoryMain.currentHandler.startUsingJsonObject(jo);
} else {
startByPath(jo);
}
} catch (Exception e) {
e.printStackTrace();
}
} }
}, if (jo.get("path").getAsString().startsWith("@")) {
10, TimeUnit.SECONDS); jo.addProperty("bcoId", jo.get("path").getAsString().substring(1));
ContractRepositoryMain.currentHandler.startUsingJsonObject(jo);
} else {
startByPath(jo);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, 10, TimeUnit.SECONDS);
} }
} }
@Sharable @Sharable
public static class NettyConnectServerHandler extends ChannelInboundHandlerAdapter { public static class NettyConnectServerHandler extends ChannelInboundHandlerAdapter {
public NettyConnectServerHandler(AtomicInteger connectNum) { public NettyConnectServerHandler(AtomicInteger connectNum) {}
}
@Override @Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception { public void channelRegistered(ChannelHandlerContext ctx) throws Exception {

View File

@ -17,7 +17,6 @@ import org.zz.gmhelper.SM2KeyPair;
import org.zz.gmhelper.SM2Util; import org.zz.gmhelper.SM2Util;
import java.io.File; import java.io.File;
import java.net.URL;
import java.util.logging.Level; import java.util.logging.Level;
public class GlobalConf { public class GlobalConf {
@ -54,7 +53,7 @@ public class GlobalConf {
public String bcoDir; public String bcoDir;
public String memoryDir; public String memoryDir;
// public String masterAddress; // public String masterAddress;
public String ipPort; public String ipPort;
public boolean isLAN = true; public boolean isLAN = true;
private String nodeCenterUrl; // 从ConfigDB读 private String nodeCenterUrl; // 从ConfigDB读
@ -64,14 +63,11 @@ public class GlobalConf {
private static GlobalConf init() { private static GlobalConf init() {
java.util.logging.Logger.getLogger(org.bdware.bdledger.api.grpc.Client.class.getName()) java.util.logging.Logger.getLogger(org.bdware.bdledger.api.grpc.Client.class.getName())
.setLevel(Level.OFF); .setLevel(Level.OFF);
Configurator.setLevel( Configurator.setLevel("io.netty.handler.codec.http.websocketx.WebSocket08FrameEncoder",
"io.netty.handler.codec.http.websocketx.WebSocket08FrameEncoder",
org.apache.logging.log4j.Level.OFF); org.apache.logging.log4j.Level.OFF);
Configurator.setLevel( Configurator.setLevel("io.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder",
"io.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder",
org.apache.logging.log4j.Level.OFF); org.apache.logging.log4j.Level.OFF);
Configurator.setLevel( Configurator.setLevel("io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker",
"io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker",
org.apache.logging.log4j.Level.OFF); org.apache.logging.log4j.Level.OFF);
KeyValueDBUtil.setupCM(); KeyValueDBUtil.setupCM();
@ -83,19 +79,17 @@ public class GlobalConf {
String dbName = CMTables.ConfigDB.toString(); String dbName = CMTables.ConfigDB.toString();
if (!KeyValueDBUtil.instance.getKeys(dbName).contains("hasInited")) { if (!KeyValueDBUtil.instance.getKeys(dbName).contains("hasInited")) {
KeyValueDBUtil.instance.setValue(dbName, "hasInited", "true"); KeyValueDBUtil.instance.setValue(dbName, "hasInited", "true");
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(dbName, "projectDir",
dbName, "projectDir", new File("./BDWareProjectDir/").getAbsolutePath()); new File("./BDWareProjectDir/").getAbsolutePath());
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(dbName, "ADSPDir",
dbName,
"ADSPDir",
new File("./BDWareProjectDir/ADSPDir/").getAbsolutePath()); new File("./BDWareProjectDir/ADSPDir/").getAbsolutePath());
File f = new File("./yjs.jar"); File f = new File("./yjs.jar");
if (f.exists()) { if (f.exists()) {
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(dbName, "yjsPath",
dbName, "yjsPath", new File("./yjs.jar").getAbsolutePath()); new File("./yjs.jar").getAbsolutePath());
} else { } else {
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(dbName, "yjsPath",
dbName, "yjsPath", new File("./cp/yjs.jar").getAbsolutePath()); new File("./cp/yjs.jar").getAbsolutePath());
} }
conf.keyPairStr = SM2Util.generateSM2KeyPair().toJson(); conf.keyPairStr = SM2Util.generateSM2KeyPair().toJson();
@ -111,40 +105,27 @@ public class GlobalConf {
// long time = System.currentTimeMillis()+15811200000; // long time = System.currentTimeMillis()+15811200000;
String licence = String licence =
ByteHexUtil.encode( ByteHexUtil
SM2Util.encrypt( .encode(SM2Util.encrypt(conf.keyPair.getPublicKey(),
conf.keyPair.getPublicKey(), (HardwareInfo.getCPUID() + "=="
(HardwareInfo.getCPUID() + (System.currentTimeMillis() + 15811200000L))
+ "==" .getBytes()));
+ (System.currentTimeMillis()
+ 15811200000L))
.getBytes()));
LOGGER.debug("licence:" + licence); LOGGER.debug("licence:" + licence);
LOGGER.debug( LOGGER.debug(String.format("deviceName %s license %s %s==%d", deviceName, licence,
String.format( HardwareInfo.getCPUID(), (System.currentTimeMillis() + 15811200000L)));
"deviceName %s license %s %s==%d",
deviceName,
licence,
HardwareInfo.getCPUID(),
(System.currentTimeMillis() + 15811200000L)));
KeyValueDBUtil.instance.setValue(dbName, "licence", licence); KeyValueDBUtil.instance.setValue(dbName, "licence", licence);
KeyValueDBUtil.instance.setValue(dbName, "nodeName", deviceName.substring(0, 10)); KeyValueDBUtil.instance.setValue(dbName, "nodeName", deviceName.substring(0, 10));
KeyValueDBUtil.instance.setValue(dbName, "masterAddress", "null"); KeyValueDBUtil.instance.setValue(dbName, "masterAddress", "null");
KeyValueDBUtil.instance.setValue(dbName, "ipPort", "null"); KeyValueDBUtil.instance.setValue(dbName, "ipPort", "null");
KeyValueDBUtil.instance.setValue(dbName, "nodeCenter", "ws://127.0.0.1:18005"); KeyValueDBUtil.instance.setValue(dbName, "nodeCenter", "ws://127.0.0.1:18005");
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(dbName, "datachainConf",
dbName, "39.104.70.160:18091\n" + "47.98.247.70:18091\n" + "47.98.248.208:18091\n"
"datachainConf", + "39.104.77.165:18091\n" + "47.98.249.131:18091");
"39.104.70.160:18091\n"
+ "47.98.247.70:18091\n"
+ "47.98.248.208:18091\n"
+ "39.104.77.165:18091\n"
+ "47.98.249.131:18091");
} }
conf.projectDir = KeyValueDBUtil.instance.getValue(dbName, "projectDir"); conf.projectDir = KeyValueDBUtil.instance.getValue(dbName, "projectDir");
conf.bcoDir = conf.projectDir+"/bco"; conf.bcoDir = conf.projectDir + "/bco";
conf.publicDir = KeyValueDBUtil.instance.getValue(dbName, "projectDir") + "/public"; conf.publicDir = KeyValueDBUtil.instance.getValue(dbName, "projectDir") + "/public";
conf.ADSPDir = KeyValueDBUtil.instance.getValue(dbName, "projectDir") + "/ADSPDir"; conf.ADSPDir = KeyValueDBUtil.instance.getValue(dbName, "projectDir") + "/ADSPDir";
conf.publicCompiledDir = conf.publicCompiledDir =
@ -159,7 +140,7 @@ public class GlobalConf {
conf.name = KeyValueDBUtil.instance.getValue(dbName, "nodeName"); conf.name = KeyValueDBUtil.instance.getValue(dbName, "nodeName");
conf.ipPort = KeyValueDBUtil.instance.getValue(dbName, "ipPort"); conf.ipPort = KeyValueDBUtil.instance.getValue(dbName, "ipPort");
conf.isLAN = "true".equals(KeyValueDBUtil.instance.getValue(dbName, "isLAN")); conf.isLAN = "true".equals(KeyValueDBUtil.instance.getValue(dbName, "isLAN"));
// conf.masterAddress = KeyValueDBUtil.instance.getValue(dbName, "masterAddress"); // conf.masterAddress = KeyValueDBUtil.instance.getValue(dbName, "masterAddress");
conf.nodeCenterUrl = KeyValueDBUtil.instance.getValue(dbName, "nodeCenter"); conf.nodeCenterUrl = KeyValueDBUtil.instance.getValue(dbName, "nodeCenter");
conf.nodeCenterWSUrl = KeyValueDBUtil.instance.getValue(dbName, "nodeCenterWS"); conf.nodeCenterWSUrl = KeyValueDBUtil.instance.getValue(dbName, "nodeCenterWS");
conf.peerID = KeyValueDBUtil.instance.getValue(dbName, "peerID"); conf.peerID = KeyValueDBUtil.instance.getValue(dbName, "peerID");
@ -211,9 +192,8 @@ public class GlobalConf {
LOGGER.debug("[GlobalConf] ProjectDir Path:" + new File(conf.projectDir).getAbsolutePath()); LOGGER.debug("[GlobalConf] ProjectDir Path:" + new File(conf.projectDir).getAbsolutePath());
LOGGER.debug("[GlobalConf] publicDir Path:" + new File(conf.publicDir).getAbsolutePath()); LOGGER.debug("[GlobalConf] publicDir Path:" + new File(conf.publicDir).getAbsolutePath());
LOGGER.debug("[GlobalConf] publicDir Path:" + new File(conf.ADSPDir).getAbsolutePath()); LOGGER.debug("[GlobalConf] publicDir Path:" + new File(conf.ADSPDir).getAbsolutePath());
LOGGER.debug( LOGGER.debug("[GlobalConf] publicDirCompiled Path:"
"[GlobalConf] publicDirCompiled Path:" + new File(conf.publicCompiledDir).getAbsolutePath());
+ new File(conf.publicCompiledDir).getAbsolutePath());
LOGGER.debug( LOGGER.debug(
"[GlobalConf] PersonalDir Path:" + new File(conf.privateDir).getAbsolutePath()); "[GlobalConf] PersonalDir Path:" + new File(conf.privateDir).getAbsolutePath());
LOGGER.debug("[GlobalConf] memorytDir Path:" + new File(conf.memoryDir).getAbsolutePath()); LOGGER.debug("[GlobalConf] memorytDir Path:" + new File(conf.memoryDir).getAbsolutePath());
@ -250,16 +230,17 @@ public class GlobalConf {
private static void verifyLicence(GlobalConf conf) { private static void verifyLicence(GlobalConf conf) {
try { try {
// String pubkey = // String pubkey =
// "OTIzNmUzMGNmOGI1ZjFkMDBjZjEyMWY4OThmM2ZmYTIwNjE2ODYxOWNiMDNhMTVlM2FiZTA0OThhNTlkZDg1MmRi" + // "OTIzNmUzMGNmOGI1ZjFkMDBjZjEyMWY4OThmM2ZmYTIwNjE2ODYxOWNiMDNhMTVlM2FiZTA0OThhNTlkZDg1MmRi"
// "MjA5Njc1NmM3ZDBhOWM3YTNkOTg2NWVlYzk2YzM1MmY0MDdkMGMyOTA4M2NkNDI4YmY1YjM5M2U5OTA1" + // +
// "NWE0MzM0MTJhM2Y2ZDhkZWVmZDk4MmI4NmZiZTMyYjhlMGE3ZWFmZmE5ODM3M2E4ZTRmNTYyNDgxNTY0" + // "MjA5Njc1NmM3ZDBhOWM3YTNkOTg2NWVlYzk2YzM1MmY0MDdkMGMyOTA4M2NkNDI4YmY1YjM5M2U5OTA1" +
// "Yjk2ZjFkMTZiODk2MGRhZDAwMTNjZDYwOGZmOTcxNjdiOWI1MDU1MjJlMzk0ODhmODczNDJjNWUwOGRj" + // "NWE0MzM0MTJhM2Y2ZDhkZWVmZDk4MmI4NmZiZTMyYjhlMGE3ZWFmZmE5ODM3M2E4ZTRmNTYyNDgxNTY0" +
// "ZjFhZjFkYzBjODUxZjRlNDg2ZWIyOTM5NDI3MDc4MjA5NDg5ODliODVhZDNlOGJlNWJiYWEzZDUyMWU2" + // "Yjk2ZjFkMTZiODk2MGRhZDAwMTNjZDYwOGZmOTcxNjdiOWI1MDU1MjJlMzk0ODhmODczNDJjNWUwOGRj" +
// "MjdmZjE3NGY4Y2ZlZDk3NTY4OWNlNDEzOGYyMTgyOWIwMDVmMzE0YjM3MmNlZmFkZjBkNmUyOTY4ZGUz" + // "ZjFhZjFkYzBjODUxZjRlNDg2ZWIyOTM5NDI3MDc4MjA5NDg5ODliODVhZDNlOGJlNWJiYWEzZDUyMWU2" +
// "ZmFlNGUxNTFkMWFmNWE4Mjc4ZjQ2MDI5ODBjY2JkMDM0ZDE0YWRjZDk1ZjI1MjY3NmRlODRjYzdkNzU5" + // "MjdmZjE3NGY4Y2ZlZDk3NTY4OWNlNDEzOGYyMTgyOWIwMDVmMzE0YjM3MmNlZmFkZjBkNmUyOTY4ZGUz" +
// "NGYyYTAxMTliYWJmYjgyMGRjMWNjZWZjNThjNWUwYWRjMDQyM2MzYzA1ODNhZTU1MWZlN2Y5YTYwYjkx" + // "ZmFlNGUxNTFkMWFmNWE4Mjc4ZjQ2MDI5ODBjY2JkMDM0ZDE0YWRjZDk1ZjI1MjY3NmRlODRjYzdkNzU5" +
// "Zjg2YWViNDNlMzU0NzlhYWI5YmFjOTAwN2IsMTAwMDEsMA=="; // "NGYyYTAxMTliYWJmYjgyMGRjMWNjZWZjNThjNWUwYWRjMDQyM2MzYzA1ODNhZTU1MWZlN2Y5YTYwYjkx" +
// "Zjg2YWViNDNlMzU0NzlhYWI5YmFjOTAwN2IsMTAwMDEsMA==";
LOGGER.debug("beforedecode:" + conf.licence); LOGGER.debug("beforedecode:" + conf.licence);
if (conf.licence != null && conf.licence.length() > 8) { if (conf.licence != null && conf.licence.length() > 8) {
@ -267,11 +248,8 @@ public class GlobalConf {
try { try {
// byte[] arr = key.encode(new BASE64Decoder().decodeBuffer(conf.licence)); // byte[] arr = key.encode(new BASE64Decoder().decodeBuffer(conf.licence));
// String actualKey = new String(arr); // String actualKey = new String(arr);
String arr = String arr = new String(SM2Util.decrypt(conf.keyPair.getPrivateKeyParameter(),
new String( ByteHexUtil.decode(conf.licence)));
SM2Util.decrypt(
conf.keyPair.getPrivateKeyParameter(),
ByteHexUtil.decode(conf.licence)));
LOGGER.debug("[GlobalConf] actualKey:" + arr); LOGGER.debug("[GlobalConf] actualKey:" + arr);
@ -325,29 +303,30 @@ public class GlobalConf {
} }
} }
// public static void initMasterAddress(String val) { // public static void initMasterAddress(String val) {
// if ("null".equals(instance.masterAddress) || instance.masterAddress.startsWith("127.0.0.1")) { // if ("null".equals(instance.masterAddress) || instance.masterAddress.startsWith("127.0.0.1"))
// resetMasterAddress(val); // {
// } // resetMasterAddress(val);
// } // }
// }
public static void initDOAConfig(DOAConf doaConf) { public static void initDOAConfig(DOAConf doaConf) {
if (instance.doaConf == null || instance.doaConf.doipAddress == null) if (instance.doaConf == null || instance.doaConf.doipAddress == null)
resetDOAConfig(JsonUtil.toJson(doaConf)); resetDOAConfig(JsonUtil.toJson(doaConf));
} }
// public static boolean resetMasterAddress(String val) { // public static boolean resetMasterAddress(String val) {
// try { // try {
// instance.masterAddress = val; // instance.masterAddress = val;
// KeyValueDBUtil.instance.setValue(CMTables.ConfigDB.toString(), "masterAddress", val); // KeyValueDBUtil.instance.setValue(CMTables.ConfigDB.toString(), "masterAddress", val);
// NetworkManager.instance.reInitNodeCenter(); // NetworkManager.instance.reInitNodeCenter();
// return true; // return true;
// } catch (Exception e) { // } catch (Exception e) {
// LOGGER.error(e.getMessage()); // LOGGER.error(e.getMessage());
// LOGGER.debug(ExceptionUtil.exceptionToString(e)); // LOGGER.debug(ExceptionUtil.exceptionToString(e));
// return false; // return false;
// } // }
// } // }
public static boolean resetNodeCenter(String val) { public static boolean resetNodeCenter(String val) {
try { try {

View File

@ -49,7 +49,7 @@ import java.util.concurrent.TimeUnit;
import static io.netty.handler.codec.http.HttpResponseStatus.OK; import static io.netty.handler.codec.http.HttpResponseStatus.OK;
public class FileActions { public class FileActions {
// private static final String PARAM_ACTION = "action"; // private static final String PARAM_ACTION = "action";
private static final Logger LOGGER = LogManager.getLogger(FileActions.class); private static final Logger LOGGER = LogManager.getLogger(FileActions.class);
private static final Set<String> TEXT_FILE_SUFFIXES = new HashSet<>(); private static final Set<String> TEXT_FILE_SUFFIXES = new HashSet<>();
static Set<String> changeSet = ConcurrentHashMap.newKeySet(); static Set<String> changeSet = ConcurrentHashMap.newKeySet();
@ -59,14 +59,13 @@ public class FileActions {
ContractManagerFrameHandler handler; ContractManagerFrameHandler handler;
static { static {
clearUploadFailed = clearUploadFailed = new TimerTask() {
new TimerTask() { @Override
@Override public void run(Timeout arg0) {
public void run(Timeout arg0) { clearUploadFailed();
clearUploadFailed(); SyncResult.timer.newTimeout(this, 5, TimeUnit.MINUTES);
SyncResult.timer.newTimeout(this, 5, TimeUnit.MINUTES); }
} };
};
SyncResult.timer.newTimeout(clearUploadFailed, 5, TimeUnit.MINUTES); SyncResult.timer.newTimeout(clearUploadFailed, 5, TimeUnit.MINUTES);
} }
@ -85,11 +84,8 @@ public class FileActions {
String fileName = compileInternal(parentPath, projectName, isPrivate, true); String fileName = compileInternal(parentPath, projectName, isPrivate, true);
File dir; File dir;
if (isPrivate) { if (isPrivate) {
dir = dir = new File(parentPath.replace(GlobalConf.instance.privateDir,
new File( GlobalConf.instance.privateCompiledDir));
parentPath.replace(
GlobalConf.instance.privateDir,
GlobalConf.instance.privateCompiledDir));
} else { } else {
dir = new File(GlobalConf.instance.publicCompiledDir); dir = new File(GlobalConf.instance.publicCompiledDir);
} }
@ -107,9 +103,7 @@ public class FileActions {
return ret != null && ret.equals("locked"); return ret != null && ret.equals("locked");
} }
@URIPath( @URIPath(method = org.bdware.server.http.HttpMethod.POST, value = {"/SCIDE/Upload", "/Upload"})
method = org.bdware.server.http.HttpMethod.POST,
value = {"/SCIDE/Upload", "/Upload"})
public static void handleUploadRequest(ChannelHandlerContext ctx, FullHttpRequest request) { public static void handleUploadRequest(ChannelHandlerContext ctx, FullHttpRequest request) {
// logger.info("[CMHttpHandler] handleUploadRequest : "); // logger.info("[CMHttpHandler] handleUploadRequest : ");
// Upload method is POST // Upload method is POST
@ -124,20 +118,16 @@ public class FileActions {
} }
} }
if (!transformedParam.containsKey("path") if (!transformedParam.containsKey("path") || !transformedParam.containsKey("fileName")
|| !transformedParam.containsKey("fileName")
|| !transformedParam.containsKey("isPrivate") || !transformedParam.containsKey("isPrivate")
|| !transformedParam.containsKey("order") || !transformedParam.containsKey("order") || !transformedParam.containsKey("count")
|| !transformedParam.containsKey("count")
|| !transformedParam.containsKey("pubKey") || !transformedParam.containsKey("pubKey")
|| !transformedParam.containsKey("sign")) { || !transformedParam.containsKey("sign")) {
DefaultFullHttpResponse fullResponse = DefaultFullHttpResponse fullResponse = new DefaultFullHttpResponse(
new DefaultFullHttpResponse( request.protocolVersion(), OK,
request.protocolVersion(), Unpooled.wrappedBuffer(
OK, "{\"status\":\"false\",\"data\":\"Missing argument, please check: path, fileName, isPrivate, order, count, pubKey, sign!\"}"
Unpooled.wrappedBuffer( .getBytes()));
"{\"status\":\"false\",\"data\":\"Missing argument, please check: path, fileName, isPrivate, order, count, pubKey, sign!\"}"
.getBytes()));
ChannelFuture f = addCrossOriginHeaderAndWrite(ctx, fullResponse); ChannelFuture f = addCrossOriginHeaderAndWrite(ctx, fullResponse);
@ -146,10 +136,8 @@ public class FileActions {
} }
// 验签 // 验签
// HttpMethod method = request.method(); // HttpMethod method = request.method();
String uri = String uri = URLDecoder.decode(request.uri()).split("\\?")[1]; // http请求中规定签名必须是最后一个且公钥名必须为pubKey否则验签失败
URLDecoder.decode(request.uri())
.split("\\?")[1]; // http请求中规定签名必须是最后一个且公钥名必须为pubKey否则验签失败
int index = uri.lastIndexOf('&'); int index = uri.lastIndexOf('&');
String str = uri.substring(0, index); String str = uri.substring(0, index);
// logger.info("uri=" + uri); // logger.info("uri=" + uri);
@ -183,9 +171,8 @@ public class FileActions {
Method mm; Method mm;
Action a = null; Action a = null;
try { try {
mm = mm = FileActions.class.getDeclaredMethod("uploadFile", JsonObject.class,
FileActions.class.getDeclaredMethod( ResultCallback.class);
"uploadFile", JsonObject.class, ResultCallback.class);
a = mm.getAnnotation(Action.class); a = mm.getAnnotation(Action.class);
} catch (SecurityException | NoSuchMethodException e1) { } catch (SecurityException | NoSuchMethodException e1) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
@ -215,28 +202,17 @@ public class FileActions {
status = "accept"; status = "accept";
} }
TimeDBUtil.instance.put( TimeDBUtil.instance.put(CMTables.LocalNodeLogDB.toString(),
CMTables.LocalNodeLogDB.toString(), "{\"action\":\"" + action + "\",\"pubKey\":\"" + transformedParam.get("pubKey")
"{\"action\":\"" + "\",\"status\":\"" + status + "\",\"date\":" + System.currentTimeMillis()
+ action
+ "\",\"pubKey\":\""
+ transformedParam.get("pubKey")
+ "\",\"status\":\""
+ status
+ "\",\"date\":"
+ System.currentTimeMillis()
+ "}"); + "}");
// logger.info("[CMHttpHandler] flag = " + flag + " flag2 = " + flag2); // logger.info("[CMHttpHandler] flag = " + flag + " flag2 = " + flag2);
if (!flag || !flag2) { if (!flag || !flag2) {
DefaultFullHttpResponse fullResponse = DefaultFullHttpResponse fullResponse = new DefaultFullHttpResponse(
new DefaultFullHttpResponse( request.protocolVersion(), OK, Unpooled.wrappedBuffer(
request.protocolVersion(), "{\"status\":\"false\",\"data\":\"Permission denied!\"}".getBytes()));
OK,
Unpooled.wrappedBuffer(
"{\"status\":\"false\",\"data\":\"Permission denied!\"}"
.getBytes()));
ChannelFuture f = addCrossOriginHeaderAndWrite(ctx, fullResponse); ChannelFuture f = addCrossOriginHeaderAndWrite(ctx, fullResponse);
f.addListener(ChannelFutureListener.CLOSE); f.addListener(ChannelFutureListener.CLOSE);
@ -256,9 +232,7 @@ public class FileActions {
ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(contractID); ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(contractID);
if (meta == null) { if (meta == null) {
DefaultFullHttpResponse fullResponse = DefaultFullHttpResponse fullResponse =
new DefaultFullHttpResponse( new DefaultFullHttpResponse(request.protocolVersion(), OK,
request.protocolVersion(),
OK,
Unpooled.wrappedBuffer( Unpooled.wrappedBuffer(
"{\"status\":\"false\",\"data\":\"no such contract!\"}" "{\"status\":\"false\",\"data\":\"no such contract!\"}"
.getBytes())); .getBytes()));
@ -267,24 +241,20 @@ public class FileActions {
return; return;
} }
if (!meta.contract.getOwner().equals(pubkey)) { if (!meta.contract.getOwner().equals(pubkey)) {
DefaultFullHttpResponse fullResponse = DefaultFullHttpResponse fullResponse = new DefaultFullHttpResponse(
new DefaultFullHttpResponse( request.protocolVersion(), OK, Unpooled.wrappedBuffer(
request.protocolVersion(), "{\"status\":\"false\",\"data\":\"not owner!\"}".getBytes()));
OK,
Unpooled.wrappedBuffer(
"{\"status\":\"false\",\"data\":\"not owner!\"}"
.getBytes()));
ChannelFuture f = addCrossOriginHeaderAndWrite(ctx, fullResponse); ChannelFuture f = addCrossOriginHeaderAndWrite(ctx, fullResponse);
f.addListener(ChannelFutureListener.CLOSE); f.addListener(ChannelFutureListener.CLOSE);
return; return;
} }
dir = new File(GlobalConf.instance.getDBPath(), meta.getName()); dir = new File(GlobalConf.instance.getDBPath(), meta.getName());
dir = new File(dir, dirName); dir = new File(dir, dirName);
if (!dir.exists()) dir.mkdirs(); if (!dir.exists())
dir.mkdirs();
} else { } else {
boolean isPrivate = boolean isPrivate = transformedParam.containsKey("isPrivate")
transformedParam.containsKey("isPrivate") && Boolean.parseBoolean(transformedParam.get("isPrivate"));
&& Boolean.parseBoolean(transformedParam.get("isPrivate"));
// TODO verify signature and // TODO verify signature and
if (isPrivate) { if (isPrivate) {
String pub = "/" + transformedParam.get("pubKey"); String pub = "/" + transformedParam.get("pubKey");
@ -298,27 +268,24 @@ public class FileActions {
} }
File target = new File(dir, fileName); File target = new File(dir, fileName);
if (fileName.contains("..")) { if (fileName.contains("..")) {
DefaultFullHttpResponse fullResponse = DefaultFullHttpResponse fullResponse = new DefaultFullHttpResponse(
new DefaultFullHttpResponse( request.protocolVersion(), OK, Unpooled.wrappedBuffer(
request.protocolVersion(), "{\"status\":\"false\",\"data\":\"FileName illegal!\"}".getBytes()));
OK,
Unpooled.wrappedBuffer(
"{\"status\":\"false\",\"data\":\"FileName illegal!\"}"
.getBytes()));
ChannelFuture f = addCrossOriginHeaderAndWrite(ctx, fullResponse); ChannelFuture f = addCrossOriginHeaderAndWrite(ctx, fullResponse);
f.addListener(ChannelFutureListener.CLOSE); f.addListener(ChannelFutureListener.CLOSE);
return; return;
} }
// LOGGER.info(request.refCnt()); // LOGGER.info(request.refCnt());
// httpDecoder.offer(request); // httpDecoder.offer(request);
// LOGGER.info(request.refCnt()); // LOGGER.info(request.refCnt());
FileOutputStream fout; FileOutputStream fout;
if (order == 0) { if (order == 0) {
try { try {
LOGGER.debug("Path:" + target.getAbsolutePath()); LOGGER.debug("Path:" + target.getAbsolutePath());
if (!target.getParentFile().exists()) target.getParentFile().mkdirs(); if (!target.getParentFile().exists())
target.getParentFile().mkdirs();
fout = new FileOutputStream(target, false); fout = new FileOutputStream(target, false);
fileMap.put(target.getAbsolutePath(), fout); fileMap.put(target.getAbsolutePath(), fout);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
@ -340,9 +307,8 @@ public class FileActions {
} }
fileMap.remove(target.getAbsolutePath()); fileMap.remove(target.getAbsolutePath());
updateTime.remove(target.getAbsolutePath()); updateTime.remove(target.getAbsolutePath());
boolean isDebug = boolean isDebug = transformedParam.containsKey("isDebug")
transformedParam.containsKey("isDebug") && Boolean.parseBoolean(transformedParam.get("isDebug"));
&& Boolean.parseBoolean(transformedParam.get("isDebug"));
String doi = null; String doi = null;
String path = target.getAbsolutePath(); String path = target.getAbsolutePath();
path = path.replaceAll("/\\./", "/"); path = path.replaceAll("/\\./", "/");
@ -359,14 +325,14 @@ public class FileActions {
retStr = retStr.replaceFirst("null", doi); retStr = retStr.replaceFirst("null", doi);
} }
} }
DefaultFullHttpResponse fullResponse = DefaultFullHttpResponse fullResponse = new DefaultFullHttpResponse(
new DefaultFullHttpResponse( request.protocolVersion(), OK, Unpooled.wrappedBuffer(retStr.getBytes()));
request.protocolVersion(), OK, Unpooled.wrappedBuffer(retStr.getBytes()));
ChannelFuture f = addCrossOriginHeaderAndWrite(ctx, fullResponse); ChannelFuture f = addCrossOriginHeaderAndWrite(ctx, fullResponse);
f.addListener(ChannelFutureListener.CLOSE); f.addListener(ChannelFutureListener.CLOSE);
} }
private static ChannelFuture addCrossOriginHeaderAndWrite(ChannelHandlerContext ctx, DefaultFullHttpResponse fullResponse) { private static ChannelFuture addCrossOriginHeaderAndWrite(ChannelHandlerContext ctx,
DefaultFullHttpResponse fullResponse) {
fullResponse.headers().add("Access-Control-Allow-Origin", "*"); fullResponse.headers().add("Access-Control-Allow-Origin", "*");
fullResponse.headers().add("Access-Control-Allow-Methods", "*"); fullResponse.headers().add("Access-Control-Allow-Methods", "*");
return ctx.write(fullResponse); return ctx.write(fullResponse);
@ -384,15 +350,12 @@ public class FileActions {
} }
private static String unzipIfYpk(File target, File dir, boolean isDebug) { private static String unzipIfYpk(File target, File dir, boolean isDebug) {
// [FileAction] unzipIfYpk, // [FileAction] unzipIfYpk,
// target:/data/bdwaas/bdcontract/./BDWareProjectDir/private/045eeda3a001faad9d636ab1e973599ea87338a9576756eb10ceeca6083c1f76aac5cd201eab21c41342eb8aac40e9b283f0b6eae019644cdcc0a9f9aeb73de8fc/ContractUNknown.ypk targetDir:/data/bdwaas/bdcontract/./BDWareProjectDir/private/045eeda3a001faad9d636ab1e973599ea87338a9576756eb10ceeca6083c1f76aac5cd201eab21c41342eb8aac40e9b283f0b6eae019644cdcc0a9f9aeb73de8fc/ContractUNknown // target:/data/bdwaas/bdcontract/./BDWareProjectDir/private/045eeda3a001faad9d636ab1e973599ea87338a9576756eb10ceeca6083c1f76aac5cd201eab21c41342eb8aac40e9b283f0b6eae019644cdcc0a9f9aeb73de8fc/ContractUNknown.ypk
LOGGER.info( // targetDir:/data/bdwaas/bdcontract/./BDWareProjectDir/private/045eeda3a001faad9d636ab1e973599ea87338a9576756eb10ceeca6083c1f76aac5cd201eab21c41342eb8aac40e9b283f0b6eae019644cdcc0a9f9aeb73de8fc/ContractUNknown
"[FileAction] unzipIfYpk, date:" LOGGER.info("[FileAction] unzipIfYpk, date:"
+ new SimpleDateFormat("MM-dd hh:mm:ss").format(new Date()) + new SimpleDateFormat("MM-dd hh:mm:ss").format(new Date()) + " -> target:"
+ " -> target:" + target.getAbsolutePath() + " dir:" + dir.getAbsolutePath());
+ target.getAbsolutePath()
+ " dir:"
+ dir.getAbsolutePath());
if (target.getParentFile().equals(dir)) { if (target.getParentFile().equals(dir)) {
if (target.getName().endsWith(".ypk")) { if (target.getName().endsWith(".ypk")) {
String dirName = target.getName().substring(0, target.getName().length() - 4); String dirName = target.getName().substring(0, target.getName().length() - 4);
@ -406,11 +369,8 @@ public class FileActions {
} }
} }
targetDir.mkdirs(); targetDir.mkdirs();
LOGGER.info( LOGGER.info("[FileAction] unzipIfYpk, target:" + target.getAbsolutePath()
"[FileAction] unzipIfYpk, target:" + " targetDir:" + targetDir.getAbsolutePath());
+ target.getAbsolutePath()
+ " targetDir:"
+ targetDir.getAbsolutePath());
YJSPacker.unpack(target.getAbsolutePath(), targetDir.getAbsolutePath()); YJSPacker.unpack(target.getAbsolutePath(), targetDir.getAbsolutePath());
final File tempDir = targetDir; final File tempDir = targetDir;
ContractManager.threadPool.execute(() -> startIfManifestDetected(tempDir, isDebug)); ContractManager.threadPool.execute(() -> startIfManifestDetected(tempDir, isDebug));
@ -428,14 +388,13 @@ public class FileActions {
String doi = "86.5000.470/do." + geneRandomID(); String doi = "86.5000.470/do." + geneRandomID();
jo.addProperty("doi", doi); jo.addProperty("doi", doi);
} }
ContractManager.threadPool.execute( ContractManager.threadPool.execute(() -> {
() -> { try {
try { GRPCPool.instance.reRegister(jo.get("doi").getAsString());
GRPCPool.instance.reRegister(jo.get("doi").getAsString()); } catch (Exception e) {
} catch (Exception e) { LOGGER.warn("unable to connect LHS: " + e.getMessage());
LOGGER.warn("unable to connect LHS: " + e.getMessage()); }
} });
});
FileWriter fw = new FileWriter(manifestFile); FileWriter fw = new FileWriter(manifestFile);
fw.write(JsonUtil.toJson(jo)); fw.write(JsonUtil.toJson(jo));
fw.close(); fw.close();
@ -460,9 +419,8 @@ public class FileActions {
LOGGER.info("startContractProcess:" + targetDir); LOGGER.info("startContractProcess:" + targetDir);
Contract c = new Contract(); Contract c = new Contract();
c.setType(ContractExecType.Sole); c.setType(ContractExecType.Sole);
String ypkPath = String ypkPath = FileActions
FileActions.autoCompile( .autoCompile(targetDir.getParentFile().getAbsolutePath(), contractName);
targetDir.getParentFile().getAbsolutePath(), contractName);
c.setScript(ypkPath); c.setScript(ypkPath);
c.setOwner(owner); c.setOwner(owner);
c.setDebug(isDebug); c.setDebug(isDebug);
@ -497,8 +455,8 @@ public class FileActions {
} }
} }
private static void writeChunk( private static void writeChunk(ChannelHandlerContext ctx, HttpPostRequestDecoder httpDecoder,
ChannelHandlerContext ctx, HttpPostRequestDecoder httpDecoder, File file) { File file) {
try { try {
if (!file.exists()) { if (!file.exists()) {
file.createNewFile(); file.createNewFile();
@ -531,8 +489,8 @@ public class FileActions {
} }
} }
private static String compileInternal( private static String compileInternal(String parPath, String projectName, boolean isPrivate,
String parPath, String projectName, boolean isPrivate, boolean isAuto) { boolean isAuto) {
File project = new File(parPath, projectName); File project = new File(parPath, projectName);
try { try {
if (project.isDirectory()) { if (project.isDirectory()) {
@ -544,24 +502,18 @@ public class FileActions {
File dir; File dir;
if (isPrivate) { if (isPrivate) {
dir = dir = new File(parPath.replace(GlobalConf.instance.privateDir,
new File( GlobalConf.instance.privateCompiledDir));
parPath.replace(
GlobalConf.instance.privateDir,
GlobalConf.instance.privateCompiledDir));
} else { } else {
dir = new File(GlobalConf.instance.publicCompiledDir); dir = new File(GlobalConf.instance.publicCompiledDir);
} }
tempZip = new File(dir, projectName + "_" + time + isAutoStr + ".ypk"); tempZip = new File(dir, projectName + "_" + time + isAutoStr + ".ypk");
if (isAuto) { if (isAuto) {
File[] files = File[] files = dir
dir.listFiles( .listFiles(pathname -> pathname.getName().startsWith(projectName + "_")
pathname -> && pathname.getName().endsWith("_Auto.ypk"));
pathname.getName().startsWith(projectName + "_") if (files != null && files.length == 1
&& pathname.getName().endsWith("_Auto.ypk"));
if (files != null
&& files.length == 1
&& !changeSet.contains(project.getAbsolutePath())) { && !changeSet.contains(project.getAbsolutePath())) {
return files[0].getName(); return files[0].getName();
} else if (files != null) { } else if (files != null) {
@ -586,8 +538,7 @@ public class FileActions {
Map<String, String> header = new HashMap<>(); Map<String, String> header = new HashMap<>();
header.put("accept", "*/*"); header.put("accept", "*/*");
header.put("connection", "Keep-Alive"); header.put("connection", "Keep-Alive");
header.put( header.put("user-agent",
"user-agent",
" Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36)"); " Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36)");
// header.put("Content-Type", "application/json"); // header.put("Content-Type", "application/json");
return header; return header;
@ -662,7 +613,8 @@ public class FileActions {
} }
private static String toProjectName(String path) { private static String toProjectName(String path) {
if (path.startsWith("/")) path = path.substring(1); if (path.startsWith("/"))
path = path.substring(1);
return path.replaceAll("/.*$", ""); return path.replaceAll("/.*$", "");
} }
@ -781,9 +733,7 @@ public class FileActions {
int counter = 0; int counter = 0;
while ((s = br.readLine()) != null) { while ((s = br.readLine()) != null) {
if (s.equals("{") if (s.equals("{") || s.equals("}") || s.equals("")
|| s.equals("}")
|| s.equals("")
|| s.trim().startsWith("\"doi\":\"") || s.trim().startsWith("\"doi\":\"")
|| s.trim().startsWith("\"doipFlag\":\"")) { || s.trim().startsWith("\"doipFlag\":\"")) {
continue; continue;
@ -862,8 +812,8 @@ public class FileActions {
returnFileListResponse(resultCallback, response, dirs, f); returnFileListResponse(resultCallback, response, dirs, f);
} }
private void returnFileListResponse( private void returnFileListResponse(ResultCallback resultCallback, Response response,
ResultCallback resultCallback, Response response, List<String> dirs, File f) { List<String> dirs, File f) {
if (f.exists() && f.isDirectory()) { if (f.exists() && f.isDirectory()) {
for (File subFile : f.listFiles()) { for (File subFile : f.listFiles()) {
dirs.add(subFile.getName()); dirs.add(subFile.getName());
@ -905,7 +855,8 @@ public class FileActions {
String fileName = subFile.getName(); String fileName = subFile.getName();
dirs.add(fileName); dirs.add(fileName);
} }
} else dirs.add(project); } else
dirs.add(project);
} }
response.data = JsonUtil.toJson(dirs); response.data = JsonUtil.toJson(dirs);
} }
@ -1005,20 +956,20 @@ public class FileActions {
response.isPrivate = true; response.isPrivate = true;
String strr = "/" + getPubkey(args); String strr = "/" + getPubkey(args);
projectDir = GlobalConf.instance.privateDir + strr; projectDir = GlobalConf.instance.privateDir + strr;
// url = // url =
// "http://" // "http://"
// + mainHost // + mainHost
// + "/SCIDE/CMManager?action=downloadContract&projectName=" // + "/SCIDE/CMManager?action=downloadContract&projectName="
// + projectName // + projectName
// + "&pubkey=" // + "&pubkey="
// + json.get("pubkey").getAsString(); // + json.get("pubkey").getAsString();
} else { } else {
projectDir = GlobalConf.instance.publicDir; projectDir = GlobalConf.instance.publicDir;
// url = // url =
// "http://" // "http://"
// + mainHost // + mainHost
// + "/SCIDE/CMManager?action=downloadContract&projectName=" // + "/SCIDE/CMManager?action=downloadContract&projectName="
// + projectName; // + projectName;
} }
url = "http://127.0.0.1:18000/Users/zhangyixuan/Grade2/temp65/TEST.txt"; url = "http://127.0.0.1:18000/Users/zhangyixuan/Grade2/temp65/TEST.txt";
@ -1087,7 +1038,7 @@ public class FileActions {
long downloaded = 0; long downloaded = 0;
byte[] buff = new byte[1024 * 100 * 4]; byte[] buff = new byte[1024 * 100 * 4];
int d = 5; int d = 5;
for (int k; (k = input.read(buff)) > 0; ) { for (int k; (k = input.read(buff)) > 0;) {
fout.write(buff, 0, k); fout.write(buff, 0, k);
downloaded += k; downloaded += k;
response.data = "downloading"; response.data = "downloading";
@ -1189,89 +1140,72 @@ public class FileActions {
case "Data Sharing Project": case "Data Sharing Project":
// 注册一个用于授权状态持久化的DO并设置到manifest // 注册一个用于授权状态持久化的DO并设置到manifest
String authInfoPersistDOI = null; String authInfoPersistDOI = null;
// try { // try {
// //
// HandleService hs = // HandleService hs =
// new // new
// HandleService(HandleServiceUtils.hrRegister); // HandleService(HandleServiceUtils.hrRegister);
// authInfoPersistDOI = // authInfoPersistDOI =
// //
// hs.registerContract(DOIPMainServer.repoIdentifier, ""); // hs.registerContract(DOIPMainServer.repoIdentifier, "");
// DigitalObject contractDO = // DigitalObject contractDO =
// new // new
// DigitalObject(authInfoPersistDOI, DoType.Json); // DigitalObject(authInfoPersistDOI, DoType.Json);
// Element e = new // Element e = new
// Element("contract", "JsonString"); // Element("contract", "JsonString");
// e.setData("{}".getBytes()); // e.setData("{}".getBytes());
// contractDO.addElements(e); // contractDO.addElements(e);
// // // //
// DOAClient.getGlobalInstance().create(DOIPMainServer.repoIdentifier, contractDO); // DOAClient.getGlobalInstance().create(DOIPMainServer.repoIdentifier,
// DoipClient doipClient = // contractDO);
// DoipClient doipClient =
// //
// DoipClient.createByRepoUrlAndMsgFmt( // DoipClient.createByRepoUrlAndMsgFmt(
// //
// DOIPMainServer.repoUrl, // DOIPMainServer.repoUrl,
// //
// DoipMessageFormat.PACKET.getName()); // DoipMessageFormat.PACKET.getName());
// DoMessage response = // DoMessage response =
// doipClient.create( // doipClient.create(
// //
// DOIPMainServer.repoIdentifier, contractDO); // DOIPMainServer.repoIdentifier, contractDO);
// if (authInfoPersistDOI == // if (authInfoPersistDOI ==
// null) return; // null) return;
// } catch (Exception ex) { // } catch (Exception ex) {
// ex.printStackTrace(); // ex.printStackTrace();
// return; // return;
// } // }
if (args.has("projectDOI")) if (args.has("projectDOI"))
manifestFout.write( manifestFout.write(("{\n \"main\":\"" + fileName + ".yjs\",\n"
("{\n \"main\":\"" + " \"doipFlag\":true,\n" + " \"doi\":\""
+ fileName + args.get("projectDOI").getAsString() + "\",\n"
+ ".yjs\",\n" + " \"authInfoPersistDOI\":\"" + authInfoPersistDOI
+ " \"doipFlag\":true,\n" + "\"\n}").getBytes());
+ " \"doi\":\""
+ args.get("projectDOI").getAsString()
+ "\",\n"
+ " \"authInfoPersistDOI\":\""
+ authInfoPersistDOI
+ "\"\n}")
.getBytes());
else else
manifestFout.write( manifestFout.write(("{\n \"main\":\"" + fileName + ".yjs\",\n"
("{\n \"main\":\"" + " \"authInfoPersistDOI\":\"" + authInfoPersistDOI
+ fileName + "\"\n}").getBytes());
+ ".yjs\",\n"
+ " \"authInfoPersistDOI\":\""
+ authInfoPersistDOI
+ "\"\n}")
.getBytes());
YJSPacker.unpack( YJSPacker.unpack(
new File("./WebContent/ProjectTemplate/naiveDAC.zip") new File("./WebContent/ProjectTemplate/naiveDAC.zip")
.getAbsolutePath(), .getAbsolutePath(),
newFile.getPath() + "/naiveDAC"); newFile.getPath() + "/naiveDAC");
mainyjsFout.write( mainyjsFout
("import \"naiveDAC/naiveDAC.yjs\"\n\n").getBytes()); .write(("import \"naiveDAC/naiveDAC.yjs\"\n\n").getBytes());
mainyjsFout.write( mainyjsFout.write(("contract " + fileName
("contract " + "{\n function onCreate(){\n initDAC(requester);\n }\n}")
+ fileName
+ "{\n function onCreate(){\n initDAC(requester);\n }\n}")
.getBytes()); .getBytes());
break; break;
default: default:
if (args.has("projectDOI")) if (args.has("projectDOI"))
manifestFout.write( manifestFout.write(("{\n \"main\":\"" + fileName
("{\n \"main\":\"" + ".yjs\",\n \"doipFlag\":true,\n \"doi\":\""
+ fileName + args.get("projectDOI").getAsString() + "\"\n}")
+ ".yjs\",\n \"doipFlag\":true,\n \"doi\":\""
+ args.get("projectDOI").getAsString()
+ "\"\n}")
.getBytes()); .getBytes());
else else
manifestFout.write( manifestFout.write(("{\n \"main\":\"" + fileName + ".yjs\"\n}")
("{\n \"main\":\"" + fileName + ".yjs\"\n}") .getBytes());
.getBytes());
mainyjsFout.write(("contract " + fileName + "{\n\n}").getBytes()); mainyjsFout.write(("contract " + fileName + "{\n\n}").getBytes());
} }
} }
@ -1333,11 +1267,8 @@ public class FileActions {
File f = new File(parPath + "/" + oldFile); File f = new File(parPath + "/" + oldFile);
if (!oldFile.contains("..") && f.exists()) { if (!oldFile.contains("..") && f.exists()) {
LOGGER.debug( LOGGER.debug("[FileController] delete:" + f.getAbsolutePath() + " exists:"
"[FileController] delete:" + f.exists());
+ f.getAbsolutePath()
+ " exists:"
+ f.exists());
if (f.isDirectory()) { if (f.isDirectory()) {
deleteDir(f); deleteDir(f);
} else { } else {
@ -1475,10 +1406,8 @@ public class FileActions {
// 文本文件 // 文本文件
if (!path.contains("..") && isTextFile(path)) { if (!path.contains("..") && isTextFile(path)) {
LOGGER.debug("[FileActions] 上传文本文件类型 : "); LOGGER.debug("[FileActions] 上传文本文件类型 : ");
BufferedWriter bw = BufferedWriter bw = new BufferedWriter(
new BufferedWriter( new FileWriter(target.getAbsolutePath() + "/" + fileName, isAppend));
new FileWriter(
target.getAbsolutePath() + "/" + fileName, isAppend));
bw.write(content); bw.write(content);
bw.close(); bw.close();
} else { // 其他类型文件 } else { // 其他类型文件
@ -1515,7 +1444,8 @@ public class FileActions {
private String getPubkey(JsonObject args) { private String getPubkey(JsonObject args) {
try { try {
if (handler != null) return handler.getPubKey(); if (handler != null)
return handler.getPubKey();
return args.get("pubKey").getAsString(); return args.get("pubKey").getAsString();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -1568,59 +1498,59 @@ public class FileActions {
resultCallback.onResult(JsonUtil.toJson(response)); resultCallback.onResult(JsonUtil.toJson(response));
} }
// @Action(userPermission = 1 << 14, async = true) // @Action(userPermission = 1 << 14, async = true)
// public void importContractInstanceCodeByDOI(JsonObject args, ResultCallback // public void importContractInstanceCodeByDOI(JsonObject args, ResultCallback
// resultCallback) { // resultCallback) {
// long s = System.currentTimeMillis(); // long s = System.currentTimeMillis();
// String ret; // String ret;
// try { // try {
// String contractDOI = args.get("doi").getAsString(); // String contractDOI = args.get("doi").getAsString();
// DigitalObject contractDO; // DigitalObject contractDO;
// DoipClient doipClient = // DoipClient doipClient =
// DoipClient.createByRepoUrlAndMsgFmt( // DoipClient.createByRepoUrlAndMsgFmt(
// DOIPMainServer.repoUrl, DoipMessageFormat.PACKET.getName()); // DOIPMainServer.repoUrl, DoipMessageFormat.PACKET.getName());
// DoMessage response = doipClient.retrieve(contractDOI, null, null); // DoMessage response = doipClient.retrieve(contractDOI, null, null);
// if (response.parameters.response == DoResponse.Success) { // if (response.parameters.response == DoResponse.Success) {
// contractDO = DigitalObject.parse(response.body); // contractDO = DigitalObject.parse(response.body);
// } else { // } else {
// DoMessage resp = DOAClient.getGlobalInstance().retrieve(contractDOI, null, // DoMessage resp = DOAClient.getGlobalInstance().retrieve(contractDOI, null,
// null); // null);
// contractDO = DigitalObject.parse(resp.body); // contractDO = DigitalObject.parse(resp.body);
// } // }
// // Dictionary<String, String> contractInfo = JsonUtil.fromJson(new // // Dictionary<String, String> contractInfo = JsonUtil.fromJson(new
// // String(contractDO.getData()), // // String(contractDO.getData()),
// // new Hashtable<String, String>().getClass()); // // new Hashtable<String, String>().getClass());
// ContractInstanceDO contractInstanceDO = // ContractInstanceDO contractInstanceDO =
// (ContractInstanceDO) // (ContractInstanceDO)
// ContractManager.toObject(contractDO.elements.get(0).getData()); // ContractManager.toObject(contractDO.elements.get(0).getData());
// String projectName = contractDOI.replace('/', '_'); // String projectName = contractDOI.replace('/', '_');
// String projectDirStr = // String projectDirStr =
// GlobalConf.instance.privateDir + "/" + getPubkey(args) + "/" + // GlobalConf.instance.privateDir + "/" + getPubkey(args) + "/" +
// projectName; // projectName;
// File projectDir = new File(projectDirStr); // File projectDir = new File(projectDirStr);
// if (projectDir.isDirectory()) { // if (projectDir.isDirectory()) {
// deleteDir(projectDir); // deleteDir(projectDir);
// } // }
// YJSPacker.unpack(contractInstanceDO.script, projectDirStr); // YJSPacker.unpack(contractInstanceDO.script, projectDirStr);
// ret = // ret =
// "Import project successfully." // "Import project successfully."
// + "\nProject Name: " // + "\nProject Name: "
// + projectName // + projectName
// + "\nContract ID: " // + "\nContract ID: "
// + contractInstanceDO.id // + contractInstanceDO.id
// + "\nContract PublicKey: " // + "\nContract PublicKey: "
// + contractInstanceDO.publicKey; // + contractInstanceDO.publicKey;
// } catch (Exception e) { // } catch (Exception e) {
// ByteArrayOutputStream bo = new ByteArrayOutputStream(); // ByteArrayOutputStream bo = new ByteArrayOutputStream();
// e.printStackTrace(new PrintStream(bo)); // e.printStackTrace(new PrintStream(bo));
// ret = bo.toString(); // ret = bo.toString();
// } // }
// Map<String, Object> r = new HashMap<>(); // Map<String, Object> r = new HashMap<>();
// r.put("action", "onImportContractInstanceCodeByDOI"); // r.put("action", "onImportContractInstanceCodeByDOI");
// r.put("data", ret); // r.put("data", ret);
// r.put("executeTime", System.currentTimeMillis() - s); // r.put("executeTime", System.currentTimeMillis() - s);
// resultCallback.onResult(JsonUtil.toJson(r)); // resultCallback.onResult(JsonUtil.toJson(r));
// } // }
private boolean isTextFile(String path) { private boolean isTextFile(String path) {
return TEXT_FILE_SUFFIXES.contains(path.substring(path.lastIndexOf("."))); return TEXT_FILE_SUFFIXES.contains(path.substring(path.lastIndexOf(".")));
@ -1658,9 +1588,8 @@ public class FileActions {
if (null != ypkPath) { if (null != ypkPath) {
try { try {
ret.put("ypk", new File(ypkPath).getName()); ret.put("ypk", new File(ypkPath).getName());
ret.put( ret.put("permissions", JsonUtil
"permissions", .parseStringAsJsonObject(CMActions.manager.parseYpkPermissions(ypkPath)));
JsonUtil.parseStringAsJsonObject(CMActions.manager.parseYpkPermissions(ypkPath)));
} catch (Exception e) { } catch (Exception e) {
ByteArrayOutputStream bo = new ByteArrayOutputStream(); ByteArrayOutputStream bo = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(bo)); e.printStackTrace(new PrintStream(bo));

View File

@ -90,7 +90,7 @@ public class ManagerActions {
data = status = GlobalConf.resetNodeName(val); data = status = GlobalConf.resetNodeName(val);
break; break;
case "masterAddress": case "masterAddress":
// data = status = GlobalConf.resetMasterAddress(val); // data = status = GlobalConf.resetMasterAddress(val);
default: default:
status = false; status = false;
data = "unsupported key:" + key; data = "unsupported key:" + key;

View File

@ -39,7 +39,8 @@ import java.util.List;
import static org.bdware.server.doip.ContractRepositoryMain.currentIrpClient; import static org.bdware.server.doip.ContractRepositoryMain.currentIrpClient;
@ChannelHandler.Sharable @ChannelHandler.Sharable
public class ContractRepositoryHandler extends RepositoryHandlerBase implements DefaultRepoHandlerInjector { public class ContractRepositoryHandler extends RepositoryHandlerBase
implements DefaultRepoHandlerInjector {
BCOManager bcoManager; BCOManager bcoManager;
static Logger LOGGER = LogManager.getLogger(ContractRepositoryHandler.class); static Logger LOGGER = LogManager.getLogger(ContractRepositoryHandler.class);
@ -47,7 +48,8 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
super(info); super(info);
GlobalConf.DOAConf doaConf = GlobalConf.instance.doaConf; GlobalConf.DOAConf doaConf = GlobalConf.instance.doaConf;
String bcoDir = GlobalConf.instance.bcoDir; String bcoDir = GlobalConf.instance.bcoDir;
bcoManager = new BCOManager(doaConf.repoDoid, new File(bcoDir), doaConf.lhsAddress, GlobalConf.instance.keyPair); bcoManager = new BCOManager(doaConf.repoDoid, new File(bcoDir), doaConf.lhsAddress,
GlobalConf.instance.keyPair);
} }
@Override @Override
@ -58,7 +60,9 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
@Override @Override
public DoipMessage handleListOps(DoipMessage doipMessage) { public DoipMessage handleListOps(DoipMessage doipMessage) {
if (!GlobalConf.instance.doaConf.repoDoid.equals(doipMessage.header.parameters.id)) { if (!GlobalConf.instance.doaConf.repoDoid.equals(doipMessage.header.parameters.id)) {
return replyStringWithStatus(doipMessage, "unsupported doid:" + doipMessage.header.parameters.id, DoipResponseCode.DoNotFound); return replyStringWithStatus(doipMessage,
"unsupported doid:" + doipMessage.header.parameters.id,
DoipResponseCode.DoNotFound);
} }
JsonArray ops = new JsonArray(); JsonArray ops = new JsonArray();
ops.add(BasicOperations.Hello.getName()); ops.add(BasicOperations.Hello.getName());
@ -69,13 +73,17 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
@Override @Override
public DoipMessage handleCreate(DoipMessage doipMessage) { public DoipMessage handleCreate(DoipMessage doipMessage) {
if (!GlobalConf.instance.doaConf.repoDoid.equals(doipMessage.header.parameters.id)) { if (!GlobalConf.instance.doaConf.repoDoid.equals(doipMessage.header.parameters.id)) {
return replyStringWithStatus(doipMessage, "unsupported doid:" + doipMessage.header.parameters.id, DoipResponseCode.DoNotFound); return replyStringWithStatus(doipMessage,
"unsupported doid:" + doipMessage.header.parameters.id,
DoipResponseCode.DoNotFound);
} }
//TODD 验证签名完事后进入 // TODD 验证签名完事后进入
if (doipMessage.credential != null && doipMessage.credential.getSigner() != null) { if (doipMessage.credential != null && doipMessage.credential.getSigner() != null) {
String permissions = KeyValueDBUtil.instance.getValue(CMTables.NodeRole.toString(), doipMessage.credential.getSigner()); String permissions = KeyValueDBUtil.instance.getValue(CMTables.NodeRole.toString(),
doipMessage.credential.getSigner());
if (!permissions.contains("ContractInstanceManager")) { if (!permissions.contains("ContractInstanceManager")) {
return replyStringWithStatus(doipMessage, "permission denied, role:" + permissions, DoipResponseCode.Declined); return replyStringWithStatus(doipMessage, "permission denied, role:" + permissions,
DoipResponseCode.Declined);
} }
} }
DigitalObject digitalObject = null; DigitalObject digitalObject = null;
@ -85,15 +93,17 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
} catch (Exception e) { } catch (Exception e) {
ByteArrayOutputStream bo = new ByteArrayOutputStream(); ByteArrayOutputStream bo = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(bo)); e.printStackTrace(new PrintStream(bo));
return replyStringWithStatus(doipMessage, "exception:" + new String(bo.toByteArray()), DoipResponseCode.Invalid); return replyStringWithStatus(doipMessage, "exception:" + new String(bo.toByteArray()),
DoipResponseCode.Invalid);
} }
digitalObject.attributes.addProperty("owner", doipMessage.credential.getSigner()); digitalObject.attributes.addProperty("owner", doipMessage.credential.getSigner());
try { try {
String result = startUsingJsonObject(digitalObject.attributes); String result = startUsingJsonObject(digitalObject.attributes);
//Please note startContractByYPK is invoked in sync mode method. // Please note startContractByYPK is invoked in sync mode method.
return replyStringWithStatus(doipMessage, result, DoipResponseCode.Success); return replyStringWithStatus(doipMessage, result, DoipResponseCode.Success);
} catch (Exception e) { } catch (Exception e) {
return replyStringWithStatus(doipMessage, e.getMessage(), DoipResponseCode.MoreThanOneErrors); return replyStringWithStatus(doipMessage, e.getMessage(),
DoipResponseCode.MoreThanOneErrors);
} }
@ -105,16 +115,19 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
if (attributes.has("bcoId")) if (attributes.has("bcoId"))
path = bcoManager.download(attributes.get("bcoId").getAsString()); path = bcoManager.download(attributes.get("bcoId").getAsString());
if (path == null) { if (path == null) {
throw new IllegalArgumentException("exception, failed to locate bcoId:" + attributes.get("bcoId").getAsString()); throw new IllegalArgumentException(
"exception, failed to locate bcoId:" + attributes.get("bcoId").getAsString());
} }
attributes.addProperty("path", path); attributes.addProperty("path", path);
Contract c = new Contract(); Contract c = new Contract();
if (attributes.has("contractExecType")) if (attributes.has("contractExecType"))
c.setType(ContractExecType.valueOf(attributes.get("contractExecType").getAsString())); c.setType(ContractExecType.valueOf(attributes.get("contractExecType").getAsString()));
else c.setType(ContractExecType.Sole); else
c.setType(ContractExecType.Sole);
if (attributes.has("shardingId")) if (attributes.has("shardingId"))
c.setShardingId(Integer.valueOf(attributes.get("shardingId").getAsString())); c.setShardingId(Integer.valueOf(attributes.get("shardingId").getAsString()));
else c.setShardingId(-1); else
c.setShardingId(-1);
c.setScript(path); c.setScript(path);
c.setOwner(attributes.get("owner").getAsString()); c.setOwner(attributes.get("owner").getAsString());
if (attributes.has("createParam")) { if (attributes.has("createParam")) {
@ -137,7 +150,8 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
return result; return result;
} }
private void registerBDOID(String contractID, String contractPubKey, String bcoId, String startResult) { private void registerBDOID(String contractID, String contractPubKey, String bcoId,
String startResult) {
try { try {
JsonObject startResultJO = JsonParser.parseString(startResult).getAsJsonObject(); JsonObject startResultJO = JsonParser.parseString(startResult).getAsJsonObject();
@ -171,7 +185,8 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
registerResult = currentIrpClient.register(base); registerResult = currentIrpClient.register(base);
else else
registerResult = currentIrpClient.reRegister(base); registerResult = currentIrpClient.reRegister(base);
// LOGGER.info("TORegister:" + new GsonBuilder().setPrettyPrinting().create().toJson(base)); // LOGGER.info("TORegister:" + new
// GsonBuilder().setPrettyPrinting().create().toJson(base));
LOGGER.info(base.identifier + " register result:" + registerResult); LOGGER.info(base.identifier + " register result:" + registerResult);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -189,13 +204,15 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
String id = doipMessage.header.parameters.attributes.get("elementId").getAsString(); String id = doipMessage.header.parameters.attributes.get("elementId").getAsString();
id = id.replaceAll(".*/", ""); id = id.replaceAll(".*/", "");
LOGGER.info("handleDelete:" + id); LOGGER.info("handleDelete:" + id);
//TODO judge whethere the do exists // TODO judge whethere the do exists
ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(id); ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(id);
if (meta == null) { if (meta == null) {
return replyStringWithStatus(doipMessage, "can't locate do", DoipResponseCode.DoNotFound); return replyStringWithStatus(doipMessage, "can't locate do",
DoipResponseCode.DoNotFound);
} }
if (meta.getStatus() == ContractStatusEnum.KILLED) { if (meta.getStatus() == ContractStatusEnum.KILLED) {
return replyStringWithStatus(doipMessage, "already deleted!", DoipResponseCode.Declined); return replyStringWithStatus(doipMessage, "already deleted!",
DoipResponseCode.Declined);
} }
DoipMessage[] content = new DoipMessage[1]; DoipMessage[] content = new DoipMessage[1];
JsonObject jo = new JsonObject(); JsonObject jo = new JsonObject();
@ -207,11 +224,14 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
@Override @Override
public DoipMessage handleRetrieve(DoipMessage doipMessage) { public DoipMessage handleRetrieve(DoipMessage doipMessage) {
if (doipMessage.header.parameters.id.equals(GlobalConf.instance.doaConf.repoDoid)) { if (doipMessage.header.parameters.id.equals(GlobalConf.instance.doaConf.repoDoid)) {
if (doipMessage.header.parameters.attributes == null || !doipMessage.header.parameters.attributes.has("element")) if (doipMessage.header.parameters.attributes == null
|| !doipMessage.header.parameters.attributes.has("element"))
return retrieveList(doipMessage); return retrieveList(doipMessage);
else else
return retrieveBDO(doipMessage, doipMessage.header.parameters.attributes.get("element").getAsString()); return retrieveBDO(doipMessage,
} else return replyStringWithStatus(doipMessage, "no such do", DoipResponseCode.Declined); doipMessage.header.parameters.attributes.get("element").getAsString());
} else
return replyStringWithStatus(doipMessage, "no such do", DoipResponseCode.Declined);
} }
@ -224,11 +244,13 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
if (client == null) { if (client == null) {
String doid = idOrName; String doid = idOrName;
int off = doid.lastIndexOf("/"); int off = doid.lastIndexOf("/");
if (off > 0) doid = doid.substring(off + 1); if (off > 0)
doid = doid.substring(off + 1);
client = CMActions.manager.getContractClientByDoi(doid); client = CMActions.manager.getContractClientByDoi(doid);
} }
if (client == null) { if (client == null) {
return replyStringWithStatus(doipMessage, "BDO instance not exist!", DoipResponseCode.DoNotFound); return replyStringWithStatus(doipMessage, "BDO instance not exist!",
DoipResponseCode.DoNotFound);
} }
byte[] data = doipMessage.body.getEncodedData(); byte[] data = doipMessage.body.getEncodedData();
if (data == null || data.length == 0) { if (data == null || data.length == 0) {
@ -264,7 +286,8 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
result.add(new JsonPrimitive("Timeout!")); result.add(new JsonPrimitive("Timeout!"));
retStatus = DoipResponseCode.MoreThanOneErrors; retStatus = DoipResponseCode.MoreThanOneErrors;
} }
DigitalObject digitalObject = new DigitalObject(doipMessage.header.parameters.id, DoType.DO); DigitalObject digitalObject =
new DigitalObject(doipMessage.header.parameters.id, DoType.DO);
digitalObject.addAttribute("status", retStatus.getName()); digitalObject.addAttribute("status", retStatus.getName());
digitalObject.addAttribute("result", result.get(0)); digitalObject.addAttribute("result", result.get(0));
@ -305,29 +328,35 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
@Op(op = BasicOperations.Extension, name = "call") @Op(op = BasicOperations.Extension, name = "call")
public DoipMessage handleCall(DoipMessage doipMessage) { public DoipMessage handleCall(DoipMessage doipMessage) {
ContractClient cc = CMActions.manager.getContractClientByDoi(doipMessage.header.parameters.id); ContractClient cc =
CMActions.manager.getContractClientByDoi(doipMessage.header.parameters.id);
if (cc == null) if (cc == null)
return replyStringWithStatus(doipMessage, "Contract instance not exist!", DoipResponseCode.DoNotFound); return replyStringWithStatus(doipMessage, "Contract instance not exist!",
DoipResponseCode.DoNotFound);
// todo transform doipMessage to args // todo transform doipMessage to args
JsonObject args = doipMessage.header.parameters.attributes; JsonObject args = doipMessage.header.parameters.attributes;
ContractRequest cr = new ContractRequest(); ContractRequest cr = new ContractRequest();
cr.setContractID(cc.getContractID()); cr.setContractID(cc.getContractID());
if (args.has("withDynamicAnalysis")) cr.withDynamicAnalysis = args.get("withDynamicAnalysis").getAsBoolean(); if (args.has("withDynamicAnalysis"))
cr.withDynamicAnalysis = args.get("withDynamicAnalysis").getAsBoolean();
if (args.has("withEvaluatesAnalysis")) if (args.has("withEvaluatesAnalysis"))
cr.withEvaluatesAnalysis = args.get("withEvaluatesAnalysis").getAsBoolean(); cr.withEvaluatesAnalysis = args.get("withEvaluatesAnalysis").getAsBoolean();
if (args.get("elementID") == null) if (args.get("elementID") == null)
return replyStringWithStatus(doipMessage, "missing elementID", DoipResponseCode.Invalid); return replyStringWithStatus(doipMessage, "missing elementID",
DoipResponseCode.Invalid);
cr.setAction(args.get("elementID").getAsString()); cr.setAction(args.get("elementID").getAsString());
if (doipMessage.body.getEncodedData() != null) cr.setArg(new String(doipMessage.body.getEncodedData())); if (doipMessage.body.getEncodedData() != null)
cr.setArg(new String(doipMessage.body.getEncodedData()));
if (doipMessage.credential != null && doipMessage.credential.getSigner() != null) if (doipMessage.credential != null && doipMessage.credential.getSigner() != null)
cr.setRequesterDOI(doipMessage.credential.getSigner()); cr.setRequesterDOI(doipMessage.credential.getSigner());
String reqID; String reqID;
if (args.has("requestID")) reqID = args.get("requestID").getAsString(); if (args.has("requestID"))
reqID = args.get("requestID").getAsString();
else { else {
reqID = System.currentTimeMillis() + "_" + (int) (Math.random() * 1000); reqID = System.currentTimeMillis() + "_" + (int) (Math.random() * 1000);
args.addProperty("requestID", reqID); args.addProperty("requestID", reqID);
@ -336,8 +365,8 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
String resp = CMActions.manager.execute(cr, null); String resp = CMActions.manager.execute(cr, null);
// StringContainer sc = new StringContainer(); // StringContainer sc = new StringContainer();
// SyncContainer sync = new SyncContainer(sc); // SyncContainer sync = new SyncContainer(sc);
return replyString(doipMessage, resp); return replyString(doipMessage, resp);
} }
@ -354,22 +383,25 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
} }
@Override @Override
public void injectListOps(DoipMessage doipMessage, DoipMessageFactory.DoipMessageBuilder builder) { public void injectListOps(DoipMessage doipMessage,
DoipMessageFactory.DoipMessageBuilder builder) {
builder.setDoipMessage(handleListOps(doipMessage)); builder.setDoipMessage(handleListOps(doipMessage));
} }
@Override @Override
public void injectUpdate(DoipMessage request, DoipMessageFactory.DoipMessageBuilder builder) { public void injectUpdate(DoipMessage request, DoipMessageFactory.DoipMessageBuilder builder) {
// use default behavior // use default behavior
} }
@Override @Override
public void injectRetrieve(DoipMessage doipMessage, DoipMessageFactory.DoipMessageBuilder builder) { public void injectRetrieve(DoipMessage doipMessage,
DoipMessageFactory.DoipMessageBuilder builder) {
builder.setDoipMessage(handleRetrieve(doipMessage)); builder.setDoipMessage(handleRetrieve(doipMessage));
} }
@Override @Override
public void injectDelete(DoipMessage doipMessage, DoipMessageFactory.DoipMessageBuilder builder) { public void injectDelete(DoipMessage doipMessage,
DoipMessageFactory.DoipMessageBuilder builder) {
builder.setDoipMessage(handleDelete(doipMessage)); builder.setDoipMessage(handleDelete(doipMessage));
} }
} }

View File

@ -37,7 +37,8 @@ public class ContractRepositoryMain {
String url = GlobalConf.instance.doaConf.doipAddress; String url = GlobalConf.instance.doaConf.doipAddress;
forwarder = new LocalDoipFrowarder(); forwarder = new LocalDoipFrowarder();
if (url == null || GlobalConf.instance.doaConf.repoDoid.isEmpty()) { if (url == null || GlobalConf.instance.doaConf.repoDoid.isEmpty()) {
LOGGER.warn("missing args, failed to start! url:" + url + " doid:" + GlobalConf.instance.doaConf.repoDoid); LOGGER.warn("missing args, failed to start! url:" + url + " doid:"
+ GlobalConf.instance.doaConf.repoDoid);
return; return;
} }
if (currentServer != null) { if (currentServer != null) {
@ -45,7 +46,8 @@ public class ContractRepositoryMain {
} }
List<DoipListenerConfig> infos = new ArrayList<>(); List<DoipListenerConfig> infos = new ArrayList<>();
infos.add(new DoipListenerConfig(url, "2.1")); infos.add(new DoipListenerConfig(url, "2.1"));
DoipServiceInfo info = new DoipServiceInfo(GlobalConf.instance.doaConf.repoDoid, GlobalConf.instance.keyPair.getPublicKeyStr(), repoType, infos); DoipServiceInfo info = new DoipServiceInfo(GlobalConf.instance.doaConf.repoDoid,
GlobalConf.instance.keyPair.getPublicKeyStr(), repoType, infos);
EndpointConfig config = new EndpointConfig(); EndpointConfig config = new EndpointConfig();
config.publicKey = GlobalConf.instance.keyPair.getPublicKeyStr(); config.publicKey = GlobalConf.instance.keyPair.getPublicKeyStr();
config.privateKey = GlobalConf.instance.keyPair.getPrivateKeyStr(); config.privateKey = GlobalConf.instance.keyPair.getPrivateKeyStr();
@ -60,15 +62,18 @@ public class ContractRepositoryMain {
SM2Signer signer = new SM2Signer(SM2KeyPair.fromJson(new Gson().toJson(config))); SM2Signer signer = new SM2Signer(SM2KeyPair.fromJson(new Gson().toJson(config)));
DelegateDoipHandler delegateHandler = new DelegateDoipHandler(requestCallback) { DelegateDoipHandler delegateHandler = new DelegateDoipHandler(requestCallback) {
@Override @Override
protected DoipMessage delegateMessage(ChannelHandlerContext context, DoipMessage message) { protected DoipMessage delegateMessage(ChannelHandlerContext context,
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder(); DoipMessage message) {
DoipMessageFactory.DoipMessageBuilder builder =
new DoipMessageFactory.DoipMessageBuilder();
builder.createResponse(DoipResponseCode.DelegateRequired, message); builder.createResponse(DoipResponseCode.DelegateRequired, message);
String id = message.header.parameters.id; String id = message.header.parameters.id;
id = id.replaceAll(".*/", ""); id = id.replaceAll(".*/", "");
ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(id); ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(id);
if (enableDelegate(meta)) { if (enableDelegate(meta)) {
LOGGER.info("delegate:" + message.requestID + " --> doipPort:" + meta.contract.getDoipPort()); LOGGER.info("delegate:" + message.requestID + " --> doipPort:"
//if port is near cmhttp server port + meta.contract.getDoipPort());
// if port is near cmhttp server port
builder.addAttributes("port", meta.contract.getDoipPort()); builder.addAttributes("port", meta.contract.getDoipPort());
DoipMessage ret; DoipMessage ret;
ret = builder.create(); ret = builder.create();
@ -77,7 +82,8 @@ public class ContractRepositoryMain {
} }
return ret; return ret;
} else { } else {
LOGGER.info("forward:" + message.requestID + " --> doipPort:" + meta.contract.getDoipPort()); LOGGER.info("forward:" + message.requestID + " --> doipPort:"
+ meta.contract.getDoipPort());
return forwarder.forward(meta, message); return forwarder.forward(meta, message);
} }

View File

@ -43,7 +43,7 @@ public class NodeCenterClientController implements NodeCenterConn {
private static boolean startCheck = false; private static boolean startCheck = false;
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;
String nodeID; String nodeID;
NodeCenterClientHandler handler; NodeCenterClientHandler handler;
// 合约contractIDmaster的公钥 // 合约contractIDmaster的公钥
@ -123,11 +123,8 @@ public class NodeCenterClientController implements NodeCenterConn {
setNodeID.put("id", keyPair.getPublicKeyStr()); setNodeID.put("id", keyPair.getPublicKeyStr());
String signature = "no signature"; String signature = "no signature";
try { try {
byte[] sig = byte[] sig = SM2Util.sign(keyPair.getPrivateKeyParameter(),
SM2Util.sign( (keyPair.getPublicKeyStr() + json.get("session").getAsString()).getBytes());
keyPair.getPrivateKeyParameter(),
(keyPair.getPublicKeyStr() + json.get("session").getAsString())
.getBytes());
signature = ByteUtils.toHexString(sig); signature = ByteUtils.toHexString(sig);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -149,8 +146,8 @@ public class NodeCenterClientController implements NodeCenterConn {
@Action @Action
public void syncPong(JsonObject json, ResultCallback resultCallback) { public void syncPong(JsonObject json, ResultCallback resultCallback) {
sync.wakeUp( sync.wakeUp(json.get("requestID").getAsString(),
json.get("requestID").getAsString(), "{\"status\":\"Success\",\"result\":\"a\"}"); "{\"status\":\"Success\",\"result\":\"a\"}");
} }
public boolean syncPing() { public boolean syncPing() {
@ -173,12 +170,9 @@ public class NodeCenterClientController implements NodeCenterConn {
} }
public void listCMInfo() { public void listCMInfo() {
sendMsg( sendMsg("{\"action\":\"listCMInfo\",\"pubKey\":\""
"{\"action\":\"listCMInfo\",\"pubKey\":\"" + KeyValueDBUtil.instance.getValue(CMTables.ConfigDB.toString(), "pubKey")
+ KeyValueDBUtil.instance.getValue(CMTables.ConfigDB.toString(), "pubKey") + "\",\"requestID\":\"" + System.currentTimeMillis() + "\"}");
+ "\",\"requestID\":\""
+ System.currentTimeMillis()
+ "\"}");
} }
@Action @Action
@ -230,7 +224,8 @@ public class NodeCenterClientController implements NodeCenterConn {
@Action(async = true) @Action(async = true)
public void publishEventFromCenter(JsonObject jo, ResultCallback rcb) { public void publishEventFromCenter(JsonObject jo, ResultCallback rcb) {
if (jo.has("event")) { if (jo.has("event")) {
CMActions.manager.deliverEvent(JsonUtil.fromJson(jo.get("event").getAsString(), REvent.class)); CMActions.manager
.deliverEvent(JsonUtil.fromJson(jo.get("event").getAsString(), REvent.class));
} }
} }
@ -257,7 +252,7 @@ public class NodeCenterClientController implements NodeCenterConn {
InputStream in = zipFile.getInputStream(zipEntry); InputStream in = zipFile.getInputStream(zipEntry);
ByteArrayOutputStream bo = new ByteArrayOutputStream(); ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] buff = new byte[1024]; byte[] buff = new byte[1024];
for (int k; (k = in.read(buff)) > 0; ) { for (int k; (k = in.read(buff)) > 0;) {
bo.write(buff, 0, k); bo.write(buff, 0, k);
} }
resp.addProperty("readmeStr", bo.toString()); resp.addProperty("readmeStr", bo.toString());
@ -278,17 +273,18 @@ public class NodeCenterClientController implements NodeCenterConn {
String requestID = jo.get("requestID").getAsString(); String requestID = jo.get("requestID").getAsString();
String requesterNodeID = jo.get("requesterNodeID").getAsString(); String requesterNodeID = jo.get("requesterNodeID").getAsString();
String crStr = jo.get("contractRequest").getAsString(); String crStr = jo.get("contractRequest").getAsString();
CMActions.manager.executeLocallyAsync(JsonUtil.fromJson(crStr, ContractRequest.class), new ResultCallback() { CMActions.manager.executeLocallyAsync(JsonUtil.fromJson(crStr, ContractRequest.class),
@Override new ResultCallback() {
public void onResult(String str) { @Override
JsonObject ret = new JsonObject(); public void onResult(String str) {
ret.addProperty("action", "onReceiveContractExecution"); JsonObject ret = new JsonObject();
ret.addProperty("requestID", requestID); ret.addProperty("action", "onReceiveContractExecution");
ret.addProperty("requesterNodeID", requesterNodeID); ret.addProperty("requestID", requestID);
ret.addProperty("contractResult", str); ret.addProperty("requesterNodeID", requesterNodeID);
sendMsg(JsonUtil.toJson(ret)); ret.addProperty("contractResult", str);
} sendMsg(JsonUtil.toJson(ret));
}, null); }
}, null);
} }
@Action(async = true) @Action(async = true)
@ -358,15 +354,15 @@ public class NodeCenterClientController implements NodeCenterConn {
String requestID = System.currentTimeMillis() + "_" + (int) (Math.random() * 100000); String requestID = System.currentTimeMillis() + "_" + (int) (Math.random() * 100000);
req.put("requestID", requestID); req.put("requestID", requestID);
sendMsg(JsonUtil.toJson(req)); sendMsg(JsonUtil.toJson(req));
//TODO use async instead? // TODO use async instead?
ContractResult cr = sync.syncSleep(requestID); ContractResult cr = sync.syncSleep(requestID);
LOGGER.debug("result: " + JsonUtil.toJson(cr)); LOGGER.debug("result: " + JsonUtil.toJson(cr));
LOGGER.info("node " + pubKey + " succeed!"); LOGGER.info("node " + pubKey + " succeed!");
if (!cr.result.equals(JsonNull.INSTANCE)) { if (!cr.result.equals(JsonNull.INSTANCE)) {
try { try {
JsonObject jo = cr.result.getAsJsonObject(); JsonObject jo = cr.result.getAsJsonObject();
NetworkManager.instance.updateAgentRouter( NetworkManager.instance.updateAgentRouter(jo.get("pubKey").getAsString(),
jo.get("pubKey").getAsString(), jo.get("masterAddress").getAsString()); jo.get("masterAddress").getAsString());
NetworkManager.instance.connectToAgent(jo.get("pubKey").getAsString(), null); NetworkManager.instance.connectToAgent(jo.get("pubKey").getAsString(), null);
return "success"; return "success";
} catch (Exception e) { } catch (Exception e) {
@ -390,18 +386,17 @@ public class NodeCenterClientController implements NodeCenterConn {
if (!cr.result.equals(JsonNull.INSTANCE)) { if (!cr.result.equals(JsonNull.INSTANCE)) {
try { try {
JsonObject jo = cr.result.getAsJsonObject(); JsonObject jo = cr.result.getAsJsonObject();
NetworkManager.instance.updateAgentRouter( NetworkManager.instance.updateAgentRouter(jo.get("pubKey").getAsString(),
jo.get("pubKey").getAsString(), jo.get("masterAddress").getAsString()); jo.get("masterAddress").getAsString());
NetworkManager.instance.connectToAgent(jo.get("pubKey").getAsString(), null); NetworkManager.instance.connectToAgent(jo.get("pubKey").getAsString(), null);
LOGGER.info( LOGGER.info(String.format("the master of contract %s: pubKey=%s address=%s",
String.format("the master of contract %s: pubKey=%s address=%s", contractID, jo.get("pubKey").getAsString(),
contractID, jo.get("masterAddress").getAsString()));
jo.get("pubKey").getAsString(),
jo.get("masterAddress").getAsString()));
contractID2PubKey.put(contractID, jo.get("pubKey").getAsString()); contractID2PubKey.put(contractID, jo.get("pubKey").getAsString());
return jo.get("pubKey").getAsString(); return jo.get("pubKey").getAsString();
} catch (Exception e) { } catch (Exception e) {
LOGGER.warn("the master of contract " + contractID + " is null! " + e.getMessage()); LOGGER.warn(
"the master of contract " + contractID + " is null! " + e.getMessage());
} }
} }
} catch (Exception e) { } catch (Exception e) {
@ -420,8 +415,8 @@ public class NodeCenterClientController implements NodeCenterConn {
return; return;
} }
startCheck = true; startCheck = true;
for (MultiContractMeta meta : for (MultiContractMeta meta : CMActions.manager.multiContractRecorder.getStatus()
CMActions.manager.multiContractRecorder.getStatus().values()) { .values()) {
String contractID = meta.getContractID(); String contractID = meta.getContractID();
LOGGER.info("check master of contract " + contractID); LOGGER.info("check master of contract " + contractID);
ContractMeta cmeta = CMActions.manager.statusRecorder.getContractMeta(meta.getID()); ContractMeta cmeta = CMActions.manager.statusRecorder.getContractMeta(meta.getID());
@ -435,20 +430,21 @@ public class NodeCenterClientController implements NodeCenterConn {
// 该合约可能在这个节点崩溃期间已经被终止不存在了这个节点不用恢复这个合约了直接从数据库中删除 // 该合约可能在这个节点崩溃期间已经被终止不存在了这个节点不用恢复这个合约了直接从数据库中删除
LOGGER.info("该合约在集群已经不存在!"); LOGGER.info("该合约在集群已经不存在!");
KeyValueDBUtil.instance.delete(CMTables.UnitContracts.toString(), contractID); KeyValueDBUtil.instance.delete(CMTables.UnitContracts.toString(), contractID);
if (KeyValueDBUtil.instance.containsKey( if (KeyValueDBUtil.instance.containsKey(CMTables.CheckPointLastHash.toString(),
CMTables.CheckPointLastHash.toString(), contractID)) { contractID)) {
KeyValueDBUtil.instance.delete( KeyValueDBUtil.instance.delete(CMTables.CheckPointLastHash.toString(),
CMTables.CheckPointLastHash.toString(), contractID); contractID);
} }
if (KeyValueDBUtil.instance.containsKey( if (KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(),
CMTables.LastExeSeq.toString(), contractID)) { contractID)) {
KeyValueDBUtil.instance.delete(CMTables.LastExeSeq.toString(), contractID); KeyValueDBUtil.instance.delete(CMTables.LastExeSeq.toString(), contractID);
} }
continue; continue;
} }
MasterClientRecoverMechAction.recoverSet.add(contractID); MasterClientRecoverMechAction.recoverSet.add(contractID);
LOGGER.info("queryUnitContractsID master为" + master.substring(0, 5) + " -> 合约id:" + contractID); LOGGER.info("queryUnitContractsID master为" + master.substring(0, 5) + " -> 合约id:"
+ contractID);
RecoverMechTimeRecorder.queryMasterFinish = System.currentTimeMillis(); RecoverMechTimeRecorder.queryMasterFinish = System.currentTimeMillis();
queryUnitContractsID2(contractID, master); queryUnitContractsID2(contractID, master);
} }
@ -489,9 +485,7 @@ public class NodeCenterClientController implements NodeCenterConn {
@Action(async = true) @Action(async = true)
public void requestLog(JsonObject json, ResultCallback rc) { public void requestLog(JsonObject json, ResultCallback rc) {
if (!json.has("requestID") if (!json.has("requestID") || !json.has("contractID") || !json.has("offset")
|| !json.has("contractID")
|| !json.has("offset")
|| !json.has("count")) { || !json.has("count")) {
LOGGER.debug( LOGGER.debug(
"[CMClientController] missing arguments, requestID / contractID / offset / count"); "[CMClientController] missing arguments, requestID / contractID / offset / count");
@ -567,7 +561,7 @@ public class NodeCenterClientController implements NodeCenterConn {
long total = tempZip.length(); long total = tempZip.length();
LOGGER.debug("temp length = " + total); LOGGER.debug("temp length = " + total);
long count = 0; long count = 0;
for (int len; (len = (fin.read(buff))) > 0; ) { for (int len; (len = (fin.read(buff))) > 0;) {
LOGGER.debug("read len = " + len); LOGGER.debug("read len = " + len);
req.put("data", ByteUtil.encodeBASE64(buff, len)); req.put("data", ByteUtil.encodeBASE64(buff, len));
count += len; count += len;
@ -631,8 +625,8 @@ public class NodeCenterClientController implements NodeCenterConn {
MasterElectTimeRecorder.slaveConnectFinish = System.currentTimeMillis(); MasterElectTimeRecorder.slaveConnectFinish = System.currentTimeMillis();
// 开启master恢复 // 开启master恢复
MasterServerRecoverMechAction.newMasterRecover( MasterServerRecoverMechAction.newMasterRecover(contractID,
contractID, json.get("members").getAsString(), onlineMembers); json.get("members").getAsString(), onlineMembers);
} }
private boolean waitForConnection(List<String> nodeNames) { private boolean waitForConnection(List<String> nodeNames) {
@ -652,7 +646,8 @@ public class NodeCenterClientController implements NodeCenterConn {
e.printStackTrace(); e.printStackTrace();
} }
} }
} else return true; } else
return true;
} }
return false; return false;
} }
@ -668,7 +663,8 @@ public class NodeCenterClientController implements NodeCenterConn {
String distributeID = null; String distributeID = null;
if (json.has("distributeID")) if (json.has("distributeID"))
distributeID = json.get("distributeID").getAsString(); distributeID = json.get("distributeID").getAsString();
else distributeID = json.get("responseID").getAsString(); else
distributeID = json.get("responseID").getAsString();
ResultCallback to = distributeReqMap.get(distributeID); ResultCallback to = distributeReqMap.get(distributeID);
distributeReqMap.remove(distributeID); distributeReqMap.remove(distributeID);
to.onResult(json.get("content").getAsString()); to.onResult(json.get("content").getAsString());
@ -720,13 +716,11 @@ public class NodeCenterClientController implements NodeCenterConn {
public void NCStartElect(JsonObject jo, ResultCallback result) { public void NCStartElect(JsonObject jo, ResultCallback result) {
String conID = jo.get("contractID").getAsString(); String conID = jo.get("contractID").getAsString();
String uniNumber = null; String uniNumber = null;
if (jo.has("nuiNumber")) uniNumber = jo.get("nuiNumber").getAsString(); if (jo.has("nuiNumber"))
uniNumber = jo.get("nuiNumber").getAsString();
ContractClient cc = CMActions.manager.getClient(conID); ContractClient cc = CMActions.manager.getClient(conID);
LOGGER.info( LOGGER.info("[CMClientController] NCStartElect : contractID=" + conID + " client==null:"
"[CMClientController] NCStartElect : contractID=" + (null == cc));
+ conID
+ " client==null:"
+ (null == cc));
// 不是自己本地的合约 // 不是自己本地的合约
if (null == cc) { if (null == cc) {
@ -773,7 +767,7 @@ public class NodeCenterClientController implements NodeCenterConn {
} }
public void run() { public void run() {
for (; ; ) { for (;;) {
if (receiveQueue.size() > 0) { if (receiveQueue.size() > 0) {
try { try {
JsonObject jo = receiveQueue.poll(); JsonObject jo = receiveQueue.poll();
@ -800,11 +794,12 @@ public class NodeCenterClientController implements NodeCenterConn {
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,
String.format("isAppend=%b isDone=%b isPrivate=%b", isAppend, isDone, isPrivate)); 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()) {

View File

@ -41,10 +41,11 @@ import java.util.concurrent.TimeUnit;
* @author OliveDS (Shuang Deng) * @author OliveDS (Shuang Deng)
*/ */
public class NetworkManager { public class NetworkManager {
//Manage server->client connection; // Manage server->client connection;
public static final Map<String, AgentConnector> CONNECTORS = new ConcurrentHashMap<>(); public static final Map<String, AgentConnector> CONNECTORS = new ConcurrentHashMap<>();
//Manage client->server connection; // Manage client->server connection;
public static final Map<String, TCPServerFrameHandler> SERVER_CONNECTORS = new ConcurrentHashMap<>(); public static final Map<String, TCPServerFrameHandler> SERVER_CONNECTORS =
new ConcurrentHashMap<>();
public static final String NODE_CENTER_CLIENT = "NODE_CENTER_CLIENT"; public static final String NODE_CENTER_CLIENT = "NODE_CENTER_CLIENT";
public static final String P2P_GRPC_CLIENT = "P2P_GRPC_CLIENT"; public static final String P2P_GRPC_CLIENT = "P2P_GRPC_CLIENT";
private static final Map<String, String> slaverRouter = new HashMap<>(); private static final Map<String, String> slaverRouter = new HashMap<>();
@ -59,18 +60,13 @@ public class NetworkManager {
} }
public static void reconnectAgent(String master) { public static void reconnectAgent(String master) {
LOGGER.debug( LOGGER.debug(String.format("master=%s\t%s", master, JsonUtil.toJson(slaverRouter)));
String.format("master=%s\t%s",
master,
JsonUtil.toJson(slaverRouter)));
try { try {
NetworkManager.AgentConnector conn; NetworkManager.AgentConnector conn;
synchronized (conn = NetworkManager.CONNECTORS.get(master)) { synchronized (conn = NetworkManager.CONNECTORS.get(master)) {
if (!conn.handler.isOpen()) { if (!conn.handler.isOpen()) {
String[] ipAndPort = slaverRouter.get(master).split(":"); String[] ipAndPort = slaverRouter.get(master).split(":");
conn.bootstrap conn.bootstrap.connect(ipAndPort[0], Integer.parseInt(ipAndPort[1])).sync()
.connect(ipAndPort[0], Integer.parseInt(ipAndPort[1]))
.sync()
.channel(); .channel();
} }
} }
@ -94,46 +90,36 @@ public class NetworkManager {
nodeCenterClientHandler = ControllerManager.createNodeCenterClientHandler(); nodeCenterClientHandler = ControllerManager.createNodeCenterClientHandler();
EventLoopGroup group = new NioEventLoopGroup(); EventLoopGroup group = new NioEventLoopGroup();
b.group(group); b.group(group);
b.channel(NioSocketChannel.class) b.channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
.handler( @Override
new ChannelInitializer<SocketChannel>() { protected void initChannel(SocketChannel ch) {
@Override ChannelPipeline p = ch.pipeline();
protected void initChannel(SocketChannel ch) { p.addLast(new DelimiterCodec()).addLast(nodeCenterClientHandler);
ChannelPipeline p = ch.pipeline(); }
p.addLast(new DelimiterCodec()).addLast(nodeCenterClientHandler); });
} ContractManager.scheduledThreadPool.scheduleWithFixedDelay(() -> {
}); try {
ContractManager.scheduledThreadPool.scheduleWithFixedDelay( // manager.clearCache();
() -> { String URL = GlobalConf.getNodeCenterUrl();
try { // LOGGER.debug("GlobalConf.getNodeCenterUrl() -> URL=" + URL);
// manager.clearCache(); URI uri = null;
String URL = GlobalConf.getNodeCenterUrl(); try {
// LOGGER.debug("GlobalConf.getNodeCenterUrl() -> URL=" + URL); uri = new URI(URL);
URI uri = null; } catch (URISyntaxException e) {
try { LOGGER.error("creating uri failed! " + e.getMessage());
uri = new URI(URL); }
} catch (URISyntaxException e) { if (!nodeCenterClientHandler.isConnected()
LOGGER.error("creating uri failed! " + e.getMessage()); || !ControllerManager.getNodeCenterController().syncPing()) {
} nodeCenterClientHandler.close();
if (!nodeCenterClientHandler.isConnected() assert null != uri;
|| !ControllerManager.getNodeCenterController().syncPing()) { b.connect(uri.getHost(), uri.getPort()).sync().channel();
nodeCenterClientHandler.close(); LOGGER.info("connect to node center: " + uri.getHost() + ":" + uri.getPort());
assert null != uri; }
b.connect(uri.getHost(), uri.getPort()).sync().channel(); } catch (Exception e) {
LOGGER.info( // e.printStackTrace();
"connect to node center: " LOGGER.warn("connecting to node center failed! " + e.getMessage());
+ uri.getHost() }
+ ":" }, 4, 30 + (int) (20 * Math.random()), TimeUnit.SECONDS);
+ uri.getPort());
}
} catch (Exception e) {
// e.printStackTrace();
LOGGER.warn("connecting to node center failed! " + e.getMessage());
}
},
4,
30 + (int) (20 * Math.random()),
TimeUnit.SECONDS);
} }
@ -142,20 +128,16 @@ public class NetworkManager {
ServerBootstrap b = new ServerBootstrap(); ServerBootstrap b = new ServerBootstrap();
EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup bossGroup = new NioEventLoopGroup(1);
b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
b.group(bossGroup, workerGroup) b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
.channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).localAddress(port)
.option(ChannelOption.SO_BACKLOG, 100)
.localAddress(port)
.childOption(ChannelOption.SO_KEEPALIVE, true) .childOption(ChannelOption.SO_KEEPALIVE, true)
.childHandler( .childHandler(new ChannelInitializer<SocketChannel>() {
new ChannelInitializer<SocketChannel>() { @Override
@Override protected void initChannel(SocketChannel arg0) {
protected void initChannel(SocketChannel arg0) { arg0.pipeline().addLast(new DelimiterCodec())
arg0.pipeline() .addLast(new TCPServerFrameHandler());
.addLast(new DelimiterCodec()) }
.addLast(new TCPServerFrameHandler()); });
}
});
b.bind(port).sync().channel(); b.bind(port).sync().channel();
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -164,7 +146,7 @@ public class NetworkManager {
} }
//TODO Remove in future // TODO Remove in future
public void sendToNodeCenter(String msg) { public void sendToNodeCenter(String msg) {
nodeCenterClientHandler.sendMsg(msg); nodeCenterClientHandler.sendMsg(msg);
} }
@ -174,9 +156,8 @@ public class NetworkManager {
} }
public void waitForNodeCenterConnected() { public void waitForNodeCenterConnected() {
for (int i = 0; for (int i = 0; i < 10 && null != nodeCenterClientHandler
i < 10 && null != nodeCenterClientHandler && !nodeCenterClientHandler.isConnected(); && !nodeCenterClientHandler.isConnected(); i++) {
i++) {
try { try {
Thread.sleep(200); Thread.sleep(200);
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -191,7 +172,7 @@ public class NetworkManager {
} }
} }
//----------AgentNetworkManagement // ----------AgentNetworkManagement
public void updateAgentRouter(String nodeID, String address) { public void updateAgentRouter(String nodeID, String address) {
@ -233,15 +214,13 @@ public class NetworkManager {
connector.handler = handler; connector.handler = handler;
b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000); b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000);
b.group(CMHttpServer.workerGroup); b.group(CMHttpServer.workerGroup);
b.channel(NioSocketChannel.class) b.channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
.handler( @Override
new ChannelInitializer<SocketChannel>() { protected void initChannel(SocketChannel ch) {
@Override ChannelPipeline p = ch.pipeline();
protected void initChannel(SocketChannel ch) { p.addLast(new DelimiterCodec()).addLast(handler);
ChannelPipeline p = ch.pipeline(); }
p.addLast(new DelimiterCodec()).addLast(handler); });
}
});
} }
reconnectAgent(master); reconnectAgent(master);
} }
@ -292,8 +271,8 @@ public class NetworkManager {
return CONNECTORS.containsKey(pubKey); return CONNECTORS.containsKey(pubKey);
} }
//-------UNUSED TOMerge------------ // -------UNUSED TOMerge------------
//UNUSED // UNUSED
public TCPClientFrameHandler createTCPClient(String peer, String ipPort) public TCPClientFrameHandler createTCPClient(String peer, String ipPort)
throws InterruptedException { throws InterruptedException {
if (peer.equals(GlobalConf.instance.peerID)) { if (peer.equals(GlobalConf.instance.peerID)) {
@ -306,30 +285,28 @@ public class NetworkManager {
NioEventLoopGroup group = new NioEventLoopGroup(); NioEventLoopGroup group = new NioEventLoopGroup();
Bootstrap b = new Bootstrap(); Bootstrap b = new Bootstrap();
final TCPClientFrameHandler handler = new TCPClientFrameHandler(peer); final TCPClientFrameHandler handler = new TCPClientFrameHandler(peer);
//tcpClientMap.put(peer, handler); // tcpClientMap.put(peer, handler);
b.group(group) b.group(group).channel(NioSocketChannel.class)
.channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress(host, port)) .remoteAddress(new InetSocketAddress(host, port))
.option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.TCP_NODELAY, true)
.handler( .handler(new ChannelInitializer<SocketChannel>() {
new ChannelInitializer<SocketChannel>() { @Override
@Override public void initChannel(SocketChannel ch) throws Exception {
public void initChannel(SocketChannel ch) throws Exception { ch.pipeline()
ch.pipeline() // .addLast(new
// .addLast(new // IdleStateHandler(0, 4, 0, TimeUnit.SECONDS))
// IdleStateHandler(0, 4, 0, TimeUnit.SECONDS)) .addLast(new DelimiterCodec())
.addLast(new DelimiterCodec()) .addLast(new IdleStateHandler(0, 5, 0))
.addLast(new IdleStateHandler(0, 5, 0)) // .addLast(new
// .addLast(new // HeartBeatEncode())
// HeartBeatEncode()) .addLast(handler);
.addLast(handler); }
} });
});
ChannelFuture f = b.connect().sync(); ChannelFuture f = b.connect().sync();
if (f.isSuccess()) { if (f.isSuccess()) {
LOGGER.info("TCP Client " + peer + " started"); LOGGER.info("TCP Client " + peer + " started");
} }
// channel = (SocketChannel) future.channel(); // channel = (SocketChannel) future.channel();
return handler; return handler;
} }
@ -352,14 +329,14 @@ public class NetworkManager {
} }
/** /**
* UNUSED send to TCP nodes, if fail send by p2p * UNUSED send to TCP nodes, if fail send by p2p
* *
* @param msg unit message * @param msg unit message
*/ */
public void send(UnitMessage msg) { public void send(UnitMessage msg) {
Iterator<String> iterator = msg.getReceiverList().iterator(); Iterator<String> iterator = msg.getReceiverList().iterator();
String peer; String peer;
// boolean sendByP2P = false; // boolean sendByP2P = false;
TCPClientFrameHandler tcpClientFrameHandler; TCPClientFrameHandler tcpClientFrameHandler;
while (iterator.hasNext()) { while (iterator.hasNext()) {
peer = iterator.next(); peer = iterator.next();
@ -368,17 +345,16 @@ public class NetworkManager {
LOGGER.info("send msg to itself " + msg); LOGGER.info("send msg to itself " + msg);
continue; continue;
} }
// tcpClientFrameHandler = NetworkManager.instance.tcpClientMap.getOrDefault(peer, null); // tcpClientFrameHandler = NetworkManager.instance.tcpClientMap.getOrDefault(peer,
// null);
if (peerID2TCPAddress.containsKey(peer)) { if (peerID2TCPAddress.containsKey(peer)) {
//recreateTCPClient(peer); // recreateTCPClient(peer);
// instance.tcpClientMap.put(peer, tcpClientFrameHandler); // instance.tcpClientMap.put(peer, tcpClientFrameHandler);
UnitMessage unitMessage = UnitMessage unitMessage = msg.toBuilder().clearReceiver().addReceiver(peer).build();
msg.toBuilder().clearReceiver().addReceiver(peer).build();
LOGGER.info("send msg by p2p to " + peer); LOGGER.info("send msg by p2p to " + peer);
JavaContractServiceGrpcServer.sendMsg(unitMessage); JavaContractServiceGrpcServer.sendMsg(unitMessage);
} else { } else {
UnitMessage unitMessage = UnitMessage unitMessage = msg.toBuilder().clearReceiver().addReceiver(peer).build();
msg.toBuilder().clearReceiver().addReceiver(peer).build();
LOGGER.info("send msg by p2p to " + peer); LOGGER.info("send msg by p2p to " + peer);
JavaContractServiceGrpcServer.sendMsg(unitMessage); JavaContractServiceGrpcServer.sendMsg(unitMessage);
} }