001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.transport.socket.nio;
021:
022: import java.net.InetSocketAddress;
023: import java.net.SocketAddress;
024: import java.net.SocketException;
025: import java.nio.channels.DatagramChannel;
026: import java.nio.channels.SelectionKey;
027:
028: import org.apache.mina.common.DefaultIoFilterChain;
029: import org.apache.mina.common.DefaultTransportMetadata;
030: import org.apache.mina.common.IoBuffer;
031: import org.apache.mina.common.IoFilterChain;
032: import org.apache.mina.common.IoHandler;
033: import org.apache.mina.common.IoProcessor;
034: import org.apache.mina.common.IoService;
035: import org.apache.mina.common.IoSession;
036: import org.apache.mina.common.RuntimeIoException;
037: import org.apache.mina.common.TransportMetadata;
038: import org.apache.mina.transport.socket.AbstractDatagramSessionConfig;
039: import org.apache.mina.transport.socket.DatagramSession;
040: import org.apache.mina.transport.socket.DatagramSessionConfig;
041: import org.apache.mina.transport.socket.DefaultDatagramSessionConfig;
042:
043: /**
044: * An {@link IoSession} for datagram transport (UDP/IP).
045: *
046: * @author The Apache MINA Project (dev@mina.apache.org)
047: * @version $Rev: 596914 $, $Date: 2007-11-20 19:49:45 -0700 (Tue, 20 Nov 2007) $
048: */
049: class NioDatagramSession extends NioSession implements DatagramSession {
050:
051: static final TransportMetadata METADATA = new DefaultTransportMetadata(
052: "nio", "datagram", true, false, InetSocketAddress.class,
053: DatagramSessionConfig.class, IoBuffer.class);
054:
055: private final IoService service;
056:
057: private final DatagramSessionConfig config = new SessionConfigImpl();
058:
059: private final IoFilterChain filterChain = new DefaultIoFilterChain(
060: this );
061:
062: private final DatagramChannel ch;
063:
064: private final IoHandler handler;
065:
066: private final InetSocketAddress localAddress;
067:
068: private final InetSocketAddress remoteAddress;
069:
070: private final IoProcessor<NioSession> processor;
071:
072: private SelectionKey key;
073:
074: /**
075: * Creates a new acceptor-side session instance.
076: */
077: NioDatagramSession(IoService service, DatagramChannel ch,
078: IoProcessor<NioSession> processor,
079: SocketAddress remoteAddress) {
080: this .service = service;
081: this .ch = ch;
082: this .handler = service.getHandler();
083: this .processor = processor;
084: this .remoteAddress = (InetSocketAddress) remoteAddress;
085: this .localAddress = (InetSocketAddress) ch.socket()
086: .getLocalSocketAddress();
087:
088: this .config.setAll(service.getSessionConfig());
089: }
090:
091: /**
092: * Creates a new connector-side session instance.
093: */
094: NioDatagramSession(IoService service, DatagramChannel ch,
095: IoProcessor<NioSession> processor) {
096: this (service, ch, processor, ch.socket()
097: .getRemoteSocketAddress());
098: }
099:
100: public IoService getService() {
101: return service;
102: }
103:
104: @Override
105: protected IoProcessor<NioSession> getProcessor() {
106: return processor;
107: }
108:
109: public DatagramSessionConfig getConfig() {
110: return config;
111: }
112:
113: public IoFilterChain getFilterChain() {
114: return filterChain;
115: }
116:
117: @Override
118: DatagramChannel getChannel() {
119: return ch;
120: }
121:
122: @Override
123: SelectionKey getSelectionKey() {
124: return key;
125: }
126:
127: @Override
128: void setSelectionKey(SelectionKey key) {
129: this .key = key;
130: }
131:
132: public IoHandler getHandler() {
133: return handler;
134: }
135:
136: public TransportMetadata getTransportMetadata() {
137: return METADATA;
138: }
139:
140: public InetSocketAddress getRemoteAddress() {
141: return remoteAddress;
142: }
143:
144: public InetSocketAddress getLocalAddress() {
145: return localAddress;
146: }
147:
148: @Override
149: public InetSocketAddress getServiceAddress() {
150: return (InetSocketAddress) super .getServiceAddress();
151: }
152:
153: private class SessionConfigImpl extends
154: AbstractDatagramSessionConfig {
155:
156: public int getReceiveBufferSize() {
157: try {
158: return ch.socket().getReceiveBufferSize();
159: } catch (SocketException e) {
160: throw new RuntimeIoException(e);
161: }
162: }
163:
164: public void setReceiveBufferSize(int receiveBufferSize) {
165: if (DefaultDatagramSessionConfig
166: .isSetReceiveBufferSizeAvailable()) {
167: try {
168: ch.socket().setReceiveBufferSize(receiveBufferSize);
169: // Re-retrieve the effective receive buffer size.
170: receiveBufferSize = ch.socket()
171: .getReceiveBufferSize();
172: } catch (SocketException e) {
173: throw new RuntimeIoException(e);
174: }
175: }
176: }
177:
178: public boolean isBroadcast() {
179: try {
180: return ch.socket().getBroadcast();
181: } catch (SocketException e) {
182: throw new RuntimeIoException(e);
183: }
184: }
185:
186: public void setBroadcast(boolean broadcast) {
187: try {
188: ch.socket().setBroadcast(broadcast);
189: } catch (SocketException e) {
190: throw new RuntimeIoException(e);
191: }
192: }
193:
194: public int getSendBufferSize() {
195: try {
196: return ch.socket().getSendBufferSize();
197: } catch (SocketException e) {
198: throw new RuntimeIoException(e);
199: }
200: }
201:
202: public void setSendBufferSize(int sendBufferSize) {
203: if (DefaultDatagramSessionConfig
204: .isSetSendBufferSizeAvailable()) {
205: try {
206: ch.socket().setSendBufferSize(sendBufferSize);
207: } catch (SocketException e) {
208: throw new RuntimeIoException(e);
209: }
210: }
211: }
212:
213: public boolean isReuseAddress() {
214: try {
215: return ch.socket().getReuseAddress();
216: } catch (SocketException e) {
217: throw new RuntimeIoException(e);
218: }
219: }
220:
221: public void setReuseAddress(boolean reuseAddress) {
222: try {
223: ch.socket().setReuseAddress(reuseAddress);
224: } catch (SocketException e) {
225: throw new RuntimeIoException(e);
226: }
227: }
228:
229: public int getTrafficClass() {
230: if (DefaultDatagramSessionConfig
231: .isGetTrafficClassAvailable()) {
232: try {
233: return ch.socket().getTrafficClass();
234: } catch (SocketException e) {
235: throw new RuntimeIoException(e);
236: }
237: } else {
238: return 0;
239: }
240: }
241:
242: public void setTrafficClass(int trafficClass) {
243: if (DefaultDatagramSessionConfig
244: .isSetTrafficClassAvailable()) {
245: try {
246: ch.socket().setTrafficClass(trafficClass);
247: } catch (SocketException e) {
248: throw new RuntimeIoException(e);
249: }
250: }
251: }
252: }
253: }
|