1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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;
62 private long newFid;
63 private Vector pathElements;
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);
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;
92
93
94 this.setPath(path);
95 }
96
97 protected final void decodeBody(StyxBuffer buf)
98 {
99
100 this.fid = buf.getUInt();
101
102 this.newFid = buf.getUInt();
103
104 int numPathElements = buf.getUShort();
105
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
115 buf.putUInt(this.fid).putUInt(this.newFid);
116
117 buf.putUShort(this.getNumPathElements());
118
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
170 String[] els = path.split("/");
171
172 for (int i = 0; i < els.length; i++)
173 {
174
175 if (!els[i].equals(".") && !els[i].equals(""))
176 {
177
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 }