]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/Request.java
122299995963562579f20cece6f169e92d923297
[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 {
73
74         Result result = null;
75
76         if(millis < 0 && !complete) {
77             /** wait potentially forever for a result to arrive */
78             session.waitForMessage(millis);
79             if((result = resultQueue.poll()) != null)
80                 return result;
81
82         } else {
83
84             while(millis >= 0 && !complete) {
85
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 
89                  * this request
90                  */
91                 long start = new Date().getTime();
92                 session.waitForMessage(millis);
93                 millis -= new Date().getTime() - start;
94                 if((result = resultQueue.poll()) != null)
95                     return result;
96             }
97         }
98
99         return null;
100     }
101
102     /**
103      * Pushes a result onto the result queue 
104      * @param result The result to push
105      */
106     public void pushResponse(Result result) {
107         resultQueue.offer(result);
108     }
109
110     /**
111      * @return This request's ID
112      */
113     public int getId() {
114         return id;
115     }
116
117     /**
118      * Removes this request from the controlling session's request set
119      */
120     public void cleanup() {
121         session.cleanupRequest(id);
122     }
123
124     /** Sets this request as complete */
125     public void setComplete() {
126         complete = true;
127     }
128 }