From 315555ae421aef4ba125b21210f603eecb58fdc0 Mon Sep 17 00:00:00 2001 From: jprocter Date: Thu, 15 Mar 2007 15:25:25 +0000 Subject: [PATCH] switched picker shutdown to after finalize() call - simulating GUI shutdown after Vamsas session shutdown. git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@364 be28352e-c001-0410-b1a7-c7978e42abec --- src/uk/ac/vamsas/test/ExampleApplication.java | 589 +++++++++++++------------ 1 file changed, 296 insertions(+), 293 deletions(-) diff --git a/src/uk/ac/vamsas/test/ExampleApplication.java b/src/uk/ac/vamsas/test/ExampleApplication.java index 0a44380..3722109 100644 --- a/src/uk/ac/vamsas/test/ExampleApplication.java +++ b/src/uk/ac/vamsas/test/ExampleApplication.java @@ -31,298 +31,301 @@ import java.util.Vector; public class ExampleApplication { - private static ClientHandle app; - - private static UserHandle user; // TODO: make this something defined by the - // api - - private static IClientFactory clientfactory; - - private static IClient vorbaclient; - - private static byte[] mydata; - - private static Vector vamsasObjects; - - private static boolean isUpdated = false; - - private static boolean isShuttingdown = false; - - private static boolean isFinalizing = false; - - private static void processVamsasDocument(IClientDocument doc) - { - doc.addVamsasRoot(Core.getDemoVamsas()); - vorbaclient.updateDocument(doc); - // merge vamsasObjects with vamsas objects in document - // get this apps 'mydata' if it hasn't got it already. - // .. access this application's 'public' mydata' if there is any. - } - - private static void addHandlers(IClient avorbaclient) - { - // make a non-volatile reference to the client instance. - final IClient vorbaclient = avorbaclient; - // register update handler - vorbaclient.addDocumentUpdateHandler(new PropertyChangeListener() { - public void propertyChange(PropertyChangeEvent evt) - { - System.out.println("Vamsas document update for " - + evt.getPropertyName() + ": " + evt.getOldValue() - + " to " + evt.getNewValue()); - // merge new data into ours. - // example - output doc - try - { - IClientDocument cdoc = vorbaclient.getClientDocument(); - uk.ac.vamsas.test.simpleclient.ArchiveReports.rootReport( - cdoc.getVamsasRoots(), true, System.out); - vorbaclient.updateDocument(cdoc); - } - catch (Exception e) - { - System.err - .println("Exception whilst dumping document tree after an update."); - e.printStackTrace(System.err); - } - isUpdated = true; // tell main thread to reflect change... - } - }); - // register close handler - vorbaclient.addVorbaEventHandler(Events.DOCUMENT_REQUESTTOCLOSE, - new PropertyChangeListener() { - public void propertyChange(PropertyChangeEvent evt) - { - System.out - .println("Received request to close vamsas document."); - // ask user for a filename to save it to. - // Then pass it to the vorba object... - vorbaclient.storeDocument(new java.io.File( - "UserLocation")); - } - }); - - // register some more handlers to monitor the session : - - vorbaclient.addVorbaEventHandler(Events.CLIENT_CREATION, - new PropertyChangeListener() { - public void propertyChange(PropertyChangeEvent evt) - { - System.out.println("New Vamsas client for " - + evt.getPropertyName() + ": " - + evt.getOldValue() + " to " - + evt.getNewValue()); - // tell app add new client to its list of clients. - } - }); - vorbaclient.addVorbaEventHandler(Events.CLIENT_FINALIZATION, - new PropertyChangeListener() { - public void propertyChange(PropertyChangeEvent evt) - { - System.out.println("Vamsas client finalizing for " - + evt.getPropertyName() + ": " - + evt.getOldValue() + " to " - + evt.getNewValue()); - // tell app to update its list of clients to communicate - // with. - } - }); - vorbaclient.addVorbaEventHandler(Events.SESSION_SHUTDOWN, - new PropertyChangeListener() { - public void propertyChange(PropertyChangeEvent evt) - { - System.out.println("Session " + evt.getPropertyName() - + " is shutting down."); - // tell app to finalize its session data before - // shutdown. - } - }); - vorbaclient.addVorbaEventHandler(Events.DOCUMENT_FINALIZEAPPDATA, - new PropertyChangeListener() { - public void propertyChange(PropertyChangeEvent evt) - { - System.out - .println("Application received a DOCUMENT_FINALIZEAPPDATA event."); - // tell app to finalize its session data prior to the - // storage of the current session as an archive. - } - }); - - } - - public static String Usage = "ExampleApplication [session urn] watch/n( future usage is :/n [+]\n" - + " is one of :\n\tsave,update,close,watch"; - - static String sess = null; - - private static boolean parseArgs(String args[]) - { - if (args.length == 0) - { - return false; - } - if (!args[0].toLowerCase().equals("watch")) - { - sess = args[0]; - } - return true; - } - - public static void main(String[] args) - { - class ExamplePicker extends Thread - { - String me = null; - - public IPickManager pm = null; - - ExamplePicker(String me, IPickManager pm) - { - this.me = me; - this.pm = pm; - } - - public void run() - { - int mcount = 1; - while (pm != null) - { - try { Thread.sleep(1000 + (long) Math.random() * 10000); } - catch (Exception e) {} - - if (pm != null) - { - pm.sendMessage(new uk.ac.vamsas.client.picking.CustomMessage("Message " + mcount++ + " from " + me)); - } - } - } - - } - - if (!parseArgs(args)) - { - System.err.print(Usage); - } - // get IClientFactory - try - { - clientfactory = new uk.ac.vamsas.client.simpleclient.SimpleClientFactory(); - } - catch (IOException e) - { - System.err.println(e + "\n" + Usage); - System.exit(1); - } - - // get an Iclient with session data - app = new ClientHandle("uk.ac.vamsas.test.ExampleApplication", "0.1"); - user = new UserHandle("arnolduser", "deathsdoor"); - try - { - vorbaclient = clientfactory.getIClient(app, user); - } - catch (NoDefaultSessionException e) - { - System.err - .println("There appear to be several sessions to choose from :"); - String[] sessions = clientfactory.getCurrentSessions(); - for (int s = 0; s < sessions.length; s++) - System.err.println(sessions[s]); - System.exit(2); - } - addHandlers(vorbaclient); - try - { - vorbaclient.joinSession(); - } - catch (Exception se) - { - se.printStackTrace(); - System.err.println(se + " when joining session.\n" + Usage); - System.exit(1); - } - // register an update listener and a close listener. - // get document data - try - { - IClientDocument cdoc = vorbaclient.getClientDocument(); - processVamsasDocument(cdoc); - } - catch (Exception e) - { - System.err - .println("Unexpected exception when retrieving the client document for the first time!"); - e.printStackTrace(System.err); - System.exit(1); - } - int update = 0; - ExamplePicker picker = new ExamplePicker(vorbaclient.getClientHandle() - .getClientUrn(), vorbaclient.getPickManager()); - - picker.start(); - picker.pm.registerMessageHandler(new IMessageHandler() { - - public void handleMessage(Message message) - { - System.out - .println("Received |" + message.getRawMessage() + "|"); - } - - }); - while (!isShuttingdown && update < 5) - { - // do something with data - // , update document, or something. - // .. - if (isUpdated) - { - System.out.println("Update handler called " + (++update) - + " times"); - System.out - .println("******************************************"); - isUpdated = false; // TODO: saner update det method. - if (update % 2 == 1) - { - try - { - IClientDocument cdoc = vorbaclient.getClientDocument(); - processVamsasDocument(cdoc); - } - catch (Exception e) - { - System.err - .println("Error when updating document after an even numbered update."); - e.printStackTrace(System.err); - } - } - } - - try { Thread.sleep(15); } - catch (Exception e) {} - - } - System.out.println("Shutting down picker."); - picker.pm = null; - while (picker.isAlive()) - { - System.out.println("Waiting for picker to die..."); - try - { - Thread.sleep(1000); - } - catch (Exception e) - { - } - ; - } - - System.out.println("Finalizing."); - // call finalizeClient - vorbaclient.finalizeClient(); - // { meanwhile, eventHandlers are called to do any saves if need be } - // and all registered listeners will be deregistered to avoid deadlock. - - // finish - } + private static ClientHandle app; + + private static UserHandle user; // TODO: make this something defined by the + // api + + private static IClientFactory clientfactory; + + private static IClient vorbaclient; + + private static byte[] mydata; + + private static Vector vamsasObjects; + + private static boolean isUpdated = false; + + private static boolean isShuttingdown = false; + + private static boolean isFinalizing = false; + + private static void processVamsasDocument(IClientDocument doc) + { + doc.addVamsasRoot(Core.getDemoVamsas()); + vorbaclient.updateDocument(doc); + // merge vamsasObjects with vamsas objects in document + // get this apps 'mydata' if it hasn't got it already. + // .. access this application's 'public' mydata' if there is any. + } + + private static void addHandlers(IClient avorbaclient) + { + // make a non-volatile reference to the client instance. + final IClient vorbaclient = avorbaclient; + // register update handler + vorbaclient.addDocumentUpdateHandler(new PropertyChangeListener() { + public void propertyChange(PropertyChangeEvent evt) + { + System.out.println("Vamsas document update for " + + evt.getPropertyName() + ": " + evt.getOldValue() + + " to " + evt.getNewValue()); + // merge new data into ours. + // example - output doc + try + { + IClientDocument cdoc = vorbaclient.getClientDocument(); + uk.ac.vamsas.test.simpleclient.ArchiveReports.rootReport( + cdoc.getVamsasRoots(), true, System.out); + vorbaclient.updateDocument(cdoc); + } + catch (Exception e) + { + System.err + .println("Exception whilst dumping document tree after an update."); + e.printStackTrace(System.err); + } + isUpdated = true; // tell main thread to reflect change... + } + }); + // register close handler + vorbaclient.addVorbaEventHandler(Events.DOCUMENT_REQUESTTOCLOSE, + new PropertyChangeListener() { + public void propertyChange(PropertyChangeEvent evt) + { + System.out + .println("Received request to close vamsas document."); + // ask user for a filename to save it to. + // Then pass it to the vorba object... + vorbaclient.storeDocument(new java.io.File( + "UserLocation")); + } + }); + + // register some more handlers to monitor the session : + + vorbaclient.addVorbaEventHandler(Events.CLIENT_CREATION, + new PropertyChangeListener() { + public void propertyChange(PropertyChangeEvent evt) + { + System.out.println("New Vamsas client for " + + evt.getPropertyName() + ": " + + evt.getOldValue() + " to " + + evt.getNewValue()); + // tell app add new client to its list of clients. + } + }); + vorbaclient.addVorbaEventHandler(Events.CLIENT_FINALIZATION, + new PropertyChangeListener() { + public void propertyChange(PropertyChangeEvent evt) + { + System.out.println("Vamsas client finalizing for " + + evt.getPropertyName() + ": " + + evt.getOldValue() + " to " + + evt.getNewValue()); + // tell app to update its list of clients to communicate + // with. + } + }); + vorbaclient.addVorbaEventHandler(Events.SESSION_SHUTDOWN, + new PropertyChangeListener() { + public void propertyChange(PropertyChangeEvent evt) + { + System.out.println("Session " + evt.getPropertyName() + + " is shutting down."); + // tell app to finalize its session data before + // shutdown. + } + }); + vorbaclient.addVorbaEventHandler(Events.DOCUMENT_FINALIZEAPPDATA, + new PropertyChangeListener() { + public void propertyChange(PropertyChangeEvent evt) + { + System.out + .println("Application received a DOCUMENT_FINALIZEAPPDATA event."); + // tell app to finalize its session data prior to the + // storage of the current session as an archive. + } + }); + + } + + public static String Usage = "ExampleApplication [session urn] watch/n( future usage is :/n [+]\n" + + " is one of :\n\tsave,update,close,watch"; + + static String sess = null; + + private static boolean parseArgs(String args[]) + { + if (args.length == 0) + { + return false; + } + if (!args[0].toLowerCase().equals("watch")) + { + sess = args[0]; + } + return true; + } + + public static void main(String[] args) + { + class ExamplePicker extends Thread + { + String me = null; + + public IPickManager pm = null; + + ExamplePicker(String me, IPickManager pm) + { + this.me = me; + this.pm = pm; + } + + public void run() + { + int mcount = 1; + while (pm != null) + { + try { Thread.sleep(1000 + (long) Math.random() * 10000); } + catch (Exception e) {} + + if (pm != null) + { + pm.sendMessage(new uk.ac.vamsas.client.picking.CustomMessage("Message " + mcount++ + " from " + me)); + } + } + } + + } + + if (!parseArgs(args)) + { + System.err.print(Usage); + } + // get IClientFactory + try + { + clientfactory = new uk.ac.vamsas.client.simpleclient.SimpleClientFactory(); + } + catch (IOException e) + { + System.err.println(e + "\n" + Usage); + System.exit(1); + } + + // get an Iclient with session data + app = new ClientHandle("uk.ac.vamsas.test.ExampleApplication", "0.1"); + user = new UserHandle("arnolduser", "deathsdoor"); + try + { + vorbaclient = clientfactory.getIClient(app, user); + } + catch (NoDefaultSessionException e) + { + System.err + .println("There appear to be several sessions to choose from :"); + String[] sessions = clientfactory.getCurrentSessions(); + for (int s = 0; s < sessions.length; s++) + System.err.println(sessions[s]); + System.exit(2); + } + addHandlers(vorbaclient); + try + { + vorbaclient.joinSession(); + } + catch (Exception se) + { + se.printStackTrace(); + System.err.println(se + " when joining session.\n" + Usage); + System.exit(1); + } + // register an update listener and a close listener. + // get document data + try + { + IClientDocument cdoc = vorbaclient.getClientDocument(); + processVamsasDocument(cdoc); + } + catch (Exception e) + { + System.err + .println("Unexpected exception when retrieving the client document for the first time!"); + e.printStackTrace(System.err); + System.exit(1); + } + int update = 0; + ExamplePicker picker = new ExamplePicker(vorbaclient.getClientHandle() + .getClientUrn(), vorbaclient.getPickManager()); + + picker.start(); + if (picker.pm!=null) { + picker.pm.registerMessageHandler(new IMessageHandler() { + + public void handleMessage(Message message) + { + System.out + .println("Received |" + message.getRawMessage() + "|"); + } + + }); + } + while (!isShuttingdown && update < 5) + { + // do something with data + // , update document, or something. + // .. + if (isUpdated) + { + System.out.println("Update handler called " + (++update) + + " times"); + System.out + .println("******************************************"); + isUpdated = false; // TODO: saner update det method. + if (update % 2 == 1) + { + try + { + IClientDocument cdoc = vorbaclient.getClientDocument(); + processVamsasDocument(cdoc); + } + catch (Exception e) + { + System.err + .println("Error when updating document after an even numbered update."); + e.printStackTrace(System.err); + } + } + } + + try { Thread.sleep(15); } + catch (Exception e) {} + + } + System.out.println("Finalizing."); + // call finalizeClient + vorbaclient.finalizeClient(); + System.out.println("Shutting down picker."); + picker.pm = null; + while (picker.isAlive()) + { + System.out.println("Waiting for picker to die..."); + try + { + Thread.sleep(1000); + } + catch (Exception e) + { + } + ; + } + + // { meanwhile, eventHandlers are called to do any saves if need be } + // and all registered listeners will be deregistered to avoid deadlock. + + // finish + System.out.println("Byee!"); + } } -- 1.7.10.2