1 22 package org.xsocket.stream; 23 24 import java.net.ServerSocket ; 25 import java.net.Socket ; 26 import java.net.SocketException ; 27 import java.net.SocketOptions ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 31 32 import static java.net.SocketOptions .*; 33 34 35 41 public final class StreamSocketConfiguration { 42 43 private final Map <Integer , Object > options = new HashMap <Integer , Object >(); 44 45 46 public void setOptions(Socket socket) throws SocketException { 47 for (java.util.Map.Entry<Integer , Object > entry : options.entrySet()) { 48 setOption(socket, entry.getKey(), entry.getValue()); 49 } 50 } 51 52 public void setOptions(ServerSocket socket) throws SocketException { 53 for (java.util.Map.Entry<Integer , Object > entry : options.entrySet()) { 54 setOption(socket, entry.getKey(), entry.getValue()); 55 } 56 } 57 58 59 60 public static void setOption(Socket socket, int optID, Object value) throws SocketException { 61 switch (optID) { 62 case TCP_NODELAY: 63 socket.setTcpNoDelay((Boolean ) value); 64 break; 65 66 case SO_TIMEOUT: 67 socket.setSoTimeout((Integer ) value); 68 break; 69 70 case SO_SNDBUF: 71 socket.setSendBufferSize((Integer ) value); 72 break; 73 74 case SO_REUSEADDR: 75 socket.setReuseAddress((Boolean ) value); 76 break; 77 78 case SO_RCVBUF: 79 socket.setReceiveBufferSize((Integer ) value); 80 break; 81 82 case SO_OOBINLINE: 83 socket.setOOBInline((Boolean ) value); 84 break; 85 86 case SO_LINGER: 87 if (value instanceof Integer ) { 88 socket.setSoLinger(true, (Integer ) value); 89 } else if (value instanceof Boolean ) { 90 if (((Boolean ) value).equals(Boolean.FALSE)) { 91 socket.setSoLinger(Boolean.FALSE, 0); 92 } 93 } 94 break; 95 96 case SO_KEEPALIVE: 97 socket.setKeepAlive((Boolean ) value); 98 break; 99 100 case IP_TOS: 101 socket.setTrafficClass((Integer ) value); 102 break; 103 104 default: 105 break; 106 } 107 } 108 109 110 static void setOption(ServerSocket socket, int optID, Object value) throws SocketException { 111 switch (optID) { 112 113 case SO_RCVBUF: 114 socket.setReceiveBufferSize((Integer ) value); 115 break; 116 117 case SO_REUSEADDR: 118 socket.setReuseAddress((Boolean ) value); 119 break; 120 121 default: 122 break; 123 } 124 } 125 126 127 Map <String , Object > toOptions() { 128 Map <String , Object > result = new HashMap <String , Object >(); 129 130 for (Integer optionKey : options.keySet()) { 131 if (optionKey == SO_SNDBUF) { 132 result.put(IConnection.SO_SNDBUF, options.get(optionKey)); 133 134 } else if(optionKey == SO_REUSEADDR) { 135 result.put(IConnection.SO_REUSEADDR, options.get(optionKey)); 136 137 } else if(optionKey == SO_RCVBUF) { 138 result.put(IConnection.SO_RCVBUF, options.get(optionKey)); 139 140 } else if(optionKey == SO_LINGER) { 141 result.put(IConnection.SO_LINGER, options.get(optionKey)); 142 143 } else if(optionKey == TCP_NODELAY) { 144 result.put(IConnection.TCP_NODELAY, options.get(optionKey)); 145 146 } else if(optionKey == SO_KEEPALIVE) { 147 result.put(IConnection.SO_KEEPALIVE, options.get(optionKey)); 148 } 149 } 150 151 152 return result; 153 } 154 155 156 157 158 public static SocketOptions fromOptions(final Map <String , Object > options) { 159 160 return new SocketOptions () { 161 162 public Object getOption(int optID) throws SocketException { 163 switch (optID) { 164 case TCP_NODELAY: 165 return options.get(IConnection.TCP_NODELAY); 166 167 case SO_SNDBUF: 168 return options.get(IConnection.SO_SNDBUF); 169 170 case SO_REUSEADDR: 171 return options.get(IConnection.SO_REUSEADDR); 172 173 case SO_RCVBUF: 174 return options.get(IConnection.SO_RCVBUF); 175 176 case SO_LINGER: 177 return options.get(IConnection.SO_LINGER); 178 179 case SO_KEEPALIVE: 180 return options.get(IConnection.SO_KEEPALIVE); 181 } 182 183 return null; 184 } 185 186 public void setOption(int optID, Object value) throws SocketException { 187 } 188 }; 189 } 190 191 192 193 public static Object getOption(Socket socket, int optID) throws SocketException { 194 switch (optID) { 195 case TCP_NODELAY: 196 return socket.getTcpNoDelay(); 197 198 case SO_TIMEOUT: 199 return socket.getSoTimeout(); 200 201 case SO_SNDBUF: 202 return socket.getSendBufferSize(); 203 204 case SO_REUSEADDR: 205 return socket.getReuseAddress(); 206 207 case SO_RCVBUF: 208 return socket.getReceiveBufferSize(); 209 210 case SO_OOBINLINE: 211 return socket.getOOBInline(); 212 213 case SO_LINGER: 214 return socket.getSoLinger(); 215 216 case SO_KEEPALIVE: 217 return socket.getKeepAlive(); 218 219 case IP_TOS: 220 return socket.getTrafficClass(); 221 222 default: 223 throw new RuntimeException ("unsupported option id: " + optID); 224 } 225 } 226 227 228 233 public void setSO_REUSEADDR(Boolean b) { 234 options.put(SocketOptions.SO_REUSEADDR, b); 235 } 236 237 238 243 public void setTCP_NODELAY(Boolean b) { 244 options.put(SocketOptions.TCP_NODELAY, b); 245 } 246 247 248 253 public void setSO_TIMEOUT(Integer i) { 254 options.put(SocketOptions.SO_TIMEOUT, i); 255 } 256 257 258 263 public void setSO_SNDBUF(Integer i) { 264 options.put(SocketOptions.SO_SNDBUF, i); 265 } 266 267 268 273 public void setSO_RCVBUF(Integer i) { 274 options.put(SocketOptions.SO_RCVBUF, i); 275 } 276 277 278 283 public void setSO_OOBINLINE(Boolean b) { 284 options.put(SocketOptions.SO_OOBINLINE, b); 285 } 286 287 292 public void setSO_KEEPALIVE(Boolean b) { 293 options.put(SocketOptions.SO_KEEPALIVE, b); 294 } 295 296 297 298 303 public void setIP_TOS(Integer i) { 304 options.put(SocketOptions.IP_TOS, i); 305 } 306 307 308 313 public void setSO_LINGER(Integer i) { 314 if (i < 0) { 315 options.put(SocketOptions.SO_LINGER, Boolean.FALSE); 316 } else { 317 options.put(SocketOptions.SO_LINGER, i); 318 } 319 } 320 } 321 | Popular Tags |