jdk/src/java.httpclient/share/classes/java/net/http/SettingsFrame.java
changeset 42483 3850c235c3fb
parent 42482 15297dde0d55
parent 42479 a80dbf731cbe
child 42489 a9e4de33da2e
equal deleted inserted replaced
42482:15297dde0d55 42483:3850c235c3fb
     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  * questions.
       
    24  */
       
    25 
       
    26 package java.net.http;
       
    27 
       
    28 import java.io.IOException;
       
    29 import java.nio.ByteBuffer;
       
    30 
       
    31 class SettingsFrame extends Http2Frame {
       
    32 
       
    33     int[] parameters;
       
    34 
       
    35     public static final int TYPE = 0x4;
       
    36 
       
    37     // Flags
       
    38     public static final int ACK = 0x1;
       
    39 
       
    40     @Override
       
    41     String flagAsString(int flag) {
       
    42         switch (flag) {
       
    43         case ACK:
       
    44             return "ACK";
       
    45         }
       
    46         return super.flagAsString(flag);
       
    47     }
       
    48 
       
    49     @Override
       
    50     public String toString() {
       
    51         StringBuilder sb = new StringBuilder();
       
    52         sb.append(super.toString())
       
    53           .append(" Settings: ");
       
    54 
       
    55         for (int i = 0; i < MAX_PARAM; i++) {
       
    56             if (parameters[i] != -1) {
       
    57                 sb.append(name(i))
       
    58                   .append("=")
       
    59                   .append(Integer.toString(parameters[i]))
       
    60                   .append(' ');
       
    61             }
       
    62         }
       
    63         return sb.toString();
       
    64     }
       
    65 
       
    66     // Parameters
       
    67     public static final int HEADER_TABLE_SIZE = 0x1;
       
    68     public static final int ENABLE_PUSH = 0x2;
       
    69     public static final int MAX_CONCURRENT_STREAMS = 0x3;
       
    70     public static final int INITIAL_WINDOW_SIZE = 0x4;
       
    71     public static final int MAX_FRAME_SIZE = 0x5;
       
    72     public static final int MAX_HEADER_LIST_SIZE = 0x6;
       
    73 
       
    74     private String name(int i) {
       
    75         switch (i+1) {
       
    76         case HEADER_TABLE_SIZE:
       
    77             return "HEADER_TABLE_SIZE";
       
    78         case ENABLE_PUSH:
       
    79             return "ENABLE_PUSH";
       
    80         case MAX_CONCURRENT_STREAMS:
       
    81             return "MAX_CONCURRENT_STREAMS";
       
    82         case INITIAL_WINDOW_SIZE:
       
    83             return "INITIAL_WINDOW_SIZE";
       
    84         case MAX_FRAME_SIZE:
       
    85             return "MAX_FRAME_SIZE";
       
    86         case MAX_HEADER_LIST_SIZE:
       
    87             return "MAX_HEADER_LIST_SIZE";
       
    88         }
       
    89         return "unknown parameter";
       
    90     }
       
    91     public static final int MAX_PARAM = 0x6;
       
    92 
       
    93     public SettingsFrame() {
       
    94         type = TYPE;
       
    95         parameters = new int [MAX_PARAM];
       
    96         for (int i=0; i < parameters.length; i++) {
       
    97             parameters[i] = -1;
       
    98         }
       
    99     }
       
   100 
       
   101     public int getParameter(int paramID) {
       
   102         if (paramID > MAX_PARAM) {
       
   103             throw new IllegalArgumentException("illegal parameter");
       
   104         }
       
   105         return parameters[paramID-1];
       
   106     }
       
   107 
       
   108     public SettingsFrame setParameter(int paramID, int value) {
       
   109         if (paramID > MAX_PARAM) {
       
   110             throw new IllegalArgumentException("illegal parameter");
       
   111         }
       
   112         parameters[paramID-1] = value;
       
   113         return this;
       
   114     }
       
   115 
       
   116     @Override
       
   117     void readIncomingImpl(ByteBufferConsumer bc) throws IOException {
       
   118         if (length % 6 != 0) {
       
   119             throw new IOException("Protocol error: invalid settings frame");
       
   120         }
       
   121         int n = length / 6;
       
   122         for (int i=0; i<n; i++) {
       
   123             int id = bc.getShort();
       
   124             int val = bc.getInt();
       
   125             if (id > 0 || id <= MAX_PARAM) {
       
   126                 // a known parameter. Ignore otherwise
       
   127                 parameters[id-1] = val;
       
   128             }
       
   129         }
       
   130     }
       
   131 
       
   132     @Override
       
   133     void computeLength() {
       
   134         length = 0;
       
   135         for (int i : parameters) {
       
   136             if (i != -1) {
       
   137                 length += 6;
       
   138             }
       
   139         }
       
   140     }
       
   141 
       
   142     @Override
       
   143     void writeOutgoing(ByteBufferGenerator bg) {
       
   144         super.writeOutgoing(bg);
       
   145         ByteBuffer buf = bg.getBuffer(length);
       
   146         for (int i = 0; i < MAX_PARAM; i++) {
       
   147             if (parameters[i] != -1) {
       
   148                 buf.putShort((short)(i+1));
       
   149                 buf.putInt(parameters[i]);
       
   150             }
       
   151         }
       
   152     }
       
   153 
       
   154     private static final int K = 1024;
       
   155 
       
   156     public static SettingsFrame getDefaultSettings() {
       
   157         SettingsFrame f = new SettingsFrame();
       
   158         // TODO: check these values
       
   159         f.setParameter(ENABLE_PUSH, 1);
       
   160         f.setParameter(HEADER_TABLE_SIZE, 4 * K);
       
   161         f.setParameter(MAX_CONCURRENT_STREAMS, 35);
       
   162         f.setParameter(INITIAL_WINDOW_SIZE, 16 * K);
       
   163         f.setParameter(MAX_FRAME_SIZE, 16 * K);
       
   164         return f;
       
   165     }
       
   166 }