Skip to content

Instantly share code, notes, and snippets.

@nsivabalan
Created August 21, 2020 14:30
Show Gist options
  • Save nsivabalan/5e9fd5bc494cd091df701ff1ef664acb to your computer and use it in GitHub Desktop.
Save nsivabalan/5e9fd5bc494cd091df701ff1ef664acb to your computer and use it in GitHub Desktop.
/**
* Call router interceptor which routes calls to corresponding transport adaptors.
*
* @param <ReqT> request msg of generic type ReqT.
* @param <RespT> response msg of generic type RespT.
* @param <T> methodInfo of generic type T.
*/
public class UberCallRouterInterceptor<ReqT, RespT, T> extends UberInterceptor<ReqT, RespT, T> {
// Adaptors will the next in the chain of interceptors.
private UbergRPCAdaptor ubergRPCAdaptor;
@Nullable private UberClientCall clientCall;
public UberCallRouterInterceptor(String identifier, UbergRPCAdaptor ubergRPCAdaptor) {
super(identifier);
this.ubergRPCAdaptor = ubergRPCAdaptor;
}
@Override
public UberInterceptor<ReqT, RespT, T> newCall(
MethodInfo<T> methodInfo, UberInternalCallOptions uberInternalCallOptions) {
this.methodInfo = methodInfo;
this.uberInternalCallOptions = uberInternalCallOptions;
// for now only grpcAdaptor
this.clientCall = ubergRPCAdaptor.newCall(methodInfo, this.uberInternalCallOptions);
return this;
}
/**
* This will be first call made by the client to register a listener for response handling and to
* send out request headers.
*/
@Override
public void start(UberNetworkListener<RespT> responseListener, UberNetworkHeaders headers) {
this.prevListener = responseListener;
onRequestHeaders(headers);
if (clientCall != null && methodInfo != null) {
clientCall.start(
new InternalListener<ReqT, RespT, T>(
identifier, uberInternalCallOptions, methodInfo, responseListener, this),
headers);
} else {
throw new IllegalStateException("Client call should not be null");
}
}
@Override
public void request(int numMessages) {
if (clientCall != null) {
clientCall.request(numMessages);
} else {
throw new IllegalStateException("Client call should not be null");
}
}
@Override
public void cancel(@Nullable String message, @Nullable Throwable cause) {
if (clientCall != null) {
clientCall.cancel(message, cause);
} else {
throw new IllegalStateException("Client call should not be null");
}
}
@Override
public void halfClose() {
if (clientCall != null) {
clientCall.halfClose();
} else {
throw new IllegalStateException("Client call should not be null");
}
}
@Override
public void sendMessage(ReqT message) {
if (clientCall != null) {
onRequestMsg(message);
clientCall.sendMessage(message);
} else {
throw new IllegalStateException("Client call should not be null");
}
}
@Override
public boolean isReady() {
if (clientCall != null) {
return clientCall.isReady();
} else {
throw new IllegalStateException("Client call should not be null");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment