]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/Request.java
32977e0aa6ef640c515d5a4e9740388020ab8e14
[OpenSRF.git] / src / java / org / opensrf / Request.java
1 package org.opensrf;
2 import java.util.Queue;
3 import java.util.concurrent.ConcurrentLinkedQueue;
4 import java.util.List;
5 import java.util.Date;
6 import org.opensrf.net.xmpp.XMPPException;
7 import org.opensrf.util.Logger;
8
9 public class Request {
10     
11     /** This request's controlling session */
12     private ClientSession session;
13     /** The method */
14     private Method method;
15     /** The ID of this request */
16     private int id;
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;
21
22     /** If true, the server has indicated that this request is complete. */
23     private boolean complete;
24
25     /**
26      * @param ses The controlling session for this request.
27      * @param id This request's ID.
28      * @param method The requested method.
29      */
30     public Request(ClientSession ses, int id, Method method) {
31         this.session = ses;
32         this.id = id;
33         this.method = method;
34         resultQueue = new ConcurrentLinkedQueue<Result>();
35         complete = false;
36         resetTimeout = false;
37     }
38
39     /**
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.
43      */
44     public Request(ClientSession ses, int id, String methodName) {
45         this(ses, id, new Method(methodName));
46     }
47
48     /**
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
53      */
54     public Request(ClientSession ses, int id, String methodName, List<Object> params) {
55         this(ses, id, new Method(methodName, params));
56     }
57
58     /**
59      * Sends the request to the server.
60      */
61     public void send() throws SessionException {
62         session.send(new Message(id, Message.REQUEST, method));
63     }
64
65     /**
66      * Receives the next result for this request.  This method
67      * will wait up to the specified number of milliseconds for 
68      * a response. 
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
72      */
73     public Result recv(long millis) throws SessionException, MethodException {
74
75         Result result = null;
76
77         if((result = resultQueue.poll()) != null)
78             return result;
79
80         if(millis < 0 && !complete) {
81             /** wait potentially forever for a result to arrive */
82             while(!complete) {
83                 session.waitForMessage(millis);
84                 if((result = resultQueue.poll()) != null)
85                     return result;
86             }
87
88         } else {
89
90             while(millis >= 0 && !complete) {
91
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 
95                  * this request */
96
97                 long start = new Date().getTime();
98                 session.waitForMessage(millis);
99                 millis -= new Date().getTime() - start;
100                 if((result = resultQueue.poll()) != null)
101                     return result;
102             }
103         }
104
105         return null;
106     }
107
108     /**
109      * Pushes a result onto the result queue 
110      * @param result The result to push
111      */
112     public void pushResponse(Result result) {
113         resultQueue.offer(result);
114     }
115
116     /**
117      * @return This request's ID
118      */
119     public int getId() {
120         return id;
121     }
122
123     /**
124      * Removes this request from the controlling session's request set
125      */
126     public void cleanup() {
127         session.cleanupRequest(id);
128     }
129
130     /** Sets this request as complete */
131     public void setComplete() {
132         complete = true;
133     }
134
135     public boolean isComplete() {
136         return complete;
137     }
138 }