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.SocketException;
024: import java.nio.channels.SelectionKey;
025: import java.nio.channels.SocketChannel;
026:
027: import org.apache.mina.common.DefaultIoFilterChain;
028: import org.apache.mina.common.DefaultTransportMetadata;
029: import org.apache.mina.common.FileRegion;
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.AbstractSocketSessionConfig;
039: import org.apache.mina.transport.socket.DefaultSocketSessionConfig;
040: import org.apache.mina.transport.socket.SocketSession;
041: import org.apache.mina.transport.socket.SocketSessionConfig;
042:
043: /**
044: * An {@link IoSession} for socket transport (TCP/IP).
045: *
046: * @author The Apache MINA Project (dev@mina.apache.org)
047: * @version $Rev: 606102 $, $Date: 2007-12-20 22:36:58 -0700 (Thu, 20 Dec 2007) $
048: */
049: class NioSocketSession extends NioSession implements SocketSession {
050:
051: static final TransportMetadata METADATA = new DefaultTransportMetadata(
052: "nio", "socket", false, true, InetSocketAddress.class,
053: SocketSessionConfig.class, IoBuffer.class, FileRegion.class);
054:
055: private final IoService service;
056:
057: private final SocketSessionConfig config = new SessionConfigImpl();
058:
059: private final IoProcessor<NioSession> processor;
060:
061: private final IoFilterChain filterChain = new DefaultIoFilterChain(
062: this );
063:
064: private final SocketChannel ch;
065:
066: private final IoHandler handler;
067:
068: private SelectionKey key;
069:
070: NioSocketSession(IoService service,
071: IoProcessor<NioSession> processor, SocketChannel ch) {
072: this .service = service;
073: this .processor = processor;
074: this .ch = ch;
075: this .handler = service.getHandler();
076: this .config.setAll(service.getSessionConfig());
077: }
078:
079: public IoService getService() {
080: return service;
081: }
082:
083: public SocketSessionConfig getConfig() {
084: return config;
085: }
086:
087: @Override
088: protected IoProcessor<NioSession> getProcessor() {
089: return processor;
090: }
091:
092: public IoFilterChain getFilterChain() {
093: return filterChain;
094: }
095:
096: public TransportMetadata getTransportMetadata() {
097: return METADATA;
098: }
099:
100: @Override
101: SocketChannel getChannel() {
102: return ch;
103: }
104:
105: @Override
106: SelectionKey getSelectionKey() {
107: return key;
108: }
109:
110: @Override
111: void setSelectionKey(SelectionKey key) {
112: this .key = key;
113: }
114:
115: public IoHandler getHandler() {
116: return handler;
117: }
118:
119: public InetSocketAddress getRemoteAddress() {
120: return (InetSocketAddress) ch.socket().getRemoteSocketAddress();
121: }
122:
123: public InetSocketAddress getLocalAddress() {
124: return (InetSocketAddress) ch.socket().getLocalSocketAddress();
125: }
126:
127: @Override
128: public InetSocketAddress getServiceAddress() {
129: return (InetSocketAddress) super .getServiceAddress();
130: }
131:
132: private class SessionConfigImpl extends AbstractSocketSessionConfig {
133: public boolean isKeepAlive() {
134: try {
135: return ch.socket().getKeepAlive();
136: } catch (SocketException e) {
137: throw new RuntimeIoException(e);
138: }
139: }
140:
141: public void setKeepAlive(boolean on) {
142: try {
143: ch.socket().setKeepAlive(on);
144: } catch (SocketException e) {
145: throw new RuntimeIoException(e);
146: }
147: }
148:
149: public boolean isOobInline() {
150: try {
151: return ch.socket().getOOBInline();
152: } catch (SocketException e) {
153: throw new RuntimeIoException(e);
154: }
155: }
156:
157: public void setOobInline(boolean on) {
158: try {
159: ch.socket().setOOBInline(on);
160: } catch (SocketException e) {
161: throw new RuntimeIoException(e);
162: }
163: }
164:
165: public boolean isReuseAddress() {
166: try {
167: return ch.socket().getReuseAddress();
168: } catch (SocketException e) {
169: throw new RuntimeIoException(e);
170: }
171: }
172:
173: public void setReuseAddress(boolean on) {
174: try {
175: ch.socket().setReuseAddress(on);
176: } catch (SocketException e) {
177: throw new RuntimeIoException(e);
178: }
179: }
180:
181: public int getSoLinger() {
182: try {
183: return ch.socket().getSoLinger();
184: } catch (SocketException e) {
185: throw new RuntimeIoException(e);
186: }
187: }
188:
189: public void setSoLinger(int linger) {
190: try {
191: if (linger < 0) {
192: ch.socket().setSoLinger(false, 0);
193: } else {
194: ch.socket().setSoLinger(true, linger);
195: }
196: } catch (SocketException e) {
197: throw new RuntimeIoException(e);
198: }
199: }
200:
201: public boolean isTcpNoDelay() {
202: if (!isConnected()) {
203: return false;
204: }
205:
206: try {
207: return ch.socket().getTcpNoDelay();
208: } catch (SocketException e) {
209: throw new RuntimeIoException(e);
210: }
211: }
212:
213: public void setTcpNoDelay(boolean on) {
214: try {
215: ch.socket().setTcpNoDelay(on);
216: } catch (SocketException e) {
217: throw new RuntimeIoException(e);
218: }
219: }
220:
221: public int getTrafficClass() {
222: if (DefaultSocketSessionConfig.isGetTrafficClassAvailable()) {
223: try {
224: return ch.socket().getTrafficClass();
225: } catch (SocketException e) {
226: // Throw an exception only when setTrafficClass is also available.
227: if (DefaultSocketSessionConfig
228: .isSetTrafficClassAvailable()) {
229: throw new RuntimeIoException(e);
230: }
231: }
232: }
233:
234: return 0;
235: }
236:
237: public void setTrafficClass(int tc) {
238: if (DefaultSocketSessionConfig.isSetTrafficClassAvailable()) {
239: try {
240: ch.socket().setTrafficClass(tc);
241: } catch (SocketException e) {
242: throw new RuntimeIoException(e);
243: }
244: }
245: }
246:
247: public int getSendBufferSize() {
248: try {
249: return ch.socket().getSendBufferSize();
250: } catch (SocketException e) {
251: throw new RuntimeIoException(e);
252: }
253: }
254:
255: public void setSendBufferSize(int size) {
256: if (DefaultSocketSessionConfig
257: .isSetSendBufferSizeAvailable()) {
258: try {
259: ch.socket().setSendBufferSize(size);
260: } catch (SocketException e) {
261: throw new RuntimeIoException(e);
262: }
263: }
264: }
265:
266: public int getReceiveBufferSize() {
267: try {
268: return ch.socket().getReceiveBufferSize();
269: } catch (SocketException e) {
270: throw new RuntimeIoException(e);
271: }
272: }
273:
274: public void setReceiveBufferSize(int size) {
275: if (DefaultSocketSessionConfig
276: .isSetReceiveBufferSizeAvailable()) {
277: try {
278: ch.socket().setReceiveBufferSize(size);
279: } catch (SocketException e) {
280: throw new RuntimeIoException(e);
281: }
282: }
283: }
284: }
285: }
|