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.client.callbacks;
30
31 import uk.ac.rdg.resc.jstyx.client.StyxConnection;
32 import uk.ac.rdg.resc.jstyx.client.CStyxFile;
33 import uk.ac.rdg.resc.jstyx.client.MessageCallback;
34
35 import uk.ac.rdg.resc.jstyx.messages.StyxMessage;
36 import uk.ac.rdg.resc.jstyx.messages.TstatMessage;
37 import uk.ac.rdg.resc.jstyx.messages.RstatMessage;
38 import uk.ac.rdg.resc.jstyx.messages.RwalkMessage;
39
40 /***
41 * Description of RefreshCallback goes here.
42 *
43 * @author jdb
44 * $Revision: 347 $
45 * $Date: 2005-08-05 14:46:40 +0100 (Fri, 05 Aug 2005) $
46 * $Log$
47 * Revision 1.1 2005/08/05 13:46:40 jonblower
48 * Factored out all callback objects from CStyxFile into separate classes
49 *
50 */
51
52 public class RefreshCallback extends MessageCallback
53 {
54 private MessageCallback callback;
55 private CStyxFile file;
56 private StyxConnection conn;
57
58 public RefreshCallback(CStyxFile file, MessageCallback callback)
59 {
60 this.file = file;
61 this.conn = this.file.getConnection();
62 this.callback = callback;
63 }
64
65 public void nextStage()
66 {
67 if (this.file.hasFid())
68 {
69 // We have a fid, so get the stat of the file
70 TstatMessage tStatMsg = new TstatMessage(this.file.getFid());
71 this.conn.sendAsync(tStatMsg, this);
72 }
73 else
74 {
75 // We need to get a fid for this file
76 this.file.walkFidAsync(this);
77 }
78 }
79
80 public void replyArrived(StyxMessage rMessage, StyxMessage tMessage)
81 {
82 if (rMessage instanceof RwalkMessage)
83 {
84 // This is the reply from getFidAsync(). Now proceed to the
85 // next stage (get the stat of the file)
86 this.nextStage();
87 }
88 else
89 {
90 // This must be an RstatMessage
91 RstatMessage rStatMsg = (RstatMessage)rMessage;
92 // Setting the DirEntry will also set the Qid of the file
93 this.file.setDirEntry(rStatMsg.getDirEntry());
94 if (this.callback != null)
95 {
96 this.callback.replyArrived(rMessage, tMessage);
97 }
98 else
99 {
100 this.file.fireStatChanged(rStatMsg);
101 }
102 }
103 }
104
105 public void error(String message, StyxMessage tMessage)
106 {
107 String errMsg = "Error getting stat of " + this.file.getPath() + ": " + message;
108 if (this.callback != null)
109 {
110 this.callback.error(errMsg, tMessage);
111 }
112 else
113 {
114 this.file.fireError(errMsg);
115 }
116 }
117 }