Skip to content

Commit

Permalink
fixed 100% load issue by upgrading to websocket 1.3.1 snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Prehn committed Jan 1, 2016
1 parent 71c339f commit d31828c
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 38 deletions.
2 changes: 1 addition & 1 deletion bundles/binding/org.openhab.binding.connectsdk/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
<classpathentry exported="true" kind="lib" path="lib/Java-WebSocket-1.3.1-SNAPSHOT.jar" sourcepath="/Users/sprehn/Code/Java-WebSocket/src/main/java"/>
<classpathentry exported="true" kind="lib" path="lib/httpcore-4.0.1.jar"/>
<classpathentry exported="true" kind="lib" path="lib/xpp3-1.1.4c.jar"/>
<classpathentry exported="true" kind="lib" path="lib/httpclient-4.0.2.jar"/>
<classpathentry exported="true" kind="lib" path="lib/json-20140107.jar" sourcepath="/Users/sprehn/.m2/repository/org/json/json/20140107/json-20140107-sources.jar"/>
<classpathentry exported="true" kind="lib" path="lib/jmdns-3.4.1.jar" sourcepath="/Users/sprehn/.m2/repository/javax/jmdns/jmdns/3.4.1/jmdns-3.4.1-sources.jar"/>
<classpathentry exported="true" kind="lib" path="lib/Java-WebSocket-1.3.0.jar"/>
<classpathentry exported="true" kind="lib" path="lib/android-4.1.1.4.jar" sourcepath="/Users/sprehn/.m2/repository/com/google/android/android/4.1.1.4/android-4.1.1.4-sources.jar"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Bundle-RequiredExecutionEnvironment: J2SE-1.5
Service-Component: OSGI-INF/binding.xml, OSGI-INF/genericbindingprovider.xml
Bundle-ClassPath: .,
lib/android-4.1.1.4.jar,
lib/Java-WebSocket-1.3.0.jar,
lib/Java-WebSocket-1.3.1-SNAPSHOT.jar,
lib/jmdns-3.4.1.jar,
lib/json-20140107.jar,
lib/httpclient-4.0.2.jar,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ bin.includes = META-INF/,\
.,\
OSGI-INF/,\
lib/android-4.1.1.4.jar,\
lib/Java-WebSocket-1.3.0.jar,\
lib/jmdns-3.4.1.jar,\
lib/json-20140107.jar,\
lib/httpclient-4.0.2.jar,\
lib/xpp3-1.1.4c.jar,\
lib/httpcore-4.0.1.jar
lib/httpcore-4.0.1.jar,\
lib/Java-WebSocket-1.3.1-SNAPSHOT.jar
output.. = target/classes/
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.connectsdk.service.webos;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
Expand All @@ -20,7 +21,8 @@
import javax.net.ssl.X509TrustManager;

import org.java_websocket.WebSocket;
import org.java_websocket.client.DefaultSSLWebSocketClientFactory;
//2016-01-01: Moved from 1.3.0 to 1.3.1-snapshot
//import org.java_websocket.client.DefaultSSLWebSocketClientFactory;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.json.JSONArray;
Expand Down Expand Up @@ -678,8 +680,15 @@ else if (payloadType.equals("hello")) {
}
}

// 2016-01-01: Moved from 1.3.0 to 1.3.1-snapshot
private void setSSLContext(SSLContext sslContext) {
setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sslContext));
//setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sslContext));
try {
super.setSocket(sslContext.getSocketFactory().createSocket());
} catch (IOException e) {
Log.e("SSL Setup", "failed to setup ssl socket",e);
}

}

protected void setupSSL() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,40 +180,47 @@ public void onPairingRequired(ConnectableDevice device, DeviceService service, P
@Override
public void onDeviceReady(ConnectableDevice device) {
logger.info("Device ready: {}", device);
handleSubscriptions(device);

for (OpenhabConnectSDKPropertyBridge b : bridges) {
b.removeAnySubscription(device); // ensure all old subscriptions are cleaned out
b.addSubscription(device, providers, eventPublisher);
}
sendHelloWorld(device);
}

@Override
public void onDeviceDisconnected(ConnectableDevice device) {
logger.info("Device disconnected: {}", device);

logger.debug("Device disconnected: {}", device);
// nothing to do here, we disconnect bridges in onDeviceRemoved method
}

@Override
public void onConnectionFailed(ConnectableDevice device, ServiceCommandError error) {
logger.warn("Connection failed: {} - error: {}", device, error.getMessage());

logger.debug("Connection failed: {} - error: {}", device, error.getMessage());
// nothing to do here, only called when pairing fails...
}

@Override
public void onCapabilityUpdated(ConnectableDevice device, List<String> added, List<String> removed) {
logger.debug("Capabilities updated: {} - added: {} - removed: {}", device, added, removed);
handleSubscriptions(device);
device.connect(); // ensure all services are connected, can be called even if some of the services are already connected
for (OpenhabConnectSDKPropertyBridge b : bridges) {
b.removeAnySubscription(device); // ensure all old subscriptions are cleaned out
b.addSubscription(device, providers, eventPublisher);
}
}
});

handleSubscriptions(device);
logger.debug("Capabilities: " + device.getFriendlyName() + " : " + device.getCapabilities().toString());

sendHelloWorld(device);

device.connect(); // ensure all services are connected, can be called even if some of the services are already connected
//logger.debug("Capabilities: " + device.getFriendlyName() + " : " + device.getCapabilities().toString());
}

@Override
public void onDeviceUpdated(DiscoveryManager manager, ConnectableDevice device) {
logger.info("Device updated: {}", device);
handleSubscriptions(device);
for (OpenhabConnectSDKPropertyBridge b : bridges) {
b.removeAnySubscription(device);
b.addSubscription(device, providers, eventPublisher);
}
device.connect(); // ensure all services are connected, can be called even if some of the services are already connected
}

@Override
Expand All @@ -222,19 +229,14 @@ public void onDeviceRemoved(DiscoveryManager manager, ConnectableDevice device)
for (OpenhabConnectSDKPropertyBridge b : bridges) {
b.removeAnySubscription(device);
}
// no need to call device.disconnect this is done by connect sdk framework for us
}

@Override
public void onDiscoveryFailed(DiscoveryManager manager, ServiceCommandError error) {
logger.warn("Discovery failed: {}", error.getMessage());
}

private void handleSubscriptions(ConnectableDevice device) {
for (OpenhabConnectSDKPropertyBridge b : bridges) {
b.updateSubscription(device, providers, eventPublisher);
}
}

private void sendHelloWorld(ConnectableDevice device) {
if (device.hasCapability(ToastControl.Show_Toast)) {
try {
Expand All @@ -244,9 +246,7 @@ private void sendHelloWorld(ConnectableDevice device) {
OutputStream b64 = Base64.getEncoder().wrap(os);
ImageIO.write(bi, "png", b64);
String result = os.toString("UTF-8");
if (!device.isConnected()) {
device.connect();
}

device.getCapability(ToastControl.class).showToast("Welcome to Openhab!", result, "png",
new ResponseListener<Object>() {

Expand All @@ -264,9 +264,7 @@ public void onError(ServiceCommandError error) {
} catch (IOException ex) {
logger.error(ex.getMessage(), ex);
}

}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.connectsdk.service.capability.listeners.ResponseListener;
import com.connectsdk.service.command.ServiceCommandError;
import com.connectsdk.service.command.ServiceSubscription;
import com.connectsdk.service.command.URLServiceSubscription;

public abstract class AbstractOpenhabConnectSDKPropertyBridge<T> implements OpenhabConnectSDKPropertyBridge {
private static final Logger logger = LoggerFactory.getLogger(AbstractOpenhabConnectSDKPropertyBridge.class);
Expand All @@ -27,20 +28,24 @@ private synchronized Map<String, ServiceSubscription<T>> getSubscriptions() {
}

@Override
public final void updateSubscription(final ConnectableDevice device,
final Collection<ConnectSDKBindingProvider> providers, final EventPublisher eventPublisher) { // here
removeAnySubscription(device);

public final void addSubscription(final ConnectableDevice device,
final Collection<ConnectSDKBindingProvider> providers, final EventPublisher eventPublisher) {
ServiceSubscription<T> listener = getSubscription(device, providers, eventPublisher);
if (listener != null) {
logger.debug("Subscribed {}:{} listener on IP: {}", getItemClass(), getItemProperty(),
device.getIpAddress());

// not sure if this is required, trying to find performance leak
if(subscriptions != null && subscriptions.containsKey(device.getIpAddress())){
throw new IllegalStateException("A listener for device "+device.getIpAddress()+" already exists for this Bridge: "+ this.getClass().getName()+ " Call removeAnySubscription first.");
}

getSubscriptions().put(device.getIpAddress(), listener);
}
}

/**
* Creates a subscription instance for this device.
* Creates a subscription instance for this device. This may return <code>null</code> if no subscription is possible or required.
*
* @param device
* @param providers
Expand All @@ -52,10 +57,16 @@ protected abstract ServiceSubscription<T> getSubscription(final ConnectableDevic

@Override
public final synchronized void removeAnySubscription(final ConnectableDevice device) { // here
if (subscriptions != null) {
if (subscriptions != null) { // only if subscriptions was initialized (lazy loading)
ServiceSubscription<T> l = subscriptions.remove(device.getIpAddress());
if (l != null) {
l.unsubscribe();

// not sure if this is required, trying to find performance leak
if(l instanceof URLServiceSubscription) {
((URLServiceSubscription<?>)l).removeListeners();
}

logger.debug("Unsubscribed {}:{} listener on IP: {}", getItemClass(), getItemProperty(),
device.getIpAddress());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface OpenhabConnectSDKPropertyBridge {

public abstract void onReceiveCommand(ConnectableDevice d, String clazz, String property, Command command);

public abstract void updateSubscription(ConnectableDevice device, Collection<ConnectSDKBindingProvider> providers,
public abstract void addSubscription(ConnectableDevice device, Collection<ConnectSDKBindingProvider> providers,
EventPublisher eventPublisher);

public abstract void removeAnySubscription(ConnectableDevice device);
Expand Down

0 comments on commit d31828c

Please sign in to comment.