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.messages;
30  
31  import uk.ac.rdg.resc.jstyx.StyxUtils;
32  
33  /***
34   * Message sent to create and open a file on a Styx server
35   *
36   * @author Jon Blower
37   * $Revision: 263 $
38   * $Date: 2005-05-25 16:39:02 +0100 (Wed, 25 May 2005) $
39   * $Log$
40   * Revision 1.5  2005/05/25 15:39:02  jonblower
41   * Bug fixes
42   *
43   * Revision 1.4  2005/03/15 09:01:48  jonblower
44   * Message type now stored as short, not int
45   *
46   * Revision 1.3  2005/03/11 14:02:15  jonblower
47   * Merged MINA-Test_20059309 into main line of development
48   *
49   * Revision 1.2.2.1  2005/03/10 11:50:59  jonblower
50   * Changed to fit with MINA framework
51   *
52   * Revision 1.2  2005/02/24 07:44:43  jonblower
53   * Added getFriendlyString()
54   *
55   * Revision 1.1.1.1  2005/02/16 18:58:29  jonblower
56   * Initial import
57   *
58   */
59  public class TcreateMessage extends StyxMessage
60  {
61      
62      private long fid;    // Before the file is created, this fid represents the
63                           // directory in which to create the file.  After the
64                           // file is created, it represents the file itself.
65      private String fileName; // Name for the file
66      private long perm;   // Permissions for the file
67      private int mode;    // When the file is opened, it will be in this mode
68      
69      
70      /*** 
71       * Creates a new TcreateMessage. This constructor will be called by the
72       * MessageRecognizer.
73       * @param length The total length of the message (including all header info)
74       * @param type The type of the message (a number between 100 and 127)
75       * @param tag The tag that identifies this message
76       */
77      public TcreateMessage(int length, short type, int tag)
78      {
79          super(length, type, tag);
80          this.name = "Tcreate";
81      }
82      
83      public TcreateMessage(long fid, String fileName, int permissions, boolean isDirectory, int mode)
84      {
85          this(0, (short)114, 0); // The length and tag will be added later
86          this.fid = fid;
87          this.fileName = fileName; // TODO: check validity of name?
88          this.perm = permissions;
89          if (isDirectory)
90          {
91              this.perm |= StyxUtils.DMDIR; // Set the directory bit
92          }
93          this.mode = mode;
94          
95          // Get the length of the name string
96          int nameLen = StyxUtils.strToUTF8(fileName).length;
97          // Set the length of the message
98          this.length = StyxUtils.HEADER_LENGTH + 4 + 2 + nameLen + 4 + 1;
99      }
100     
101     protected final void decodeBody(StyxBuffer buf)
102     {
103         this.fid = buf.getUInt();
104         this.fileName = buf.getString();
105         this.perm = buf.getUInt();
106         this.mode = buf.getUByte();
107     }
108     
109     protected final void encodeBody(StyxBuffer buf)
110     {
111         buf.putUInt(this.fid).putString(this.fileName).putUInt(this.perm).putUByte(this.mode);
112     }
113     
114     public long getFid()
115     {
116         return this.fid;
117     }
118     
119     public String getFileName()
120     {
121         return this.fileName;
122     }
123     
124     public long getPerm()
125     {
126         return this.perm;
127     }
128     
129     public int getMode()
130     {
131         return this.mode;
132     }
133     
134     protected String getElements()
135     {
136         return ", " + this.fid + ", " + this.fileName + ", " + this.perm + ", " + 
137             this.mode;
138     }
139     
140     public String toFriendlyString()
141     {
142         // TODO: make mode/perm more friendly-looking ("rwxr-xr-x" etc)
143         return "fid: " + this.fid + ", name: " + this.fileName + ", perm: " +
144             this.perm + ", mode: " + this.mode;
145     }
146     
147 }