Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
estonlabs.cxtl.common.AbstractStreamFactory Maven / Gradle / Ivy
package estonlabs.cxtl.common;
import estonlabs.cxtl.common.codec.Codec;
import estonlabs.cxtl.common.stream.core.HeaderCreator;
import estonlabs.cxtl.common.stream.core.TyrusWebsocketConnection;
import estonlabs.cxtl.common.stream.core.WebsocketConnection;
import estonlabs.cxtl.common.stream.managed.InboundMessage;
import estonlabs.cxtl.common.stream.managed.ManagedWsSession;
import estonlabs.cxtl.common.stream.managed.OutboundMessage;
import estonlabs.cxtl.common.stream.pojo.PojoWebsocketConnection;
import estonlabs.cxtl.exchanges.a.specification.domain.Exchange;
import estonlabs.cxtl.exchanges.a.specification.lib.StreamFactory;
import lombok.Getter;
import lombok.NonNull;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URI;
import java.util.function.Function;
import java.util.function.Supplier;
@Getter
public abstract class AbstractStreamFactory> implements StreamFactory {
protected final Exchange exchange;
protected final Codec codec;
protected final Class type;
protected Supplier pingGenerator;
protected Runnable ping;
protected Supplier pong;
protected long pingWindow;
protected long staleWindow = -1;
protected boolean expectPingResponse;
protected Proxy httpProxy = null;
/**
* A factory for creating a websocket connection
*
* @param codec - the codec for encoding and decoding
* @param type
*/
public AbstractStreamFactory(Exchange exchange, Codec codec, Class type) {
this.exchange = exchange;
this.codec = codec;
this.type = type;
}
@Override
public StreamFactory ping(long pingWindow, @NonNull Runnable ping, boolean expectPingResponse) {
this.pingWindow = pingWindow;
this.ping = ping;
this.expectPingResponse = expectPingResponse;
return me();
}
@Override
public CONCRETE ping(long pingWindow, @NonNull Supplier pingGenerator) {
this.pingWindow = pingWindow;
this.pingGenerator = pingGenerator;
this.expectPingResponse = true;
return me();
}
@Override
public CONCRETE pong(Supplier pong) {
this.pong = pong;
return me();
}
@Override
public CONCRETE staleWindow(long staleWindow) {
this.staleWindow = staleWindow;
return me();
}
@Override
public CONCRETE httpProxy(Proxy httpProxy) {
this.httpProxy = httpProxy;
return me();
}
@Override
public CONCRETE httpProxy(URI httpProxy) {
return httpProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(httpProxy.getHost(), httpProxy.getPort())));
}
protected WebsocketConnection newPublicWebsocket(URI uri, HeaderCreator headerCreator) {
return new PojoWebsocketConnection<>(new TyrusWebsocketConnection(uri, headerCreator, httpProxy),
id -> createSession(id, null)
);
}
protected WebsocketConnection newPrivateWebsocket(URI uri, HeaderCreator headerCreator) {
return newPrivateWebsocket(uri, headerCreator, null);
}
protected WebsocketConnection newPrivateWebsocketWithProxy(URI uri, Proxy httpProxy, HeaderCreator headerCreator) {
return newPrivateWebsocket(uri, headerCreator, httpProxy, null);
}
protected WebsocketConnection newPrivateWebsocket(URI uri, HeaderCreator headerCreator, Function,ManagedWsSession> adapter) {
return newPrivateWebsocket(uri, headerCreator, httpProxy, adapter);
}
protected WebsocketConnection newPrivateWebsocket(URI uri, HeaderCreator headerCreator, Proxy httpProxy, Function,ManagedWsSession> adapter) {
return new PojoWebsocketConnection<>(new TyrusWebsocketConnection(uri, headerCreator, httpProxy),
id -> {
return createPrivateSession(id, adapter);
}
);
}
protected ManagedWsSession createSession(String id, Function,ManagedWsSession> adapter) {
ManagedWsSession session = new ManagedWsSession<>(id, codec, type);
if (pingGenerator != null) {
session.enablePing(pingWindow, pingGenerator);
}else if (ping != null) {
session.enablePing(pingWindow, ping,expectPingResponse);
}
if (staleWindow > 0) {
session.enableStaleFeedCheck(staleWindow);
}
session.enablePong(pong);
return adapter==null?session:adapter.apply(session);
}
protected ManagedWsSession createPrivateSession(String id, Function,ManagedWsSession> adapter) {
return createSession(id, adapter);
}
protected abstract CONCRETE me();
@Override
public Exchange getExchange() {
return exchange;
}
}