mev-4.0.01/source/org/tigr/remote/protocol/communication/http/Communicator.java

Code
Comments
Other
Rev Date Author Line
2 26 Feb 07 jari 1 /*
2 26 Feb 07 jari 2 Copyright @ 1999-2003, The Institute for Genomic Research (TIGR).
2 26 Feb 07 jari 3 All rights reserved.
2 26 Feb 07 jari 4 */
2 26 Feb 07 jari 5 /*
2 26 Feb 07 jari 6  * $RCSfile: Communicator.java,v $
2 26 Feb 07 jari 7  * $Revision: 1.4 $
2 26 Feb 07 jari 8  * $Date: 2006/02/23 21:00:01 $
2 26 Feb 07 jari 9  * $Author: caliente $
2 26 Feb 07 jari 10  * $State: Exp $
2 26 Feb 07 jari 11  */
2 26 Feb 07 jari 12 package org.tigr.remote.protocol.communication.http;
2 26 Feb 07 jari 13
2 26 Feb 07 jari 14 import org.tigr.remote.RemoteException;
2 26 Feb 07 jari 15 import org.tigr.remote.communication.ClientCommunicator;
2 26 Feb 07 jari 16 import org.tigr.remote.communication.JobControl;
2 26 Feb 07 jari 17 import org.tigr.remote.protocol.FinishedJob;
2 26 Feb 07 jari 18 import org.tigr.remote.protocol.Request;
2 26 Feb 07 jari 19 import org.tigr.remote.protocol.Response;
2 26 Feb 07 jari 20 import org.tigr.remote.protocol.StartingJob;
2 26 Feb 07 jari 21 import org.tigr.remote.protocol.StopJob;
2 26 Feb 07 jari 22 import org.tigr.remote.protocol.communication.Presentation;
2 26 Feb 07 jari 23 import org.tigr.util.ConfMap;
2 26 Feb 07 jari 24
2 26 Feb 07 jari 25 public class Communicator implements ClientCommunicator {
2 26 Feb 07 jari 26
2 26 Feb 07 jari 27     private ConfMap m_conf;
2 26 Feb 07 jari 28     private Presentation m_presentation;
2 26 Feb 07 jari 29     private int i = 0;
2 26 Feb 07 jari 30
2 26 Feb 07 jari 31     /**
2 26 Feb 07 jari 32      * Constructs a <code>Communicator</code> with specified configuration.
2 26 Feb 07 jari 33      */
2 26 Feb 07 jari 34     public Communicator(ConfMap conf) throws RemoteException {
2 26 Feb 07 jari 35         m_conf = conf;
2 26 Feb 07 jari 36         m_presentation = new Presentation( conf,  new HttpTransport( conf ) );
2 26 Feb 07 jari 37     }
2 26 Feb 07 jari 38
2 26 Feb 07 jari 39     /**
2 26 Feb 07 jari 40      * Starts a remote execution.
2 26 Feb 07 jari 41      * @return the JobControl of a started job.
2 26 Feb 07 jari 42      */
2 26 Feb 07 jari 43     public JobControl postJob( StartingJob job ) throws RemoteException {
2 26 Feb 07 jari 44         Request req = new Request( job );
2 26 Feb 07 jari 45         m_presentation.sendRequest( req );
2 26 Feb 07 jari 46         return new SyncJobControl( this, job.getId(), m_conf.getInt("remote.polling", 10) );
2 26 Feb 07 jari 47     }
2 26 Feb 07 jari 48
2 26 Feb 07 jari 49     /**
2 26 Feb 07 jari 50      * Returns result of a remote execution.
2 26 Feb 07 jari 51      */
2 26 Feb 07 jari 52     protected FinishedJob getResult() throws RemoteException {
2 26 Feb 07 jari 53         Response response = m_presentation.getResponse();
2 26 Feb 07 jari 54         return response.getJob();
2 26 Feb 07 jari 55     }
2 26 Feb 07 jari 56
2 26 Feb 07 jari 57     /**
2 26 Feb 07 jari 58      * Returns unique identifier for a new job.
2 26 Feb 07 jari 59      */
2 26 Feb 07 jari 60     public String getNewJobId() { return ++i + "";}
2 26 Feb 07 jari 61
2 26 Feb 07 jari 62     /**
2 26 Feb 07 jari 63      * Class to control synchronized communication.
2 26 Feb 07 jari 64      */
2 26 Feb 07 jari 65     private class SyncJobControl implements JobControl {
2 26 Feb 07 jari 66
2 26 Feb 07 jari 67         private Communicator m_comm;
2 26 Feb 07 jari 68         private String m_jobId;
2 26 Feb 07 jari 69         private int m_polling;
2 26 Feb 07 jari 70         private FinishedJob m_result = null;
2 26 Feb 07 jari 71
2 26 Feb 07 jari 72         /**
2 26 Feb 07 jari 73          * Constructs a <code>SyncJobControl</code> for specified communicator,
2 26 Feb 07 jari 74          * id and polling parameter. 
2 26 Feb 07 jari 75          * @param polling the polling interval in seconds.
2 26 Feb 07 jari 76          */
2 26 Feb 07 jari 77         public SyncJobControl( Communicator comm, String id, int polling ) {
2 26 Feb 07 jari 78             m_comm = comm;
2 26 Feb 07 jari 79             m_jobId = id;
2 26 Feb 07 jari 80             m_polling = polling;
2 26 Feb 07 jari 81         }
2 26 Feb 07 jari 82
2 26 Feb 07 jari 83         /**
2 26 Feb 07 jari 84          * Waiting for a server reply.
2 26 Feb 07 jari 85          */
2 26 Feb 07 jari 86         private void waitForReply() throws RemoteException {
2 26 Feb 07 jari 87             while (m_result == null) {
2 26 Feb 07 jari 88                 try {
2 26 Feb 07 jari 89                     Thread.sleep( m_polling*1000 );
2 26 Feb 07 jari 90                 } catch (InterruptedException e) {
2 26 Feb 07 jari 91                     throw new RemoteException("Thread was interrupted", e);
2 26 Feb 07 jari 92                 }
2 26 Feb 07 jari 93                 m_result = m_comm.getResult();
2 26 Feb 07 jari 94             }
2 26 Feb 07 jari 95         }
2 26 Feb 07 jari 96
2 26 Feb 07 jari 97         /**
2 26 Feb 07 jari 98          * Sends an instance of <code>StopJob</code> to a server.
2 26 Feb 07 jari 99          */
2 26 Feb 07 jari 100         public void terminate() throws RemoteException {
2 26 Feb 07 jari 101             m_comm.postJob( new StopJob( getJobId() ) );
2 26 Feb 07 jari 102         }
2 26 Feb 07 jari 103
2 26 Feb 07 jari 104         /**
2 26 Feb 07 jari 105          * Returns the result of remote execution.
2 26 Feb 07 jari 106          */
2 26 Feb 07 jari 107         public FinishedJob getResult() throws RemoteException {
2 26 Feb 07 jari 108             waitForReply();
2 26 Feb 07 jari 109             if (m_result == null)
2 26 Feb 07 jari 110                 throw new RemoteException("No result available");
2 26 Feb 07 jari 111             FinishedJob result = m_result;
2 26 Feb 07 jari 112             m_result = null;
2 26 Feb 07 jari 113             return result;
2 26 Feb 07 jari 114         }
2 26 Feb 07 jari 115
2 26 Feb 07 jari 116         /**
2 26 Feb 07 jari 117          * Returns job unique identifier.
2 26 Feb 07 jari 118          */
2 26 Feb 07 jari 119         private String getJobId() { return m_jobId;}
2 26 Feb 07 jari 120     }
2 26 Feb 07 jari 121 }
2 26 Feb 07 jari 122