查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2013 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.handler.codec.spdy;
17  
18  import org.jboss.netty.util.internal.StringUtil;
19  
20  import java.util.Map;
21  import java.util.Set;
22  import java.util.TreeMap;
23  
24  /**
25   * The default {@link SpdySettingsFrame} implementation.
26   */
27  public class DefaultSpdySettingsFrame implements SpdySettingsFrame {
28  
29      private boolean clear;
30      private final Map<Integer, Setting> settingsMap = new TreeMap<Integer, Setting>();
31  
32      public Set<Integer> getIds() {
33          return settingsMap.keySet();
34      }
35  
36      public boolean isSet(int id) {
37          Integer key = id;
38          return settingsMap.containsKey(key);
39      }
40  
41      public int getValue(int id) {
42          Integer key = id;
43          if (settingsMap.containsKey(key)) {
44              return settingsMap.get(key).getValue();
45          } else {
46              return -1;
47          }
48      }
49  
50      public void setValue(int id, int value) {
51          setValue(id, value, false, false);
52      }
53  
54      public void setValue(int id, int value, boolean persistValue, boolean persisted) {
55          if (id < 0 || id > SpdyCodecUtil.SPDY_SETTINGS_MAX_ID) {
56              throw new IllegalArgumentException("Setting ID is not valid: " + id);
57          }
58          Integer key = id;
59          if (settingsMap.containsKey(key)) {
60              Setting setting = settingsMap.get(key);
61              setting.setValue(value);
62              setting.setPersist(persistValue);
63              setting.setPersisted(persisted);
64          } else {
65              settingsMap.put(key, new Setting(value, persistValue, persisted));
66          }
67      }
68  
69      public void removeValue(int id) {
70          Integer key = id;
71          if (settingsMap.containsKey(key)) {
72              settingsMap.remove(key);
73          }
74      }
75  
76      public boolean isPersistValue(int id) {
77          Integer key = id;
78          if (settingsMap.containsKey(key)) {
79              return settingsMap.get(key).isPersist();
80          } else {
81              return false;
82          }
83      }
84  
85      public void setPersistValue(int id, boolean persistValue) {
86          Integer key = id;
87          if (settingsMap.containsKey(key)) {
88              settingsMap.get(key).setPersist(persistValue);
89          }
90      }
91  
92      public boolean isPersisted(int id) {
93          Integer key = id;
94          if (settingsMap.containsKey(key)) {
95              return settingsMap.get(key).isPersisted();
96          } else {
97              return false;
98          }
99      }
100 
101     public void setPersisted(int id, boolean persisted) {
102         Integer key = id;
103         if (settingsMap.containsKey(key)) {
104             settingsMap.get(key).setPersisted(persisted);
105         }
106     }
107 
108     public boolean clearPreviouslyPersistedSettings() {
109         return clear;
110     }
111 
112     public void setClearPreviouslyPersistedSettings(boolean clear) {
113         this.clear = clear;
114     }
115 
116     private Set<Map.Entry<Integer, Setting>> getSettings() {
117         return settingsMap.entrySet();
118     }
119 
120     private void appendSettings(StringBuilder buf) {
121         for (Map.Entry<Integer, Setting> e: getSettings()) {
122             Setting setting = e.getValue();
123             buf.append("--> ");
124             buf.append(e.getKey().toString());
125             buf.append(':');
126             buf.append(setting.getValue());
127             buf.append(" (persist value: ");
128             buf.append(setting.isPersist());
129             buf.append("; persisted: ");
130             buf.append(setting.isPersisted());
131             buf.append(')');
132             buf.append(StringUtil.NEWLINE);
133         }
134     }
135 
136     @Override
137     public String toString() {
138         StringBuilder buf = new StringBuilder();
139         buf.append(getClass().getSimpleName());
140         buf.append(StringUtil.NEWLINE);
141         appendSettings(buf);
142         buf.setLength(buf.length() - StringUtil.NEWLINE.length());
143         return buf.toString();
144     }
145 
146     private static final class Setting {
147 
148         private int value;
149         private boolean persist;
150         private boolean persisted;
151 
152         Setting(int value, boolean persist, boolean persisted) {
153             this.value = value;
154             this.persist = persist;
155             this.persisted = persisted;
156         }
157 
158         int getValue() {
159             return value;
160         }
161 
162         void setValue(int value) {
163             this.value = value;
164         }
165 
166         boolean isPersist() {
167             return persist;
168         }
169 
170         void setPersist(boolean persist) {
171             this.persist = persist;
172         }
173 
174         boolean isPersisted() {
175             return persisted;
176         }
177 
178         void setPersisted(boolean persisted) {
179             this.persisted = persisted;
180         }
181     }
182 }