Skip to content

Commit

Permalink
Refactor code related to WebSocket client
Browse files Browse the repository at this point in the history
  • Loading branch information
irunika authored and irunika committed Jun 18, 2018
1 parent d7b458a commit b2bbc00
Show file tree
Hide file tree
Showing 18 changed files with 140 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ public final class Constants {
public static final String WEBSOCKET_FRAME_BLOCKING_HANDLER = "WEBSOCKET_FRAME_BLOCKING_HANDLER";
public static final int WEBSOCKET_STATUS_CODE_NORMAL_CLOSURE = 1000;
public static final int WEBSOCKET_STATUS_CODE_GOING_AWAY = 1001;
public static final int WEBSOCKET_STATUS_CODE_PROTOCOL_ERROR = 1002;
public static final int WEBSOCKET_STATUS_CODE_ABNORMAL_CLOSURE = 1006;
public static final int WEBSOCKET_STATUS_CODE_UNEXPECTED_CONDITION = 1011;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.wso2.transport.http.netty.config.ListenerConfiguration;
import org.wso2.transport.http.netty.config.SenderConfiguration;
import org.wso2.transport.http.netty.contract.websocket.WebSocketClientConnector;
import org.wso2.transport.http.netty.contract.websocket.WsClientConnectorConfig;
import org.wso2.transport.http.netty.contract.websocket.WebSocketClientConnectorConfig;
import org.wso2.transport.http.netty.listener.ServerBootstrapConfiguration;

import java.util.Map;
Expand Down Expand Up @@ -57,7 +57,7 @@ HttpClientConnector createHttpClientConnector(Map<String, Object> transportPrope
* @param clientConnectorConfig Properties to create a client connector.
* @return WebSocketClientConnector.
*/
WebSocketClientConnector createWsClientConnector(WsClientConnectorConfig clientConnectorConfig);
WebSocketClientConnector createWsClientConnector(WebSocketClientConnectorConfig clientConnectorConfig);

/**
* Shutdown all the server channels and the accepted channels. It also shutdown all the eventloop groups.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,35 @@

package org.wso2.transport.http.netty.contract.websocket;

import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.HttpHeaders;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Sender configuration for WebSocket client connector.
* Configuration for WebSocket client connector.
*/
public class WsClientConnectorConfig {
public class WebSocketClientConnectorConfig {

private final String remoteAddress;
private List<String> subProtocols;
private int idleTimeoutInSeconds;
private boolean autoRead;
private final Map<String, String> headers = new HashMap<>();

public WsClientConnectorConfig(String remoteAddress) {
this.remoteAddress = remoteAddress;
this.idleTimeoutInSeconds = -1;
this.autoRead = true;
private final HttpHeaders headers;

public WebSocketClientConnectorConfig(String remoteAddress) {
this(remoteAddress, null, -1, true);
}

public WsClientConnectorConfig(String remoteAddress, List<String> subProtocols,
int idleTimeoutInSeconds, boolean autoRead) {
public WebSocketClientConnectorConfig(String remoteAddress, List<String> subProtocols,
int idleTimeoutInSeconds, boolean autoRead) {
this.remoteAddress = remoteAddress;
this.subProtocols = subProtocols;
this.idleTimeoutInSeconds = idleTimeoutInSeconds;
this.autoRead = autoRead;
this.headers = new DefaultHttpHeaders();
}

/**
Expand Down Expand Up @@ -126,7 +126,7 @@ public String getRemoteAddress() {
* @param headers Headers map.
*/
public void addHeaders(Map<String, String> headers) {
this.headers.putAll(headers);
headers.forEach(this.headers::add);
}

/**
Expand All @@ -136,15 +136,15 @@ public void addHeaders(Map<String, String> headers) {
* @param value Value of the header.
*/
public void addHeader(String key, String value) {
this.headers.put(key, value);
this.headers.add(key, value);
}

/**
* Get all the headers.
*
* @return all the headers as a map.
*/
public Map<String, String> getHeaders() {
public HttpHeaders getHeaders() {
return headers;
}

Expand All @@ -155,7 +155,7 @@ public Map<String, String> getHeaders() {
* @return true of the header is present.
*/
public boolean containsHeader(String key) {
return headers.containsKey(key);
return headers.contains(key);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,15 @@

package org.wso2.transport.http.netty.contract.websocket;

import java.nio.ByteBuffer;

/**
* This message contains the details of WebSocket bong message.
*/
public interface WebSocketControlMessage extends WebSocketMessage {
public interface WebSocketControlMessage extends WebSocketBinaryMessage {

/**
* Get the control signal.
*
* @return the control signal as a {@link WebSocketControlSignal}.
*/
WebSocketControlSignal getControlSignal();

/**
* Get the payload of the control signal.
*
* @return the payload of the control signal.
*/
ByteBuffer getPayload();

/**
* Get the binary data as a byte array.
*
* @return the binary data as a byte array.
*/
byte[] getByteArray();
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.wso2.transport.http.netty.contract.HttpWsConnectorFactory;
import org.wso2.transport.http.netty.contract.ServerConnector;
import org.wso2.transport.http.netty.contract.websocket.WebSocketClientConnector;
import org.wso2.transport.http.netty.contract.websocket.WsClientConnectorConfig;
import org.wso2.transport.http.netty.contract.websocket.WebSocketClientConnectorConfig;
import org.wso2.transport.http.netty.contractimpl.websocket.DefaultWebSocketClientConnector;
import org.wso2.transport.http.netty.listener.ServerBootstrapConfiguration;
import org.wso2.transport.http.netty.listener.ServerConnectorBootstrap;
Expand Down Expand Up @@ -96,7 +96,7 @@ public HttpClientConnector createHttpClientConnector(
}

@Override
public WebSocketClientConnector createWsClientConnector(WsClientConnectorConfig clientConnectorConfig) {
public WebSocketClientConnector createWsClientConnector(WebSocketClientConnectorConfig clientConnectorConfig) {
return new DefaultWebSocketClientConnector(clientConnectorConfig, clientGroup);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
package org.wso2.transport.http.netty.contractimpl.websocket;

import io.netty.channel.EventLoopGroup;
import io.netty.handler.codec.http.HttpHeaders;
import org.wso2.transport.http.netty.contract.websocket.ClientHandshakeFuture;
import org.wso2.transport.http.netty.contract.websocket.WebSocketClientConnector;
import org.wso2.transport.http.netty.contract.websocket.WsClientConnectorConfig;
import org.wso2.transport.http.netty.contract.websocket.WebSocketClientConnectorConfig;
import org.wso2.transport.http.netty.sender.websocket.WebSocketClient;

import java.util.Map;

/**
* Implementation of WebSocket client connector.
*/
Expand All @@ -35,11 +34,11 @@ public class DefaultWebSocketClientConnector implements WebSocketClientConnector
private final String remoteUrl;
private final String subProtocols;
private final int idleTimeout;
private final Map<String, String> customHeaders;
private final HttpHeaders customHeaders;
private final EventLoopGroup wsClientEventLoopGroup;
private final boolean autoRead;

public DefaultWebSocketClientConnector(WsClientConnectorConfig clientConnectorConfig,
public DefaultWebSocketClientConnector(WebSocketClientConnectorConfig clientConnectorConfig,
EventLoopGroup wsClientEventLoopGroup) {
this.remoteUrl = clientConnectorConfig.getRemoteAddress();
this.subProtocols = clientConnectorConfig.getSubProtocolsAsCSV();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class DefaultWebSocketMessage implements WebSocketMessage {
protected boolean secureConnection;
protected boolean isServerMessage;
protected WebSocketConnection webSocketConnection;
protected String sessionlID;
protected String sessionID;

public void setProperty(String key, Object value) {
properties.put(key, value);
Expand All @@ -54,8 +54,8 @@ public Map<String, Object> getProperties() {
return properties;
}

public void setSessionlID(String sessionlID) {
this.sessionlID = sessionlID;
public void setSessionID(String sessionID) {
this.sessionID = sessionID;
}

public void setTarget(String target) {
Expand All @@ -76,7 +76,7 @@ public String getListenerInterface() {
return listenerInterface;
}

public void setIsConnectionSecured(boolean isConnectionSecured) {
public void setIsSecureConnection(boolean isConnectionSecured) {
this.secureConnection = isConnectionSecured;
}

Expand Down Expand Up @@ -105,6 +105,6 @@ public WebSocketConnection getWebSocketConnection() {

@Override
public String getSessionID() {
return sessionlID;
return sessionID;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@ private void notifyIdleTimeout() throws WebSocketConnectorException {
private void setupCommonProperties(DefaultWebSocketMessage webSocketMessage) {
webSocketMessage.setTarget(target);
webSocketMessage.setListenerInterface(interfaceId);
webSocketMessage.setIsConnectionSecured(securedConnection);
webSocketMessage.setIsSecureConnection(securedConnection);
webSocketMessage.setWebSocketConnection(webSocketConnection);
webSocketMessage.setSessionlID(webSocketConnection.getId());
webSocketMessage.setSessionID(webSocketConnection.getId());
webSocketMessage.setIsServerMessage(isServer);
webSocketMessage.setProperty(Constants.LISTENER_PORT,
((InetSocketAddress) ctx.channel().localAddress()).getPort());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,23 @@

import org.wso2.transport.http.netty.contract.websocket.WebSocketControlMessage;
import org.wso2.transport.http.netty.contract.websocket.WebSocketControlSignal;
import org.wso2.transport.http.netty.contractimpl.websocket.DefaultWebSocketMessage;

import java.nio.ByteBuffer;

/**
* Implementation of WebSocket control message.
*/
public class DefaultWebSocketControlMessage extends DefaultWebSocketMessage implements WebSocketControlMessage {
public class DefaultWebSocketControlMessage extends DefaultWebSocketBinaryMessage implements WebSocketControlMessage {

private final WebSocketControlSignal controlSignal;
private final ByteBuffer buffer;

public DefaultWebSocketControlMessage(WebSocketControlSignal controlSignal, ByteBuffer buffer) {
super(buffer, true);
this.controlSignal = controlSignal;
this.buffer = buffer;
}

@Override
public WebSocketControlSignal getControlSignal() {
return controlSignal;
}

@Override
public byte[] getByteArray() {
byte[] bytes;
if (buffer.hasArray()) {
bytes = buffer.array();
} else {
int remaining = buffer.remaining();
bytes = new byte[remaining];
for (int i = 0; i < remaining; i++) {
bytes[i] = buffer.get();
}
}
return bytes;
}

@Override
public ByteBuffer getPayload() {
return buffer;
}
}
Loading

0 comments on commit b2bbc00

Please sign in to comment.