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.TopenMessage;
37  import uk.ac.rdg.resc.jstyx.messages.RopenMessage;
38  import uk.ac.rdg.resc.jstyx.messages.RwalkMessage;
39  
40  /***
41   * Callback used when opening a file (see CStyxFile.openAsync())
42   *
43   * @author jdb
44   * $Revision: 604 $
45   * $Date: 2006-03-21 14:58:42 +0000 (Tue, 21 Mar 2006) $
46   * $Log$
47   * Revision 1.3  2006/03/21 14:58:42  jonblower
48   * Implemented clear-text password-based authentication and did some simple tests
49   *
50   * Revision 1.2  2005/08/08 09:36:19  jonblower
51   * Minor changes
52   *
53   * Revision 1.1  2005/08/05 13:46:40  jonblower
54   * Factored out all callback objects from CStyxFile into separate classes
55   *
56   */
57  
58  public class OpenCallback extends MessageCallback
59  {
60      private CStyxFile file;
61      private StyxConnection conn;
62      private int theMode;
63      private MessageCallback callback;
64  
65      public OpenCallback(CStyxFile file, int mode, MessageCallback callback)
66      {
67          this.file = file;
68          this.conn = this.file.getConnection();
69          this.theMode = mode;
70          this.callback = callback;
71      }
72  
73      public void nextStage()
74      {
75          if (!this.file.hasFid())
76          {
77              this.file.walkFidAsync(this);
78          }
79          else if (!this.file.isOpen())
80          {
81              // We have a fid that we can open. Open the file
82              TopenMessage tOpenMsg = new TopenMessage(this.file.getFid(), this.theMode);
83              this.conn.sendAsync(tOpenMsg, this, this.file.isAuth());
84          }
85          else
86          {
87              // The file is already open. Check that the mode is correct
88              if (this.file.getMode() == this.theMode)
89              {
90                  if (this.callback != null)
91                  {
92                      // Notify callback with dummy Ropen message
93                      // TODO will this null cause problems further down the line?
94                      this.callback.replyArrived(new RopenMessage(null, -1), null);                        
95                  }
96                  else
97                  {
98                      this.file.fireOpen();
99                  }
100             }
101             else
102             {
103                 this.error("File already open under a different mode", null);
104             }
105         }
106     }
107 
108     public void replyArrived(StyxMessage rMessage, StyxMessage tMessage)
109     {
110         if (rMessage instanceof RwalkMessage)
111         {
112             // This is the reponse from getFidAsync()
113             // We've just got the open fid for the file. Go to the next stage
114             // (i.e. actually open the file)
115             this.nextStage();
116         }
117         else
118         {
119             // Must be an RopenMessage
120             RopenMessage rOpenMsg = (RopenMessage)rMessage;
121             this.file.setMode(this.theMode);
122             this.file.setIoUnit((int)rOpenMsg.getIoUnit());
123             if (this.callback != null)
124             {
125                 this.callback.replyArrived(rMessage, tMessage);
126             }
127             else
128             {
129                 this.file.fireOpen();
130             }
131         }
132     }
133 
134     public void error(String message, StyxMessage tMessage)
135     {
136         String errMsg = "Error opening " + this.file.getPath() + ": " + message;
137         if (this.callback != null)
138         {
139             this.callback.error(errMsg, tMessage);
140         }
141         else
142         {
143             this.file.fireError(errMsg);
144         }
145     }
146 }