From 2ff0192bcad84be890245afdd6878db190337494 Mon Sep 17 00:00:00 2001 From: erickson Date: Sun, 16 Dec 2007 19:55:41 +0000 Subject: [PATCH] adding a basic mutlisession manager git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1174 9efc2488-bf62-4759-914b-345cdb29e865 --- src/java/org/opensrf/MultiSession.java | 123 ++++++++++++++++++++++++ src/java/org/opensrf/test/TestMultiSession.java | 26 +++++ 2 files changed, 149 insertions(+) create mode 100644 src/java/org/opensrf/MultiSession.java create mode 100644 src/java/org/opensrf/test/TestMultiSession.java diff --git a/src/java/org/opensrf/MultiSession.java b/src/java/org/opensrf/MultiSession.java new file mode 100644 index 0000000..a312aff --- /dev/null +++ b/src/java/org/opensrf/MultiSession.java @@ -0,0 +1,123 @@ +package org.opensrf; +import java.util.List; +import java.util.ArrayList; +import org.opensrf.util.ConfigException; + +public class MultiSession { + + class RequestContainer { + Request request; + int id; + RequestContainer(Request r) { + request = r; + } + } + + private boolean complete; + private List requests; + private int lastId; + + public MultiSession() { + requests = new ArrayList(); + } + + public boolean isComplete() { + return complete; + } + + public int lastId() { + return lastId; + } + + /** + * Adds a new request to the set of requests. + * @param service The OpenSRF service + * @param method The OpenSRF method + * @param params The array of method params + * @return The request ID, which is used to map results from recv() to the original request. + */ + public int request(String service, String method, Object[] params) throws SessionException, ConfigException { + ClientSession ses = new ClientSession(service); + return request(ses.request(method, params)); + } + + + public int request(String service, String method) throws SessionException, ConfigException { + ClientSession ses = new ClientSession(service); + return request(ses.request(method)); + } + + private int request(Request req) { + RequestContainer c = new RequestContainer(req); + c.id = requests.size(); + requests.add(c); + return c.id; + } + + + /** + * Calls recv on all pending requests until there is data to return. The ID which + * maps the received object to the request can be retrieved by calling lastId(). + * @param millis Number of milliseconds to wait for some data to arrive. + * @return The object result or null if all requests are complete + * @throws MethodException Thrown if no response is received within + * the given timeout or the method fails. + */ + public Object recv(int millis) throws MethodException { + if(complete) return null; + + Request req = null; + Result res = null; + RequestContainer cont = null; + + long duration = 0; + long blockTime = 100; + + /* if there is only 1 outstanding request, don't poll */ + if(requests.size() == 1) + blockTime = millis; + + while(true) { + for(int i = 0; i < requests.size(); i++) { + + cont = requests.get(i); + req = cont.request; + + try { + if(i == 0) { + res = req.recv(blockTime); + } else { + res = req.recv(0); + } + } catch(SessionException e) { + throw new MethodException(e); + } + + if(res != null) break; + } + + if(res != null) break; + duration += blockTime; + + if(duration >= millis) { + System.out.println("duration = " + duration + " millis = " + millis); + throw new MethodException("No request received within " + millis + " milliseconds"); + } + } + + if(res.getStatusCode() != 200) { + throw new MethodException("Request " + cont.id + " failed with status code " + + res.getStatusCode() + " and status message " + res.getStatus()); + } + + if(req.isComplete()) + requests.remove(requests.indexOf(cont)); + + if(requests.size() == 0) + complete = true; + + lastId = cont.id; + return res.getContent(); + } +} + diff --git a/src/java/org/opensrf/test/TestMultiSession.java b/src/java/org/opensrf/test/TestMultiSession.java new file mode 100644 index 0000000..bb0f1a1 --- /dev/null +++ b/src/java/org/opensrf/test/TestMultiSession.java @@ -0,0 +1,26 @@ +package org.opensrf.test; +import org.opensrf.*; +import org.opensrf.util.*; + +public class TestMultiSession { + public static void main(String[] args) { + try { + String config = args[0]; + + Sys.bootstrapClient(config, "/config/opensrf"); + MultiSession ses = new MultiSession(); + + for(int i = 0; i < 40; i++) { + ses.request("opensrf.settings", "opensrf.system.time"); + } + + while(!ses.isComplete()) + System.out.println("result = " + ses.recv(5000) + " and id = " + ses.lastId()); + + System.out.println("done"); + Sys.shutdown(); + } catch(Exception e) { + e.printStackTrace(); + } + } +} -- 2.11.0