A gRPC InternalServer which accepts connections via a host AndroidService. Multiple incoming connections transports may be active at a time. IMPORTANT : This implementation must comply with this published wire format. https://github.com/grpc/proposal/blob/master/L73-java-binderchannel/
| 60 | * https://github.com/grpc/proposal/blob/master/L73-java-binderchannel/wireformat.md |
| 61 | */ |
| 62 | @ThreadSafe |
| 63 | public final class BinderServer implements InternalServer, LeakSafeOneWayBinder.TransactionHandler { |
| 64 | private static final Logger logger = Logger.getLogger(BinderServer.class.getName()); |
| 65 | |
| 66 | private final ObjectPool<ScheduledExecutorService> executorServicePool; |
| 67 | private final ObjectPool<? extends Executor> executorPool; |
| 68 | private final ImmutableList<ServerStreamTracer.Factory> streamTracerFactories; |
| 69 | private final AndroidComponentAddress listenAddress; |
| 70 | private final LeakSafeOneWayBinder hostServiceBinder; |
| 71 | private final BinderTransportSecurity.ServerPolicyChecker serverPolicyChecker; |
| 72 | private final InboundParcelablePolicy inboundParcelablePolicy; |
| 73 | private final OneWayBinderProxy.Decorator clientBinderDecorator; |
| 74 | |
| 75 | @GuardedBy("this") |
| 76 | private ServerListener listener; |
| 77 | |
| 78 | @GuardedBy("this") |
| 79 | private ScheduledExecutorService executorService; |
| 80 | |
| 81 | @Nullable // Before start() and after termination. |
| 82 | @GuardedBy("this") |
| 83 | private Executor executor; |
| 84 | |
| 85 | @GuardedBy("this") |
| 86 | private boolean shutdown; |
| 87 | |
| 88 | private BinderServer(Builder builder) { |
| 89 | this.listenAddress = checkNotNull(builder.listenAddress); |
| 90 | this.executorPool = checkNotNull(builder.executorPool); |
| 91 | this.executorServicePool = builder.executorServicePool; |
| 92 | this.streamTracerFactories = |
| 93 | ImmutableList.copyOf(checkNotNull(builder.streamTracerFactories, "streamTracerFactories")); |
| 94 | this.serverPolicyChecker = BinderInternal.createPolicyChecker(builder.serverSecurityPolicy); |
| 95 | this.inboundParcelablePolicy = builder.inboundParcelablePolicy; |
| 96 | this.clientBinderDecorator = builder.clientBinderDecorator; |
| 97 | hostServiceBinder = new LeakSafeOneWayBinder(this); |
| 98 | } |
| 99 | |
| 100 | /** Return the binder we're listening on. */ |
| 101 | public IBinder getHostBinder() { |
| 102 | return hostServiceBinder; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public synchronized void start(ServerListener serverListener) throws IOException { |
| 107 | listener = new ActiveTransportTracker(serverListener, this::onTerminated); |
| 108 | executorService = executorServicePool.getObject(); |
| 109 | executor = executorPool.getObject(); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public SocketAddress getListenSocketAddress() { |
| 114 | return listenAddress; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public List<? extends SocketAddress> getListenSocketAddresses() { |
| 119 | return ImmutableList.of(listenAddress); |