Skip to content
This repository has been archived by the owner on Aug 13, 2019. It is now read-only.

Commit

Permalink
Added Deacon wrapper class (again) that isolates Android-specific cod…
Browse files Browse the repository at this point in the history
…e in order to enable standalone testing of Deacon without and Android instance
  • Loading branch information
davidrea committed May 4, 2010
1 parent 6daab84 commit 117f758
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 14 deletions.
47 changes: 47 additions & 0 deletions src/org/deacon/Deacon.java
@@ -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) {

This comment has been minimized.

Copy link
@davidrea

davidrea May 4, 2010

Author 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();
}


}
26 changes: 12 additions & 14 deletions src/org/deacon/DeaconService.java
Expand Up @@ -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;
Expand All @@ -25,13 +22,6 @@ public class DeaconService extends DeaconObservable {
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
Expand All @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -101,6 +90,15 @@ public DeaconService(String host, int port) throws UnknownHostException, IOExcep
this.subscriptions = new ArrayList<String>();
}

/**
* 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
Expand Down Expand Up @@ -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);
Expand Down

1 comment on commit 117f758

@davidrea
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.