From 65c3d9c98163645f20b42cf7f8b9818315078689 Mon Sep 17 00:00:00 2001 From: erickson Date: Wed, 16 May 2007 14:38:02 +0000 Subject: [PATCH] implemented client bootstrap. created a status object to model connection statuses. remodeled the client test code to run arbitrary methods git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@890 9efc2488-bf62-4759-914b-345cdb29e865 --- src/java/org/opensrf/ClientSession.java | 44 +++++++++++- src/java/org/opensrf/Request.java | 9 ++- src/java/org/opensrf/SessionException.java | 13 ++++ src/java/org/opensrf/Stack.java | 15 ++++- src/java/org/opensrf/Status.java | 63 +++++++++++++++++ src/java/org/opensrf/Sys.java | 40 +++++++++++ src/java/org/opensrf/test/TestClient.java | 67 ++++++++----------- src/java/org/opensrf/util/Config.java | 12 ++-- .../org/opensrf/util/ConfigException.java | 3 + 9 files changed, 218 insertions(+), 48 deletions(-) create mode 100644 src/java/org/opensrf/SessionException.java create mode 100644 src/java/org/opensrf/Status.java create mode 100644 src/java/org/opensrf/Sys.java diff --git a/src/java/org/opensrf/ClientSession.java b/src/java/org/opensrf/ClientSession.java index 09cfce1..27935ab 100644 --- a/src/java/org/opensrf/ClientSession.java +++ b/src/java/org/opensrf/ClientSession.java @@ -8,6 +8,7 @@ import java.util.Random; import java.util.Arrays; import org.opensrf.util.*; +import org.opensrf.net.xmpp.*; /** @@ -111,7 +112,7 @@ public class ClientSession extends Session { */ 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; @@ -128,11 +129,52 @@ public class ClientSession extends Session { ); } + 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); + } + } + */ } diff --git a/src/java/org/opensrf/Request.java b/src/java/org/opensrf/Request.java index 001ce43..1222999 100644 --- a/src/java/org/opensrf/Request.java +++ b/src/java/org/opensrf/Request.java @@ -73,7 +73,7 @@ public class Request { 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) @@ -81,7 +81,7 @@ public class Request { } 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 @@ -120,4 +120,9 @@ public class Request { public void cleanup() { session.cleanupRequest(id); } + + /** Sets this request as complete */ + public void setComplete() { + complete = true; + } } diff --git a/src/java/org/opensrf/SessionException.java b/src/java/org/opensrf/SessionException.java new file mode 100644 index 0000000..bd90a76 --- /dev/null +++ b/src/java/org/opensrf/SessionException.java @@ -0,0 +1,13 @@ +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); + } +} + diff --git a/src/java/org/opensrf/Stack.java b/src/java/org/opensrf/Stack.java index 6db285e..89649a5 100644 --- a/src/java/org/opensrf/Stack.java +++ b/src/java/org/opensrf/Stack.java @@ -66,10 +66,23 @@ public class Stack { * 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()); + } + } } /** diff --git a/src/java/org/opensrf/Status.java b/src/java/org/opensrf/Status.java new file mode 100644 index 0000000..8026c7b --- /dev/null +++ b/src/java/org/opensrf/Status.java @@ -0,0 +1,63 @@ +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; + } +} + + diff --git a/src/java/org/opensrf/Sys.java b/src/java/org/opensrf/Sys.java new file mode 100644 index 0000000..e403879 --- /dev/null +++ b/src/java/org/opensrf/Sys.java @@ -0,0 +1,40 @@ +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); + } + } +} + diff --git a/src/java/org/opensrf/test/TestClient.java b/src/java/org/opensrf/test/TestClient.java index 8cd7865..df6db94 100644 --- a/src/java/org/opensrf/test/TestClient.java +++ b/src/java/org/opensrf/test/TestClient.java @@ -1,61 +1,48 @@ 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 "+ + " [, ]"); + 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 params = new ArrayList(); + 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 "); - 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)); } } diff --git a/src/java/org/opensrf/util/Config.java b/src/java/org/opensrf/util/Config.java index 7b3a0bf..00b703a 100644 --- a/src/java/org/opensrf/util/Config.java +++ b/src/java/org/opensrf/util/Config.java @@ -42,10 +42,14 @@ public class Config { * 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); + } } /** diff --git a/src/java/org/opensrf/util/ConfigException.java b/src/java/org/opensrf/util/ConfigException.java index be7c0cf..c1c491e 100644 --- a/src/java/org/opensrf/util/ConfigException.java +++ b/src/java/org/opensrf/util/ConfigException.java @@ -8,4 +8,7 @@ public class ConfigException extends Exception { public ConfigException(String info) { super(info); } + public ConfigException(String info, Throwable t) { + super(info, t); + } } -- 2.43.2