Permalink
Browse files

Added Deacon wrapper class (again) that isolates Android-specific cod…

…e in order to enable standalone testing of Deacon without and Android instance
  • Loading branch information...
1 parent 6daab84 commit 117f7586b37d3d994afc7f948aefa4e5a5000856 @davidrea committed May 4, 2010
Showing with 59 additions and 14 deletions.
  1. +47 −0 src/org/deacon/Deacon.java
  2. +12 −14 src/org/deacon/DeaconService.java
View
@@ -0,0 +1,47 @@
+package org.deacon;
+
+import java.io.IOException;
+import java.net.UnknownHostException;
+
+import android.os.Handler;
+import android.os.Message;
+import android.util.Log;
+
+/**
+ * @author dave
+ *
+ * Deacon class
+ * A thin wrapper around DeaconService which
+ *
+ */
+public class Deacon extends DeaconService {
+
+ private Handler handler = new Handler() {
+ @Override
+ public void handleMessage(final Message msg) {
+ parse((String)msg.obj);
+ }
+ };
+
+ /**
+ * @param host The Meteor server to which this client should connect
+ * @param port The client port that the Meteor server is listening on (default is usually 4670)
+ * @throws UnknownHostException
+ * @throws IOException
+ */
+ public Deacon(String host, int port) throws UnknownHostException, IOException {
+ super(host, port);
+ }
+
+ /**
+ * Android-wrapped version of socketLine; called whenever a line is received from the Meteor server
+ * This version implements Android thread-safe message passing rather than calling parse() directly (from the deaconThread)
+ */
+ @Override
+ protected void socketLine(String line) {
@davidrea

davidrea May 4, 2010

Owner

Not sure why, but Java refused to let me override this method when it was declared 'private' in both the super and sub classes.

+ Message msg = Message.obtain(handler, 0, line);
+ msg.sendToTarget();
+ }
+
+
+}
@@ -10,9 +10,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import android.os.Handler;
-import android.os.Message;
-
public class DeaconService extends DeaconObservable {
private final String host;
@@ -25,13 +22,6 @@
private BufferedReader in = null;
private boolean running=false;
- private Handler handler = new Handler() {
- @Override
- public void handleMessage(final Message msg) {
- parse((String)msg.obj);
- }
- };
-
/**
* Thread to execute Meteor HTTP GETs and collect the results;
* this thread implements the server interaction mode
@@ -45,7 +35,7 @@ public void run() {
try {
sock = new Socket(host, port);
- System.out.println("Opened socket connection");
+// System.out.println("Opened socket connection");
out = new PrintWriter(sock.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(sock.getInputStream()), 1024);
} catch (UnknownHostException e) {
@@ -71,8 +61,7 @@ public void run() {
// Wait for a response from the channel
while( (response=in.readLine()) != null && running) {
// System.out.println("Got response: " + response);
- Message msg = Message.obtain(handler, 0, response);
- msg.sendToTarget();
+ socketLine(response);
}
out.close();
in.close();
@@ -102,6 +91,15 @@ public DeaconService(String host, int port) throws UnknownHostException, IOExcep
}
/**
+ * Called when a new line is received from the Meteor server; enables standalone testing
+ * Overridden by Android-specific wrapper class
+ * @param line
+ */
+ protected void socketLine(String line) {
+ parse(line);
+ }
+
+ /**
* Adds a subscription in the DeaconService to the specified Meteor server channel
* @param String chan The channel name on the Meteor server
* @param int backtrack The number of previously-pushed messages to request upon susbcribing
@@ -175,7 +173,7 @@ public boolean isRunning() {
*
* @param meteorMessage
*/
- private synchronized void parse(String meteorMessage) {
+ protected synchronized void parse(String meteorMessage) {
// TODO This is probably well-suited to a command pattern
// Tried to implement this but figured we should get it working for POC first, then refactor
// System.out.println("DeaconService.parse: Got meteorMessage="+meteorMessage);

1 comment on commit 117f758

Owner

davidrea commented on 117f758 May 4, 2010

This commit implements a thin wrapper around DeaconService, that isolates the Android specific code. It maintains our flexibiltiy to test Deacon standalone (by instantiating DeaconService directly) or within the bounds of an Android app (by instantiating the Deacon wrapper subclass). If everybody likes the way this came out, I will merge the uses-header branch into the master branch.

Please sign in to comment.