diff --git a/src/main/java/org/bdware/server/NodeCenterServer.java b/src/main/java/org/bdware/server/NodeCenterServer.java index 754c8f3..052c66c 100644 --- a/src/main/java/org/bdware/server/NodeCenterServer.java +++ b/src/main/java/org/bdware/server/NodeCenterServer.java @@ -21,11 +21,9 @@ import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.config.Configurator; -import org.bdware.sc.DoConfig; import org.bdware.sc.db.KeyValueDBUtil; import org.bdware.sc.db.MultiIndexTimeRocksDBUtil; import org.bdware.sc.db.TimeDBUtil; -import org.bdware.server.irp.LocalLHSProxy; import org.bdware.server.nodecenter.*; import org.bdware.server.permission.Role; import org.bdware.server.ws.DelimiterCodec; @@ -70,14 +68,6 @@ public class NodeCenterServer { } public static void configServer(CMDConf cmdConf) { - LocalLHSProxy.port = cmdConf.doipPort; - LocalLHSProxy.enabled = true; - if (cmdConf.disableDoRepo) { - DoConfig.callContractUsingDOI = false; - } - if (cmdConf.disableLocalLhs) { - LocalLHSProxy.enabled = false; - } if (!cmdConf.enableSsl.isEmpty()) { try { String[] filePaths = cmdConf.enableSsl.split(":"); diff --git a/src/main/java/org/bdware/server/irp/LocalLHSProxy.java b/src/main/java/org/bdware/server/irp/LocalLHSProxy.java deleted file mode 100644 index 5de7bdd..0000000 --- a/src/main/java/org/bdware/server/irp/LocalLHSProxy.java +++ /dev/null @@ -1,123 +0,0 @@ -package org.bdware.server.irp; - -import com.sun.net.httpserver.HttpExchange; -import com.sun.net.httpserver.HttpHandler; -import com.sun.net.httpserver.HttpServer; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.bdware.sc.util.JsonUtil; -import org.rocksdb.Options; -import org.rocksdb.RocksDB; -import org.rocksdb.RocksDBException; - -import java.io.File; -import java.io.IOException; -import java.io.OutputStream; -import java.io.UnsupportedEncodingException; -import java.net.InetSocketAddress; -import java.net.URLDecoder; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -public class LocalLHSProxy { - private static final Logger LOGGER = LogManager.getLogger(LocalLHSProxy.class); - public static String PREFIX = "86.5000.470/"; - public static int port = 18007; - public static boolean enabled = false; - public static RocksDB db; - static RegisterHandler registerHandler; - - static { - RocksDB.loadLibrary(); - } - - public static void main(String[] args) throws Exception { - start(); - } - - public static void start() throws IOException, RocksDBException { - String dbPath = "./handleRecords/"; - Options rockopts = new Options().setCreateIfMissing(true); - rockopts.useFixedLengthPrefixExtractor(15); - LOGGER.info("actual rocksdb path: " + new File(dbPath).getAbsolutePath()); - File lock = new File(dbPath, "LOCK"); - if (lock.exists()) { - LOGGER.trace("remove file " + lock.getAbsolutePath() + ": " + lock.delete()); - } - db = RocksDB.open(rockopts, dbPath); - - registerHandler = new RegisterHandler(db); - - LOGGER.info("listen to " + port); - HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); - - server.createContext("/test", new TestHandler()); - server.createContext("/resolve", new ResolveHandler()); - server.createContext("/local/", registerHandler); - server.createContext("/view", new ViewHandler(db)); - server.start(); - } - - private static Map formData2Dic(String formData) { - Map result = new HashMap<>(); - if (formData == null || formData.trim().length() == 0) { - return result; - } - final String[] items = formData.split("&"); - Arrays.stream(items) - .forEach( - item -> { - final String[] keyAndVal = item.split("="); - if (keyAndVal.length == 2) { - try { - final String key = URLDecoder.decode(keyAndVal[0], "utf8"); - final String val = URLDecoder.decode(keyAndVal[1], "utf8"); - result.put(key, val); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } - } - }); - return result; - } - - static class TestHandler implements HttpHandler { - @Override - public void handle(HttpExchange exchange) { - String response = "hello world"; - try { - exchange.sendResponseHeaders(200, 0); - OutputStream os = exchange.getResponseBody(); - os.write(response.getBytes()); - os.close(); - } catch (Exception e) { - e.printStackTrace(); - } - } - } - - static class ResolveHandler implements HttpHandler { - @Override - public void handle(HttpExchange exchange) { - try { - // 获得查询字符串(get) - String queryString = exchange.getRequestURI().getQuery(); - Map queryStringInfo = formData2Dic(queryString); - String id = queryStringInfo.get("identifier"); - Map respMap; - respMap = registerHandler.handleResolve(queryStringInfo); - respMap.forEach((k, v) -> { - LOGGER.debug(k + "->" + v); - }); - exchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*"); - exchange.sendResponseHeaders(200, 0); - OutputStream os = exchange.getResponseBody(); - os.write(JsonUtil.toJson(respMap).getBytes()); - os.close(); - } catch (Exception e) { - e.printStackTrace(); - } - } - } -} diff --git a/src/main/java/org/bdware/server/irp/RegisterHandler.java b/src/main/java/org/bdware/server/irp/RegisterHandler.java deleted file mode 100644 index 8026e95..0000000 --- a/src/main/java/org/bdware/server/irp/RegisterHandler.java +++ /dev/null @@ -1,246 +0,0 @@ -package org.bdware.server.irp; - -import com.sun.net.httpserver.HttpExchange; -import com.sun.net.httpserver.HttpHandler; -import org.apache.commons.io.IOUtils; -import org.bdware.sc.util.JsonUtil; -import org.rocksdb.RocksDB; - -import java.io.IOException; -import java.io.OutputStream; -import java.io.UnsupportedEncodingException; -import java.net.URLDecoder; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; -import java.util.Random; - -public class RegisterHandler implements HttpHandler { - RocksDB db; - String dbPath = "./handleRecords/"; - - public RegisterHandler(RocksDB rocksDB) { - db = rocksDB; - } - - public Map HandleRequest(Map postInfo) { - Map respMap = new HashMap<>(); - String action = postInfo.get("action"); - switch (action) { - case "resolve": - respMap = handleResolve(postInfo); - break; - case "register": - case "forceRegister": - respMap = handleRegister(postInfo); - break; - case "unregister": - case "forceDelete": - respMap = handleUnRegister(postInfo); - break; - case "reregister": - respMap = handleReregister(postInfo); - break; - default: - System.out.println("error response"); - } - return respMap; - } - - public Map handleRegister(Map reqMap) { - Map respMap = new HashMap<>(); - String type; - switch (reqMap.get("hrType")) { - case "do": - type = "do."; - ViewHandler.handleRecordCount.doCount++; - break; - case "dou": - type = "dou."; - ViewHandler.handleRecordCount.userCount++; - break; - case "doip": - type = "doip."; - ViewHandler.handleRecordCount.repoCount++; - break; - default: - type = "ukw."; - break; - } - String identifier = LocalLHSProxy.PREFIX + type + geneRandomID() + "_bdw"; - reqMap.remove("action"); - reqMap.remove("hrType"); - reqMap.put("identifier", identifier); - String handleRecord = JsonUtil.toJson(reqMap); - try { - db.put(identifier.getBytes(), handleRecord.getBytes()); - System.out.println("successful update do, identifier: " + identifier); - respMap.put("identifier", identifier); - respMap.put("status", "1"); - respMap.put("response", "register DO success, identifier: " + identifier); - ViewHandler.addRegister(); - } catch (Exception e) { - e.printStackTrace(); - respMap.put("status", "0"); - respMap.put("response", "respond failed from LHS"); - } - return respMap; - } - - public Map handleReregister(Map reqMap) { - Map respMap = new HashMap<>(); - String identifier; - if (reqMap.get("identifier") != null) identifier = reqMap.get("identifier"); - else { - respMap.put("status", "0"); - respMap.put("response", "identifier not found"); - return respMap; - } - try { - reqMap.remove("action"); - reqMap.remove("hrType"); - String handleRecord = JsonUtil.toJson(reqMap); - if (db.get(identifier.getBytes()) == null) { - String idType; - if ((identifier.split("/")).length > 2) - idType = identifier.split("/")[1].split("\\.")[0]; - else - idType = identifier.split("\\.")[0]; - switch (idType) { - case "do": - ViewHandler.handleRecordCount.doCount++; - break; - case "dou": - ViewHandler.handleRecordCount.userCount++; - break; - case "doip": - ViewHandler.handleRecordCount.repoCount++; - break; - default: - break; - } - } else { - db.delete(identifier.getBytes()); - } - - db.put(identifier.getBytes(), handleRecord.getBytes()); - System.out.println("successful update do, identifier: " + identifier); - respMap.put("identifier", identifier); - respMap.put("status", "1"); - respMap.put("response", "reRegister DO success, identifier: " + identifier); - } catch (Exception e) { - e.printStackTrace(); - respMap.put("status", "0"); - respMap.put("response", "unregister failed: " + e.getMessage()); - } - return respMap; - } - - public Map handleUnRegister(Map reqMap) { - Map respMap = new HashMap<>(); - String identifier; - if (reqMap.get("identifier") != null) identifier = reqMap.get("identifier"); - else { - respMap.put("status", "0"); - respMap.put("response", "identifier not found"); - return respMap; - } - try { - switch (identifier.split("\\.")[2]) { - case "470/do": - ViewHandler.handleRecordCount.doCount--; - break; - case "470/doip": - ViewHandler.handleRecordCount.repoCount--; - break; - case "470/dou": - ViewHandler.handleRecordCount.userCount--; - break; - } - db.delete(identifier.getBytes()); - respMap.put("status", "1"); - respMap.put("response", "success delete: " + identifier); - ViewHandler.addDelete(); - } catch (Exception e) { - e.printStackTrace(); - respMap.put("status", "0"); - respMap.put("response", "unregister failed: " + e.getMessage()); - } - return respMap; - } - - public Map handleResolve(Map reqMap) { - Map respMap = new HashMap<>(); - String identifier; - if (reqMap.get("identifier") != null) identifier = reqMap.get("identifier"); - else { - respMap.put("status", "0"); - respMap.put("response", "identifier not found"); - return respMap; - } - try { - if (db.get(identifier.getBytes()) == null) { - respMap.put("status", "0"); - respMap.put("response", "not exist"); - return respMap; - } - String result = new String(db.get(identifier.getBytes())); - respMap = JsonUtil.fromJson(result, HashMap.class); - ViewHandler.addResolve(); - } catch (Exception e) { - e.printStackTrace(); - respMap.put("status", "0"); - respMap.put("response", "resolve failed: " + e.getMessage()); - } - return respMap; - } - - public String geneRandomID() { - String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - Random random = new Random(); - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < 10; i++) { - int number = random.nextInt(62); - sb.append(str.charAt(number)); - } - return sb.toString(); - } - - @Override - public void handle(HttpExchange exchange) throws IOException { - Map respMap; - try { - //获得表单提交数据(post) - String postString = IOUtils.toString(exchange.getRequestBody()); - Map postInfo = formData2Dic(postString); - respMap = HandleRequest(postInfo); - exchange.sendResponseHeaders(200, 0); - OutputStream os = exchange.getResponseBody(); - os.write(JsonUtil.toJson(respMap).getBytes()); - os.close(); - } catch (Exception e) { - e.printStackTrace(); - } - } - - public Map formData2Dic(String formData) { - Map result = new HashMap<>(); - if (formData == null || formData.trim().length() == 0) { - return result; - } - final String[] items = formData.split("&"); - Arrays.stream(items).forEach(item -> { - final String[] keyAndVal = item.split("="); - if (keyAndVal.length == 2) { - try { - final String key = URLDecoder.decode(keyAndVal[0], "utf8"); - final String val = URLDecoder.decode(keyAndVal[1], "utf8"); - result.put(key, val); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } - } - }); - return result; - } -} diff --git a/src/main/java/org/bdware/server/irp/StatisticsByDay.java b/src/main/java/org/bdware/server/irp/StatisticsByDay.java deleted file mode 100644 index 967d326..0000000 --- a/src/main/java/org/bdware/server/irp/StatisticsByDay.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.bdware.server.irp; - -public class StatisticsByDay { - int resolveStatics; - int deleteStatics; - int registerStatics; - - public StatisticsByDay(){ - registerStatics = resolveStatics = deleteStatics = 0; - } -} diff --git a/src/main/java/org/bdware/server/irp/ViewHandler.java b/src/main/java/org/bdware/server/irp/ViewHandler.java deleted file mode 100644 index ef914ad..0000000 --- a/src/main/java/org/bdware/server/irp/ViewHandler.java +++ /dev/null @@ -1,257 +0,0 @@ -package org.bdware.server.irp; - -import com.google.gson.reflect.TypeToken; -import com.sun.net.httpserver.HttpExchange; -import com.sun.net.httpserver.HttpHandler; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.bdware.sc.util.JsonUtil; -import org.bdware.server.NodeCenterServer; -import org.rocksdb.ReadOptions; -import org.rocksdb.RocksDB; -import org.rocksdb.RocksDBException; -import org.rocksdb.RocksIterator; - -import java.io.IOException; -import java.io.OutputStream; -import java.io.UnsupportedEncodingException; -import java.net.URLDecoder; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.*; -import java.util.concurrent.TimeUnit; - -public class ViewHandler implements HttpHandler { - public static final String STATISTICS_KEY = "STATISTICS"; - public static final String COUNT_KEY = "COUNT"; - private static final Logger LOGGER = LogManager.getLogger(ViewHandler.class); - public static HashMap lhsStatics = new HashMap<>(); - public static HandleRecordCount handleRecordCount; - RocksDB db; - - public ViewHandler(RocksDB rocksDB) throws RocksDBException { - db = rocksDB; - loadFromDB(); - NodeCenterServer.scheduledThreadPool.scheduleWithFixedDelay( - () -> { - try { - saveToDB(); - } catch (RocksDBException e) { - LOGGER.error("saving to DB failed! " + e.getMessage()); - } - }, - 0, - 1, - TimeUnit.SECONDS); - } - - public static String getToday() { - SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd"); - Date date = new Date(System.currentTimeMillis()); - return formatter.format(date); - } - - public static void addResolve() { - String todayDate = ViewHandler.getToday(); - if (lhsStatics.get(todayDate) == null) { - StatisticsByDay statisticsByDay = new StatisticsByDay(); - statisticsByDay.resolveStatics++; - lhsStatics.put(todayDate, statisticsByDay); - } else { - lhsStatics.get(todayDate).resolveStatics++; - } - } - - public static void addRegister() { - String todayDate = ViewHandler.getToday(); - if (lhsStatics.get(todayDate) == null) { - StatisticsByDay statisticsByDay = new StatisticsByDay(); - statisticsByDay.registerStatics++; - lhsStatics.put(todayDate, statisticsByDay); - } else { - lhsStatics.get(todayDate).registerStatics++; - } - } - - public static void addDelete() { - String todayDate = ViewHandler.getToday(); - if (lhsStatics.get(todayDate) == null) { - StatisticsByDay statisticsByDay = new StatisticsByDay(); - statisticsByDay.deleteStatics++; - lhsStatics.put(todayDate, statisticsByDay); - } else { - lhsStatics.get(todayDate).deleteStatics++; - } - } - - @Override - public void handle(HttpExchange httpExchange) throws IOException { - Map respMap; - try { - //获得表单提交数据(post) - String postString = httpExchange.getRequestURI().getQuery(); - Map params = formData2Dic(postString); - respMap = HandleRequest(params); - httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*"); - httpExchange.sendResponseHeaders(200, 0); - OutputStream os = httpExchange.getResponseBody(); - os.write(JsonUtil.toJson(respMap).getBytes()); - os.close(); - } catch (Exception e) { - e.printStackTrace(); - } - } - - private Map HandleRequest(Map postInfo) { - Map respMap = new HashMap<>(); - String action = postInfo.get("action"); - switch (action) { - case "count": - respMap = getLHSCount(); - break; - case "statistics": - respMap = getLHSStatics(postInfo); - break; - default: - System.out.println("error response"); - } - return respMap; - } - - private void loadFromDB() throws RocksDBException { - byte[] statisticBytes = db.get(STATISTICS_KEY.getBytes()); - if (statisticBytes == null) { - lhsStatics = new HashMap<>(); - } else { - lhsStatics = JsonUtil.fromJson(new String(statisticBytes), new TypeToken>() { - }.getType()); - } - byte[] countBytes = db.get(COUNT_KEY.getBytes()); - if (countBytes == null) { - handleRecordCount = new HandleRecordCount(); - handleRecordCount.doCount = handleRecordCount.repoCount = handleRecordCount.userCount = 0; - } else { - handleRecordCount = JsonUtil.fromJson(new String(countBytes), HandleRecordCount.class); - } - } - - private void saveToDB() throws RocksDBException { - String statisticStr = JsonUtil.toJson(lhsStatics); - db.put(STATISTICS_KEY.getBytes(), statisticStr.getBytes()); - String countStr = JsonUtil.toJson(handleRecordCount); - db.put(COUNT_KEY.getBytes(), countStr.getBytes()); - } - - public Map getLHSStatics(Map postInfo) { - if (postInfo.containsKey("day")) { - return getLHSStaticsWithTime(postInfo); - } - HashMap respMap = new HashMap<>(); - for (String key : lhsStatics.keySet()) { - respMap.put(key + "", JsonUtil.toJson(lhsStatics.get(key))); - } - return respMap; - } - - public Map getLHSStaticsWithTime(Map postInfo) { - long day = Long.parseLong(postInfo.get("day")); - LOGGER.info(day); - List resolve = new ArrayList<>(); - List delete = new ArrayList<>(); - List register = new ArrayList<>(); - - for (int i = 0; i < day; i++) { - resolve.add(0); - delete.add(0); - register.add(0); - } - - long interval = 24 * 60 * 60 * 1000; //一天 - long cur = System.currentTimeMillis(); - long startTime = cur - day * interval; - // logger.info(JsonUtil.toJson(lhsStatics)); - try { - for (String k : lhsStatics.keySet()) { - long time = new SimpleDateFormat("yyyyMMdd").parse(k).getTime(); - if (time < startTime || time > cur) { - continue; - } - int index = (int) ((time - startTime) / interval); - //logger.info(index); - resolve.set(index, resolve.get(index) + lhsStatics.get(k).resolveStatics); - delete.set(index, delete.get(index) + lhsStatics.get(k).deleteStatics); - register.set(index, register.get(index) + lhsStatics.get(k).registerStatics); - } - } catch (ParseException e) { - e.printStackTrace(); - } - - HashMap respMap = new HashMap<>(); - respMap.put("resolve", resolve); - respMap.put("delete", delete); - respMap.put("register", register); - - return respMap; - } - - public Map getLHSCount() { - HashMap respMap = new HashMap<>(); - respMap.put("repoCount", String.valueOf(handleRecordCount.repoCount)); - respMap.put("doCount", String.valueOf(handleRecordCount.doCount)); - - ReadOptions readOptions = new ReadOptions().setPrefixSameAsStart(true); - RocksIterator it1 = db.newIterator(readOptions); - - ArrayList doIDList = new ArrayList<>(); - it1.seek("86.5000.470/do.".getBytes()); - int total = 0; - while (it1.isValid() && total < 10) { - doIDList.add(new String(it1.key())); - total++; - it1.next(); - } - it1.close(); - - RocksIterator it2 = db.newIterator(readOptions); - ArrayList repoIDList = new ArrayList<>(); - it2.seek("86.5000.470/doip.".getBytes()); - total = 0; - while (it2.isValid() && total < 10) { - repoIDList.add(new String(it2.key())); - total++; - it2.next(); - } - - respMap.put("repoIDList", repoIDList); - respMap.put("doIDList", doIDList); - return respMap; - - } - - public Map formData2Dic(String formData) { - Map result = new HashMap<>(); - if (formData == null || formData.trim().length() == 0) { - return result; - } - final String[] items = formData.split("&"); - Arrays.stream(items).forEach(item -> { - final String[] keyAndVal = item.split("="); - if (keyAndVal.length == 2) { - try { - final String key = URLDecoder.decode(keyAndVal[0], "utf8"); - final String val = URLDecoder.decode(keyAndVal[1], "utf8"); - result.put(key, val); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } - } - }); - return result; - } - - static class HandleRecordCount { - int repoCount; - int doCount; - int userCount; - } -}