001: // Modified or written by Object Mentor, Inc. for inclusion with FitNesse.
002: // Copyright (c) 2002 Cunningham & Cunningham, Inc.
003: // Released under the terms of the GNU General Public License version 2 or later.
004: package fit;
005:
006: import java.io.*;
007: import java.net.Socket;
008: import fitnesse.util.StreamReader;
009: import fitnesse.components.*;
010: import fit.exception.FitParseException;
011:
012: public class FitServer {
013: public String input;
014: public Fixture fixture = new Fixture();
015: public FixtureListener fixtureListener = new TablePrintingFixtureListener();
016: private Counts counts = new Counts();
017: private OutputStream socketOutput;
018: private StreamReader socketReader;
019: private boolean verbose = false;
020: private String host;
021: private int port;
022: private int socketToken;;
023: private Socket socket;
024:
025: public FitServer(String host, int port, boolean verbose) {
026: this .host = host;
027: this .port = port;
028: this .verbose = verbose;
029: }
030:
031: public FitServer() {
032: }
033:
034: public static void main(String argv[]) throws Exception {
035: FitServer fitServer = new FitServer();
036: fitServer.run(argv);
037: System.exit(fitServer.exitCode());
038: }
039:
040: public void run(String argv[]) throws Exception {
041: args(argv);
042: establishConnection();
043: validateConnection();
044: process();
045: closeConnection();
046: exit();
047: }
048:
049: public void closeConnection() throws IOException {
050: socket.close();
051: }
052:
053: public void process() {
054: fixture.listener = fixtureListener;
055: try {
056: int size = 1;
057: while ((size = FitProtocol.readSize(socketReader)) != 0) {
058: try {
059: print("processing document of size: " + size + "\n");
060: String document = FitProtocol.readDocument(
061: socketReader, size);
062: //TODO MDM if the page name was always the first line of the body, it could be printed here.
063: Parse tables = new Parse(document);
064: newFixture().doTables(tables);
065: print("\tresults: " + fixture.counts() + "\n");
066: counts.tally(fixture.counts);
067: } catch (FitParseException e) {
068: exception(e);
069: }
070: }
071: print("completion signal recieved" + "\n");
072: } catch (Exception e) {
073: exception(e);
074: }
075: }
076:
077: public String readDocument() throws Exception {
078: int size = FitProtocol.readSize(socketReader);
079: return FitProtocol.readDocument(socketReader, size);
080: }
081:
082: protected Fixture newFixture() {
083: fixture = new Fixture();
084: fixture.listener = fixtureListener;
085: return fixture;
086: }
087:
088: public void args(String[] argv) {
089: CommandLine commandLine = new CommandLine(
090: "[-v] host port socketToken");
091: if (commandLine.parse(argv)) {
092: host = commandLine.getArgument("host");
093: port = Integer.parseInt(commandLine.getArgument("port"));
094: socketToken = Integer.parseInt(commandLine
095: .getArgument("socketToken"));
096: verbose = commandLine.hasOption("v");
097: } else
098: usage();
099: }
100:
101: private void usage() {
102: System.out
103: .println("usage: java fit.FitServer [-v] host port socketTicket");
104: System.out.println("\t-v\tverbose");
105: System.exit(-1);
106: }
107:
108: protected void exception(Exception e) {
109: print("Exception occurred!" + "\n");
110: print("\t" + e.getMessage() + "\n");
111: Parse tables = new Parse("span", "Exception occurred: ", null,
112: null);
113: fixture.exception(tables, e);
114: counts.exceptions += 1;
115: fixture.listener.tableFinished(tables);
116: fixture.listener.tablesFinished(counts); //TODO shouldn't this be fixture.counts
117: }
118:
119: public void exit() throws Exception {
120: print("exiting" + "\n");
121: print("\tend results: " + counts.toString() + "\n");
122: }
123:
124: public int exitCode() {
125: return counts.wrong + counts.exceptions;
126: }
127:
128: public void establishConnection() throws Exception {
129: establishConnection(makeHttpRequest());
130: }
131:
132: public void establishConnection(String httpRequest)
133: throws Exception {
134: socket = new Socket(host, port);
135: socketOutput = socket.getOutputStream();
136: socketReader = new StreamReader(socket.getInputStream());
137: byte[] bytes = httpRequest.getBytes("UTF-8");
138: socketOutput.write(bytes);
139: socketOutput.flush();
140: print("http request sent" + "\n");
141: }
142:
143: private String makeHttpRequest() {
144: return "GET /?responder=socketCatcher&ticket=" + socketToken
145: + " HTTP/1.1\r\n\r\n";
146: }
147:
148: public void validateConnection() throws Exception {
149: print("validating connection...");
150: int statusSize = FitProtocol.readSize(socketReader);
151: if (statusSize == 0)
152: print("...ok" + "\n");
153: else {
154: String errorMessage = FitProtocol.readDocument(
155: socketReader, statusSize);
156: print("...failed because: " + errorMessage + "\n");
157: System.out
158: .println("An error occured while connecting to client.");
159: System.out.println(errorMessage);
160: System.exit(-1);
161: }
162: }
163:
164: public Counts getCounts() {
165: return counts;
166: }
167:
168: private void print(String message) {
169: if (verbose)
170: System.out.print(message);
171: }
172:
173: public static byte[] readTable(Parse table) throws Exception {
174: ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
175: OutputStreamWriter streamWriter = new OutputStreamWriter(
176: byteBuffer, "UTF-8");
177: PrintWriter writer = new PrintWriter(streamWriter);
178: Parse more = table.more;
179: table.more = null;
180: if (table.trailer == null)
181: table.trailer = "";
182: table.print(writer);
183: table.more = more;
184: writer.close();
185: return byteBuffer.toByteArray();
186: }
187:
188: public void writeCounts(Counts count) throws IOException {
189: //TODO This can't be right.... which counts should be used?
190: FitProtocol.writeCounts(counts, socketOutput);
191: }
192:
193: class TablePrintingFixtureListener implements FixtureListener {
194: public void tableFinished(Parse table) {
195: try {
196: byte[] bytes = readTable(table);
197: if (bytes.length > 0)
198: FitProtocol.writeData(bytes, socketOutput);
199: } catch (Exception e) {
200: e.printStackTrace();
201: }
202: }
203:
204: public void tablesFinished(Counts count) {
205: try {
206: FitProtocol.writeCounts(count, socketOutput);
207: } catch (IOException e) {
208: e.printStackTrace();
209: }
210: }
211: }
212: }
|