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.support;
21  
22  import java.net.SocketAddress;
23  import java.net.SocketException;
24  import java.nio.channels.DatagramChannel;
25  import java.nio.channels.SelectionKey;
26  
27  import org.apache.mina.common.BroadcastIoSession;
28  import org.apache.mina.common.IoFilterChain;
29  import org.apache.mina.common.IoHandler;
30  import org.apache.mina.common.IoService;
31  import org.apache.mina.common.IoServiceConfig;
32  import org.apache.mina.common.IoSession;
33  import org.apache.mina.common.IoSessionConfig;
34  import org.apache.mina.common.RuntimeIOException;
35  import org.apache.mina.common.TransportType;
36  import org.apache.mina.common.WriteFuture;
37  import org.apache.mina.common.IoFilter.WriteRequest;
38  import org.apache.mina.common.support.BaseIoSession;
39  import org.apache.mina.transport.socket.nio.DatagramServiceConfig;
40  import org.apache.mina.transport.socket.nio.DatagramSessionConfig;
41  import org.apache.mina.util.Queue;
42  
43  /**
44   * An {@link IoSession} for datagram transport (UDP/IP).
45   * 
46   * @author The Apache Directory Project (mina-dev@directory.apache.org)
47   * @version $Rev: 580333 $, $Date: 2007-09-28 21:45:13 +0900 (Fri, 28 Sep 2007) $
48   */
49  class DatagramSessionImpl extends BaseIoSession implements BroadcastIoSession {
50      private final IoService wrapperManager;
51  
52      private final IoServiceConfig serviceConfig;
53  
54      private final DatagramSessionConfig config = new SessionConfigImpl();
55  
56      private final DatagramService managerDelegate;
57  
58      private final DatagramFilterChain filterChain;
59  
60      private final DatagramChannel ch;
61  
62      private final Queue writeRequestQueue;
63  
64      private final IoHandler handler;
65  
66      private final SocketAddress localAddress;
67  
68      private final SocketAddress serviceAddress;
69  
70      private SocketAddress remoteAddress;
71  
72      private SelectionKey key;
73  
74      private int readBufferSize;
75  
76      /**
77       * Creates a new instance.
78       */
79      DatagramSessionImpl(IoService wrapperManager,
80              DatagramService managerDelegate, IoServiceConfig serviceConfig,
81              DatagramChannel ch, IoHandler defaultHandler,
82              SocketAddress serviceAddress, SocketAddress localAddress) {
83          this.wrapperManager = wrapperManager;
84          this.managerDelegate = managerDelegate;
85          this.filterChain = new DatagramFilterChain(this);
86          this.ch = ch;
87          this.writeRequestQueue = new Queue();
88          this.handler = defaultHandler;
89          this.remoteAddress = ch.socket().getRemoteSocketAddress();
90  
91          this.serviceAddress = serviceAddress;
92          this.localAddress = localAddress;
93          this.serviceConfig = serviceConfig;
94  
95          // Apply the initial session settings
96          IoSessionConfig sessionConfig = serviceConfig.getSessionConfig();
97          if (sessionConfig instanceof DatagramSessionConfig) {
98              DatagramSessionConfig cfg = (DatagramSessionConfig) sessionConfig;
99              this.config.setBroadcast(cfg.isBroadcast());
100             this.config.setReceiveBufferSize(cfg.getReceiveBufferSize());
101             this.config.setReuseAddress(cfg.isReuseAddress());
102             this.config.setSendBufferSize(cfg.getSendBufferSize());
103 
104             if (this.config.getTrafficClass() != cfg.getTrafficClass()) {
105                 this.config.setTrafficClass(cfg.getTrafficClass());
106             }
107         }
108     }
109 
110     public IoService getService() {
111         return wrapperManager;
112     }
113 
114     public IoServiceConfig getServiceConfig() {
115         return serviceConfig;
116     }
117 
118     public IoSessionConfig getConfig() {
119         return config;
120     }
121 
122     DatagramService getManagerDelegate() {
123         return managerDelegate;
124     }
125 
126     public IoFilterChain getFilterChain() {
127         return filterChain;
128     }
129 
130     DatagramChannel getChannel() {
131         return ch;
132     }
133 
134     SelectionKey getSelectionKey() {
135         return key;
136     }
137 
138     void setSelectionKey(SelectionKey key) {
139         this.key = key;
140     }
141 
142     public IoHandler getHandler() {
143         return handler;
144     }
145 
146     protected void close0() {
147         IoServiceConfig config = getServiceConfig();
148         if (config instanceof DatagramServiceConfig) {
149             ((DatagramServiceConfig) config).getSessionRecycler().remove(this);
150         }
151         filterChain.fireFilterClose(this);
152     }
153 
154     Queue getWriteRequestQueue() {
155         return writeRequestQueue;
156     }
157 
158     protected void write0(WriteRequest writeRequest) {
159         filterChain.fireFilterWrite(this, writeRequest);
160     }
161 
162     public TransportType getTransportType() {
163         return TransportType.DATAGRAM;
164     }
165 
166     public SocketAddress getRemoteAddress() {
167         return remoteAddress;
168     }
169 
170     void setRemoteAddress(SocketAddress remoteAddress) {
171         this.remoteAddress = remoteAddress;
172     }
173 
174     public SocketAddress getLocalAddress() {
175         return localAddress;
176     }
177 
178     public SocketAddress getServiceAddress() {
179         return serviceAddress;
180     }
181 
182     protected void updateTrafficMask() {
183         managerDelegate.updateTrafficMask(this);
184     }
185 
186     int getReadBufferSize() {
187         return readBufferSize;
188     }
189 
190     private class SessionConfigImpl extends DatagramSessionConfigImpl implements
191             DatagramSessionConfig {
192         public int getReceiveBufferSize() {
193             try {
194                 return ch.socket().getReceiveBufferSize();
195             } catch (SocketException e) {
196                 throw new RuntimeIOException(e);
197             }
198         }
199 
200         public void setReceiveBufferSize(int receiveBufferSize) {
201             if (DatagramSessionConfigImpl.isSetReceiveBufferSizeAvailable()) {
202                 try {
203                     ch.socket().setReceiveBufferSize(receiveBufferSize);
204                     // Re-retrieve the effective receive buffer size.
205                     receiveBufferSize = ch.socket().getReceiveBufferSize();
206                     DatagramSessionImpl.this.readBufferSize = receiveBufferSize;
207                 } catch (SocketException e) {
208                     throw new RuntimeIOException(e);
209                 }
210             }
211         }
212 
213         public boolean isBroadcast() {
214             try {
215                 return ch.socket().getBroadcast();
216             } catch (SocketException e) {
217                 throw new RuntimeIOException(e);
218             }
219         }
220 
221         public void setBroadcast(boolean broadcast) {
222             try {
223                 ch.socket().setBroadcast(broadcast);
224             } catch (SocketException e) {
225                 throw new RuntimeIOException(e);
226             }
227         }
228 
229         public int getSendBufferSize() {
230             try {
231                 return ch.socket().getSendBufferSize();
232             } catch (SocketException e) {
233                 throw new RuntimeIOException(e);
234             }
235         }
236 
237         public void setSendBufferSize(int sendBufferSize) {
238             if (DatagramSessionConfigImpl.isSetSendBufferSizeAvailable()) {
239                 try {
240                     ch.socket().setSendBufferSize(sendBufferSize);
241                 } catch (SocketException e) {
242                     throw new RuntimeIOException(e);
243                 }
244             }
245         }
246 
247         public boolean isReuseAddress() {
248             try {
249                 return ch.socket().getReuseAddress();
250             } catch (SocketException e) {
251                 throw new RuntimeIOException(e);
252             }
253         }
254 
255         public void setReuseAddress(boolean reuseAddress) {
256             try {
257                 ch.socket().setReuseAddress(reuseAddress);
258             } catch (SocketException e) {
259                 throw new RuntimeIOException(e);
260             }
261         }
262 
263         public int getTrafficClass() {
264             if (DatagramSessionConfigImpl.isGetTrafficClassAvailable()) {
265                 try {
266                     return ch.socket().getTrafficClass();
267                 } catch (SocketException e) {
268                     throw new RuntimeIOException(e);
269                 }
270             } else {
271                 return 0;
272             }
273         }
274 
275         public void setTrafficClass(int trafficClass) {
276             if (DatagramSessionConfigImpl.isSetTrafficClassAvailable()) {
277                 try {
278                     ch.socket().setTrafficClass(trafficClass);
279                 } catch (SocketException e) {
280                     throw new RuntimeIOException(e);
281                 }
282             }
283         }
284     }
285 }