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, MethodException {
76 if((result = resultQueue.poll()) != null)
79 if(millis < 0 && !complete) {
80 /** wait potentially forever for a result to arrive */
82 session.waitForMessage(millis);
83 if((result = resultQueue.poll()) != null)
89 while(millis >= 0 && !complete) {
91 /** wait up to millis milliseconds for a result. waitForMessage()
92 * will return if a response to any request arrives, so we keep track
93 * of how long we've been waiting in total for a response to
96 long start = new Date().getTime();
97 session.waitForMessage(millis);
98 millis -= new Date().getTime() - start;
99 if((result = resultQueue.poll()) != null)
108 * Pushes a result onto the result queue
109 * @param result The result to push
111 public void pushResponse(Result result) {
112 resultQueue.offer(result);
116 * @return This request's ID
123 * Removes this request from the controlling session's request set
125 public void cleanup() {
126 session.cleanupRequest(id);
129 /** Sets this request as complete */
130 public void setComplete() {