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.server;
30  
31  import org.apache.mina.protocol.ProtocolSession;
32  
33  import uk.ac.rdg.resc.jstyx.StyxUtils;
34  
35  /***
36   * Server's representation of a client of a StyxFile; created when a client
37   * succesfully opens a StyxFile
38   *
39   * @author Jon Blower
40   * $Revision: 136 $
41   * $Date: 2005-03-11 14:02:17 +0000 (Fri, 11 Mar 2005) $
42   * $Log$
43   * Revision 1.2  2005/03/11 14:02:16  jonblower
44   * Merged MINA-Test_20059309 into main line of development
45   *
46   * Revision 1.1.1.1.2.1  2005/03/09 19:44:18  jonblower
47   * Changes concerned with migration to MINA
48   *
49   * Revision 1.1.1.1  2005/02/16 18:58:33  jonblower
50   * Initial import
51   *
52   */
53  public class StyxFileClient
54  {
55      
56      private ProtocolSession session;  // The connection that the client sits on
57      private long fid;              // The client's identifier for the file
58      
59      private boolean truncate;      // true if the client wants to truncate the file
60                                     // after writing data. If the file is append-only
61                                     // the file won't be truncated.
62      private boolean deleteOnClunk; // true if the client wants the file to be
63                                     // removed when the file's fid is clunked
64      private long offset;           // When the client reads from or writes to a
65                                     // file, this records the new offset (i.e. it
66                                     // is a client-specific file pointer)
67      private int nextFileToRead;    // used when reading a directory; stores the 
68                                     // index of the next child of a StyxFile to include
69                                     // in a RreadMessage
70      private int mode;              // 0 if OREAD, 1 if OWRITE, 2 if ORDWR, 3 if EXEC
71      
72      /***
73       * A new StyxFileClient
74       * @param session The connection the client is using
75       * @param fid The client's handle to the file. Remember that clients might
76       * have multiple fids opened on the same file
77       * @param mode the file mode as it appears in the TopenMessage (including
78       * the OTRUNC and ORCLOSE bits)
79       */
80      public StyxFileClient(ProtocolSession session, long fid, int mode)
81      {
82          this.session = session;
83          this.fid = fid;
84          if ((mode & StyxUtils.OTRUNC) == StyxUtils.OTRUNC)
85          {
86              this.truncate = true;
87          }
88          if ((mode & StyxUtils.ORCLOSE) == StyxUtils.ORCLOSE)
89          {
90              this.deleteOnClunk = true;
91          }
92          this.mode = mode & 3; // Mask off all but last two bits
93          this.offset = 0;
94          this.nextFileToRead = 0;
95      }
96      
97      public ProtocolSession getSession()
98      {
99          return this.session;
100     }
101     
102     public long getFid()
103     {
104         return this.fid;
105     }
106     
107     public boolean deleteOnClunk()
108     {
109         return this.deleteOnClunk;
110     }
111     
112     public boolean truncate()
113     {
114         return this.truncate;
115     }
116     
117     public void setOffset(long offset)
118     {
119         this.offset = offset;
120     }
121     
122     public long getOffset()
123     {
124         return this.offset;
125     }
126     
127     /***
128      * Checks to see if the client can read from this file (i.e. if the client
129      * has opened the file with read access)
130      */
131     public boolean canRead()
132     {
133         return (this.mode == StyxUtils.OREAD) || (this.mode == StyxUtils.ORDWR);
134     }
135     
136     /***
137      * Checks to see if the client can write to this file (i.e. if the client
138      * has opened the file with write access)
139      */
140     public boolean canWrite()
141     {
142         return (this.mode == StyxUtils.OWRITE) || (this.mode == StyxUtils.ORDWR);
143     }
144 
145     public int getNextFileToRead()
146     {
147         return nextFileToRead;
148     }
149 
150     public void setNextFileToRead(int nextFileToRead)
151     {
152         this.nextFileToRead = nextFileToRead;
153     }
154 }