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