Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moves dispatching functionality out of Fixture class into a Dispatcher class #281

Merged
merged 5 commits into from
Sep 3, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/eg/bowling/fixtures/ScoreGameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@ public void test() throws Exception {
"</tr>" +
"</table>");
sg.doTable(table);
Assert.assertEquals("10 right, 0 wrong, 0 ignored, 0 exceptions",sg.counts());
Assert.assertEquals("10 right, 0 wrong, 0 ignored, 0 exceptions",sg.counts.toString());
}
}
121 changes: 121 additions & 0 deletions src/fit/Dispatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package fit;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import fit.exception.FitFailureException;


public final class Dispatcher {
public Counts counts;
public final Map<String, Object> summary;

private final FixtureListener listener;

private static boolean forcedAbort = false; //Semaphores


public static class RunTime {
long start = System.currentTimeMillis();
long elapsed = 0;

public String toString() {
elapsed = System.currentTimeMillis() - start;
if (elapsed > 600000) {
return d(3600000) + ":" + d(600000) + d(60000) + ":" + d(10000) + d(1000);
} else {
return d(60000) + ":" + d(10000) + d(1000) + "." + d(100) + d(10);
}
}

String d(long scale) {
long report = elapsed / scale;
elapsed -= report * scale;
return Long.toString(report);
}
}

public Dispatcher(FixtureListener listener) {
counts = new Counts();
summary = new HashMap<String, Object>();
this.listener = listener;
}

public Dispatcher() {
this(new NullFixtureListener());
}

public static void setForcedAbort(boolean state) {
forcedAbort = state;
} //Semaphores

public void doTables(Parse tables) {
summary.put("run date", new Date());
summary.put("run elapsed time", new RunTime());
counts = new Counts();
while (tables != null) {
processTable(tables);
tables = tables.more;
}
listener.tablesFinished(counts);
cleanup();
}

protected void cleanup() {
Fixture.ClearSymbols();
SemaphoreFixture.ClearSemaphores(); //Semaphores: clear all at end
}

private void processTable(Parse table) {
Parse heading = table.at(0, 0, 0);
if (forcedAbort) {
ignore(heading); //Semaphores: ignore on failed lock
} else if (heading != null) {
try {
Fixture fixture = getLinkedFixtureWithArgs(table);
fixture.doTable(table);
} catch (Throwable e) {
exception(heading, e);
} finally {
listener.tableFinished(table);
}
}
}

private void ignore(Parse cell) {
cell.addToTag(" class=\"ignore\"");
counts.ignores++;
}

private Fixture getLinkedFixtureWithArgs(Parse tables) throws Throwable {
Parse header = tables.at(0, 0, 0);
Fixture fixture = Fixture.loadFixture(header.text());
fixture.counts = counts;
fixture.summary = summary;
fixture.getArgsForTable(tables);
return fixture;
}

public void exception(Parse cell, Throwable exception) {
while (exception.getClass().equals(InvocationTargetException.class)) {
exception = ((InvocationTargetException) exception).getTargetException();
}
if (isFriendlyException(exception)) {
cell.addToBody("<hr/>" + Fixture.label(exception.getMessage()));
} else {
final StringWriter buf = new StringWriter();
exception.printStackTrace(new PrintWriter(buf));
cell.addToBody("<hr><pre><div class=\"fit_stacktrace\">" + (buf.toString()) + "</div></pre>");
}
cell.addToTag(" class=\"error\"");
counts.exceptions++;
}

private boolean isFriendlyException(Throwable exception) {
return exception instanceof FitFailureException;
}
}
22 changes: 12 additions & 10 deletions src/fit/FileRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
public class FileRunner {

public String input;
public Parse tables;
public Fixture fixture = new Fixture();
public PrintWriter output;

protected Parse tables;
protected Dispatcher dispatcher = new Dispatcher();
protected PrintWriter output;


public static void main(String argv[]) {
new FileRunner().run(argv);
}
Expand All @@ -34,7 +36,7 @@ public void run(String argv[]) {
public void process() {
try {
tables = new Parse(input);
fixture.doTables(tables);
dispatcher.doTables(tables);
} catch (Exception e) {
exception(e);
}
Expand All @@ -48,9 +50,9 @@ public void args(String[] argv) {
}
File in = new File(argv[0]);
File out = new File(argv[1]);
fixture.summary.put("input file", in.getAbsolutePath());
fixture.summary.put("input update", new Date(in.lastModified()));
fixture.summary.put("output file", out.getAbsolutePath());
dispatcher.summary.put("input file", in.getAbsolutePath());
dispatcher.summary.put("input update", new Date(in.lastModified()));
dispatcher.summary.put("output file", out.getAbsolutePath());
try {
input = read(in);
output = new PrintWriter(new BufferedWriter(new FileWriter(out)));
Expand All @@ -70,13 +72,13 @@ protected String read(File input) throws IOException {

protected void exception(Exception e) {
tables = new Parse("body", "Unable to parse input. Input ignored.", null, null);
fixture.exception(tables, e);
dispatcher.exception(tables, e);
}

protected void exit() {
output.close();
System.err.println(fixture.counts());
System.exit(fixture.counts.wrong + fixture.counts.exceptions);
System.err.println(dispatcher.counts.toString());
System.exit(dispatcher.counts.wrong + dispatcher.counts.exceptions);
}

}
103 changes: 41 additions & 62 deletions src/fit/FitServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
import util.FileUtil;
import fit.exception.FitParseException;


public class FitServer {
public String input;
public Fixture fixture = new Fixture();
public FixtureListener fixtureListener = new TablePrintingFixtureListener();
private Counts counts = new Counts();
private final Dispatcher dispatcher;
private final Counts overallCounts;
private final FixtureListener fixtureListener;

private OutputStream socketOutput;
private StreamReader socketReader;
private boolean verbose = false;
Expand All @@ -28,12 +29,16 @@ public class FitServer {
private boolean sentinel;

public FitServer(String host, int port, boolean verbose) {
this();
Copy link
Contributor

Choose a reason for hiding this comment

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

indentation issue here

this.host = host;
this.port = port;
this.verbose = verbose;
}

public FitServer() {
fixtureListener = new TablePrintingFixtureListener();
dispatcher = new Dispatcher(fixtureListener);
overallCounts = new Counts();
}

public static void main(String argv[]) throws Exception {
Expand All @@ -60,6 +65,25 @@ public void run(String argv[]) throws Exception {
exit();
}

public void args(String[] argv) {
CommandLine commandLine = new CommandLine("[-v][-x][-s] host port socketToken");
if (commandLine.parse(argv)) {
host = commandLine.getArgument("host");
port = Integer.parseInt(commandLine.getArgument("port"));
socketToken = Integer.parseInt(commandLine.getArgument("socketToken"));
verbose = commandLine.hasOption("v");
noExit = commandLine.hasOption("x");
sentinel = commandLine.hasOption("s");
} else
usage();
}

private void usage() {
System.out.println("usage: java fit.FitServer [-v] host port socketTicket");
System.out.println("\t-v\tverbose");
System.exit(-1);
}

public static String sentinelName(int thePort) {
return String.format("fitserverSentinel%d", thePort);
}
Expand All @@ -69,7 +93,6 @@ public void closeConnection() throws IOException {
}

public void process() {
fixture.listener = fixtureListener;
try {
int size = 1;
while ((size = FitProtocol.readSize(socketReader)) != 0) {
Expand All @@ -78,68 +101,36 @@ public void process() {
String document = FitProtocol.readDocument(socketReader, size);
//TODO MDM if the page name was always the first line of the body, it could be printed here.
Parse tables = new Parse(document);
newFixture().doTables(tables);
print("\tresults: " + fixture.counts() + "\n");
counts.tally(fixture.counts);
}
catch (FitParseException e) {
dispatcher.doTables(tables);
print("\tresults: " + dispatcher.counts.toString() + "\n");
overallCounts.tally(dispatcher.counts);
} catch (FitParseException e) {
exception(e);
}
}
print("completion signal recieved" + "\n");
}
catch (Exception e) {
} catch (Exception e) {
exception(e);
}
}

public String readDocument() throws Exception {
int size = FitProtocol.readSize(socketReader);
return FitProtocol.readDocument(socketReader, size);
}

protected Fixture newFixture() {
fixture = new Fixture();
fixture.listener = fixtureListener;
return fixture;
}

public void args(String[] argv) {
CommandLine commandLine = new CommandLine("[-v][-x][-s] host port socketToken");
if (commandLine.parse(argv)) {
host = commandLine.getArgument("host");
port = Integer.parseInt(commandLine.getArgument("port"));
socketToken = Integer.parseInt(commandLine.getArgument("socketToken"));
verbose = commandLine.hasOption("v");
noExit = commandLine.hasOption("x");
sentinel = commandLine.hasOption("s");
} else
usage();
}

private void usage() {
System.out.println("usage: java fit.FitServer [-v] host port socketTicket");
System.out.println("\t-v\tverbose");
System.exit(-1);
}

protected void exception(Exception e) {
print("Exception occurred!" + "\n");
print("\t" + e.getMessage() + "\n");
Parse tables = new Parse("span", "Exception occurred: ", null, null);
fixture.exception(tables, e);
counts.exceptions += 1;
fixture.listener.tableFinished(tables);
fixture.listener.tablesFinished(counts); //TODO shouldn't this be fixture.counts
dispatcher.exception(tables, e);
overallCounts.exceptions += 1;
fixtureListener.tableFinished(tables);
fixtureListener.tablesFinished(dispatcher.counts);
}

public void exit() throws Exception {
print("exiting" + "\n");
print("\tend results: " + counts.toString() + "\n");
print("\tend results: " + overallCounts.toString() + "\n");
}

public int exitCode() {
return counts.wrong + counts.exceptions;
return overallCounts.wrong + overallCounts.exceptions;
}

public void establishConnection() throws Exception {
Expand Down Expand Up @@ -174,10 +165,6 @@ public void validateConnection() throws Exception {
}
}

public Counts getCounts() {
return counts;
}

private void print(String message) {
if (verbose)
System.out.print(message);
Expand All @@ -197,31 +184,23 @@ public static byte[] readTable(Parse table) throws Exception {
return byteBuffer.toByteArray();
}

public void writeCounts(Counts count) throws IOException {
//TODO This can't be right.... which counts should be used?
FitProtocol.writeCounts(counts, socketOutput);
}

class TablePrintingFixtureListener implements FixtureListener {
public void tableFinished(Parse table) {
try {
byte[] bytes = readTable(table);
if (bytes.length > 0)
FitProtocol.writeData(bytes, socketOutput);
}
catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
}

public void tablesFinished(Counts count) {
try {
FitProtocol.writeCounts(count, socketOutput);
}
catch (IOException e) {
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

Loading