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: private SocketDealer dealer;
14: private Socket socket;
15: private ResponseSender sender;
16: private PuppetResponse response;
17:
18: public Response makeResponse(FitNesseContext context,
19: Request request) throws Exception {
20: dealer = context.socketDealer;
21: ticketNumber = Integer.parseInt(request.getInput("ticket")
22: .toString());
23: response = new PuppetResponse(this );
24: return response;
25: }
26:
27: public void readyToSend(ResponseSender sender) throws Exception {
28: socket = sender.getSocket();
29: this .sender = sender;
30: if (dealer.isWaiting(ticketNumber))
31: dealer.dealSocketTo(ticketNumber, this );
32: else {
33: String errorMessage = "There are no clients waiting for a socket with ticketNumber "
34: + ticketNumber;
35: FitProtocol.writeData(errorMessage, socket
36: .getOutputStream());
37: response.setStatus(404);
38: sender.close();
39: }
40: }
41:
42: public Socket donateSocket() {
43: return socket;
44: }
45:
46: public void finishedWithSocket() throws Exception {
47: sender.close();
48: }
49: }
|