return tcpClient(DEFAULT_BIND_ADDRESS, port);
/** * Bind a new TCP client to the localhost on port 12012. By default the default client implementation is scanned * from the classpath on Class init. Support for Netty first and ZeroMQ then is provided as long as the relevant * library dependencies are on the classpath. * <p> * A {@link reactor.io.net.tcp.TcpClient} is a specific kind of {@link org.reactivestreams.Publisher} that will emit: * - onNext {@link reactor.io.net.ChannelStream} to consume data from * - onComplete when client is shutdown * - onError when any exception (more specifically IO exception) occurs * From the emitted {@link ReactorChannel}, one can decide to add in-channel consumers to read any incoming * data. * <p> * To reply data on the active connection, {@link ReactorChannel#writeWith} can subscribe to any passed {@link org.reactivestreams.Publisher}. * <p> * Note that {@link reactor.rx.Stream#getCapacity} will be used to switch on/off a channel in auto-read / flush on * write mode. * If the capacity is Long.MAX_Value, write on flush and auto read will apply. Otherwise, data will be flushed every * capacity batch size and read will pause when capacity number of elements have been dispatched. * <p> * Emitted channels will run on the same thread they have beem receiving IO events. * Apart from dispatching the write, it is possible to use {@link reactor.rx.Stream#dispatchOn} to process requests * asynchronously. * <p> * By default the type of emitted data or received data is {@link reactor.io.buffer.Buffer} * * @return a new Stream of ChannelStream, typically a peer of connections. */ public static TcpClient<Buffer, Buffer> tcpClient() { return tcpClient(DEFAULT_BIND_ADDRESS); }
return tcpClient(bindAddress, DEFAULT_PORT);
Function<? super Spec.TcpClientSpec<IN, OUT>, ? extends Spec.TcpClientSpec<IN, OUT>> configuringFunction ) { return tcpClient(DEFAULT_TCP_CLIENT_TYPE, configuringFunction);
return tcpClient(new Function<Spec.TcpClientSpec<Buffer, Buffer>, Spec.TcpClientSpec<Buffer, Buffer>>() { @Override public Spec.TcpClientSpec<Buffer, Buffer> apply(Spec.TcpClientSpec<Buffer, Buffer> clientSpec) {
public Promise<ChannelStream<T, T>> createClient(final String addrs, final int socketType) { Assert.isTrue(!shutdown, "This ZeroMQ instance has been shut down"); TcpClient<T, T> client = NetStreams.tcpClient(ZeroMQTcpClient.class, new NetStreams.TcpClientFactory<T, T>() { @Override public Spec.TcpClientSpec<T, T> apply(Spec.TcpClientSpec<T, T> spec) { return spec.env(env).dispatcher(dispatcher).codec(codec) .options(new ZeroMQClientSocketOptions() .context(zmqCtx) .connectAddresses(addrs) .socketType(socketType)); } }); final Promise<ChannelStream<T, T>> promise = Promises.ready(env, dispatcher); client.start(new ReactorChannelHandler<T, T, ChannelStream<T, T>>() { @Override public Publisher<Void> apply(ChannelStream<T, T> ttChannelStream) { promise.onNext(ttChannelStream); return Streams.never(); } }); synchronized (peers) { peers.add(client); } return promise; }
@Override public ListenableFuture<Void> connect(TcpConnectionHandler<P> connectionHandler, ReconnectStrategy strategy) { Assert.notNull(connectionHandler, "TcpConnectionHandler must not be null"); Assert.notNull(strategy, "ReconnectStrategy must not be null"); final TcpClient<Message<P>, Message<P>> tcpClient; Runnable cleanupTask; synchronized (this.tcpClients) { if (this.stopping) { IllegalStateException ex = new IllegalStateException("Shutting down."); connectionHandler.afterConnectFailure(ex); return new PassThroughPromiseToListenableFutureAdapter<Void>(Promises.<Void>error(ex)); } tcpClient = NetStreams.tcpClient(REACTOR_TCP_CLIENT_TYPE, this.tcpClientSpecFactory); this.tcpClients.add(tcpClient); cleanupTask = new Runnable() { @Override public void run() { synchronized (tcpClients) { tcpClients.remove(tcpClient); } } }; } Stream<Tuple2<InetSocketAddress, Integer>> stream = tcpClient.start( new MessageChannelStreamHandler<P>(connectionHandler, cleanupTask), new ReactorReconnectAdapter(strategy)); return new PassThroughPromiseToListenableFutureAdapter<Void>(stream.next().after()); }
return new PassThroughPromiseToListenableFutureAdapter<Void>(Promises.<Void>error(ex)); tcpClient = NetStreams.tcpClient(REACTOR_TCP_CLIENT_TYPE, this.tcpClientSpecFactory); this.tcpClients.add(tcpClient); cleanupTask = new Runnable() {