01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse.testutil;
04:
05: import java.net.*;
06: import fitnesse.http.Request;
07: import fitnesse.responders.run.SocketDealer;
08:
09: public class FitSocketReceiver {
10: public static int DEFAULT_SOCKET = 9123;
11:
12: public ServerSocket serverSocket;
13:
14: public Socket socket;
15:
16: public int port = DEFAULT_SOCKET;
17:
18: public SocketDealer dealer;
19:
20: public FitSocketReceiver(int port, SocketDealer dealer) {
21: this .port = port;
22: this .dealer = dealer;
23: }
24:
25: public void receiveSocket() throws Exception {
26: serverSocket = new ServerSocket(port);
27: new Thread() {
28: public void run() {
29: try {
30: socket = serverSocket.accept();
31: serverSocket.close();
32: Request request = new Request(socket
33: .getInputStream());
34: request.parse();
35:
36: int ticket = Integer.parseInt(request.getInput(
37: "ticket").toString());
38: dealSocket(ticket);
39: } catch (SocketException se) {
40: } catch (Exception e) {
41: e.printStackTrace();
42: }
43: }
44: }.start();
45: }
46:
47: protected void dealSocket(int ticket) throws Exception {
48: dealer.dealSocketTo(ticket, new SimpleSocketDoner(socket));
49: }
50:
51: public void close() throws Exception {
52: if (serverSocket != null)
53: serverSocket.close();
54: if (socket != null)
55: socket.close();
56: }
57: }
|