01: package example;
02:
03: import com.caucho.util.L10N;
04: import java.util.logging.Logger;
05: import java.util.logging.Level;
06:
07: import com.caucho.vfs.*;
08: import com.caucho.server.port.ServerRequest;
09: import com.caucho.server.connection.Connection;
10: import java.io.IOException;
11:
12: /**
13: * Protocol specific information for each request. An instance of this
14: * object may be reused to reduce memory allocations.
15: */
16: public class Magic8BallRequest implements ServerRequest {
17: static protected final Logger log = Logger
18: .getLogger(Magic8BallRequest.class.getName());
19: static final L10N L = new L10N(Magic8BallRequest.class);
20:
21: Connection _conn;
22: Magic8BallProtocol _protocol;
23:
24: // the parser is reset for each request
25: Parser _parser = new Parser();
26:
27: Magic8Ball _magic8ball = new Magic8Ball();
28:
29: /**
30: *
31: */
32: public Magic8BallRequest(Magic8BallProtocol protocol,
33: Connection conn) {
34: _protocol = protocol;
35: _conn = conn;
36: }
37:
38: /**
39: * Initialize the connection. At this point, the current thread is the
40: * connection thread.
41: */
42: public void init() {
43: }
44:
45: /**
46: * Handle a new connection. The controlling Server may call
47: * handleRequest again after the connection completes, so the
48: * implementation must initialize any variables for each connection.
49: */
50: public boolean handleRequest() throws IOException {
51: ReadStream readStream = _conn.getReadStream();
52: WriteStream writeStream = _conn.getWriteStream();
53:
54: try {
55: _parser.init(readStream);
56:
57: AbstractCommand cmd = null;
58: do {
59: String result = null;
60: String error = null;
61:
62: cmd = _parser.nextCommand();
63: if (_parser.isError()) {
64: error = _parser.getError();
65: } else if (cmd != null) {
66: result = cmd.act(_magic8ball);
67: if (cmd.isError())
68: error = cmd.getError();
69: }
70:
71: if (error != null) {
72: writeStream.print("ERROR: ");
73: writeStream.println(_parser.getError());
74: break;
75: } else if (result != null) {
76: writeStream.print("RESULT: ");
77: writeStream.println(result);
78: }
79: } while (cmd != null);
80: } catch (Throwable e) {
81: log.log(Level.WARNING, e.toString(), e);
82: }
83:
84: return false;
85: }
86: }
|