org.bdware.ypkdeploy.RouterSetter Maven / Gradle / Ivy
The newest version!
package org.bdware.ypkdeploy;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.bdware.client.ContractRequest;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import org.zz.gmhelper.SM2KeyPair;
import org.zz.gmhelper.SM2Util;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
public class RouterSetter {
static class Task {
List tasks;
}
static class SetUpperTask {
public String nodeName;
public String publicKey;
public String privateKey;
public String upperIP;
public String ipAndPort;
}
public static ExecutorService es = Executors.newFixedThreadPool(100);
public static void goByStr(String content) {
Task entry = new Gson().fromJson(content, Task.class);
AtomicInteger count = new AtomicInteger(0);
for (SetUpperTask task : entry.tasks)
es.execute(new Runnable() {
@Override
public void run() {
try {
executeTask(task);
} catch (Exception e) {
e.printStackTrace();
}
count.incrementAndGet();
}
});
for (; count.get() < entry.tasks.size();) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("Sleep 10s, count:" + count.get());
}
}
private static void executeTask(SetUpperTask task) throws Exception {
SM2KeyPair keyPair = SM2KeyPair.fromJson(new Gson().toJson(task));
SmartContractClientExt ext = new SmartContractClientExt(
String.format("ws://%s/SCIDE/SCExecutor", task.ipAndPort), keyPair);
System.out.println("[RouterSetter] try connect " + task.nodeName + " " + task.ipAndPort);
ext.waitForConnect();
System.out.println("[RouterSetter] try login " + task.nodeName + " " + task.ipAndPort);
ext.loginSync();
System.out.println("[RouterSetter] loginDone " + task.nodeName + " " + task.ipAndPort);
ContractRequest cr = new ContractRequest();
cr.setContractID("Router");
cr.setAction("setPubKey");
cr.setArg(String.format("{\"pubKey\":\"%s\"}", task.publicKey));
cr.doSignature(keyPair);
JsonObject result = ext.executeContractSync(cr);
System.out.println("[RouterSetter] setPubKey " + task.nodeName + " result:" + result);
cr = new ContractRequest();
cr.setContractID("Router");
cr.setAction("setUpperIP");
cr.setArg(String.format("{\"upperIP\":\"%s\"}", task.upperIP));
cr.doSignature(keyPair);
result = ext.executeContractSync(cr);
System.out.println("[RouterSetter] setUpperIP " + task.nodeName + " result:" + result);
cr = new ContractRequest();
cr.setContractID("Router");
cr.setAction("setName");
cr.setArg(String.format("{\"name\":\"%s\"}", task.nodeName));
cr.doSignature(keyPair);
result = ext.executeContractSync(cr);
System.out.println("[RouterSetter] setName " + task.nodeName + " result:" + result);
cr = new ContractRequest();
cr.setContractID("Router");
cr.setAction("setUpperPort");
cr.setArg(String.format("{\"upperPort\":\"%s\"}", 21060));
cr.doSignature(keyPair);
result = ext.executeContractSync(cr);
System.out.println("[RouterSetter] setUpperPort " + task.nodeName + " result:" + result);
cr = new ContractRequest();
cr.setAction("setSignature");
cr.setContractID("Router");
String toSign = task.nodeName + "|" + task.publicKey;
String signature = ByteUtils
.toHexString(SM2Util.sign(keyPair.getPrivateKeyParameter(), toSign.getBytes()));
cr.setArg(String.format("{\"signature\":\"%s\"}", signature));
cr.doSignature(keyPair);
result = ext.executeContractSync(cr);
System.out.println("[RouterSetter] setSignature" + task.nodeName + " result:" + result);
cr = new ContractRequest();
cr.setContractID("Router");
cr.setAction("reInit");
cr.setArg("");
cr.doSignature(keyPair);
result = ext.executeContractSync(cr);
System.out.println("[RouterSetter] reInit " + task.nodeName + " result:" + result);
}
public static void goByPath(String path) {
String conf = DeployConfGenerator.getFileContent(path);
goByStr(conf);
}
public static void main(String[] args) {
System.out.println("Usage: set deployentry.json or ");
if (args.length > 0)
goByPath(args[0]);
else
goByPath("./setRouter.json");
}
}