Skip to content

Commit

Permalink
Move into interface
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtisnelson committed Oct 7, 2014
1 parent e0d4920 commit 38e763d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,25 +209,18 @@ class WearScriptConnectionImpl extends WearScriptConnection {

@Override
public void onConnect() {
super.onConnect();
makeCall(ONCONNECT, "");
// NOTE(brandyn): This ensures that we are only calling the function once
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
public void onReceive(String channel, byte[] dataRaw, List<Value> data) {
super.onReceive(channel, dataRaw, data);
// BUG(brandyn): If the channel should go to a subchannel now it won't make it,
// we should modify channel name before this call
makeCall(channel, "'" + Base64.encodeToString(dataRaw, Base64.NO_WRAP) + "'");
Expand Down Expand Up @@ -345,4 +338,13 @@ public void onReceive(String channel, byte[] dataRaw, List<Value> data) {
}
}
}

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
Expand Up @@ -19,7 +19,7 @@
import static org.msgpack.template.Templates.TValue;
import static org.msgpack.template.Templates.tList;

public abstract class WearScriptConnection implements SocketController.Listener {
public abstract class WearScriptConnection {
private static final String TAG = "WearScriptConnection";
private static final String LISTEN_CHAN = "subscriptions";

Expand Down Expand Up @@ -58,26 +58,11 @@ public static Value mapValue(Map<String, ArrayList<String>> data) {
return ValueFactory.createMapValue(mapArray.toArray(new Value[mapArray.size()]));
}

public void onConnect() {
publishChannels();
}

public void onReceive(String channel, byte[] dataRaw, List<Value> data) {
protected abstract void onReceive(String channel, byte[] dataRaw, List<Value> data);

}
protected abstract void onConnect();

public void onDisconnect() {

}

public void onMessage(String channel, byte[] message, List<Value> input) {
if (channel.equals(LISTEN_CHAN)) {
String d = input.get(1).asRawValue().getString();
Value[] channels = input.get(2).asArrayValue().getElementArray();
setDeviceChannels(d, channels);
}
onReceiveDispatch(channel, message, input);
}
protected abstract void onDisconnect();

private void onReceiveDispatch(String channel, byte[] dataRaw, List<Value> data) {
String channelPart = existsInternal(channel);
Expand Down Expand Up @@ -213,7 +198,7 @@ public void connect(URI uri) {
this.uri = uri;
Log.i(TAG, "Lifecycle: Socket connecting");
mSocket = new SocketController(uri);
mSocket.setListener(this);
mSocket.setListener(mSocketListener);
}
}

Expand Down Expand Up @@ -295,10 +280,11 @@ private void pinger() {
new Thread(new Runnable() {
public void run() {
while (true) {
if (mSocket == null)
return;
Log.w(TAG, "Lifecycle: Pinger...");
publish(LISTEN_CHAN, WearScriptConnection.this.groupDevice, channelsValue());
if (mSocket == null) return;
Log.i(TAG, "Lifecycle: Pinger...");
if(mSocket.isConnected()) {
publish(LISTEN_CHAN, WearScriptConnection.this.groupDevice, channelsValue());
}
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
Expand All @@ -321,4 +307,27 @@ public void run() {
}
}).start();
}

private SocketController.Listener mSocketListener = new SocketController.Listener() {
@Override
public void onConnect() {
publishChannels();
WearScriptConnection.this.onConnect();
}

@Override
public void onDisconnect() {
WearScriptConnection.this.onDisconnect();
}

@Override
public void onMessage(String channel, byte[] message, List<Value> input) {
if (channel.equals(LISTEN_CHAN)) {
String d = input.get(1).asRawValue().getString();
Value[] channels = input.get(2).asArrayValue().getElementArray();
setDeviceChannels(d, channels);
}
onReceiveDispatch(channel, message, input);
}
};
}

0 comments on commit 38e763d

Please sign in to comment.