2 import java.util.Queue;
3 import java.util.concurrent.ConcurrentLinkedQueue;
6 import org.opensrf.net.xmpp.XMPPException;
7 import org.opensrf.util.Logger;
11 /** This request's controlling session */
12 private ClientSession session;
14 private Method method;
15 /** The ID of this request */
17 /** Queue of Results */
18 private Queue<Result> resultQueue;
19 /** If true, the receive timeout for this request should be reset */
20 private boolean resetTimeout;
22 /** If true, the server has indicated that this request is complete. */
23 private boolean complete;
26 * @param ses The controlling session for this request.
27 * @param id This request's ID.
28 * @param method The requested method.
30 public Request(ClientSession ses, int id, Method method) {
34 resultQueue = new ConcurrentLinkedQueue<Result>();
40 * @param ses The controlling session for this request.
41 * @param id This request's ID.
42 * @param methodName The requested method's API name.
44 public Request(ClientSession ses, int id, String methodName) {
45 this(ses, id, new Method(methodName));
49 * @param ses The controlling session for this request.
50 * @param id This request's ID.
51 * @param methodName The requested method's API name.
52 * @param params The list of request params
54 public Request(ClientSession ses, int id, String methodName, List<Object> params) {
55 this(ses, id, new Method(methodName, params));
59 * Sends the request to the server.
61 public void send() throws SessionException {
62 session.send(new Message(id, Message.REQUEST, method, session.getLocale()));
66 * Receives the next result for this request. This method
67 * will wait up to the specified number of milliseconds for
69 * @param millis Number of milliseconds to wait for a result. If
70 * negative, this method will wait indefinitely.
71 * @return The result or null if none arrives in time
73 public Result recv(long millis) throws SessionException, MethodException {
77 if((result = resultQueue.poll()) != null)
80 if(millis < 0 && !complete) {
81 /** wait potentially forever for a result to arrive */
83 session.waitForMessage(millis);
84 if((result = resultQueue.poll()) != null)
90 while(millis >= 0 && !complete) {
92 /** wait up to millis milliseconds for a result. waitForMessage()
93 * will return if a response to any request arrives, so we keep track
94 * of how long we've been waiting in total for a response to
97 long start = new Date().getTime();
98 session.waitForMessage(millis);
99 millis -= new Date().getTime() - start;
100 if((result = resultQueue.poll()) != null)
109 * Pushes a result onto the result queue
110 * @param result The result to push
112 public void pushResponse(Result result) {
113 resultQueue.offer(result);
117 * @return This request's ID
124 * Removes this request from the controlling session's request set
126 public void cleanup() {
127 session.cleanupRequest(id);
130 /** Sets this request as complete */
131 public void setComplete() {
135 public boolean isComplete() {