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.spdy.api;
20
21 import java.util.HashMap;
22 import java.util.Map;
23
24 /**
25 * <p>An enumeration of stream statuses.</p>
26 */
27 public enum StreamStatus
28 {
29 /**
30 * <p>The stream status indicating a protocol error</p>
31 */
32 PROTOCOL_ERROR(1, 1),
33 /**
34 * <p>The stream status indicating that the stream is not valid</p>
35 */
36 INVALID_STREAM(2, 2),
37 /**
38 * <p>The stream status indicating that the stream has been refused</p>
39 */
40 REFUSED_STREAM(3, 3),
41 /**
42 * <p>The stream status indicating that the implementation does not support the SPDY version of the stream</p>
43 */
44 UNSUPPORTED_VERSION(4, 4),
45 /**
46 * <p>The stream status indicating that the stream is no longer needed</p>
47 */
48 CANCEL_STREAM(5, 5),
49 /**
50 * <p>The stream status indicating an implementation error</p>
51 */
52 INTERNAL_ERROR(6, 6),
53 /**
54 * <p>The stream status indicating a flow control error</p>
55 */
56 FLOW_CONTROL_ERROR(7, 7),
57 /**
58 * <p>The stream status indicating a stream opened more than once</p>
59 */
60 STREAM_IN_USE(-1, 8),
61 /**
62 * <p>The stream status indicating data on a stream already closed</p>
63 */
64 STREAM_ALREADY_CLOSED(-1, 9),
65 /**
66 * <p>The stream status indicating credentials not valid</p>
67 */
68 INVALID_CREDENTIALS(-1, 10),
69 /**
70 * <p>The stream status indicating that the implementation could not support a frame too large</p>
71 */
72 FRAME_TOO_LARGE(-1, 11);
73
74 /**
75 * @param version the SPDY protocol version
76 * @param code the stream status code
77 * @return a {@link StreamStatus} from the given version and code,
78 * or null if no such status exists
79 */
80 public static StreamStatus from(short version, int code)
81 {
82 switch (version)
83 {
84 case SPDY.V2:
85 return Codes.v2Codes.get(code);
86 case SPDY.V3:
87 return Codes.v3Codes.get(code);
88 default:
89 throw new IllegalStateException();
90 }
91 }
92
93 private final int v2Code;
94 private final int v3Code;
95
96 private StreamStatus(int v2Code, int v3Code)
97 {
98 this.v2Code = v2Code;
99 if (v2Code >= 0)
100 Codes.v2Codes.put(v2Code, this);
101 this.v3Code = v3Code;
102 if (v3Code >= 0)
103 Codes.v3Codes.put(v3Code, this);
104 }
105
106 /**
107 * @param version the SPDY protocol version
108 * @return the stream status code
109 */
110 public int getCode(short version)
111 {
112 switch (version)
113 {
114 case SPDY.V2:
115 return v2Code;
116 case SPDY.V3:
117 return v3Code;
118 default:
119 throw new IllegalStateException();
120 }
121 }
122
123 private static class Codes
124 {
125 private static final Map<Integer, StreamStatus> v2Codes = new HashMap<>();
126 private static final Map<Integer, StreamStatus> v3Codes = new HashMap<>();
127 }
128 }