org.dsa.iot.dslink.provider.netty.DefaultWsProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dslink Show documentation
Show all versions of dslink Show documentation
SDK for the IoT DSA protocol
package org.dsa.iot.dslink.provider.netty;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.*;
import io.netty.handler.codec.http.websocketx.*;
import io.netty.handler.codec.http.websocketx.extensions.WebSocketClientExtensionHandler;
import io.netty.handler.codec.http.websocketx.extensions.WebSocketClientExtensionHandshaker;
import io.netty.handler.codec.http.websocketx.extensions.compression.PerMessageDeflateClientExtensionHandshaker;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import io.netty.util.CharsetUtil;
import org.dsa.iot.dslink.connection.NetworkClient;
import org.dsa.iot.dslink.provider.WsProvider;
import org.dsa.iot.dslink.util.URLInfo;
import org.dsa.iot.dslink.util.http.WsClient;
import org.dsa.iot.dslink.util.json.EncodingFormat;
import org.dsa.iot.dslink.util.json.JsonObject;
import org.dsa.iot.shared.SharedObjects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.TrustManagerFactory;
import java.net.URI;
import java.net.URISyntaxException;
/**
* @author Samuel Grenier
*/
public class DefaultWsProvider extends WsProvider {
private static final Logger LOGGER;
@Override
public void connect(WsClient client) {
if (client == null) {
throw new NullPointerException("client");
}
final URLInfo url = client.getUrl();
String full = url.protocol + "://" + url.host
+ ":" + url.port + url.path;
full = full.replaceAll("%", "%25");
URI uri;
try {
uri = new URI(full);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
WebSocketVersion v = WebSocketVersion.V13;
HttpHeaders h = new DefaultHttpHeaders();
final WebSocketClientHandshaker wsch = WebSocketClientHandshakerFactory
.newHandshaker(uri, v, null, true, h, Integer.MAX_VALUE);
final WebSocketHandler handler = new WebSocketHandler(wsch, client);
Bootstrap b = new Bootstrap();
b.group(SharedObjects.getLoop());
b.channel(NioSocketChannel.class);
b.handler(new ChannelInitializer() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
if (url.secure) {
TrustManagerFactory man = InsecureTrustManagerFactory.INSTANCE;
SslContextBuilder scb = SslContextBuilder.forClient();
SslContext con = scb.trustManager(man).build();
p.addLast(con.newHandler(ch.alloc()));
}
p.addLast(new HttpClientCodec());
p.addLast(new HttpObjectAggregator(8192));
WebSocketClientExtensionHandshaker com
= new PerMessageDeflateClientExtensionHandshaker();
p.addLast(new WebSocketClientExtensionHandler(com));
p.addLast(handler);
}
});
b.connect(url.host, url.port);
}
private static class WebSocketHandler extends SimpleChannelInboundHandler