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.responders.run;
04:
05: import fitnesse.*;
06: import fitnesse.components.*;
07: import fitnesse.http.*;
08: import java.net.Socket;
09:
10: public class SocketCatchingResponder implements Responder, SocketDoner,
11: ResponsePuppeteer {
12: private int ticketNumber;
13:
14: private SocketDealer dealer;
15:
16: private Socket socket;
17:
18: private ResponseSender sender;
19:
20: private PuppetResponse response;
21:
22: public Response makeResponse(FitNesseContext context,
23: Request request) throws Exception {
24: dealer = context.socketDealer;
25: ticketNumber = Integer.parseInt(request.getInput("ticket")
26: .toString());
27: response = new PuppetResponse(this );
28: return response;
29: }
30:
31: public void readyToSend(ResponseSender sender) throws Exception {
32: socket = sender.getSocket();
33: this .sender = sender;
34: if (dealer.isWaiting(ticketNumber))
35: dealer.dealSocketTo(ticketNumber, this );
36: else {
37: String errorMessage = "There are no clients waiting for a socket with ticketNumber "
38: + ticketNumber;
39: FitProtocol.writeData(errorMessage, socket
40: .getOutputStream());
41: response.setStatus(404);
42: sender.close();
43: }
44: }
45:
46: public Socket donateSocket() {
47: return socket;
48: }
49:
50: public void finishedWithSocket() throws Exception {
51: sender.close();
52: }
53: }
|