5 import java.util.HashMap;
6 import java.util.ArrayList;
7 import java.util.Random;
8 import java.util.Arrays;
10 import org.opensrf.util.*;
11 import org.opensrf.net.xmpp.*;
15 * Models an OpenSRF client session.
17 public class ClientSession extends Session {
19 /** The remote service to communicate with */
20 private String service;
22 private String domain;
24 private String router;
27 * original remote node. The current remote node will change based
28 * on server responses. This is used to reset the remote node to
31 private String origRemoteNode;
32 /** The next request id */
34 /** The requests this session has sent */
35 private Map<Integer, Request> requests;
38 * Creates a new client session. Initializes the
39 * @param service The remote service.
41 public ClientSession(String service) throws ConfigException {
46 * Creates a new client session. Initializes the
47 * @param service The remote service.
48 * @param locale The locale for this session.
50 public ClientSession(String service, String locale) throws ConfigException {
51 this.service = service;
55 /** generate the remote node string */
56 domain = (String) Config.global().getFirst("/domain");
57 router = Config.global().getString("/router_name");
58 setRemoteNode(router + "@" + domain + "/" + service);
59 origRemoteNode = getRemoteNode();
62 /** create a random thread */
63 long time = new Date().getTime();
64 Random rand = new Random(time);
65 setThread(rand.nextInt()+""+rand.nextInt()+""+time+Thread.currentThread().getId());
68 requests = new HashMap<Integer, Request>();
73 * Creates a new request to send to our remote service.
74 * @param method The method API name
75 * @param params The list of method parameters
76 * @return The request object.
78 public Request request(String method, List<Object> params) throws SessionException {
79 return request(new Request(this, nextId++, method, params));
83 * Creates a new request to send to our remote service.
84 * @param method The method API name
85 * @param params The list of method parameters
86 * @return The request object.
88 public Request request(String method, Object[] params) throws SessionException {
89 return request(new Request(this, nextId++, method, Arrays.asList(params)));
94 * Creates a new request to send to our remote service.
95 * @param method The method API name
96 * @return The request object.
98 public Request request(String method) throws SessionException {
99 return request(new Request(this, nextId++, method));
103 private Request request(Request req) throws SessionException {
104 if(getConnectState() != ConnectState.CONNECTED)
106 requests.put(new Integer(req.getId()), req);
113 * Resets the remoteNode to its original state.
115 public void resetRemoteId() {
116 setRemoteNode(origRemoteNode);
121 * Pushes a response onto the result queue of the appropriate request.
122 * @param msg The received RESULT Message
124 public void pushResponse(Message msg) {
126 Request req = findRequest(msg.getId());
128 /** LOG that we've received a result to a non-existant request */
129 System.err.println(msg.getId() +" has no corresponding request");
132 OSRFObject payload = (OSRFObject) msg.get("payload");
134 /** build a result and push it onto the request's result queue */
137 payload.getString("status"),
138 payload.getInt("statusCode"),
139 payload.get("content")
144 public Request findRequest(int reqId) {
145 return requests.get(new Integer(reqId));
149 * Removes a request for this session's request set
151 public void cleanupRequest(int reqId) {
152 requests.remove(new Integer(reqId));
155 public void setRequestComplete(int reqId) {
156 Request req = findRequest(reqId);
157 if(req == null) return;
161 public static Object atomicRequest(String service, String method, Object[] params) throws MethodException {
163 ClientSession session = new ClientSession(service);
164 Request osrfRequest = session.request(method, params);
165 Result result = osrfRequest.recv(600000);
166 if(result.getStatusCode() != 200)
167 throw new MethodException(
168 "Request "+service+":"+method+":"+" failed with status code " + result.getStatusCode());
169 return result.getContent();
170 } catch(Exception e) {
171 throw new MethodException(e);