Search in sources :

Example 41 with Address

use of io.scalecube.transport.Address in project scalecube by scalecube.

the class NettyClientTransport method connect.

private Observable<ChannelContext> connect(Address address) {
    AsyncSubject<ChannelContext> subject = AsyncSubject.create();
    ChannelFuture channelFuture = bootstrap.connect(address.host(), address.port());
    channelFuture.addListener((ChannelFutureListener) future -> {
        Throwable error = future.cause();
        if (future.isSuccess()) {
            future.channel().pipeline().fireChannelActive();
            try {
                ChannelContext channelContext = ChannelSupport.getChannelContextOrThrow(future.channel());
                channelContext.listenClose(input -> outboundChannels.remove(address));
                subject.onNext(channelContext);
                subject.onCompleted();
            } catch (Exception throwable) {
                error = throwable;
            }
        }
        if (error != null) {
            outboundChannels.remove(address);
            subject.onError(future.cause());
        }
    });
    Scheduler scheduler = Schedulers.from(channelFuture.channel().eventLoop());
    return subject.subscribeOn(scheduler);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelContext(io.scalecube.streams.ChannelContext) Address(io.scalecube.transport.Address) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Scheduler(rx.Scheduler) ChannelFuture(io.netty.channel.ChannelFuture) Observable(rx.Observable) ConcurrentMap(java.util.concurrent.ConcurrentMap) Consumer(java.util.function.Consumer) Bootstrap(io.netty.bootstrap.Bootstrap) AsyncSubject(rx.subjects.AsyncSubject) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Schedulers(rx.schedulers.Schedulers) ChannelContext(io.scalecube.streams.ChannelContext) Scheduler(rx.Scheduler)

Example 42 with Address

use of io.scalecube.transport.Address in project scalecube by scalecube.

the class DefaultEventStreamTest method testChannelContextCreateIfAbsentConsumerNotCalled.

@Test
public void testChannelContextCreateIfAbsentConsumerNotCalled() {
    Address address = Address.from("localhost:8080");
    ChannelContext channelContext = ChannelContext.create(address);
    String id = channelContext.getId();
    AtomicBoolean consumerCalled = new AtomicBoolean();
    // create context on top of exsting context attributes => result from map
    ChannelContext channelContext1 = ChannelContext.createIfAbsent(id, address, input -> consumerCalled.set(true));
    assertFalse(consumerCalled.get());
    assertEquals(channelContext, channelContext1);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Address(io.scalecube.transport.Address) Test(org.junit.Test)

Example 43 with Address

use of io.scalecube.transport.Address in project scalecube by scalecube.

the class AddressTest method testParseHostPort.

@Test
public void testParseHostPort() throws Exception {
    Address address1 = Address.from("localhost:5810");
    assertEquals(5810, address1.port());
    assertEquals(Addressing.getLocalIpAddress().getHostAddress(), address1.host());
    Address address2 = Address.from("127.0.0.1:5810");
    assertEquals(5810, address1.port());
    assertEquals(Addressing.getLocalIpAddress().getHostAddress(), address2.host());
    assertEquals(address1, address2);
}
Also used : Address(io.scalecube.transport.Address) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Aggregations

Address (io.scalecube.transport.Address)43 Test (org.junit.Test)33 BaseTest (io.scalecube.testlib.BaseTest)22 Transport (io.scalecube.transport.Transport)20 ArrayList (java.util.ArrayList)13 List (java.util.List)12 InetAddress (java.net.InetAddress)10 CompletableFuture (java.util.concurrent.CompletableFuture)5 TimeUnit (java.util.concurrent.TimeUnit)5 Member (io.scalecube.cluster.Member)4 Collection (java.util.Collection)4 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)3 ALIVE (io.scalecube.cluster.membership.MemberStatus.ALIVE)3 Collectors (java.util.stream.Collectors)3 Lists (com.google.common.collect.Lists)2 Bootstrap (io.netty.bootstrap.Bootstrap)2 ClusterConfig (io.scalecube.cluster.ClusterConfig)2 DummyMembershipProtocol (io.scalecube.cluster.membership.DummyMembershipProtocol)2 MemberStatus (io.scalecube.cluster.membership.MemberStatus)2 SUSPECT (io.scalecube.cluster.membership.MemberStatus.SUSPECT)2