]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/ClientSession.java
27935aba6f5e1dd253ed45d8f55dd89aa0d58d25
[OpenSRF.git] / src / java / org / opensrf / ClientSession.java
1 package org.opensrf;
2 import java.util.Date;
3 import java.util.List;
4 import java.util.Map;
5 import java.util.HashMap;
6 import java.util.ArrayList;
7 import java.util.Random;
8 import java.util.Arrays;
9
10 import org.opensrf.util.*;
11 import org.opensrf.net.xmpp.*;
12
13
14 /**
15  * Models an OpenSRF client session.
16  */
17 public class ClientSession extends Session {
18
19     /** The remote service to communicate with */
20     private String service;
21     /** OpenSRF domain */
22     private String domain;
23     /** Router name */
24     private String router;
25
26     /** 
27      * original remote node.  The current remote node will change based 
28      * on server responses.  This is used to reset the remote node to 
29      * its original state.
30      */
31     private String origRemoteNode;
32     /** The next request id */
33     private int nextId;
34     /** The requests this session has sent */
35     private Map<Integer, Request> requests;
36
37     /**
38      * Creates a new client session.  Initializes the 
39      * @param service The remove service.
40      */
41     public ClientSession(String service) throws ConfigException {
42         this.service = service;
43
44         /** generate the remote node string */
45         domain = (String) Config.getFirst("/domains/domain");
46         router = Config.getString("/router_name");
47         setRemoteNode(router + "@" + domain + "/" + service);
48         origRemoteNode = getRemoteNode();
49
50
51         /** create a random thread */
52         long time = new Date().getTime();
53         Random rand = new Random(time);
54         setThread(rand.nextInt()+""+rand.nextInt()+""+time);
55
56         nextId = 0;
57         requests = new HashMap<Integer, Request>();
58         cacheSession();
59     }
60
61     /**
62      * Creates a new request to send to our remote service.
63      * @param method The method API name
64      * @param params The list of method parameters
65      * @return The request object.
66      */
67     public Request request(String method, List<Object> params) throws SessionException {
68         return request(new Request(this, nextId++, method, params));
69     }
70
71     /**
72      * Creates a new request to send to our remote service.
73      * @param method The method API name
74      * @param params The list of method parameters
75      * @return The request object.
76      */
77     public Request request(String method, Object[] params) throws SessionException {
78         return request(new Request(this, nextId++, method, Arrays.asList(params)));
79     }
80
81
82     /**
83      * Creates a new request to send to our remote service.
84      * @param method The method API name
85      * @return The request object.
86      */
87     public Request request(String method) throws SessionException {
88         return request(new Request(this, nextId++, method));
89     }
90
91
92     private Request request(Request req) throws SessionException {
93         if(getConnectState() != ConnectState.CONNECTED)
94             resetRemoteId();
95         requests.put(new Integer(req.getId()), req);
96         req.send();
97         return req;
98     }
99
100
101     /**
102      * Resets the remoteNode to its original state.
103      */
104     public void resetRemoteId() {
105         setRemoteNode(origRemoteNode);
106     }
107
108
109     /**
110      * Pushes a response onto the result queue of the appropriate request.
111      * @param msg The received RESULT Message
112      */
113     public void pushResponse(Message msg) {
114
115         Request req = findRequest(msg.getId());
116         if(req == null) {
117             /** LOG that we've received a result to a non-existant request */
118             return;
119         }
120         OSRFObject payload = (OSRFObject) msg.get("payload");
121
122         /** build a result and push it onto the request's result queue */
123         req.pushResponse(
124             new Result( 
125                 payload.getString("status"), 
126                 payload.getInt("statusCode"),
127                 payload.get("content")
128             )
129         );
130     }
131
132     public Request findRequest(int reqId) {
133         return requests.get(new Integer(reqId));
134     }
135
136     /**
137      * Removes a request for this session's request set
138      */
139     public void cleanupRequest(int reqId) {
140         requests.remove(new Integer(reqId));
141     }
142
143      public void setRequestComplete(int reqId) {
144         Request req = findRequest(reqId);
145         if(req == null) return;
146         req.setComplete();
147     }
148
149
150     /**
151      * Connects to the OpenSRF network so that client sessions may communicate.
152      * @param configFile The OpenSRF config file 
153      * @param configContext Where in the XML document the config chunk lives.  This
154      * allows an OpenSRF client config chunk to live in XML files where other config
155      * information lives.
156      */
157     /*
158     public static void bootstrap(String configFile, String configContext) 
159             throws ConfigException, SessionException  {
160
161         Config config = new Config(configContext);
162         config.parse(configFile);
163         Config.setConfig(config);
164
165         String username = Config.getString("/username");
166         String passwd = Config.getString("/passwd");
167         String host = (String) Config.getFirst("/domains/domain");
168         int port = Config.getInt("/port");
169
170         try {
171             XMPPSession xses = new XMPPSession(host, port);
172             xses.connect(username, passwd, "test-java");
173             XMPPSession.setGlobalSession(xses);
174         } catch(XMPPException e) {
175             throw new SessionException("Unable to bootstrap client", e);
176         }
177     }
178     */
179 }
180