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  import java.util.Vector;
34  
35  /***
36   * Message sent to traverse a directory tree
37   *
38   * @author Jon Blower
39   * $Revision: 146 $
40   * $Date: 2005-03-15 09:01:48 +0000 (Tue, 15 Mar 2005) $
41   * $Log$
42   * Revision 1.4  2005/03/15 09:01:48  jonblower
43   * Message type now stored as short, not int
44   *
45   * Revision 1.3  2005/03/11 14:02:16  jonblower
46   * Merged MINA-Test_20059309 into main line of development
47   *
48   * Revision 1.2.2.1  2005/03/10 11:50:59  jonblower
49   * Changed to fit with MINA framework
50   *
51   * Revision 1.2  2005/02/24 07:44:44  jonblower
52   * Added getFriendlyString()
53   *
54   * Revision 1.1.1.1  2005/02/16 18:58:29  jonblower
55   * Initial import
56   *
57   */
58  public class TwalkMessage extends StyxMessage
59  {
60      
61      private long fid;            // The fid of the start point of the traversal
62      private long newFid;         // The fid that will be assigned to the destination file
63      private Vector pathElements; // Contains the individual elements of the path of the traversal
64      
65      /*** 
66       * Creates a new TwalkMessage. This constructor will be called by the
67       * MessageRecognizer.
68       * @param length The total length of the message (including all header info)
69       * @param type The type of the message (a number between 100 and 127)
70       * @param tag The tag that identifies this message
71       */
72      public TwalkMessage(int length, short type, int tag)
73      {
74          super(length, type, tag);
75          this.name = "Twalk";
76          this.pathElements = new Vector(5, 2); // Start off with space for 5 elements
77      }
78      
79      /***
80       * This constructor should be called when constructing a TwalkMessage from
81       * scratch
82       * @param fid The fid representing the start point of the traversal
83       * @param newFid The fid that will represent the end point of the traversal
84       * @param path The /-delimited path
85       */
86      public TwalkMessage(long fid, long newFid, String path)
87      {
88          this(0, (short)110, 0);
89          this.fid = fid;
90          this.newFid = newFid;
91          this.length = StyxUtils.HEADER_LENGTH + 4 + 4 + 2; // This length will be increased
92                                                     // as the individual path elements
93                                                     // are added by setPath()
94          this.setPath(path);
95      }
96      
97      protected final void decodeBody(StyxBuffer buf)
98      {
99          // Read the original fid
100         this.fid = buf.getUInt();
101         // Read the new fid
102         this.newFid = buf.getUInt();
103         // Read the number of path elements
104         int numPathElements = buf.getUShort();
105         // Read the path elements
106         for (int i = 0; i < numPathElements; i++)
107         {
108             this.pathElements.add(buf.getString());
109         }
110     }
111     
112     protected final void encodeBody(StyxBuffer buf)
113     {
114         // Write the old and new fids
115         buf.putUInt(this.fid).putUInt(this.newFid);
116         // Write the number of path elements
117         buf.putUShort(this.getNumPathElements());
118         // Write the path elements
119         for (int i = 0; i < this.getNumPathElements(); i++)
120         {
121             buf.putString((String)this.pathElements.get(i));
122         }
123     }
124     
125     public long getFid()
126     {
127         return this.fid;
128     }
129     
130     public void setFid(long fid)
131     {
132         this.fid = fid;
133     }
134     
135     public long getNewFid()
136     {
137         return this.newFid;
138     }
139     
140     public void setNewFid(long newFid)
141     {
142         this.newFid = newFid;
143     }
144     
145     /***
146      * @return the path of this TwalkMessage as a string
147      */
148     public String getPath()
149     {
150         StringBuffer s = new StringBuffer();
151         for (int i = 0; i < this.pathElements.size(); i++)
152         {
153             s.append((String)this.pathElements.get(i));
154             if (i < this.pathElements.size() - 1)
155             {
156                 s.append("/");
157             }
158         }
159         return s.toString();
160     }
161     
162     /***
163      * @param path The /-delimited path (if this starts with a / it is an
164      * absolute path, otherwise it is a relative path (relative to the file
165      * represented by fid)
166      */
167     public void setPath(String path)
168     {
169         // Now parse the path elements: first, split the string using the / delimiter
170         String[] els = path.split("/");
171         // Cycle through the elements in turn, checking them
172         for (int i = 0; i < els.length; i++)
173         {
174             // We ignore elements that represent the current directory
175             if (!els[i].equals(".") && !els[i].equals(""))
176             {
177                 // TODO: Check that there are no illegal characters?
178                 this.addPathElement(els[i]);
179             }
180         }
181     }
182     
183     /***
184      * @return the number of elements in the path
185      */
186     public int getNumPathElements()
187     {
188         return this.pathElements.size();
189     }
190     
191     /***
192      * @return the path elements as a String array
193      */
194     public String[] getPathElements()
195     {
196         return (String[])this.pathElements.toArray(new String[0]);
197     }
198     
199     /***
200      * Add an element to the array of path elements
201      */
202     public void addPathElement(String s)
203     {
204         this.pathElements.add(s);
205         int len = StyxUtils.strToUTF8(s).length;
206         this.length += 2 + len;
207     }
208     
209     protected String getElements()
210     {
211         String s = ", " + this.fid + ", " + this.newFid + ", " + this.getNumPathElements();
212         for (int i = 0; i < this.pathElements.size(); i++)
213         {
214             s += ", " + (String)this.pathElements.get(i);
215         }
216         return s;
217     }
218     
219     public String toFriendlyString()
220     {
221         String pathEls = "";
222         for (int i = 0; i < this.pathElements.size(); i++)
223         {
224             pathEls += (String)this.pathElements.get(i);
225             if (i < this.pathElements.size() - 1)
226             {
227                 pathEls += "/";
228             }
229         }
230         return "fid: " + this.fid + ", new fid: " + this.newFid + ", path: "
231             + pathEls;
232     }
233     
234 }