1 | /* |
2 | * @(#) $Id: SocketSession.java 210062 2005-07-11 03:52:38Z trustin $ |
3 | * |
4 | * Copyright 2004 The Apache Software Foundation |
5 | * |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * 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, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | * |
18 | */ |
19 | package org.apache.mina.io.socket; |
20 | |
21 | import java.net.SocketAddress; |
22 | import java.nio.channels.SelectionKey; |
23 | import java.nio.channels.SocketChannel; |
24 | |
25 | import org.apache.mina.common.BaseSession; |
26 | import org.apache.mina.common.ByteBuffer; |
27 | import org.apache.mina.common.SessionConfig; |
28 | import org.apache.mina.common.TransportType; |
29 | import org.apache.mina.io.IoHandler; |
30 | import org.apache.mina.io.IoFilterChain; |
31 | import org.apache.mina.io.IoSession; |
32 | import org.apache.mina.io.IoSessionFilterChain; |
33 | import org.apache.mina.io.IoSessionManagerFilterChain; |
34 | import org.apache.mina.util.Queue; |
35 | |
36 | /** |
37 | * An {@link IoSession} for socket transport (TCP/IP). |
38 | * |
39 | * @author Trustin Lee (trustin@apache.org) |
40 | * @version $Rev: 210062 $, $Date: 2005-07-11 12:52:38 +0900 $ |
41 | */ |
42 | class SocketSession extends BaseSession implements IoSession |
43 | { |
44 | private final IoSessionManagerFilterChain managerFilterChain; |
45 | |
46 | private final IoSessionFilterChain filterChain; |
47 | |
48 | private final SocketChannel ch; |
49 | |
50 | private final SocketSessionConfig config; |
51 | |
52 | private final Queue writeBufferQueue; |
53 | |
54 | private final Queue writeMarkerQueue; |
55 | |
56 | private final IoHandler handler; |
57 | |
58 | private final SocketAddress remoteAddress; |
59 | |
60 | private final SocketAddress localAddress; |
61 | |
62 | private SelectionKey key; |
63 | |
64 | private boolean disposed; |
65 | |
66 | /** |
67 | * Creates a new instance. |
68 | */ |
69 | SocketSession( IoSessionManagerFilterChain managerFilterChain, |
70 | SocketChannel ch, IoHandler defaultHandler ) |
71 | { |
72 | this.managerFilterChain = managerFilterChain; |
73 | this.filterChain = new IoSessionFilterChain( managerFilterChain ); |
74 | this.ch = ch; |
75 | this.config = new SocketSessionConfig( this ); |
76 | this.writeBufferQueue = new Queue(); |
77 | this.writeMarkerQueue = new Queue(); |
78 | this.handler = defaultHandler; |
79 | this.remoteAddress = ch.socket().getRemoteSocketAddress(); |
80 | this.localAddress = ch.socket().getLocalSocketAddress(); |
81 | } |
82 | |
83 | IoSessionManagerFilterChain getManagerFilterChain() |
84 | { |
85 | return managerFilterChain; |
86 | } |
87 | |
88 | public IoFilterChain getFilterChain() |
89 | { |
90 | return filterChain; |
91 | } |
92 | |
93 | SocketChannel getChannel() |
94 | { |
95 | return ch; |
96 | } |
97 | |
98 | SelectionKey getSelectionKey() |
99 | { |
100 | return key; |
101 | } |
102 | |
103 | void setSelectionKey( SelectionKey key ) |
104 | { |
105 | this.key = key; |
106 | } |
107 | |
108 | public IoHandler getHandler() |
109 | { |
110 | return handler; |
111 | } |
112 | |
113 | synchronized void notifyClose() |
114 | { |
115 | if( !disposed ) |
116 | { |
117 | disposed = true; |
118 | notify(); |
119 | } |
120 | } |
121 | |
122 | |
123 | public synchronized void close( boolean wait ) |
124 | { |
125 | if( disposed ) |
126 | { |
127 | return; |
128 | } |
129 | |
130 | SocketIoProcessor.getInstance().removeSession( this ); |
131 | if( wait ) |
132 | { |
133 | while( disposed ) |
134 | { |
135 | try |
136 | { |
137 | wait(); |
138 | } |
139 | catch( InterruptedException e ) |
140 | { |
141 | } |
142 | } |
143 | } |
144 | } |
145 | |
146 | Queue getWriteBufferQueue() |
147 | { |
148 | return writeBufferQueue; |
149 | } |
150 | |
151 | Queue getWriteMarkerQueue() |
152 | { |
153 | return writeMarkerQueue; |
154 | } |
155 | |
156 | public void write( ByteBuffer buf, Object marker ) |
157 | { |
158 | filterChain.filterWrite( this, buf, marker ); |
159 | } |
160 | |
161 | public int getScheduledWriteRequests() |
162 | { |
163 | synchronized( writeBufferQueue ) |
164 | { |
165 | return writeBufferQueue.size(); |
166 | } |
167 | } |
168 | |
169 | public TransportType getTransportType() |
170 | { |
171 | return TransportType.SOCKET; |
172 | } |
173 | |
174 | public boolean isConnected() |
175 | { |
176 | return ch.isConnected(); |
177 | } |
178 | |
179 | public SessionConfig getConfig() |
180 | { |
181 | return config; |
182 | } |
183 | |
184 | public SocketAddress getRemoteAddress() |
185 | { |
186 | return remoteAddress; |
187 | } |
188 | |
189 | public SocketAddress getLocalAddress() |
190 | { |
191 | return localAddress; |
192 | } |
193 | } |