001: package org.apache.mina.transport.socket.apr;
002:
003: import java.net.InetSocketAddress;
004:
005: import org.apache.mina.common.DefaultTransportMetadata;
006: import org.apache.mina.common.IoBuffer;
007: import org.apache.mina.common.IoProcessor;
008: import org.apache.mina.common.IoService;
009: import org.apache.mina.common.RuntimeIoException;
010: import org.apache.mina.common.TransportMetadata;
011: import org.apache.mina.transport.socket.AbstractSocketSessionConfig;
012: import org.apache.mina.transport.socket.SocketSession;
013: import org.apache.mina.transport.socket.SocketSessionConfig;
014: import org.apache.tomcat.jni.Socket;
015:
016: class AprSocketSession extends AprSession implements SocketSession {
017:
018: static final TransportMetadata METADATA = new DefaultTransportMetadata(
019: "apr", "socket", false, true, InetSocketAddress.class,
020: SocketSessionConfig.class, IoBuffer.class);
021:
022: private final SocketSessionConfig config = new SessionConfigImpl();
023:
024: AprSocketSession(IoService service,
025: IoProcessor<AprSession> processor, long descriptor)
026: throws Exception {
027: super (service, processor, descriptor);
028: this .config.setAll(service.getSessionConfig());
029: }
030:
031: public SocketSessionConfig getConfig() {
032: return config;
033: }
034:
035: public TransportMetadata getTransportMetadata() {
036: return METADATA;
037: }
038:
039: private class SessionConfigImpl extends AbstractSocketSessionConfig {
040:
041: public boolean isKeepAlive() {
042: try {
043: return Socket.optGet(getDescriptor(),
044: Socket.APR_SO_KEEPALIVE) == 1;
045: } catch (Exception e) {
046: throw new RuntimeIoException(
047: "Failed to get SO_KEEPALIVE.", e);
048: }
049: }
050:
051: public void setKeepAlive(boolean on) {
052: Socket.optSet(getDescriptor(), Socket.APR_SO_KEEPALIVE,
053: on ? 1 : 0);
054: }
055:
056: public boolean isOobInline() {
057: return false;
058: }
059:
060: public void setOobInline(boolean on) {
061: }
062:
063: public boolean isReuseAddress() {
064: try {
065: return Socket.optGet(getDescriptor(),
066: Socket.APR_SO_REUSEADDR) == 1;
067: } catch (Exception e) {
068: throw new RuntimeIoException(
069: "Failed to get SO_REUSEADDR.", e);
070: }
071: }
072:
073: public void setReuseAddress(boolean on) {
074: Socket.optSet(getDescriptor(), Socket.APR_SO_REUSEADDR,
075: on ? 1 : 0);
076: }
077:
078: public int getSoLinger() {
079: try {
080: return Socket.optGet(getDescriptor(),
081: Socket.APR_SO_LINGER);
082: } catch (Exception e) {
083: throw new RuntimeIoException(
084: "Failed to get SO_LINGER.", e);
085: }
086: }
087:
088: public void setSoLinger(int linger) {
089: // TODO: Figure out how to disable this.
090: Socket
091: .optSet(getDescriptor(), Socket.APR_SO_LINGER,
092: linger);
093: }
094:
095: public boolean isTcpNoDelay() {
096: try {
097: return Socket.optGet(getDescriptor(),
098: Socket.APR_TCP_NODELAY) == 1;
099: } catch (Exception e) {
100: throw new RuntimeIoException(
101: "Failed to get TCP_NODELAY.", e);
102: }
103: }
104:
105: public void setTcpNoDelay(boolean on) {
106: Socket.optSet(getDescriptor(), Socket.APR_TCP_NODELAY,
107: on ? 1 : 0);
108: }
109:
110: public int getTrafficClass() {
111: return 0;
112: }
113:
114: public void setTrafficClass(int tc) {
115: }
116:
117: public int getSendBufferSize() {
118: try {
119: return Socket.optGet(getDescriptor(),
120: Socket.APR_SO_SNDBUF);
121: } catch (Exception e) {
122: throw new RuntimeException("APR Exception", e);
123: }
124: }
125:
126: public void setSendBufferSize(int size) {
127: Socket.optSet(getDescriptor(), Socket.APR_SO_SNDBUF, size);
128: }
129:
130: public int getReceiveBufferSize() {
131: try {
132: return Socket.optGet(getDescriptor(),
133: Socket.APR_SO_RCVBUF);
134: } catch (Exception e) {
135: throw new RuntimeException("APR Exception", e);
136: }
137: }
138:
139: public void setReceiveBufferSize(int size) {
140: Socket.optSet(getDescriptor(), Socket.APR_SO_RCVBUF, size);
141: }
142: }
143: }
|