proxy server

This commit is contained in:
insanity 2017-06-28 18:19:26 +09:00
parent 280b88baaa
commit 4d6ff514b5

View File

@ -1,6 +1,9 @@
package com.loafle.overflow.proxy;
import com.loafle.overflow.api.OverflowApiServerGrpc;
import com.loafle.overflow.api.ServerInput;
import com.loafle.overflow.api.ServerOutput;
import com.loafle.overflow.api.ServerParam;
import com.loafle.overflow.module.domain.service.DomainService;
import com.loafle.overflow.module.email.service.EmailAuthService;
import com.loafle.overflow.module.infra.service.*;
@ -11,9 +14,13 @@ import com.loafle.overflow.module.probe.service.ProbeTaskService;
import com.loafle.overflow.module.sensor.service.SensorItemService;
import com.loafle.overflow.module.sensor.service.SensorService;
import com.loafle.overflow.module.target.service.TargetService;
import io.grpc.ServerBuilder;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;
@ -24,6 +31,8 @@ import java.util.logging.Logger;
public class ServiceProxy {
private static final Logger logger = Logger.getLogger(ServiceProxy.class.getName());
private io.grpc.Server server;
private static Map<String, Object> serviceMap = null;
public ServiceProxy() {
@ -49,7 +58,83 @@ public class ServiceProxy {
serviceMap.put("infraService", ctx.getBean(InfraServiceService.class));
}
public String process(ServerInput input, Object service) {
return null;
public void start(int port) throws IOException {
server = ServerBuilder.forPort(port)
.addService(new ServiceImpl())
.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");
ServiceProxy.this.stop();
System.err.println("*** server shut down");
}
});
}
public void stop() {
if (server != null) {
server.shutdown();
}
}
public void blockUntilShutdown() throws InterruptedException {
if (server != null) {
server.awaitTermination();
}
}
static class ServiceImpl extends OverflowApiServerGrpc.OverflowApiServerImplBase {
@Override
public void exec(ServerInput request,
io.grpc.stub.StreamObserver<ServerOutput> responseObserver) {
String targetServiceName = request.getTarget();
Object service = serviceMap.get(targetServiceName);
if(service != null) {
try {
String jsonResult = process(request, service);
ServerOutput reply = ServerOutput.newBuilder()
.setResult(jsonResult)
.build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}catch(Exception e) {
e.printStackTrace();
responseObserver.onError(e);
}
}else {
responseObserver.onError(new Exception("Not assigned Service :" + service));
}
}
private String process(ServerInput request, Object service) throws Exception {
String methodName = request.getMethod();
List<ServerParam> params = request.getParamsList();
ObjectMapper mapper = new ObjectMapper();
Object retObj = null;
//nigagara hawai
//nigagara hawai
//nigagara hawai
//nigagara hawai
//nigagara hawai
//nigagara hawai
//nigagara hawai
//nigagara hawai
//nigagara hawai
return mapper.writeValueAsString(retObj);
}
}
}