View Javadoc

1   /*
2    * Copyright (c) 2005 The University of Reading
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    * 1. Redistributions of source code must retain the above copyright
9    *    notice, this list of conditions and the following disclaimer.
10   * 2. Redistributions in binary form must reproduce the above copyright
11   *    notice, this list of conditions and the following disclaimer in the
12   *    documentation and/or other materials provided with the distribution.
13   * 3. Neither the name of the University of Reading, nor the names of the
14   *    authors or contributors may be used to endorse or promote products
15   *    derived from this software without specific prior written permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20   * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   */
28  
29  package uk.ac.rdg.resc.jstyx.gridservice.server;
30  
31  import java.util.Vector;
32  
33  import uk.ac.rdg.resc.jstyx.StyxException;
34  import uk.ac.rdg.resc.jstyx.server.StyxFile;
35  import uk.ac.rdg.resc.jstyx.server.StyxFileClient;
36  
37  /***
38   * File that is read in order to get the exit code from the service instance.
39   * If the exit code has not yet been set, read requests will be queued and replied
40   * to when the exit code is set.
41   * @todo This class might be more generally useful - refactor?
42   *
43   * @author Jon Blower
44   * $Revision: 510 $
45   * $Date: 2005-12-01 08:26:45 +0000 (Thu, 01 Dec 2005) $
46   * $Log$
47   * Revision 1.2  2005/12/01 08:26:45  jonblower
48   * Fixed javadoc comment
49   *
50   * Revision 1.1  2005/11/14 21:31:54  jonblower
51   * Got SGSRun working for SC2005 demo
52   *
53   */
54  
55  public class ExitCodeFile extends StyxFile
56  {
57      
58      private String exitCode;
59      private Vector queuedRequests;
60      
61      /*** Creates a new instance of ExitCodeFile */
62      public ExitCodeFile() throws StyxException
63      {
64          super("exitCode", 0444); // This is a read-only file
65          this.exitCode = null;
66          this.queuedRequests = new Vector();
67      }
68      
69      public synchronized void read(StyxFileClient client, long offset, int count,
70          int tag) throws StyxException
71      {
72          if (this.exitCode != null)
73          {
74              // Exit code has been set: reply to the client 
75              this.processAndReplyRead(this.exitCode, client, offset, count, tag);
76          }
77          else
78          {
79              // Enqueue this request
80              DataRequest dr = new DataRequest(client, offset, count, tag);
81              synchronized(this.queuedRequests)
82              {
83                  this.queuedRequests.add(dr);
84              }
85          }
86      }
87      
88      /***
89       * Sets the exit code.  When this is called, all waiting clients will be notified
90       * of the new exit code.
91       */
92      public synchronized void setExitCode(int exitCode)
93      {
94          this.exitCode = "" + exitCode;
95          synchronized(this.queuedRequests)
96          {
97              for (int i = 0; i < this.queuedRequests.size(); i++)
98              {
99                  DataRequest dr = (DataRequest)this.queuedRequests.get(i);
100                 this.processAndReplyRead(this.exitCode, dr.client, dr.offset,
101                     dr.count, dr.tag);
102             }
103         }
104     }
105     
106     /***
107      * Class representing a request for data that we have enqueued
108      */
109     private static class DataRequest
110     {
111         private StyxFileClient client;
112         private int tag;
113         private long offset; // The offset requested by the client
114         private int count; // The number of bytes requested by the client
115         
116         private DataRequest(StyxFileClient client, long offset, int count, int tag)
117         {
118             this.client = client;
119             this.tag = tag;
120             this.offset = offset;
121             this.count = count;
122         }
123     }
124     
125 }