1 package org.apache.mina.transport.socket.apr;
2
3 import java.net.InetSocketAddress;
4
5 import org.apache.mina.common.DefaultTransportMetadata;
6 import org.apache.mina.common.IoBuffer;
7 import org.apache.mina.common.IoProcessor;
8 import org.apache.mina.common.IoService;
9 import org.apache.mina.common.RuntimeIoException;
10 import org.apache.mina.common.TransportMetadata;
11 import org.apache.mina.transport.socket.AbstractSocketSessionConfig;
12 import org.apache.mina.transport.socket.SocketSession;
13 import org.apache.mina.transport.socket.SocketSessionConfig;
14 import org.apache.tomcat.jni.Socket;
15
16 class AprSocketSession extends AprSession implements SocketSession {
17
18 static final TransportMetadata METADATA =
19 new DefaultTransportMetadata(
20 "apr", "socket", false, true,
21 InetSocketAddress.class,
22 SocketSessionConfig.class,
23 IoBuffer.class);
24
25 private final SocketSessionConfig config = new SessionConfigImpl();
26
27 AprSocketSession(
28 IoService service, IoProcessor<AprSession> processor, long descriptor) throws Exception {
29 super(service, processor, descriptor);
30 this.config.setAll(service.getSessionConfig());
31 }
32
33
34 public SocketSessionConfig getConfig() {
35 return config;
36 }
37
38 public TransportMetadata getTransportMetadata() {
39 return METADATA;
40 }
41
42
43 private class SessionConfigImpl extends AbstractSocketSessionConfig {
44
45 public boolean isKeepAlive() {
46 try {
47 return Socket.optGet(getDescriptor(), Socket.APR_SO_KEEPALIVE) == 1;
48 } catch (Exception e) {
49 throw new RuntimeIoException("Failed to get SO_KEEPALIVE.", e);
50 }
51 }
52
53 public void setKeepAlive(boolean on) {
54 Socket.optSet(getDescriptor(), Socket.APR_SO_KEEPALIVE, on ? 1 : 0);
55 }
56
57 public boolean isOobInline() {
58 return false;
59 }
60
61 public void setOobInline(boolean on) {
62 }
63
64 public boolean isReuseAddress() {
65 try {
66 return Socket.optGet(getDescriptor(), Socket.APR_SO_REUSEADDR) == 1;
67 } catch (Exception e) {
68 throw new RuntimeIoException("Failed to get SO_REUSEADDR.", e);
69 }
70 }
71
72 public void setReuseAddress(boolean on) {
73 Socket.optSet(getDescriptor(), Socket.APR_SO_REUSEADDR, on ? 1 : 0);
74 }
75
76 public int getSoLinger() {
77 try {
78 return Socket.optGet(getDescriptor(), Socket.APR_SO_LINGER);
79 } catch (Exception e) {
80 throw new RuntimeIoException("Failed to get SO_LINGER.", e);
81 }
82 }
83
84 public void setSoLinger(int linger) {
85
86 Socket.optSet(getDescriptor(), Socket.APR_SO_LINGER, linger);
87 }
88
89 public boolean isTcpNoDelay() {
90 try {
91 return Socket.optGet(getDescriptor(), Socket.APR_TCP_NODELAY) == 1;
92 } catch (Exception e) {
93 throw new RuntimeIoException("Failed to get TCP_NODELAY.", e);
94 }
95 }
96
97 public void setTcpNoDelay(boolean on) {
98 Socket.optSet(getDescriptor(), Socket.APR_TCP_NODELAY, on ? 1 : 0);
99 }
100
101 public int getTrafficClass() {
102 return 0;
103 }
104
105 public void setTrafficClass(int tc) {
106 }
107
108 public int getSendBufferSize() {
109 try {
110 return Socket.optGet(getDescriptor(), Socket.APR_SO_SNDBUF);
111 } catch (Exception e) {
112 throw new RuntimeException("APR Exception", e);
113 }
114 }
115
116 public void setSendBufferSize(int size) {
117 Socket.optSet(getDescriptor(), Socket.APR_SO_SNDBUF, size);
118 }
119
120 public int getReceiveBufferSize() {
121 try {
122 return Socket.optGet(getDescriptor(), Socket.APR_SO_RCVBUF);
123 } catch (Exception e) {
124 throw new RuntimeException("APR Exception", e);
125 }
126 }
127
128 public void setReceiveBufferSize(int size) {
129 Socket.optSet(getDescriptor(), Socket.APR_SO_RCVBUF, size);
130 }
131 }
132 }