All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jauntsdn.rsocket.RSocketsFactory Maven / Gradle / Ivy

There is a newer version: 0.9.8
Show newest version
package com.jauntsdn.rsocket;

import static com.jauntsdn.rsocket.StreamErrorMappers.*;

import com.jauntsdn.rsocket.frame.decoder.PayloadDecoder;
import com.jauntsdn.rsocket.keepalive.KeepAliveHandler;
import com.jauntsdn.rsocket.lease.RequesterLeaseHandler;
import com.jauntsdn.rsocket.lease.ResponderLeaseHandler;
import io.netty.buffer.ByteBufAllocator;
import java.util.function.Consumer;
import java.util.function.LongConsumer;
import javax.annotation.Nullable;
import org.reactivestreams.Subscriber;
import reactor.core.publisher.EmitterProcessor;
import reactor.core.scheduler.Scheduler;

interface RSocketsFactory {

  RSocketResponder createResponder(
      ByteBufAllocator allocator,
      DuplexConnection connection,
      RSocket requestHandler,
      PayloadDecoder payloadDecoder,
      Consumer errorConsumer,
      ErrorFrameMapper errorFrameMapper);

  RSocketRequester createRequester(
      ByteBufAllocator allocator,
      DuplexConnection connection,
      PayloadDecoder payloadDecoder,
      Consumer errorConsumer,
      ErrorFrameMapper errorFrameMapper,
      StreamIdSupplier streamIdSupplier,
      int keepAliveTickPeriod,
      int keepAliveAckTimeout,
      KeepAliveHandler keepAliveHandler);

  static RSocketsFactory createClient(
      boolean isLease, Scheduler scheduler, Leases.ClientConfigurer leaseConfigurer) {
    if (isLease) {
      EmitterProcessor rtt = EmitterProcessor.create();
      return new LeaseRSocketsFactory(rtt, leaseConfigurer.configure(rtt, scheduler));
    } else {
      return new DefaultRSocketsFactory();
    }
  }

  static RSocketsFactory createServer(
      boolean isLease, Scheduler scheduler, Leases.ServerConfigurer leaseConfigurer) {
    if (isLease) {
      return new LeaseRSocketsFactory(leaseConfigurer.configure(scheduler));
    } else {
      return new DefaultRSocketsFactory();
    }
  }

  final class LeaseRSocketsFactory implements RSocketsFactory {
    private final LongConsumer rttConsumer;
    private final Leases leases;

    public LeaseRSocketsFactory(@Nullable Subscriber rttConsumer, Leases leases) {
      this.rttConsumer = rttConsumer == null ? null : rttConsumer::onNext;
      this.leases = leases;
    }

    public LeaseRSocketsFactory(Leases leases) {
      this(null, leases);
    }

    @Override
    public RSocketResponder createResponder(
        ByteBufAllocator allocator,
        DuplexConnection connection,
        RSocket requestHandler,
        PayloadDecoder payloadDecoder,
        Consumer errorConsumer,
        ErrorFrameMapper errorFrameMapper) {

      ResponderLeaseHandler leaseHandler =
          new ResponderLeaseHandler.Impl<>(
              allocator, leases.sender(), errorConsumer, leases.stats());

      return new LeaseRSocketResponder(
          allocator,
          connection,
          requestHandler,
          payloadDecoder,
          errorConsumer,
          errorFrameMapper,
          leaseHandler);
    }

    @Override
    public RSocketRequester createRequester(
        ByteBufAllocator allocator,
        DuplexConnection connection,
        PayloadDecoder payloadDecoder,
        Consumer errorConsumer,
        ErrorFrameMapper errorFrameMapper,
        StreamIdSupplier streamIdSupplier,
        int keepAliveTickPeriod,
        int keepAliveAckTimeout,
        KeepAliveHandler keepAliveHandler) {
      RequesterLeaseHandler.Impl leaseHandler = new RequesterLeaseHandler.Impl();

      return new LeaseRSocketRequester(
          allocator,
          connection,
          payloadDecoder,
          errorConsumer,
          errorFrameMapper,
          streamIdSupplier,
          keepAliveTickPeriod,
          keepAliveAckTimeout,
          keepAliveHandler,
          leaseHandler,
          rttConsumer);
    }
  }

  final class DefaultRSocketsFactory implements RSocketsFactory {

    @Override
    public RSocketResponder createResponder(
        ByteBufAllocator allocator,
        DuplexConnection connection,
        RSocket requestHandler,
        PayloadDecoder payloadDecoder,
        Consumer errorConsumer,
        ErrorFrameMapper errorFrameMapper) {

      return new RSocketResponder(
          allocator, connection, requestHandler, payloadDecoder, errorConsumer, errorFrameMapper);
    }

    @Override
    public RSocketRequester createRequester(
        ByteBufAllocator allocator,
        DuplexConnection connection,
        PayloadDecoder payloadDecoder,
        Consumer errorConsumer,
        ErrorFrameMapper errorFrameMapper,
        StreamIdSupplier streamIdSupplier,
        int keepAliveTickPeriod,
        int keepAliveAckTimeout,
        KeepAliveHandler keepAliveHandler) {
      return new RSocketRequester(
          allocator,
          connection,
          payloadDecoder,
          errorConsumer,
          errorFrameMapper,
          streamIdSupplier,
          keepAliveTickPeriod,
          keepAliveAckTimeout,
          keepAliveHandler);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy