Search in sources :

Example 1 with ConnectableActor

use of org.nustaq.kontraktor.remoting.base.ConnectableActor in project kontraktor by RuedigerMoeller.

the class KxReactiveStreams method connect.

/**
 * @param eventType
 * @param connectable
 * @param disconHandler - can be null
 * @param <T>
 * @return
 */
public <T> IPromise<KxPublisher<T>> connect(Class<T> eventType, ConnectableActor connectable, Callback<ActorClientConnector> disconHandler) {
    Callback<ActorClientConnector> discon = (acc, err) -> {
        Log.Info(this, "Client disconnected");
        acc.closeClient();
        if (disconHandler != null) {
            disconHandler.complete(acc, err);
        }
    };
    IPromise connect = connectable.actorClass(KxPublisherActor.class).inboundQueueSize(scheduler.getDefaultQSize()).connect(discon, r -> {
        Object remoteref = r;
        if (((KxPublisherActor) remoteref)._callerSideSubscribers != null) {
            ((KxPublisherActor) remoteref)._callerSideSubscribers.forEach(subs -> {
                ((Subscriber) subs).onError(new IOException("connection lost"));
            });
            ((KxPublisherActor) remoteref)._callerSideSubscribers = null;
        }
    });
    Promise<KxPublisher<T>> res = new Promise<>();
    connect.then((publisher, err) -> {
        if (publisher != null) {
            ((KxPublisherActor) publisher)._streams = this;
            res.resolve((KxPublisher<T>) publisher);
        } else {
            res.reject(err);
        }
    });
    return res;
}
Also used : ActorPublisher(org.nustaq.kontraktor.remoting.base.ActorPublisher) Iterator(java.util.Iterator) ActorClientConnector(org.nustaq.kontraktor.remoting.base.ActorClientConnector) Spliterators(java.util.Spliterators) java.util.stream(java.util.stream) Processor(org.reactivestreams.Processor) Publisher(org.reactivestreams.Publisher) Collection(java.util.Collection) SimpleScheduler(org.nustaq.kontraktor.impl.SimpleScheduler) IOException(java.io.IOException) Function(java.util.function.Function) Consumer(java.util.function.Consumer) SyncProcessor(org.nustaq.kontraktor.reactivestreams.impl.SyncProcessor) KxSubscriber(org.nustaq.kontraktor.reactivestreams.impl.KxSubscriber) ConnectableActor(org.nustaq.kontraktor.remoting.base.ConnectableActor) KxPublisherActor(org.nustaq.kontraktor.reactivestreams.impl.KxPublisherActor) CallerSideMethod(org.nustaq.kontraktor.annotations.CallerSideMethod) Log(org.nustaq.kontraktor.util.Log) org.nustaq.kontraktor(org.nustaq.kontraktor) Subscriber(org.reactivestreams.Subscriber) Spliterator(java.util.Spliterator) KxSubscriber(org.nustaq.kontraktor.reactivestreams.impl.KxSubscriber) Subscriber(org.reactivestreams.Subscriber) ActorClientConnector(org.nustaq.kontraktor.remoting.base.ActorClientConnector) IOException(java.io.IOException)

Example 2 with ConnectableActor

use of org.nustaq.kontraktor.remoting.base.ConnectableActor in project kontraktor by RuedigerMoeller.

the class Basics method testConnectionCloseOnCompleteWS.

@Test
public void testConnectionCloseOnCompleteWS() throws InterruptedException {
    WebSocketPublisher publisher = new WebSocketPublisher().hostName("localhost").urlPath("/ws").port(8082);
    ConnectableActor connectable = new WebSocketConnectable().url("ws://localhost:8082/ws");
    concloseTest(publisher, connectable);
}
Also used : WebSocketPublisher(org.nustaq.kontraktor.remoting.http.undertow.WebSocketPublisher) ConnectableActor(org.nustaq.kontraktor.remoting.base.ConnectableActor) WebSocketConnectable(org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable) Test(org.junit.Test)

Example 3 with ConnectableActor

use of org.nustaq.kontraktor.remoting.base.ConnectableActor in project kontraktor by RuedigerMoeller.

the class KLoadTest method run.

public void run() {
    boolean http = false;
    ConnectableActor connectable;
    if (!http) {
        connectable = new WebSocketConnectable(MyHttpApp.class, "ws://localhost:8080/ws").serType(SerializerType.JsonNoRef);
    } else {
        connectable = new HttpConnectable(MyHttpApp.class, "http://localhost:8080/api").serType(SerializerType.JsonNoRef);
    }
    connectable.connect((connector, error) -> {
        System.out.println("connection lost " + connector);
    }).then((res, err) -> {
        myApp = (MyHttpApp) res;
        myApp.login("dummy", "dummy").then((s, err1) -> {
            session = s;
            System.out.println("session connected " + session);
            session.subscribe((result, e) -> {
                count++;
                lastBcast.set(System.currentTimeMillis());
            });
        });
    });
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) SerializerType(org.nustaq.kontraktor.remoting.encoding.SerializerType) ConnectableActor(org.nustaq.kontraktor.remoting.base.ConnectableActor) MyHttpApp(sample.httpjs.MyHttpApp) HttpClientConnector(org.nustaq.kontraktor.remoting.http.HttpClientConnector) HttpConnectable(org.nustaq.kontraktor.remoting.http.HttpConnectable) WebSocketConnectable(org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable) MyHttpAppSession(sample.httpjs.MyHttpAppSession) ArrayList(java.util.ArrayList) HttpConnectable(org.nustaq.kontraktor.remoting.http.HttpConnectable) ConnectableActor(org.nustaq.kontraktor.remoting.base.ConnectableActor) WebSocketConnectable(org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable)

Example 4 with ConnectableActor

use of org.nustaq.kontraktor.remoting.base.ConnectableActor in project kontraktor by RuedigerMoeller.

the class TestCrashClose method main.

public static void main(String[] args) {
    boolean http = false;
    ConnectableActor connectable;
    if (!http) {
        connectable = new WebSocketConnectable(MyHttpApp.class, "ws://localhost:8080/ws").serType(SerializerType.JsonNoRef);
    } else {
        connectable = new HttpConnectable(MyHttpApp.class, "http://localhost:8080/api").serType(SerializerType.JsonNoRef);
    }
    connectable.connect((connector, error) -> {
        System.out.println("connection lost " + connector);
    }).then((res, err) -> {
        MyHttpApp myApp = (MyHttpApp) res;
        myApp.login("dummy", "dummy").then((s, err1) -> {
            MyHttpAppSession session = s;
            System.out.println("session connected " + session);
            session.subscribe((result, e) -> {
                System.exit(0);
            });
        });
    });
}
Also used : SerializerType(org.nustaq.kontraktor.remoting.encoding.SerializerType) ConnectableActor(org.nustaq.kontraktor.remoting.base.ConnectableActor) MyHttpApp(sample.httpjs.MyHttpApp) HttpConnectable(org.nustaq.kontraktor.remoting.http.HttpConnectable) WebSocketConnectable(org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable) MyHttpAppSession(sample.httpjs.MyHttpAppSession) HttpConnectable(org.nustaq.kontraktor.remoting.http.HttpConnectable) MyHttpApp(sample.httpjs.MyHttpApp) ConnectableActor(org.nustaq.kontraktor.remoting.base.ConnectableActor) MyHttpAppSession(sample.httpjs.MyHttpAppSession) WebSocketConnectable(org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable)

Example 5 with ConnectableActor

use of org.nustaq.kontraktor.remoting.base.ConnectableActor in project kontraktor by RuedigerMoeller.

the class RemotingTest method fromRemote.

private void fromRemote(ConnectableActor con) {
    RemotingTA remote = (RemotingTA) con.connect().await();
    IPromise fin = new Promise();
    AtomicInteger count = new AtomicInteger();
    Integer expect = remote.sayHello(10, (str, err) -> {
        if (Actors.isCont(err)) {
            System.out.println("received:" + str);
            count.incrementAndGet();
        } else {
            fin.complete();
        }
    }).await();
    fin.await();
    Assert.assertTrue(expect.intValue() == count.intValue());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TCPConnectable(org.nustaq.kontraktor.remoting.tcp.TCPConnectable) SerializerType(org.nustaq.kontraktor.remoting.encoding.SerializerType) TCPPublisher(org.nustaq.kontraktor.remoting.tcp.TCPPublisher) Test(org.junit.Test) Promise(org.nustaq.kontraktor.Promise) WebSocketPublisher(org.nustaq.kontraktor.remoting.http.undertow.WebSocketPublisher) IPromise(org.nustaq.kontraktor.IPromise) HttpPublisher(org.nustaq.kontraktor.remoting.http.undertow.HttpPublisher) ConnectableActor(org.nustaq.kontraktor.remoting.base.ConnectableActor) Http4K(org.nustaq.kontraktor.remoting.http.undertow.Http4K) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Actors(org.nustaq.kontraktor.Actors) HttpConnectable(org.nustaq.kontraktor.remoting.http.HttpConnectable) TCPNIOPublisher(org.nustaq.kontraktor.remoting.tcp.TCPNIOPublisher) WebSocketConnectable(org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable) Assert(org.junit.Assert) Promise(org.nustaq.kontraktor.Promise) IPromise(org.nustaq.kontraktor.IPromise) IPromise(org.nustaq.kontraktor.IPromise) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

ConnectableActor (org.nustaq.kontraktor.remoting.base.ConnectableActor)5 WebSocketConnectable (org.nustaq.kontraktor.remoting.websockets.WebSocketConnectable)4 SerializerType (org.nustaq.kontraktor.remoting.encoding.SerializerType)3 HttpConnectable (org.nustaq.kontraktor.remoting.http.HttpConnectable)3 Test (org.junit.Test)2 WebSocketPublisher (org.nustaq.kontraktor.remoting.http.undertow.WebSocketPublisher)2 MyHttpApp (sample.httpjs.MyHttpApp)2 MyHttpAppSession (sample.httpjs.MyHttpAppSession)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 Spliterator (java.util.Spliterator)1 Spliterators (java.util.Spliterators)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 java.util.stream (java.util.stream)1 Assert (org.junit.Assert)1