2 import java.util.Queue;
3 import java.util.concurrent.ConcurrentLinkedQueue;
6 import org.opensrf.net.xmpp.XMPPException;
10 /** This request's controlling session */
11 private ClientSession session;
13 private Method method;
14 /** The ID of this request */
16 /** Queue of Results */
17 private Queue<Result> resultQueue;
18 /** If true, the receive timeout for this request should be reset */
19 private boolean resetTimeout;
21 /** If true, the server has indicated that this request is complete. */
22 private boolean complete;
25 * @param ses The controlling session for this request.
26 * @param id This request's ID.
27 * @param method The requested method.
29 public Request(ClientSession ses, int id, Method method) {
33 resultQueue = new ConcurrentLinkedQueue<Result>();
39 * @param ses The controlling session for this request.
40 * @param id This request's ID.
41 * @param methodName The requested method's API name.
43 public Request(ClientSession ses, int id, String methodName) {
44 this(ses, id, new Method(methodName));
48 * @param ses The controlling session for this request.
49 * @param id This request's ID.
50 * @param methodName The requested method's API name.
51 * @param params The list of request params
53 public Request(ClientSession ses, int id, String methodName, List<Object> params) {
54 this(ses, id, new Method(methodName, params));
58 * Sends the request to the server.
60 public void send() throws SessionException {
61 session.send(new Message(id, Message.REQUEST, method));
65 * Receives the next result for this request. This method
66 * will wait up to the specified number of milliseconds for
68 * @param millis Number of milliseconds to wait for a result. If
69 * negative, this method will wait indefinitely.
70 * @return The result or null if none arrives in time
72 public Result recv(long millis) throws SessionException {
77 /** wait potentially forever for a result to arrive */
78 session.waitForMessage(millis);
79 if((result = resultQueue.poll()) != null)
86 /** wait up to millis milliseconds for a result. waitForMessage()
87 * will return if a response to any request arrives, so we keep track
88 * of how long we've been waiting in total for a response to
91 long start = new Date().getTime();
92 session.waitForMessage(millis);
93 millis -= new Date().getTime() - start;
94 if((result = resultQueue.poll()) != null)
103 * Pushes a result onto the result queue
104 * @param result The result to push
106 public void pushResponse(Result result) {
107 resultQueue.offer(result);
111 * @return This request's ID
118 * Removes this request from the controlling session's request set
120 public void cleanup() {
121 session.cleanupRequest(id);