< prev index next >
1 /*
2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 */
24
25 package java.net.http;
26
27 import java.io.IOException;
28 import java.nio.ByteBuffer;
29
30 class SettingsFrame extends Http2Frame {
31
32 int[] parameters;
33
34 public final static int TYPE = 0x4;
35
36 // Flags
37 public static final int ACK = 0x1;
38
39 @Override
40 String flagAsString(int flag) {
41 switch (flag) {
42 case ACK:
43 return "ACK";
44 }
45 return super.flagAsString(flag);
46 }
47
48 @Override
49 public String toString() {
50 StringBuilder sb = new StringBuilder();
51 sb.append(super.toString())
52 .append(" Settings: ");
53
54 for (int i = 0; i < MAX_PARAM; i++) {
55 if (parameters[i] != -1) {
56 sb.append(name(i))
57 .append("=")
58 .append(Integer.toString(parameters[i]))
59 .append(' ');
60 }
61 }
62 return sb.toString();
63 }
64
65 // Parameters
66 public static final int HEADER_TABLE_SIZE = 0x1;
67 public static final int ENABLE_PUSH = 0x2;
68 public static final int MAX_CONCURRENT_STREAMS = 0x3;
69 public static final int INITIAL_WINDOW_SIZE = 0x4;
70 public static final int MAX_FRAME_SIZE = 0x5;
71 public static final int MAX_HEADER_LIST_SIZE = 0x6;
72
73 private String name(int i) {
74 switch (i+1) {
75 case HEADER_TABLE_SIZE:
76 return "HEADER_TABLE_SIZE";
77 case ENABLE_PUSH:
78 return "ENABLE_PUSH";
79 case MAX_CONCURRENT_STREAMS:
80 return "MAX_CONCURRENT_STREAMS";
81 case INITIAL_WINDOW_SIZE:
82 return "INITIAL_WINDOW_SIZE";
83 case MAX_FRAME_SIZE:
84 return "MAX_FRAME_SIZE";
85 case MAX_HEADER_LIST_SIZE:
86 return "MAX_HEADER_LIST_SIZE";
87 }
88 return "unknown parameter";
89 }
90 public static final int MAX_PARAM = 0x6;
91
92 public SettingsFrame() {
93 type = TYPE;
94 parameters = new int [MAX_PARAM];
95 for (int i=0; i < parameters.length; i++) {
96 parameters[i] = -1;
97 }
98 }
99
100 public int getParameter(int paramID) {
101 if (paramID > MAX_PARAM) {
102 throw new IllegalArgumentException("illegal parameter");
103 }
104 return parameters[paramID-1];
105 }
106
107 public SettingsFrame setParameter(int paramID, int value) {
108 if (paramID > MAX_PARAM) {
109 throw new IllegalArgumentException("illegal parameter");
110 }
111 parameters[paramID-1] = value;
112 return this;
113 }
114
115 @Override
116 void readIncomingImpl(ByteBufferConsumer bc) throws IOException {
117 if (length % 6 != 0) {
118 throw new IOException("Protocol error: invalid settings frame");
119 }
120 int n = length / 6;
121 for (int i=0; i<n; i++) {
122 int id = bc.getShort();
123 int val = bc.getInt();
124 if (id > 0 || id <= MAX_PARAM) {
125 // a known parameter. Ignore otherwise
126 parameters[id-1] = val;
127 }
128 }
129 }
130
131 @Override
132 void setLength() {
133 length = 0;
134 for (int i : parameters) {
135 if (i != -1) {
136 length += 6;
137 }
138 }
139 }
140
141 @Override
142 void writeOutgoing(ByteBufferGenerator bg) {
143 super.writeOutgoing(bg);
144 ByteBuffer buf = bg.getBuffer(length);
145 for (int i = 0; i < MAX_PARAM; i++) {
146 if (parameters[i] != -1) {
147 buf.putShort((short)(i+1));
148 buf.putInt(parameters[i]);
149 }
150 }
151 }
152
153 private static final int K = 1024;
154
155 public static SettingsFrame getDefaultSettings() {
156 SettingsFrame f = new SettingsFrame();
157 // TODO: check these values
158 f.setParameter(ENABLE_PUSH, 1);
159 f.setParameter(HEADER_TABLE_SIZE, 4 * K);
160 f.setParameter(MAX_CONCURRENT_STREAMS, 35);
161 f.setParameter(INITIAL_WINDOW_SIZE, 16 * K);
162 f.setParameter(MAX_FRAME_SIZE, 16 * K);
163 return f;
164 }
165 }
< prev index next >