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 org.apache.mina.protocol.ProtocolViolationException;
32  
33  import uk.ac.rdg.resc.jstyx.types.ULong;
34  
35  /***
36   * Message sent to read data from a file
37   *
38   * @author Jon Blower
39   * $Revision: 176 $
40   * $Date: 2005-03-24 09:48:32 +0000 (Thu, 24 Mar 2005) $
41   * $Log$
42   * Revision 1.5  2005/03/24 09:48:31  jonblower
43   * Changed 'count' from long to int throughout for reading and writing
44   *
45   * Revision 1.4  2005/03/15 09:01:48  jonblower
46   * Message type now stored as short, not int
47   *
48   * Revision 1.3  2005/03/11 14:02:16  jonblower
49   * Merged MINA-Test_20059309 into main line of development
50   *
51   * Revision 1.2.2.1  2005/03/10 11:50:59  jonblower
52   * Changed to fit with MINA framework
53   *
54   * Revision 1.2  2005/02/24 07:44:44  jonblower
55   * Added getFriendlyString()
56   *
57   * Revision 1.1.1.1  2005/02/16 18:58:29  jonblower
58   * Initial import
59   *
60   */
61  public class TreadMessage extends StyxMessage
62  {
63      
64      private long fid;     // The fid to read data from
65      private ULong offset; // The offset in the file from which to read data
66      private int count;    // The number of bytes to read from the file    
67      
68      /***
69       * Creates a new TreadMessage. This constructor will be called by the
70       * MessageRecognizer.
71       * @param length The total length of the message (including all header info)
72       * @param type The type of the message (a number between 100 and 127)
73       * @param tag The tag that identifies this message
74       */
75      public TreadMessage(int length, short type, int tag)
76      {
77          super(length, type, tag);
78          this.name = "Tread";
79      }
80      
81      public TreadMessage(long fid, ULong offset, int count)
82      {
83          this(23, (short)116, 0); // The tag will be set later
84          this.fid = fid;
85          this.offset = offset;
86          if (count < 0)
87          {
88              throw new IllegalArgumentException("count < 0");
89          }
90          this.count = count;
91      }
92      
93      protected final void decodeBody(StyxBuffer buf)
94          throws ProtocolViolationException
95      {
96          this.fid = buf.getUInt();
97          this.offset = buf.getULong();
98          long lngCount = buf.getUInt();
99          if (lngCount < 0 || lngCount > Integer.MAX_VALUE)
100         {
101             throw new ProtocolViolationException("Got illegal count of " + lngCount);
102         }
103         // We now know that this cast is safe
104         this.count = (int)lngCount;
105     }
106     
107     protected final void encodeBody(StyxBuffer buf)
108     {
109         buf.putUInt(this.fid).putULong(this.offset).putUInt(this.count);
110     }
111     
112     public long getFid()
113     {
114         return fid;
115     }
116     
117     public void setFid(long fid)
118     {
119         this.fid = fid;
120     }
121 
122     public ULong getOffset()
123     {
124         return offset;
125     }
126 
127     public void setOffset(ULong offset)
128     {
129         this.offset = offset;
130     }
131 
132     public int getCount()
133     {
134         return count;
135     }
136 
137     public void setCount(int count)
138     {
139         this.count = count;
140     }
141     
142     protected String getElements()
143     {
144         return ", " + this.fid + ", " + this.offset + ", " + this.count;
145     }
146     
147     public String toFriendlyString()
148     {
149         return "fid: " + this.fid + ", offset: " + this.offset + ", count: "
150             + this.count;
151     }
152     
153 }