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.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.TwalkMessage;
37  import uk.ac.rdg.resc.jstyx.messages.RwalkMessage;
38  
39  /***
40   * A callback object used to get a fid for a CStyxFile.
41   *
42   * @author Jon Blower
43   * $Revision: 347 $
44   * $Date: 2005-08-05 14:46:40 +0100 (Fri, 05 Aug 2005) $
45   * $Log$
46   * Revision 1.1  2005/08/05 13:46:40  jonblower
47   * Factored out all callback objects from CStyxFile into separate classes
48   *
49   */
50      
51  public class GetFidCallback extends MessageCallback
52  {
53      private MessageCallback callback;
54      private CStyxFile file;
55      private StyxConnection conn;
56  
57      public GetFidCallback(CStyxFile file, MessageCallback callback)
58      {
59          this.file = file;
60          this.conn = this.file.getConnection();
61          this.callback = callback;
62      }
63  
64      public void walkFid()
65      {
66          // Need to get a fid for this file
67          TwalkMessage tWalkMsg = new TwalkMessage(this.conn.getRootFid(),
68              this.conn.getFreeFid(), this.file.getPath());
69          this.conn.sendAsync(tWalkMsg, this);
70      }
71  
72      public void replyArrived(StyxMessage rMessage, StyxMessage tMessage)
73      {
74          // Message can only be an RwalkMessage
75          RwalkMessage rWalkMsg = (RwalkMessage)rMessage;
76          TwalkMessage tWalkMsg = (TwalkMessage)tMessage;
77          // Check that the walk was successful
78          if (tWalkMsg.getNumPathElements() == rWalkMsg.getNumSuccessfulWalks())
79          {
80              this.file.setFid(tWalkMsg.getNewFid());
81              if (rWalkMsg.getNumSuccessfulWalks() > 0)
82              {
83                  // We can't get the qid from the Rwalk if this was a zero-
84                  // length walk
85                  this.file.setQid(rWalkMsg.getQid(rWalkMsg.getNumSuccessfulWalks() - 1));
86              }
87              if (this.callback != null)
88              {
89                  this.callback.replyArrived(rWalkMsg, tMessage);
90              }
91          }
92          else
93          {
94              // The walk failed at some point
95              String errMsg = "'" + 
96                  tWalkMsg.getPathElements()[rWalkMsg.getNumSuccessfulWalks()]
97                  + "' does not exist.";
98              this.error(errMsg, tWalkMsg);
99          }
100     }
101 
102     public void error(String message, StyxMessage tMessage)
103     {
104         // The walk was not successful.  Return the fid to the pool
105         // and throw an error.
106         if (tMessage != null)
107         {
108             TwalkMessage tWalkMsg = (TwalkMessage)tMessage;
109             conn.returnFid(tWalkMsg.getNewFid());
110         }
111         String errMsg = "Error getting fid for " + this.file.getPath() + ": " + message;
112         if (this.callback != null)
113         {
114             this.callback.error(errMsg, tMessage);
115         }
116         else
117         {
118             this.file.fireError(errMsg);
119         }
120     }
121 }