KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xsocket > stream > StreamSocketConfiguration


1 // $Id: IoHandler.java 1004 2007-03-08 06:05:15Z grro $
2
/*
3  * Copyright (c) xsocket.org, 2006 - 2007. All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
20  * The latest copy of this software may be found on http://www.xsocket.org/
21  */

22 package org.xsocket.stream;
23
24 import java.net.ServerSocket JavaDoc;
25 import java.net.Socket JavaDoc;
26 import java.net.SocketException JavaDoc;
27 import java.net.SocketOptions JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31
32 import static java.net.SocketOptions JavaDoc.*;
33
34
35 /**
36  * socket configuration.
37  *
38  * @deprecated
39  * @author grro@xsocket.org
40  */

41 public final class StreamSocketConfiguration {
42     
43     private final Map JavaDoc<Integer JavaDoc, Object JavaDoc> options = new HashMap JavaDoc<Integer JavaDoc, Object JavaDoc>();
44
45     
46     public void setOptions(Socket JavaDoc socket) throws SocketException JavaDoc {
47         for (java.util.Map.Entry<Integer JavaDoc, Object JavaDoc> entry : options.entrySet()) {
48             setOption(socket, entry.getKey(), entry.getValue());
49         }
50     }
51
52     public void setOptions(ServerSocket JavaDoc socket) throws SocketException JavaDoc {
53         for (java.util.Map.Entry<Integer JavaDoc, Object JavaDoc> entry : options.entrySet()) {
54             setOption(socket, entry.getKey(), entry.getValue());
55         }
56     }
57
58     
59     
60     public static void setOption(Socket JavaDoc socket, int optID, Object JavaDoc value) throws SocketException JavaDoc {
61         switch (optID) {
62         case TCP_NODELAY:
63             socket.setTcpNoDelay((Boolean JavaDoc) value);
64             break;
65
66         case SO_TIMEOUT:
67             socket.setSoTimeout((Integer JavaDoc) value);
68             break;
69
70         case SO_SNDBUF:
71             socket.setSendBufferSize((Integer JavaDoc) value);
72             break;
73
74         case SO_REUSEADDR:
75             socket.setReuseAddress((Boolean JavaDoc) value);
76             break;
77             
78         case SO_RCVBUF:
79             socket.setReceiveBufferSize((Integer JavaDoc) value);
80             break;
81
82         case SO_OOBINLINE:
83             socket.setOOBInline((Boolean JavaDoc) value);
84             break;
85
86         case SO_LINGER:
87             if (value instanceof Integer JavaDoc) {
88                 socket.setSoLinger(true, (Integer JavaDoc) value);
89             } else if (value instanceof Boolean JavaDoc) {
90                 if (((Boolean JavaDoc) value).equals(Boolean.FALSE)) {
91                     socket.setSoLinger(Boolean.FALSE, 0);
92                 }
93             }
94             break;
95                 
96         case SO_KEEPALIVE:
97             socket.setKeepAlive((Boolean JavaDoc) value);
98             break;
99
100         case IP_TOS:
101             socket.setTrafficClass((Integer JavaDoc) value);
102             break;
103
104         default:
105             break;
106         }
107     }
108
109     
110     static void setOption(ServerSocket JavaDoc socket, int optID, Object JavaDoc value) throws SocketException JavaDoc {
111         switch (optID) {
112             
113         case SO_RCVBUF:
114             socket.setReceiveBufferSize((Integer JavaDoc) value);
115             break;
116             
117         case SO_REUSEADDR:
118             socket.setReuseAddress((Boolean JavaDoc) value);
119             break;
120
121         default:
122             break;
123         }
124     }
125     
126     
127     Map JavaDoc<String JavaDoc, Object JavaDoc> toOptions() {
128         Map JavaDoc<String JavaDoc, Object JavaDoc> result = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
129         
130         for (Integer JavaDoc 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 JavaDoc fromOptions(final Map JavaDoc<String JavaDoc, Object JavaDoc> options) {
159
160         return new SocketOptions JavaDoc() {
161             
162             public Object JavaDoc getOption(int optID) throws SocketException JavaDoc {
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 JavaDoc value) throws SocketException JavaDoc {
187             }
188         };
189     }
190     
191     
192
193     public static Object JavaDoc getOption(Socket JavaDoc socket, int optID) throws SocketException JavaDoc {
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 JavaDoc("unsupported option id: " + optID);
224         }
225     }
226     
227     
228     /**
229      * set SO_REUSEADDR
230      *
231      * @param b SO_REUSEADDR or null to use default
232      */

233     public void setSO_REUSEADDR(Boolean JavaDoc b) {
234         options.put(SocketOptions.SO_REUSEADDR, b);
235     }
236         
237     
238     /**
239      * set TCP_NODELAY
240      *
241      * @param b TCP_NODELAY or null to use default
242      */

243     public void setTCP_NODELAY(Boolean JavaDoc b) {
244         options.put(SocketOptions.TCP_NODELAY, b);
245     }
246         
247     
248     /**
249      * set SO_TIMEOUT
250      *
251      * @param i SO_TIMEOUT or null to use default
252      */

253     public void setSO_TIMEOUT(Integer JavaDoc i) {
254         options.put(SocketOptions.SO_TIMEOUT, i);
255     }
256
257     
258     /**
259      * set SO_SNDBUF
260      *
261      * @param i SO_SNDBUF or null to use default
262      */

263     public void setSO_SNDBUF(Integer JavaDoc i) {
264         options.put(SocketOptions.SO_SNDBUF, i);
265     }
266         
267
268     /**
269      * set SO_RCVBUF
270      *
271      * @param i SO_RCVBUF or null to use default
272      */

273     public void setSO_RCVBUF(Integer JavaDoc i) {
274         options.put(SocketOptions.SO_RCVBUF, i);
275     }
276
277     
278     /**
279      * set SO_OOBINLINE
280      *
281      * @param b SO_OOBINLINE or null to use default
282      */

283     public void setSO_OOBINLINE(Boolean JavaDoc b) {
284         options.put(SocketOptions.SO_OOBINLINE, b);
285     }
286
287     /**
288      * set SO_KEEPALIVE
289      *
290      * @param b SO_KEEPALIVE or null to use default
291      */

292     public void setSO_KEEPALIVE(Boolean JavaDoc b) {
293         options.put(SocketOptions.SO_KEEPALIVE, b);
294     }
295     
296
297     
298     /**
299      * set IP_TOS
300      *
301      * @param i IP_TOS or null to use default
302      */

303     public void setIP_TOS(Integer JavaDoc i) {
304         options.put(SocketOptions.IP_TOS, i);
305     }
306     
307     
308     /**
309      * set SO_LINGER
310      *
311      * @param i SO_LINGER or null to use default or negative value to disable
312      */

313     public void setSO_LINGER(Integer JavaDoc 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