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