View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.transport.socket.nio;
21  
22  import java.net.SocketException;
23  import java.nio.channels.DatagramChannel;
24  
25  import org.apache.mina.core.RuntimeIoException;
26  import org.apache.mina.transport.socket.AbstractDatagramSessionConfig;
27  import org.apache.mina.transport.socket.DefaultDatagramSessionConfig;
28  
29  /**
30   * TODO Add documentation
31   * 
32   * @author The Apache MINA Project (dev@mina.apache.org)
33   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
34   */
35  class NioDatagramSessionConfig extends AbstractDatagramSessionConfig {
36      private final DatagramChannel c;
37  
38      NioDatagramSessionConfig(DatagramChannel c) {
39          this.c = c;
40      }
41  
42      public int getReceiveBufferSize() {
43          try {
44              return c.socket().getReceiveBufferSize();
45          } catch (SocketException e) {
46              throw new RuntimeIoException(e);
47          }
48      }
49  
50      public void setReceiveBufferSize(int receiveBufferSize) {
51          if (DefaultDatagramSessionConfig.isSetReceiveBufferSizeAvailable()) {
52              try {
53                  c.socket().setReceiveBufferSize(receiveBufferSize);
54                  // Re-retrieve the effective receive buffer size.
55                  
56                  // TODO clean this dead code. 
57                  // Do we really need to do this ???
58                  // Is there any secret reason why we should read the 
59                  // receiveBufferSize just after having set it ? I don't think so...
60                  // Commented :
61                  //c.socket().getReceiveBufferSize();
62              } catch (SocketException e) {
63                  throw new RuntimeIoException(e);
64              }
65          }
66      }
67  
68      public boolean isBroadcast() {
69          try {
70              return c.socket().getBroadcast();
71          } catch (SocketException e) {
72              throw new RuntimeIoException(e);
73          }
74      }
75  
76      public void setBroadcast(boolean broadcast) {
77          try {
78              c.socket().setBroadcast(broadcast);
79          } catch (SocketException e) {
80              throw new RuntimeIoException(e);
81          }
82      }
83  
84      public int getSendBufferSize() {
85          try {
86              return c.socket().getSendBufferSize();
87          } catch (SocketException e) {
88              throw new RuntimeIoException(e);
89          }
90      }
91  
92      public void setSendBufferSize(int sendBufferSize) {
93          if (DefaultDatagramSessionConfig.isSetSendBufferSizeAvailable()) {
94              try {
95                  c.socket().setSendBufferSize(sendBufferSize);
96              } catch (SocketException e) {
97                  throw new RuntimeIoException(e);
98              }
99          }
100     }
101 
102     public boolean isReuseAddress() {
103         try {
104             return c.socket().getReuseAddress();
105         } catch (SocketException e) {
106             throw new RuntimeIoException(e);
107         }
108     }
109 
110     public void setReuseAddress(boolean reuseAddress) {
111         try {
112             c.socket().setReuseAddress(reuseAddress);
113         } catch (SocketException e) {
114             throw new RuntimeIoException(e);
115         }
116     }
117 
118     public int getTrafficClass() {
119         if (DefaultDatagramSessionConfig.isGetTrafficClassAvailable()) {
120             try {
121                 return c.socket().getTrafficClass();
122             } catch (SocketException e) {
123                 throw new RuntimeIoException(e);
124             }
125         } else {
126             return 0;
127         }
128     }
129 
130     public void setTrafficClass(int trafficClass) {
131         if (DefaultDatagramSessionConfig.isSetTrafficClassAvailable()) {
132             try {
133                 c.socket().setTrafficClass(trafficClass);
134             } catch (SocketException e) {
135                 throw new RuntimeIoException(e);
136             }
137         }
138     }
139 }