import java.util.Arrays;
import org.opensrf.util.*;
+import org.opensrf.net.xmpp.*;
/**
*/
public void pushResponse(Message msg) {
- Request req = requests.get(new Integer(msg.getId()));
+ Request req = findRequest(msg.getId());
if(req == null) {
/** LOG that we've received a result to a non-existant request */
return;
);
}
+ public Request findRequest(int reqId) {
+ return requests.get(new Integer(reqId));
+ }
+
/**
* Removes a request for this session's request set
*/
public void cleanupRequest(int reqId) {
requests.remove(new Integer(reqId));
}
+
+ public void setRequestComplete(int reqId) {
+ Request req = findRequest(reqId);
+ if(req == null) return;
+ req.setComplete();
+ }
+
+
+ /**
+ * Connects to the OpenSRF network so that client sessions may communicate.
+ * @param configFile The OpenSRF config file
+ * @param configContext Where in the XML document the config chunk lives. This
+ * allows an OpenSRF client config chunk to live in XML files where other config
+ * information lives.
+ */
+ /*
+ public static void bootstrap(String configFile, String configContext)
+ throws ConfigException, SessionException {
+
+ Config config = new Config(configContext);
+ config.parse(configFile);
+ Config.setConfig(config);
+
+ String username = Config.getString("/username");
+ String passwd = Config.getString("/passwd");
+ String host = (String) Config.getFirst("/domains/domain");
+ int port = Config.getInt("/port");
+
+ try {
+ XMPPSession xses = new XMPPSession(host, port);
+ xses.connect(username, passwd, "test-java");
+ XMPPSession.setGlobalSession(xses);
+ } catch(XMPPException e) {
+ throw new SessionException("Unable to bootstrap client", e);
+ }
+ }
+ */
}
Result result = null;
- if(millis < 0) {
+ if(millis < 0 && !complete) {
/** wait potentially forever for a result to arrive */
session.waitForMessage(millis);
if((result = resultQueue.poll()) != null)
} else {
- while(millis >= 0) {
+ while(millis >= 0 && !complete) {
/** wait up to millis milliseconds for a result. waitForMessage()
* will return if a response to any request arrives, so we keep track
public void cleanup() {
session.cleanupRequest(id);
}
+
+ /** Sets this request as complete */
+ public void setComplete() {
+ complete = true;
+ }
}
--- /dev/null
+package org.opensrf;
+/**
+ * Used by sessions to indicate communication errors
+ */
+public class SessionException extends Exception {
+ public SessionException(String info) {
+ super(info);
+ }
+ public SessionException(String info, Throwable cause) {
+ super(info, cause);
+ }
+}
+
* Process a server response
*/
private static void processResponse(ClientSession session, Message msg) {
- if(msg.RESULT.equals(msg.getType())) {
+ String type = msg.getType();
+ if(msg.RESULT.equals(type)) {
session.pushResponse(msg);
return;
}
+
+ if(msg.STATUS.equals(type)) {
+
+ OSRFObject obj = (OSRFObject) msg.getPayload();
+ Status stat = new Status(obj.getString("status"), obj.getInt("statusCode"));
+ int statusCode = stat.getStatusCode();
+ String status = stat.getStatus();
+
+ if(statusCode == stat.COMPLETE) {
+ session.setRequestComplete(msg.getId());
+ }
+ }
}
/**
--- /dev/null
+package org.opensrf;
+import org.opensrf.util.*;
+
+public class Status {
+
+ public static final int CONTINUE = 100;
+ public static final int OK = 200;
+ public static final int ACCEPTED = 202;
+ public static final int COMPLETE = 205;
+ public static final int REDIRECTED = 307;
+ public static final int EST = 400;
+ public static final int STATUS_UNAUTHORIZED = 401;
+ public static final int FORBIDDEN = 403;
+ public static final int NOTFOUND = 404;
+ public static final int NOTALLOWED = 405;
+ public static final int TIMEOUT = 408;
+ public static final int EXPFAILED = 417;
+ public static final int INTERNALSERVERERROR = 500;
+ public static final int NOTIMPLEMENTED = 501;
+ public static final int VERSIONNOTSUPPORTED = 505;
+
+ private OSRFRegistry registry = OSRFRegistry.registerObject(
+ "osrfConnectStatus",
+ OSRFRegistry.WireProtocol.HASH,
+ new String[] {"status", "statusCode"});
+
+ /** The name of the status */
+ String status;
+ /** The status code */
+ int statusCode;
+
+ public Status(String status, int statusCode) {
+ this.status = status;
+ this.statusCode = statusCode;
+ }
+
+ public int getStatusCode() {
+ return statusCode;
+ }
+ public String getStatus() {
+ return status;
+ }
+
+ /**
+ * Implements the generic get() API required by OSRFSerializable
+ */
+ public Object get(String field) {
+ if("status".equals(field))
+ return getStatus();
+ if("statusCode".equals(field))
+ return new Integer(getStatusCode());
+ return null;
+ }
+
+ /**
+ * @return The osrfMessage registry.
+ */
+ public OSRFRegistry getRegistry() {
+ return registry;
+ }
+}
+
+
--- /dev/null
+package org.opensrf;
+
+import org.opensrf.util.*;
+import org.opensrf.net.xmpp.*;
+
+
+public class Sys {
+
+ /**
+ * Connects to the OpenSRF network so that client sessions may communicate.
+ * @param configFile The OpenSRF config file
+ * @param configContext Where in the XML document the config chunk lives. This
+ * allows an OpenSRF client config chunk to live in XML files where other config
+ * information lives.
+ */
+ public static void bootstrapClient(String configFile, String configContext)
+ throws ConfigException, SessionException {
+
+ /** create the config parser */
+ Config config = new Config(configContext);
+ config.parse(configFile);
+ Config.setConfig(config); /* set this as the global config */
+
+ /** Collect the network connection info from the config */
+ String username = Config.getString("/username");
+ String passwd = Config.getString("/passwd");
+ String host = (String) Config.getFirst("/domains/domain");
+ int port = Config.getInt("/port");
+
+ try {
+ /** Connect to the Jabber network */
+ XMPPSession xses = new XMPPSession(host, port);
+ xses.connect(username, passwd, "test-java"); /* XXX */
+ XMPPSession.setGlobalSession(xses);
+ } catch(XMPPException e) {
+ throw new SessionException("Unable to bootstrap client", e);
+ }
+ }
+}
+
package org.opensrf.test;
import org.opensrf.*;
import org.opensrf.util.*;
-import org.opensrf.net.xmpp.*;
-import java.io.PrintStream;
import java.util.Map;
import java.util.Date;
+import java.util.List;
+import java.util.ArrayList;
+import java.io.PrintStream;
public class TestClient {
public static void main(String args[]) throws Exception {
PrintStream out = System.out;
+ String service;
+ String method;
try {
+ Sys.bootstrapClient(args[0], "/config/opensrf");
+ service = args[1];
+ method = args[2];
+ } catch(ArrayIndexOutOfBoundsException e) {
+ out.println( "usage: org.opensrf.test.TestClient "+
+ "<osrfConfigFile> <service> <method> [<JSONparam1>, <JSONparam2>]");
+ return;
+ }
- /** setup the config parser */
- String configFile = args[0];
- Config config = new Config("/config/opensrf");
- config.parse(configFile);
- Config.setConfig(config);
-
- /** Connect to jabber */
- String username = Config.getString("/username");
- String passwd = Config.getString("/passwd");
- String host = (String) Config.getFirst("/domains/domain");
- int port = Config.getInt("/port");
- XMPPSession xses = new XMPPSession(host, port);
- xses.connect(username, passwd, "test-java-client");
- XMPPSession.setGlobalSession(xses);
-
- /** build the client session and send the request */
- ClientSession session = new ClientSession("opensrf.settings");
- Request request = session.request(
- "opensrf.settings.host_config.get",
- new String[] {args[1]}
- );
-
- Result result = request.recv(10000);
- if(result == null) {
- out.println("no result");
- return;
- }
-
- out.println("status = " + result.getStatus());
- out.println("status code = " + result.getStatusCode());
+ /** build the client session and send the request */
+ ClientSession session = new ClientSession(service);
+ List<Object> params = new ArrayList<Object>();
+ JSONReader reader;
- out.println("setting config memcache server(s) = " +
- new JSONWriter(
- Utils.findPath( (Map) result.getContent(),
- "/cache/global/servers/server")
- ).write());
+ for(int i = 3; i < args.length; i++) /* add the params */
+ params.add(new JSONReader(args[i]).read());
+ Request request = session.request(method, params);
- } catch(ArrayIndexOutOfBoundsException e) {
- out.println("usage: org.opensrf.test.TestClient <osrfConfigFile> <domain>");
- return;
+ Result result;
+ long start = new Date().getTime();
+ while( (result = request.recv(60000)) != null ) {
+ out.println("status = " + result.getStatus());
+ out.println("status code = " + result.getStatusCode());
+ out.println("result JSON: " + new JSONWriter(result.getContent()).write());
}
+ out.println("Request took: " + (new Date().getTime() - start));
}
}
* Parses an XML config file.
* @param filename The path to the file to parse.
*/
- public void parse(String filename) throws Exception {
- String xml = Utils.fileToString(filename);
- JSONObject jobj = XML.toJSONObject(xml);
- configObject = (Map) new JSONReader(jobj.toString()).readObject();
+ public void parse(String filename) throws ConfigException {
+ try {
+ String xml = Utils.fileToString(filename);
+ JSONObject jobj = XML.toJSONObject(xml);
+ configObject = (Map) new JSONReader(jobj.toString()).readObject();
+ } catch(Exception e) {
+ throw new ConfigException("Error parsing config", e);
+ }
}
/**
public ConfigException(String info) {
super(info);
}
+ public ConfigException(String info, Throwable t) {
+ super(info, t);
+ }
}