Skip to content

Commit

Permalink
Merge pull request #8 from kurtisnelson/kn/new-websockets
Browse files Browse the repository at this point in the history
Clean websockets
  • Loading branch information
scottgwald committed Oct 8, 2014
2 parents 84a3f33 + 605670a commit 6071d47
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 213 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
language: android
script: ./gradlew build check
android:
components:
- build-tools-20.0.0
- build-tools-19.1.0
9 changes: 6 additions & 3 deletions WearScript/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repositories {

android {
compileSdkVersion 19
buildToolsVersion "19.1.0"
buildToolsVersion "20.0.0"

defaultConfig {
applicationId "com.dappervision.wearscript"
Expand All @@ -26,12 +26,15 @@ android {
versionCode 2
versionName "0.3.0"
}
lintOptions {
abortOnError false
}
}

dependencies {
compile 'com.android.support:support-v4:20.0.0'
compile 'com.android.support:support-v13:20.0.+'
compile 'com.crittercism:crittercism-android-agent:+'
compile 'com.android.support:support-v13:20.0.0'
compile 'com.crittercism:crittercism-android-agent:4.5.3'
compile 'de.greenrobot:eventbus:2.2.0'
compile 'com.polysfactory.lib.glass.bluetooth:glass-bluetooth:0.1.2'
compile 'com.radiusnetworks:AndroidIBeaconLibrary:0.7.6@aar'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.dappervision.wearscript.HardwareDetector;
import com.dappervision.wearscript.Log;
import com.dappervision.wearscript.Utils;
import com.dappervision.wearscript.WearScriptConnection;
import com.dappervision.wearscript.network.WearScriptConnection;
import com.dappervision.wearscript.events.ChannelSubscribeEvent;
import com.dappervision.wearscript.events.ChannelUnsubscribeEvent;
import com.dappervision.wearscript.events.GistSyncEvent;
Expand All @@ -23,7 +23,6 @@
import org.json.simple.JSONValue;
import org.msgpack.MessagePack;
import org.msgpack.type.Value;
import org.msgpack.util.json.JSON;

import java.util.List;
import java.util.TreeMap;
Expand Down Expand Up @@ -215,14 +214,9 @@ public void onConnect() {
unregisterCallback(ONCONNECT);
}

@Override
protected void onDisconnect() {

private TreeMap toMap(Value map) {
TreeMap<String, Value> mapOut = new TreeMap<String, Value>();
Value[] kv = map.asMapValue().getKeyValueArray();
for (int i = 0; i < kv.length / 2; i++) {
mapOut.put(kv[i * 2].asRawValue().getString(), kv[i * 2 + 1]);
}
return mapOut;
}

@Override
Expand Down Expand Up @@ -343,10 +337,14 @@ public void onReceive(String channel, byte[] dataRaw, List<Value> data) {
Utils.eventBusPost(new WarpHEvent(h));
}
}
}

@Override
public void onDisconnect() {

private static TreeMap toMap(Value map) {
TreeMap<String, Value> mapOut = new TreeMap<String, Value>();
Value[] kv = map.asMapValue().getKeyValueArray();
for (int i = 0; i < kv.length / 2; i++) {
mapOut.put(kv[i * 2].asRawValue().getString(), kv[i * 2 + 1]);
}
return mapOut;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package com.dappervision.wearscript.network;

import com.codebutler.android_websockets.WebSocketClient;
import com.dappervision.wearscript.Log;

import org.apache.http.message.BasicNameValuePair;
import org.msgpack.MessagePack;
import org.msgpack.type.Value;
import org.msgpack.type.ValueFactory;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.SynchronousQueue;

import static org.msgpack.template.Templates.TValue;
import static org.msgpack.template.Templates.tList;

public class SocketController {
private static final String TAG = "SocketController";

private static MessagePack mMsgpack = new MessagePack();

private WebSocketClient mSocketClient;
private Listener mListener;
private boolean mConnected, mInUse;
private Queue<byte[]> mBuffer;

public SocketController(URI uri) {
mInUse = true;
mConnected = false;
mBuffer = new SynchronousQueue<byte[]>();
List<BasicNameValuePair> extraHeaders = Arrays.asList();
mSocketClient = new WebSocketClient(uri, mLocalListener, extraHeaders);
mSocketClient.connect();
}

public void send(byte[] msg) {
if(isConnected()) {
mSocketClient.send(msg);
}else {
mBuffer.add(msg);
}
}

public boolean isConnected() {
return mConnected;
}

public void disconnect() {
mInUse = false;
mBuffer.clear();
mSocketClient.disconnect();
mSocketClient.getHandlerThread().quit();
}

public void setListener(Listener listener) {
mListener = listener;
}

private void reconnect() {
mSocketClient.connect();
}

private WebSocketClient.Listener mLocalListener = new WebSocketClient.Listener() {
@Override
public void onConnect() {
mConnected = true;
while(!mBuffer.isEmpty()) {
mSocketClient.send(mBuffer.poll());
}
if(mInUse && mListener != null) {
mListener.onConnect();
}
}

@Override
public void onMessage(String s) {
// Unused
}

@Override
public void onDisconnect(int i, String s) {
Log.w(TAG, "Lifecycle: Underlying socket disconnected: i: " + i + " s: " + s);
mConnected = false;
if(mInUse) {
mListener.onDisconnect();
reconnect();
}
}

@Override
public void onError(Exception e) {
Log.w(TAG, "Lifecycle: Underlying socket errored: " + e.getLocalizedMessage());
mConnected = false;
if(mInUse) {
mListener.onDisconnect();
reconnect();
}
}

@Override
public void onMessage(byte[] message) {
if (!mInUse) return;
try {
handleMessage(message);
} catch (Exception e) {
Log.e(TAG, String.format("onMessage: %s", e.toString()));
}
}

private void handleMessage(byte[] message) throws IOException {
String channel = "";
List<Value> input = mMsgpack.read(message, tList(TValue));
channel = input.get(0).asRawValue().getString();
Log.d(TAG, String.format("Got %s", channel));

mListener.onMessage(channel, message, input);
}
};

interface Listener {

void onConnect();

void onDisconnect();

void onMessage(String channel, byte[] message, List<Value> input);
}

public static byte[] encode(Object... data) {
List<Value> out = new ArrayList<Value>();
for (Object i : data) {
Class c = i.getClass();
if (c.equals(String.class))
out.add(ValueFactory.createRawValue((String) i));
else if (c.equals(Double.class))
out.add(ValueFactory.createFloatValue((Double) i));
else if (Value.class.isAssignableFrom(c))
out.add((Value) i);
else if (c.equals(Boolean.class))
out.add(ValueFactory.createBooleanValue((Boolean) i));
else {
android.util.Log.e(TAG, "Unhandled class: " + c);
return null;
}
}
try {
return mMsgpack.write(out);
} catch (IOException e) {
android.util.Log.e(TAG, "Could not encode msgpack");
}
return null;
}
}
Loading

0 comments on commit 6071d47

Please sign in to comment.