This commit is contained in:
insanity 2017-04-13 10:45:07 +09:00
commit e7f0e37d08
7 changed files with 523 additions and 0 deletions

61
grpc.iml Normal file
View File

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/target/generated-sources/protobuf/grpc-java" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/target/generated-sources/protobuf/java" isTestSource="false" generated="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: io.grpc:grpc-netty:1.2.0" level="project" />
<orderEntry type="library" name="Maven: io.grpc:grpc-core:1.2.0" level="project" />
<orderEntry type="library" name="Maven: com.google.errorprone:error_prone_annotations:2.0.11" level="project" />
<orderEntry type="library" name="Maven: com.google.code.findbugs:jsr305:3.0.0" level="project" />
<orderEntry type="library" name="Maven: io.grpc:grpc-context:1.2.0" level="project" />
<orderEntry type="library" name="Maven: com.google.instrumentation:instrumentation-api:0.3.0" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-codec-http2:4.1.8.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-codec-http:4.1.8.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-codec:4.1.8.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-handler:4.1.8.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-buffer:4.1.8.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-common:4.1.8.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-handler-proxy:4.1.8.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-transport:4.1.8.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-resolver:4.1.8.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-codec-socks:4.1.8.Final" level="project" />
<orderEntry type="library" name="Maven: io.grpc:grpc-protobuf:1.2.0" level="project" />
<orderEntry type="library" name="Maven: com.google.protobuf:protobuf-java:3.2.0" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:19.0" level="project" />
<orderEntry type="library" name="Maven: com.google.protobuf:protobuf-java-util:3.2.0" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.7" level="project" />
<orderEntry type="library" name="Maven: io.grpc:grpc-protobuf-lite:1.2.0" level="project" />
<orderEntry type="library" name="Maven: io.grpc:grpc-stub:1.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:1.9.5" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:1.0" level="project" />
<orderEntry type="library" name="Maven: com.loafle.overflow:crawler_sql:1.0.0-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: com.loafle.overflow:crawler_snmp:1.0.0-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: org.snmp4j:snmp4j:2.5.0" level="project" />
<orderEntry type="library" name="Maven: log4j:log4j:1.2.14" level="project" />
<orderEntry type="library" name="Maven: com.loafle.overflow:crawler:1.0.0-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: com.loafle.overflow:crawler_redis:1.0.0-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: redis.clients:jedis:2.9.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-pool2:2.4.2" level="project" />
<orderEntry type="library" name="Maven: com.loafle.overflow:crawler_mongo:1.0.0-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: org.mongodb:mongo-java-driver:3.4.2" level="project" />
<orderEntry type="library" name="Maven: com.loafle.overflow:crawler_wmi:1.0.0-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: com.loafle.overflow:crawler_java:1.0.0-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: com.hynnet:jacob:1.18" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: ch.qos.logback:logback-classic:1.2.3" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: ch.qos.logback:logback-core:1.2.3" level="project" />
</component>
</module>

125
pom.xml Normal file
View File

@ -0,0 +1,125 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.loafle</groupId>
<artifactId>maven_parent_jar</artifactId>
<version>1.0.0-RELEASE</version>
</parent>
<groupId>com.loafle.overflow</groupId>
<artifactId>grpc</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>com.loafle.overflow.grpc</name>
<properties>
<grpc.version>1.2.0</grpc.version>
</properties>
<dependencies>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<!--crawlers-->
<!--<dependency>-->
<!--<groupId>com.loafle.overflow</groupId>-->
<!--<artifactId>crawler_jmx</artifactId>-->
<!--<version>1.0.0-SNAPSHOT</version>-->
<!--</dependency>-->
<dependency>
<groupId>com.loafle.overflow</groupId>
<artifactId>crawler_sql</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.loafle.overflow</groupId>
<artifactId>crawler_snmp</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.loafle.overflow</groupId>
<artifactId>crawler_redis</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.loafle.overflow</groupId>
<artifactId>crawler_mongo</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.loafle.overflow</groupId>
<artifactId>crawler_wmi</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.4.1.Final</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.2.0:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -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<String, Crawler> 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<String, Crawler>();
}
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<Output> 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<Output> 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<Output> 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<Output> 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();
}
}

View File

@ -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;
}

0
src/main/resources/_ Normal file
View File

View File

@ -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();
}
}
}
}

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="3 seconds">
<contextName>grpc</contextName>
<!-- TRACE > DEBUG > INFO > WARN > ERROR -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{32} - %msg%n
</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
<logger name="com.loafle.overflow" level="ALL" />
</configuration>