From e7f0e37d082838471779997398ce0efff9e40b5e Mon Sep 17 00:00:00 2001 From: insanity Date: Thu, 13 Apr 2017 10:45:07 +0900 Subject: [PATCH] 1.0.0 --- grpc.iml | 61 ++++++ pom.xml | 125 ++++++++++++ src/main/java/com/loafle/overflow/Server.java | 179 ++++++++++++++++++ src/main/proto/remote.proto | 65 +++++++ src/main/resources/_ | 0 .../java/com/loafle/overflow/TestClient.java | 76 ++++++++ src/test/resources/logback.xml | 17 ++ 7 files changed, 523 insertions(+) create mode 100644 grpc.iml create mode 100644 pom.xml create mode 100644 src/main/java/com/loafle/overflow/Server.java create mode 100644 src/main/proto/remote.proto create mode 100644 src/main/resources/_ create mode 100644 src/test/java/com/loafle/overflow/TestClient.java create mode 100644 src/test/resources/logback.xml diff --git a/grpc.iml b/grpc.iml new file mode 100644 index 0000000..f39fcfc --- /dev/null +++ b/grpc.iml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..56dcb32 --- /dev/null +++ b/pom.xml @@ -0,0 +1,125 @@ + + + 4.0.0 + + + com.loafle + maven_parent_jar + 1.0.0-RELEASE + + + com.loafle.overflow + grpc + jar + 1.0.0-SNAPSHOT + com.loafle.overflow.grpc + + + 1.2.0 + + + + + io.grpc + grpc-netty + ${grpc.version} + + + io.grpc + grpc-protobuf + ${grpc.version} + + + io.grpc + grpc-stub + ${grpc.version} + + + org.mockito + mockito-core + 1.9.5 + test + + + + + + + + + + + com.loafle.overflow + crawler_sql + 1.0.0-SNAPSHOT + + + + com.loafle.overflow + crawler_snmp + 1.0.0-SNAPSHOT + + + + com.loafle.overflow + crawler_redis + 1.0.0-SNAPSHOT + + + + com.loafle.overflow + crawler_mongo + 1.0.0-SNAPSHOT + + + + com.loafle.overflow + crawler_wmi + 1.0.0-SNAPSHOT + + + + + + + kr.motd.maven + os-maven-plugin + 1.4.1.Final + + + + + org.xolstice.maven.plugins + protobuf-maven-plugin + 0.5.0 + + com.google.protobuf:protoc:3.2.0:exe:${os.detected.classifier} + grpc-java + io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier} + + + + + compile + compile-custom + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + 2.6 + + + jar-with-dependencies + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/loafle/overflow/Server.java b/src/main/java/com/loafle/overflow/Server.java new file mode 100644 index 0000000..3e540aa --- /dev/null +++ b/src/main/java/com/loafle/overflow/Server.java @@ -0,0 +1,179 @@ +package com.loafle.overflow; + +import com.google.protobuf.ByteString; +import com.loafle.overflow.crawler.mongo.MongoCrawler; +import com.loafle.overflow.crawler.redis.RedisCralwer; +import com.loafle.overflow.crawler.snmp.SNMPCrawler; +import com.loafle.overflow.crawler.sql.SQLCrawler; +import com.loafle.overflow.crawler.wmi.WMICrawler; +import com.loafle.overflow.rpc.*; +import io.grpc.ServerBuilder; +import io.grpc.stub.StreamObserver; +import com.loafle.overflow.crawler.Crawler; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectOutput; +import java.io.ObjectOutputStream; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Logger; + +/** + * Created by insanity on 17. 4. 12. + */ +public class Server { + + private static final Logger logger = Logger.getLogger(Server.class.getName()); + private io.grpc.Server server; + + private static Map crawlerMap = null; + + public Server() { + crawlerMap = new ConcurrentHashMap<>(); + + addDelegate(Crawlers.SQL.name(), new SQLCrawler()); + addDelegate(Crawlers.SNMP.name(), new SNMPCrawler()); + addDelegate(Crawlers.REDIS.name(), new RedisCralwer()); + addDelegate(Crawlers.MONGO.name(), new MongoCrawler()); + addDelegate(Crawlers.WMI.name(), new WMICrawler()); + } + + private int addDelegate(String name, Crawler crawler) { + if(this.crawlerMap == null) { + this.crawlerMap = new HashMap(); + } + + if(this.crawlerMap.containsKey(name)) { + return 1; + } + + this.crawlerMap.put(name, crawler); + + return 0; + } + + private void start() throws IOException { + /* The port on which the server should run */ + int port = 50052; + server = ServerBuilder.forPort(port) + .addService(new ConfigImpl()) + .addService(new DataImpl()) + .build() + .start(); + logger.info("Server started, listening on " + port); + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + // Use stderr here since the logger may have been reset by its JVM shutdown hook. + System.err.println("*** shutting down gRPC server since JVM is shutting down"); + Server.this.stop(); + System.err.println("*** server shut down"); + } + }); + } + + private void stop() { + if (server != null) { + server.shutdown(); + } + } + + /** + * Await termination on the main thread since the grpc library uses daemon threads. + */ + private void blockUntilShutdown() throws InterruptedException { + if (server != null) { + server.awaitTermination(); + } + } + + static class DataImpl extends DataGrpc.DataImplBase { + @Override + public void get(Input req, StreamObserver responseObserver) { + Crawler crawler = crawlerMap.get(req.getName().name()); + if(crawler != null) { + try { + Object obj = crawler.get(req.getId()); + Output reply = Output.newBuilder() + .setData(null) + .setStartDate(333) + .setEndDate(333) + .build(); + responseObserver.onNext(reply); + responseObserver.onCompleted(); + }catch(Exception e) { + e.printStackTrace(); + } + } + } + } + + static class ConfigImpl extends ConfigGrpc.ConfigImplBase { + + @Override + public void init(InputArray reqs, StreamObserver responseObserver) { + Output reply = Output.newBuilder() + .setData(ByteString.copyFromUtf8("Init")) + .setStartDate(0) + .setEndDate(0) + .build(); + responseObserver.onNext(reply); + responseObserver.onCompleted(); + } + + @Override + public void add(Input req, StreamObserver responseObserver) { + Crawler crawler = crawlerMap.get(req.getName().name()); + if(crawler != null) { + try { + Object obj = crawler.add(req.getId()); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectOutput out = new ObjectOutputStream(bos); + out.writeObject(obj); + Output reply = Output.newBuilder() + //.setData(ByteString.copyFrom(bos.toByteArray())) /////////////////// + .setData(ByteString.copyFrom(bos.toByteArray())) /////////////////// + .setStartDate(0) + .setEndDate(0) + .build(); + responseObserver.onNext(reply); + responseObserver.onCompleted(); + }catch(Exception e) { + e.printStackTrace(); + } + } + } + + @Override + public void remove(Input req, StreamObserver responseObserver) { + Crawler crawler = crawlerMap.get(req.getName().name()); + if(crawler != null) { + try { + Object obj = crawler.remove(req.getId()); + Output reply = Output.newBuilder() + .setData(null) /////////////////// + .setStartDate(0) + .setEndDate(0) + .build(); + responseObserver.onNext(reply); + responseObserver.onCompleted(); + }catch(Exception e) { + e.printStackTrace(); + } + } + } + + } + + /** + * Main launches the server from the command line. + */ + public static void main(String[] args) throws IOException, InterruptedException { + final Server server = new Server(); + server.start(); + server.blockUntilShutdown(); + } +} + diff --git a/src/main/proto/remote.proto b/src/main/proto/remote.proto new file mode 100644 index 0000000..575f878 --- /dev/null +++ b/src/main/proto/remote.proto @@ -0,0 +1,65 @@ +syntax = "proto3"; + +option java_multiple_files = true; +option java_package = "com.loafle.overflow.rpc"; + +enum Crawlers { + HEALTH_ACTIVEDIRECTORY = 0; + HEALTH_DNS = 1; + HEALTH_FTP = 2; + HEALTH_FTPS = 3; + HEALTH_IMAP = 4; + HEALTH_LDAP = 5; + HEALTH_MONGODB = 6; + HEALTH_MSSQL = 7; + HEALTH_MYSQL = 8; + HEALTH_MARIADB = 9; + HEALTH_PGSQL = 10; + HEALTH_NETBIOS = 11; + HEALTH_ORACLE = 12; + HEALTH_POP3 = 13; + HEALTH_REDIS = 14; + HEALTH_RMI = 15; + HEALTH_SMB = 16; + HEALTH_SMTP = 17; + HEALTH_SNMPV2C = 18; + HEALTH_SNMPV3 = 19; + HEALTH_SSH = 20; + HEALTH_TELNET = 21; + HEALTH_WMI = 22; + HEALTH_CASSANDRA = 23; + HEALTH_HTTP = 24; + SQL = 25; + SNMP = 26; + WMI = 27; + JMX = 28; + REDIS = 29; + MONGO = 30; +} + +service Config { + rpc Add (Input) returns (Output) {} + rpc Remove (Input) returns (Output) {} + rpc Init (InputArray) returns (Output) {} +} + +service Data { + rpc Get (Input) returns (Output) {} +} + +message InputArray { + repeated Input in = 1; +} + +message Input { + Crawlers name = 1; + string id = 2; + string path = 3; +} + +// The response message containing the greetings +message Output { + int64 startDate = 1; + int64 endDate = 2; + bytes data = 3; +} \ No newline at end of file diff --git a/src/main/resources/_ b/src/main/resources/_ new file mode 100644 index 0000000..e69de29 diff --git a/src/test/java/com/loafle/overflow/TestClient.java b/src/test/java/com/loafle/overflow/TestClient.java new file mode 100644 index 0000000..2f2d594 --- /dev/null +++ b/src/test/java/com/loafle/overflow/TestClient.java @@ -0,0 +1,76 @@ +package com.loafle.overflow; + +import com.loafle.overflow.rpc.*; +import io.grpc.ManagedChannel; +import io.grpc.ManagedChannelBuilder; +import io.grpc.StatusRuntimeException; +import org.junit.Test; + +import java.util.concurrent.TimeUnit; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Created by insanity on 17. 4. 12. + */ +public class TestClient { + + + private static final Logger logger = Logger.getLogger(TestClient.class.getName()); + + private final ManagedChannel channel; + private final ConfigGrpc.ConfigBlockingStub configStub; + private final DataGrpc.DataBlockingStub dataStub; + + public TestClient() { + channel = ManagedChannelBuilder.forAddress("192.168.1.105", 50052).usePlaintext(true).build(); + configStub = ConfigGrpc.newBlockingStub(channel); + dataStub = DataGrpc.newBlockingStub(channel); + } + + + public void shutdown() throws InterruptedException { + channel.shutdown().awaitTermination(5, TimeUnit.SECONDS); + } + + /** Say hello to server. */ + public void add() { + Input request = Input.newBuilder().setId("test111").setName(Crawlers.SNMP).build(); + Output response; + try { + response = configStub.add(request); + } catch (StatusRuntimeException e) { + logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus()); + return; + } + logger.info("res : " + response.getData()); + } + + public void get() { + Input request = Input.newBuilder().setId("test111").setName(Crawlers.SNMP).build(); + Output response; + try { + response = dataStub.get(request); + } catch (StatusRuntimeException e) { + logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus()); + return; + } + logger.info("res : " + response.getStartDate()); + } + + @Test + public void testRPCServer() { + TestClient client = new TestClient(); + try { + client.get(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + client.shutdown(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } +} \ No newline at end of file diff --git a/src/test/resources/logback.xml b/src/test/resources/logback.xml new file mode 100644 index 0000000..3683ca2 --- /dev/null +++ b/src/test/resources/logback.xml @@ -0,0 +1,17 @@ + + + grpc + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{32} - %msg%n + + + + + + + + + \ No newline at end of file