View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.websocket.common.extensions.mux.op;
20  
21  import java.nio.ByteBuffer;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.eclipse.jetty.websocket.common.extensions.mux.MuxControlBlock;
26  import org.eclipse.jetty.websocket.common.extensions.mux.MuxOp;
27  
28  public class MuxDropChannel implements MuxControlBlock
29  {
30      /**
31       * Outlined in <a href="https://tools.ietf.org/html/draft-ietf-hybi-websocket-multiplexing-05#section-9.4.1">Section 9.4.1. Drop Reason Codes</a>
32       */
33      public static enum Reason
34      {
35          // Normal Close : (1000-1999)
36          NORMAL_CLOSURE(1000),
37  
38          // Failures in Physical Connection : (2000-2999)
39          PHYSICAL_CONNECTION_FAILED(2000),
40          INVALID_ENCAPSULATING_MESSAGE(2001),
41          CHANNEL_ID_TRUNCATED(2002),
42          ENCAPSULATED_FRAME_TRUNCATED(2003),
43          UNKNOWN_MUX_CONTROL_OPC(2004),
44          UNKNOWN_MUX_CONTROL_BLOCK(2005),
45          CHANNEL_ALREADY_EXISTS(2006),
46          NEW_CHANNEL_SLOT_VIOLATION(2007),
47          NEW_CHANNEL_SLOT_OVERFLOW(2008),
48          BAD_REQUEST(2009),
49          UNKNOWN_REQUEST_ENCODING(2010),
50          BAD_RESPONSE(2011),
51          UNKNOWN_RESPONSE_ENCODING(2012),
52  
53          // Failures in Logical Connections : (3000-3999)
54          LOGICAL_CHANNEL_FAILED(3000),
55          SEND_QUOTA_VIOLATION(3005),
56          SEND_QUOTA_OVERFLOW(3006),
57          IDLE_TIMEOUT(3007),
58          DROP_CHANNEL_ACK(3008),
59  
60          // Other Peer Actions : (4000-4999)
61          USE_ANOTHER_PHYSICAL_CONNECTION(4001),
62          BUSY(4002);
63  
64          private static final Map<Integer, Reason> codeMap;
65  
66          static
67          {
68              codeMap = new HashMap<>();
69              for (Reason r : values())
70              {
71                  codeMap.put(r.getValue(),r);
72              }
73          }
74  
75          public static Reason valueOf(int code)
76          {
77              return codeMap.get(code);
78          }
79  
80          private final int code;
81  
82          private Reason(int code)
83          {
84              this.code = code;
85          }
86  
87          public int getValue()
88          {
89              return code;
90          }
91      }
92  
93      public static MuxDropChannel parse(long channelId, ByteBuffer payload)
94      {
95          // TODO Auto-generated method stub
96          return null;
97      }
98  
99      private final long channelId;
100     private final Reason code;
101     private String phrase;
102     private int rsv;
103 
104     /**
105      * Normal Drop. no reason Phrase.
106      * 
107      * @param channelId
108      *            the logical channel Id to perform drop against.
109      */
110     public MuxDropChannel(long channelId)
111     {
112         this(channelId,Reason.NORMAL_CLOSURE,null);
113     }
114 
115     /**
116      * Drop with reason code and optional phrase
117      * 
118      * @param channelId
119      *            the logical channel Id to perform drop against.
120      * @param code
121      *            reason code
122      * @param phrase
123      *            optional human readable phrase
124      */
125     public MuxDropChannel(long channelId, int code, String phrase)
126     {
127         this(channelId, Reason.valueOf(code), phrase);
128     }
129 
130     /**
131      * Drop with reason code and optional phrase
132      * 
133      * @param channelId
134      *            the logical channel Id to perform drop against.
135      * @param code
136      *            reason code
137      * @param phrase
138      *            optional human readable phrase
139      */
140     public MuxDropChannel(long channelId, Reason code, String phrase)
141     {
142         this.channelId = channelId;
143         this.code = code;
144         this.phrase = phrase;
145     }
146 
147     public ByteBuffer asReasonBuffer()
148     {
149         // TODO: convert to reason buffer
150         return null;
151     }
152 
153     public long getChannelId()
154     {
155         return channelId;
156     }
157 
158     public Reason getCode()
159     {
160         return code;
161     }
162 
163     @Override
164     public int getOpCode()
165     {
166         return MuxOp.DROP_CHANNEL;
167     }
168 
169     public String getPhrase()
170     {
171         return phrase;
172     }
173 
174     public int getRsv()
175     {
176         return rsv;
177     }
178 
179     public void setRsv(int rsv)
180     {
181         this.rsv = rsv;
182     }
183 }