From fca62deb016628527a7f43306b462114f1b69471 Mon Sep 17 00:00:00 2001 From: snoop Date: Mon, 10 Apr 2017 16:32:36 +0900 Subject: [PATCH] changed JAVA Json RPC --- src/main/java/com/loafle/rpc/api/Remote.java | 27 ++++++++- .../java/com/loafle/rpc/api/RemoteImpl.java | 58 ++++++++++++++++++- .../java/com/loafle/rpc/crawler/Crawler.java | 34 +++++++++++ 3 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/loafle/rpc/crawler/Crawler.java diff --git a/src/main/java/com/loafle/rpc/api/Remote.java b/src/main/java/com/loafle/rpc/api/Remote.java index ba3710c..8d779fb 100644 --- a/src/main/java/com/loafle/rpc/api/Remote.java +++ b/src/main/java/com/loafle/rpc/api/Remote.java @@ -1,14 +1,35 @@ package com.loafle.rpc.api; import com.fasterxml.jackson.core.JsonProcessingException; + import com.googlecode.jsonrpc4j.JsonRpcMethod; import com.googlecode.jsonrpc4j.JsonRpcService; -import com.loafle.rpc.model.Input; +import com.loafle.rpc.crawler.Crawler; + import com.loafle.rpc.model.Output; + + @JsonRpcService("/rpc") public interface Remote { - @JsonRpcMethod("Remote.Request") - Output request(Input t) throws JsonProcessingException; + + @JsonRpcMethod("RPC.init") + Output init(String[] paths) throws JsonProcessingException; + + + @JsonRpcMethod("RPC.add") + Output add(String type, String id) throws JsonProcessingException ; + +// @JsonRpcMethod("Remote.remove") + int addDelegate(String name , Crawler crawler); + + @JsonRpcMethod("RPC.get") + Output get(String type, String id) throws JsonProcessingException; + + + @JsonRpcMethod("RPC.remove") + Output remove(String type, String id) throws JsonProcessingException; + + } \ No newline at end of file diff --git a/src/main/java/com/loafle/rpc/api/RemoteImpl.java b/src/main/java/com/loafle/rpc/api/RemoteImpl.java index 8d4ef27..8dccaf1 100644 --- a/src/main/java/com/loafle/rpc/api/RemoteImpl.java +++ b/src/main/java/com/loafle/rpc/api/RemoteImpl.java @@ -2,23 +2,77 @@ package com.loafle.rpc.api; import com.fasterxml.jackson.core.JsonProcessingException; import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl; +import com.loafle.rpc.crawler.Crawler; import com.loafle.rpc.model.Input; import com.loafle.rpc.model.Output; import org.springframework.stereotype.Service; import java.util.Date; +import java.util.HashMap; +import java.util.Map; @Service @AutoJsonRpcServiceImpl public class RemoteImpl implements Remote { + private Map crawlerMap = null; + + @Override - public Output request(Input i) throws JsonProcessingException { + public Output init(String[] paths) throws JsonProcessingException { + + for (String v : paths) { + System.out.println(v); + } Output out = new Output(); out.setStartDate(new Date()); out.setEndDate(new Date()); - out.setData("Not Delegate".getBytes()); + out.setData("Method init".getBytes()); return out; } + @Override + public Output add(String type, String id) throws JsonProcessingException { + return null; + } + + @Override + public 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; + } + + @Override + public Output get(String type, String id) throws JsonProcessingException { + + Crawler crawler = this.crawlerMap.get(type); + if(crawler == null) { + System.out.println(type + "_crawler == null"); + } else { + return crawler.get(id); + } + + System.out.println("type : " + type); + System.out.println("id : " + id); + Output out = new Output(); + out.setStartDate(new Date()); + out.setEndDate(new Date()); + out.setData("Not Delegate".getBytes()); + return out; + } + + @Override + public Output remove(String type, String id) throws JsonProcessingException { + return null; + } } \ No newline at end of file diff --git a/src/main/java/com/loafle/rpc/crawler/Crawler.java b/src/main/java/com/loafle/rpc/crawler/Crawler.java new file mode 100644 index 0000000..dc36912 --- /dev/null +++ b/src/main/java/com/loafle/rpc/crawler/Crawler.java @@ -0,0 +1,34 @@ +package com.loafle.rpc.crawler; + +import com.fasterxml.jackson.core.JsonProcessingException; + +import com.loafle.rpc.model.Output; + +/** + * Created by root on 17. 4. 10. + */ +public abstract class Crawler { + + public Output add(String id) throws JsonProcessingException { + + return null; + } + + public abstract Output get(String id) throws JsonProcessingException ; + + public Output getConfig(String id) throws JsonProcessingException { + + return null; + } + + public Output init(String config) throws JsonProcessingException { + + return null; + } + + public Output remove(String id) throws JsonProcessingException { + + return null; + } + +}