private Listener getListener()

in grpc-gcp/src/main/java/com/google/cloud/grpc/GcpClientCall.java [180:215]


  private Listener<RespT> getListener(final Listener<RespT> responseListener) {

    return new ForwardingClientCallListener.SimpleForwardingClientCallListener<RespT>(
        responseListener) {
      // Decrement the stream number by one when the call is closed.
      @Override
      public void onClose(Status status, Metadata trailers) {
        if (!decremented.getAndSet(true)) {
          delegateChannelRef.activeStreamsCountDecr(startNanos, status, false);
        }
        // If the operation completed successfully, bind/unbind the affinity key.
        if (keys != null && status.getCode() == Status.Code.OK) {
          if (affinity.getCommand() == AffinityConfig.Command.UNBIND) {
            delegateChannel.unbind(keys);
          } else if (affinity.getCommand() == AffinityConfig.Command.BIND) {
            delegateChannel.bind(delegateChannelRef, keys);
          }
        }
        responseListener.onClose(status, trailers);
      }

      // If the command is "BIND", fetch the affinitykey from the response message and bind it
      // with the channelRef.
      @Override
      public void onMessage(RespT message) {
        delegateChannelRef.messageReceived();
        if (!received) {
          received = true;
          if (keys == null) {
            keys = delegateChannel.checkKeys(message, false, methodDescriptor);
          }
        }
        responseListener.onMessage(message);
      }
    };
  }