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 uk.ac.rdg.resc.jstyx.StyxUtils;
32  
33  /***
34   * Message sent to negotiate the protocol version and maximum message size of a 
35   * Styx Connection
36   *
37   * @author Jon Blower
38   * $Revision: 507 $
39   * $Date: 2005-12-01 08:21:56 +0000 (Thu, 01 Dec 2005) $
40   * $Log$
41   * Revision 1.5  2005/12/01 08:21:56  jonblower
42   * Fixed javadoc comments
43   *
44   * Revision 1.4  2005/03/15 09:01:48  jonblower
45   * Message type now stored as short, not int
46   *
47   * Revision 1.3  2005/03/11 14:02:16  jonblower
48   * Merged MINA-Test_20059309 into main line of development
49   *
50   * Revision 1.2.2.1  2005/03/10 11:50:59  jonblower
51   * Changed to fit with MINA framework
52   *
53   * Revision 1.1  2005/03/09 16:58:42  jonblower
54   * Changes to MINA-related classes
55   *
56   * Revision 1.2  2005/02/24 07:44:44  jonblower
57   * Added getFriendlyString()
58   *
59   * Revision 1.1.1.1  2005/02/16 18:58:29  jonblower
60   * Initial import
61   *
62   */
63  public class TversionMessage extends StyxMessage
64  {
65      
66      private long maxMessageSize; // The maximum size of a message that will be 
67                                   // sent on this connection by either party
68      private String version;      // The version of the protocol 
69      
70      /*** 
71       * Creates a new TversionMessage. This constructor will be called by the
72       * StyxMessage.createStyxMessage() method
73       * @param length The total length of the message (including all header info)
74       * @param type The type of the message (a number between 100 and 127)
75       * @param tag The tag that identifies this message
76       */
77      public TversionMessage(int length, short type, int tag)
78      {
79          super(length, type, tag);
80          this.name = "Tversion";
81      }
82      
83      /***
84       * Default constructor, sets version to "9P2000" and maximum message size
85       * to 8216 (so that 8192 bytes can be read or written with a Tread/Twrite)
86       */
87      public TversionMessage()
88      {
89          this(19, (short)100, StyxUtils.NOTAG);
90          this.maxMessageSize = 8216;
91          this.setVersion("9P2000");
92      }
93      
94      /***
95       * Creates a new TversionMessage, with the supplied maximum message size.
96       * Sets the version to "9P2000"
97       * @param maxMessageSize The requested maximum size of a message that will 
98       * be sent on this connection by either party
99       */
100     public TversionMessage(long maxMessageSize)
101     {
102         this();
103         this.maxMessageSize = maxMessageSize;
104     }
105     
106     /***
107      * Creates a new TversionMessage, with the supplied maximum message size
108      * and version string. Note that the version string should always be "9P2000";
109      * this constructor is used mainly for debugging.
110      * @param maxMessageSize The requested maximum size of a message that will
111      * be sent on this connection by either party
112      * @param protocolVersion The version string (Should always be "9P2000")
113      */
114     public TversionMessage(long maxMessageSize, String protocolVersion)
115     {
116         this();
117         this.maxMessageSize = maxMessageSize;
118         this.setVersion(protocolVersion);
119     }
120     
121     protected final void decodeBody(StyxBuffer styxBuf)
122     {
123         // Read the maximum message size
124         this.maxMessageSize = styxBuf.getUInt();
125         // Read the version string
126         this.version = styxBuf.getString();
127     }
128     
129     protected final void encodeBody(StyxBuffer styxBuf)
130     {
131         // Write the max message size, then the version string
132         styxBuf.putUInt(this.maxMessageSize).putString(this.version);
133     }
134     
135     /***
136      * @return The requested maximum size of a message that will 
137      * be sent on this connection by either party
138      */
139     public long getMaxMessageSize()
140     {
141         return this.maxMessageSize;
142     }
143     
144     /***
145      * @param maxMessageSize The requested maximum size of a message that will 
146      * be sent on this connection by either party
147      */
148     public void setMaxMessageSize(long maxMessageSize)
149     {
150         this.maxMessageSize = maxMessageSize;
151     }
152     
153     /***
154      * @return The version string (normally "9P2000")
155      */
156     public String getVersion()
157     {
158         return this.version;
159     }
160     
161     /***
162      * @param version The version string (normally "9P2000")
163      */
164     public void setVersion(String version)
165     {
166         this.version = version;
167         int versionLen = StyxUtils.strToUTF8(version).length;
168         this.length = StyxUtils.HEADER_LENGTH + 4 + 2 + versionLen;
169     }
170     
171     protected String getElements()
172     {
173         return ", " + this.maxMessageSize + ", " + this.version;
174     }
175     
176     public String toFriendlyString()
177     {
178         return "Max msg size: " + this.maxMessageSize + " bytes, version = "
179             + this.version;
180     }
181     
182 }