KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xsocket > datagram > DatagramSocketConfiguration


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.datagram;
23
24 import java.net.DatagramSocket JavaDoc;
25 import java.net.SocketException JavaDoc;
26 import java.net.SocketOptions JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import static java.net.SocketOptions JavaDoc.*;
31
32
33 /**
34  * socket configuration.
35  *
36  * @deprecated use set/getOptions methods of endpoint instead
37  *
38  * @author grro@xsocket.org
39  */

40 public final class DatagramSocketConfiguration {
41     
42     private final Map JavaDoc<Integer JavaDoc, Object JavaDoc> options = new HashMap JavaDoc<Integer JavaDoc, Object JavaDoc>();
43
44
45     void setOptions(DatagramSocket JavaDoc socket) throws SocketException JavaDoc {
46         for (java.util.Map.Entry<Integer JavaDoc, Object JavaDoc> entry : options.entrySet()) {
47             setOption(socket, entry.getKey(), entry.getValue());
48         }
49     }
50     
51     Map JavaDoc<String JavaDoc, Object JavaDoc> toOptions() {
52         Map JavaDoc<String JavaDoc, Object JavaDoc> result = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
53         
54         for (Integer JavaDoc optionKey : options.keySet()) {
55             if (optionKey == SO_SNDBUF) {
56                 result.put(IEndpoint.SO_SNDBUF, options.get(optionKey));
57                 
58             } else if(optionKey == SO_REUSEADDR) {
59                 result.put(IEndpoint.SO_REUSEADDR, options.get(optionKey));
60                 
61             } else if(optionKey == SO_RCVBUF) {
62                 result.put(IEndpoint.SO_RCVBUF, options.get(optionKey));
63                 
64             } else if(optionKey == IP_TOS) {
65                 result.put(IEndpoint.IP_TOS, options.get(optionKey));
66                 
67             }
68         }
69         
70         return result;
71     }
72
73            
74     
75     static void setOption(DatagramSocket JavaDoc socket, int optID, Object JavaDoc value) throws SocketException JavaDoc {
76         switch (optID) {
77
78         case SO_TIMEOUT:
79             socket.setSoTimeout((Integer JavaDoc) value);
80             break;
81
82         case SO_SNDBUF:
83             socket.setSendBufferSize((Integer JavaDoc) value);
84             break;
85
86         case SO_REUSEADDR:
87             socket.setReuseAddress((Boolean JavaDoc) value);
88             break;
89             
90         case SO_RCVBUF:
91             socket.setReceiveBufferSize((Integer JavaDoc) value);
92             break;
93
94         case IP_TOS:
95             socket.setTrafficClass((Integer JavaDoc) value);
96             break;
97
98         default:
99             break;
100         }
101     }
102     
103     
104     static Object JavaDoc getOption(DatagramSocket JavaDoc socket, int optID) throws SocketException JavaDoc {
105         switch (optID) {
106         case SO_TIMEOUT:
107             return socket.getSoTimeout();
108
109         case SO_SNDBUF:
110             return socket.getSendBufferSize();
111
112         case SO_REUSEADDR:
113             return socket.getReuseAddress();
114             
115         case SO_RCVBUF:
116             return socket.getReceiveBufferSize();
117
118         case SO_LINGER:
119         case IP_TOS:
120             return socket.getTrafficClass();
121
122         default:
123             throw new RuntimeException JavaDoc("unsupported option id: " + optID);
124         }
125     }
126     
127     
128     /**
129      * set SO_TIMEOUT
130      *
131      * @param i SO_TIMEOUT or null to use default
132      */

133     public void setSO_TIMEOUT(Integer JavaDoc i) {
134         options.put(SocketOptions.SO_TIMEOUT, i);
135     }
136
137     
138     /**
139      * set SO_SNDBUF
140      *
141      * @param i SO_SNDBUF or null to use default
142      */

143     public void setSO_SNDBUF(Integer JavaDoc i) {
144         options.put(SocketOptions.SO_SNDBUF, i);
145     }
146     
147
148     /**
149      * set SO_RCVBUF
150      *
151      * @param i SO_RCVBUF or null to use default
152      */

153     public void setSO_RCVBUF(Integer JavaDoc i) {
154         options.put(SocketOptions.SO_RCVBUF, i);
155     }
156
157     
158     /**
159      * set SO_REUSEADDR
160      *
161      * @param b SO_REUSEADDR or null to use default
162      */

163     public void setSO_REUSEADDR(Boolean JavaDoc b) {
164         options.put(SocketOptions.SO_REUSEADDR, b);
165     }
166
167
168     /**
169      * set IP_TOS
170      *
171      * @param i IP_TOS or null to use default
172      */

173     public void setIP_TOS(Integer JavaDoc i) {
174         options.put(SocketOptions.IP_TOS, i);
175     }
176 }
177
Popular Tags