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 jdk.incubator.http.internal.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     @Override
 104     public int type() {
 105         return TYPE;
 106     }
 107 
 108     public int getParameter(int paramID) {
 109         if (paramID > MAX_PARAM) {
 110             throw new IllegalArgumentException("illegal parameter");
 111         }
 112         return parameters[paramID-1];
 113     }
 114 
 115     public SettingsFrame setParameter(int paramID, int value) {
 116         if (paramID > MAX_PARAM) {
 117             throw new IllegalArgumentException("illegal parameter");
 118         }
 119         parameters[paramID-1] = value;
 120         return this;
 121     }
 122 
 123     int length() {
 124         int len = 0;
 125         for (int i : parameters) {
 126             if (i != -1) {
 127                 len  += 6;
 128             }
 129         }
 130         return len;
 131     }
 132 
 133     void toByteBuffer(ByteBuffer buf) {
 134         for (int i = 0; i < MAX_PARAM; i++) {
 135             if (parameters[i] != -1) {
 136                 buf.putShort((short) (i + 1));
 137                 buf.putInt(parameters[i]);
 138             }
 139         }
 140     }
 141 
 142     public byte[] toByteArray() {
 143         byte[] bytes = new byte[length()];
 144         ByteBuffer buf = ByteBuffer.wrap(bytes);
 145         toByteBuffer(buf);
 146         return bytes;
 147     }
 148 
 149     private static final int K = 1024;
 150 
 151     public static SettingsFrame getDefaultSettings() {
 152         SettingsFrame f = new SettingsFrame();
 153         // TODO: check these values
 154         f.setParameter(ENABLE_PUSH, 1);
 155         f.setParameter(HEADER_TABLE_SIZE, 4 * K);
 156         f.setParameter(MAX_CONCURRENT_STREAMS, 35);
 157         f.setParameter(INITIAL_WINDOW_SIZE, 64 * K - 1);
 158         f.setParameter(MAX_FRAME_SIZE, 16 * K);
 159         return f;
 160     }
 161 }