Skip to content

Commit

Permalink
Merge pull request #740 from vnorigoog/latest
Browse files Browse the repository at this point in the history
fixed a lot of nits and reorg'ed the code
  • Loading branch information
vnorigoog authored Feb 26, 2020
2 parents 086e1cf + edaccf6 commit 9ce4ea5
Show file tree
Hide file tree
Showing 15 changed files with 218 additions and 134 deletions.
128 changes: 73 additions & 55 deletions src/connector/grpcweb.iml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;

public class DebugInfo {
class DebugInfo {
private static final Logger LOGGER = Logger.getLogger(DebugInfo.class.getName());

public void printRequest(HttpServletRequest req) {
Expand Down
20 changes: 13 additions & 7 deletions src/connector/src/main/java/com/google/grpcweb/Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,29 @@
* Object Creation Factory that maintains Singletons.
* This could be replaced with Injection.
*
* This is primarily to make unittesting easier for the callers for this factory
* This is primarily to make unittesting easier for callers of objects supplied by this factory.
*/
public class Factory {
class Factory {
private final DebugInfo mDebugInfo;
private final Reflect mReflect;
private final Flags mFlags;
private final SendResponse mSendResponse;
private final GrpcServiceConnectionManager mGrpcServiceConnectionManager;

public Factory(Flags flags) {
public Factory(int grpcPortNum) {
mDebugInfo = new DebugInfo();
mReflect = new Reflect();
mFlags = flags;
mSendResponse = new SendResponse();
mGrpcServiceConnectionManager = new GrpcServiceConnectionManager(grpcPortNum);
}

DebugInfo getDebugInfo() { return mDebugInfo;}
Reflect getReflect() { return mReflect;}
GrpcWebHandler getGrpcWebHandler() { return new GrpcWebHandler();}
Flags getFlags() {return mFlags;}
MessageHandler getMessageHandler() { return new MessageHandler();}
SendResponse getSendResponse() {return mSendResponse;}
GrpcServiceConnectionManager getGrpcServiceConnectionManager() {
return mGrpcServiceConnectionManager;
}
RpcMetadataHandler getRpcMetadataHandler() {
return new RpcMetadataHandler(this);
}
}
13 changes: 0 additions & 13 deletions src/connector/src/main/java/com/google/grpcweb/Flags.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.google.grpcweb;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

/**
* Manages the connection pool to talk to the grpc-service
*/
class GrpcServiceConnectionManager {
private final int mGrpcPortNum;

GrpcServiceConnectionManager(int grpcPortNum) {
mGrpcPortNum = grpcPortNum;
}

ManagedChannel getChannel() {
// TODO: Manage a connection pool.
return ManagedChannelBuilder.forAddress("localhost", mGrpcPortNum)
.usePlaintext()
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.google.grpcweb;

import com.google.common.annotations.VisibleForTesting;

/**
* The main class that handles all the grpc-web traffic.
*/
public class GrpcWebTrafficHandler {
private final RequestHandler mRequestHandler;
private final Factory mFactory;

public GrpcWebTrafficHandler(int grpcPortNum) {
mFactory = new Factory(grpcPortNum);
mRequestHandler = new RequestHandler(mFactory);
}

@VisibleForTesting
GrpcWebTrafficHandler(Factory factory) {
mFactory = factory;
mRequestHandler = new RequestHandler(factory);
}

public RequestHandler getRequestHandler() { return mRequestHandler;}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.IOUtils;

public class GrpcWebHandler {
private static final Logger LOGGER = Logger.getLogger(GrpcWebHandler.class.getName());
class MessageHandler {
private static final Logger LOGGER = Logger.getLogger(MessageHandler.class.getName());

enum ContentType {
GRPC_WEB_PROTO,
Expand Down
4 changes: 2 additions & 2 deletions src/connector/src/main/java/com/google/grpcweb/Reflect.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;

public class Reflect {
class Reflect {
private static final Logger LOGGER = Logger.getLogger(Reflect.class.getName());

public Reflect() {}

/**
* get class and method.
*/
public Pair<Class, String> getClassAndMethod(HttpServletRequest req)
Pair<Class, String> getClassAndMethod(HttpServletRequest req)
throws IllegalArgumentException {
String pathInfo = req.getPathInfo();
if (!pathInfo.startsWith("/$rpc/")) {
Expand Down
16 changes: 6 additions & 10 deletions src/connector/src/main/java/com/google/grpcweb/RequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@
import java.lang.reflect.Method;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;

public class RequestHandler extends AbstractHandler {
class RequestHandler extends AbstractHandler {
private static final Logger LOGGER = Logger.getLogger(RequestHandler.class.getName());

private final Factory mFactory;

public RequestHandler(Factory factory) {
RequestHandler(Factory factory) {
mFactory = factory;
}

Expand All @@ -30,22 +28,20 @@ public void handle(

try {
mFactory.getDebugInfo().printRequest(req);
Flags mFlags = mFactory.getFlags();

// From the request, get the rpc-method name and class name and then get their corresponding
// concrete objects.
Pair<Class, String> classAndMethod = mFactory.getReflect().getClassAndMethod(req);

// get the BlockingStub for the rpc call
Object rpcStub = RpcMetadataHandler.getRpcStub(classAndMethod.getLeft(),
mFlags.getGrpcPortNum());
Object rpcStub = mFactory.getRpcMetadataHandler().getRpcStub(classAndMethod.getLeft());

// get the rpc-method to be called from the BlockingStub Object..
Method rpcMethod = RpcMetadataHandler.getRpcMethod(rpcStub, classAndMethod.getRight());

GrpcWebHandler grpcWebHandler = mFactory.getGrpcWebHandler();
GrpcWebHandler.ContentType contentType = grpcWebHandler.validateContentType(req);
Object outObj = grpcWebHandler.invokeRpcAndGetResult(req, contentType,
MessageHandler messageHandler = mFactory.getMessageHandler();
MessageHandler.ContentType contentType = messageHandler.validateContentType(req);
Object outObj = messageHandler.invokeRpcAndGetResult(req, contentType,
rpcStub, rpcMethod);

// get bytes from outObj protobuf
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.google.grpcweb;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.lang.reflect.Method;

public class RpcMetadataHandler {
public static Object getRpcStub(Class cls, int grpcPortNum) {
class RpcMetadataHandler {
private final Factory mFactory;

RpcMetadataHandler(Factory factory) {
mFactory = factory;
}

Object getRpcStub(Class cls) {
Method newBlockingStubMethod;
try {
newBlockingStubMethod = cls.getDeclaredMethod("newBlockingStub",
Expand All @@ -15,10 +20,7 @@ public static Object getRpcStub(Class cls, int grpcPortNum) {
}

// invoke the newBlockingStub() method
final ManagedChannel channel =
ManagedChannelBuilder.forAddress("localhost", grpcPortNum)
.usePlaintext()
.build();
ManagedChannel channel = mFactory.getGrpcServiceConnectionManager().getChannel();
Object stub;
try {
stub = newBlockingStubMethod.invoke(null, channel);
Expand All @@ -35,7 +37,7 @@ public static Object getRpcStub(Class cls, int grpcPortNum) {
/**
* Find method with one param!
*/
public static Method getRpcMethod(Object stub, String rpcMethodName) {
static Method getRpcMethod(Object stub, String rpcMethodName) {
Method rpcMethodObj = null;
for (Method m : stub.getClass().getMethods()) {
if (m.getName().equals(rpcMethodName) && (m.getParameterCount() == 1)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

public class SendResponse {
class SendResponse {
/**
* Write response out.
*/
public void writeResponse(HttpServletResponse response, byte[] out) throws IOException {
void writeResponse(HttpServletResponse response, byte[] out) throws IOException {
ServletOutputStream oStream = response.getOutputStream();
oStream.write((byte) 0x00);
oStream.write(lengthToBytes(out.length));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package com.google.grpcweb.example;

import com.google.grpcweb.example.EchoRequest;
import com.google.grpcweb.example.EchoResponse;
import com.google.grpcweb.example.EchoServiceGrpc;
import com.google.grpcweb.RequestHandler;
import com.google.grpcweb.Factory;
import com.google.grpcweb.Flags;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
import java.util.logging.Logger;

/**
* Implements the service methods defined in echo_service_example.proto
*/
public class EchoService extends EchoServiceGrpc.EchoServiceImplBase {
private static final Logger LOGGER = Logger.getLogger(EchoService.class.getName());

Expand All @@ -23,30 +18,4 @@ public void echoBack(EchoRequest request, StreamObserver<EchoResponse> responseO
responseObserver.onNext(response);
responseObserver.onCompleted();
}

private static Server startGrpcService(int port) throws Exception {
Server grpcServer = ServerBuilder.forPort(port)
.addService(new EchoService())
.build();
grpcServer.start();
System.out.println("**** started gRPC Service on port# " + port);
return grpcServer;
}

private static void listenForGrpcWebReq(int port, Factory factory) throws Exception {
org.eclipse.jetty.server.Server jServer = new org.eclipse.jetty.server.Server(port);
jServer.setHandler(new RequestHandler(factory));
jServer.start();
System.out.println("**** started gRPC-web Service on port# " + port);
}

public static void main(String[] args) throws Exception {
// PUNT accept these as flags
int grpcPort = 9090;
int grpcWebPort = 8081;

Server grpcServer = startGrpcService(grpcPort);
listenForGrpcWebReq(grpcWebPort, new Factory(new Flags(grpcPort, grpcWebPort)));
grpcServer.awaitTermination();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.google.grpcweb.example;

import com.google.grpcweb.GrpcWebTrafficHandler;
import java.util.logging.Logger;

/**
* A simple class w/ single method to start listening on the given grpc-web port#.
*/
class GrpcWebListener {
private static final Logger LOGGER = Logger.getLogger(GrpcWebListener.class.getName());
private final int mGrpcPort;
private final int mGrpcWebPort;

GrpcWebListener(int grpcPort, int grpcWebPort) {
mGrpcPort = grpcPort;
mGrpcWebPort = grpcWebPort;
}

void listenForGrpcWebReq() throws Exception {
// Initialize the Main Module for grpc-web
// It takes a single param: the port# the gRPC Service itself is listening on.
// It is used by the grpc-web code to talk to the service by connecting to it on that port.
GrpcWebTrafficHandler grpcWebTrafficHandler = new GrpcWebTrafficHandler(mGrpcPort);

// Start a jetty server to listen on the grpc-web port#
org.eclipse.jetty.server.Server jServer = new org.eclipse.jetty.server.Server(mGrpcWebPort);
jServer.setHandler(grpcWebTrafficHandler.getRequestHandler());
jServer.start();

LOGGER.info("**** started gRPC-web Service on port# " + mGrpcWebPort);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.google.grpcweb.example;

import io.grpc.Server;
import io.grpc.ServerBuilder;
import java.util.logging.Logger;

public class TestEchoService {
private static final Logger LOGGER = Logger.getLogger(TestEchoService.class.getName());

private static Server startGrpcService(int port) throws Exception {
Server grpcServer = ServerBuilder.forPort(port)
.addService(new EchoService())
.build();
grpcServer.start();
LOGGER.info("**** started gRPC Service on port# " + port);
return grpcServer;
}

public static void main(String[] args) throws Exception {
// PUNT accept these as flags in pom.xml
int grpcPort = 9090;
int grpcWebPort = 8081;

Server grpcServer = startGrpcService(grpcPort);
new GrpcWebListener(grpcPort, grpcWebPort).listenForGrpcWebReq();
grpcServer.awaitTermination();
}
}

0 comments on commit 9ce4ea5

Please sign in to comment.