src/java.net.http/share/classes/jdk/internal/net/http/frame/SettingsFrame.java
branchhttp-client-branch
changeset 56092 fd85b2bf2b0d
parent 56089 42208b2f224e
child 56227 278e1c6c3e99
equal deleted inserted replaced
56091:aedd6133e7a0 56092:fd85b2bf2b0d
       
     1 /*
       
     2  * Copyright (c) 2015, 2018, 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 jdk.internal.net.http.frame;
       
    27 
       
    28 import java.nio.ByteBuffer;
       
    29 import java.util.Arrays;
       
    30 
       
    31 public class SettingsFrame extends Http2Frame {
       
    32 
       
    33     private final int[] parameters;
       
    34 
       
    35     public static final int TYPE = 0x4;
       
    36 
       
    37     // Flags
       
    38     public static final int ACK = 0x1;
       
    39 
       
    40     @Override
       
    41     public 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(int flags) {
       
    94         super(0, flags);
       
    95         parameters = new int [MAX_PARAM];
       
    96         Arrays.fill(parameters, -1);
       
    97     }
       
    98 
       
    99     public SettingsFrame() {
       
   100         this(0);
       
   101     }
       
   102 
       
   103     public SettingsFrame(SettingsFrame other) {
       
   104         super(0, other.flags);
       
   105         parameters = Arrays.copyOf(other.parameters, MAX_PARAM);
       
   106     }
       
   107 
       
   108     @Override
       
   109     public int type() {
       
   110         return TYPE;
       
   111     }
       
   112 
       
   113     public int getParameter(int paramID) {
       
   114         if (paramID > MAX_PARAM) {
       
   115             throw new IllegalArgumentException("illegal parameter");
       
   116         }
       
   117         return parameters[paramID-1];
       
   118     }
       
   119 
       
   120     public SettingsFrame setParameter(int paramID, int value) {
       
   121         if (paramID > MAX_PARAM) {
       
   122             throw new IllegalArgumentException("illegal parameter");
       
   123         }
       
   124         parameters[paramID-1] = value;
       
   125         return this;
       
   126     }
       
   127 
       
   128     int length() {
       
   129         int len = 0;
       
   130         for (int i : parameters) {
       
   131             if (i != -1) {
       
   132                 len  += 6;
       
   133             }
       
   134         }
       
   135         return len;
       
   136     }
       
   137 
       
   138     void toByteBuffer(ByteBuffer buf) {
       
   139         for (int i = 0; i < MAX_PARAM; i++) {
       
   140             if (parameters[i] != -1) {
       
   141                 buf.putShort((short) (i + 1));
       
   142                 buf.putInt(parameters[i]);
       
   143             }
       
   144         }
       
   145     }
       
   146 
       
   147     public byte[] toByteArray() {
       
   148         byte[] bytes = new byte[length()];
       
   149         ByteBuffer buf = ByteBuffer.wrap(bytes);
       
   150         toByteBuffer(buf);
       
   151         return bytes;
       
   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, 64 * K - 1);
       
   163         f.setParameter(MAX_FRAME_SIZE, 16 * K);
       
   164         return f;
       
   165     }
       
   166 }