1 | /* |
2 | * @(#) $Id: DatagramSession.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.datagram; |
20 | |
21 | import java.net.SocketAddress; |
22 | import java.nio.channels.DatagramChannel; |
23 | import java.nio.channels.SelectionKey; |
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.IoSessionManager; |
34 | import org.apache.mina.io.IoSessionManagerFilterChain; |
35 | import org.apache.mina.util.Queue; |
36 | |
37 | /** |
38 | * An {@link IoSession} for datagram transport (UDP/IP). |
39 | * |
40 | * @author Trustin Lee (trustin@apache.org) |
41 | * @version $Rev: 210062 $, $Date: 2005-07-11 12:52:38 +0900 $ |
42 | */ |
43 | class DatagramSession extends BaseSession implements IoSession |
44 | { |
45 | private final IoSessionManagerFilterChain managerFilterChain; |
46 | |
47 | private final IoSessionFilterChain filterChain; |
48 | |
49 | private final DatagramChannel ch; |
50 | |
51 | private final DatagramSessionConfig config; |
52 | |
53 | private final Queue writeBufferQueue; |
54 | |
55 | private final Queue writeMarkerQueue; |
56 | |
57 | private final IoHandler handler; |
58 | |
59 | private final SocketAddress localAddress; |
60 | |
61 | private SocketAddress remoteAddress; |
62 | |
63 | private SelectionKey key; |
64 | |
65 | private boolean disposed; |
66 | |
67 | /** |
68 | * Creates a new instance. |
69 | */ |
70 | DatagramSession( IoSessionManagerFilterChain managerFilterChain, |
71 | DatagramChannel ch, IoHandler defaultHandler ) |
72 | { |
73 | this.managerFilterChain = managerFilterChain; |
74 | this.filterChain = new IoSessionFilterChain( managerFilterChain ); |
75 | this.ch = ch; |
76 | this.config = new DatagramSessionConfig( ch ); |
77 | this.writeBufferQueue = new Queue(); |
78 | this.writeMarkerQueue = new Queue(); |
79 | this.handler = defaultHandler; |
80 | this.remoteAddress = ch.socket().getRemoteSocketAddress(); |
81 | this.localAddress = ch.socket().getLocalSocketAddress(); |
82 | } |
83 | |
84 | IoSessionManagerFilterChain getManagerFilterChain() |
85 | { |
86 | return managerFilterChain; |
87 | } |
88 | |
89 | public IoFilterChain getFilterChain() |
90 | { |
91 | return filterChain; |
92 | } |
93 | |
94 | DatagramChannel getChannel() |
95 | { |
96 | return ch; |
97 | } |
98 | |
99 | SelectionKey getSelectionKey() |
100 | { |
101 | return key; |
102 | } |
103 | |
104 | void setSelectionKey( SelectionKey key ) |
105 | { |
106 | this.key = key; |
107 | } |
108 | |
109 | public IoHandler getHandler() |
110 | { |
111 | return handler; |
112 | } |
113 | |
114 | synchronized void notifyClose() |
115 | { |
116 | if( !disposed ) |
117 | { |
118 | disposed = true; |
119 | notify(); |
120 | } |
121 | } |
122 | |
123 | public synchronized void close( boolean wait ) |
124 | { |
125 | if( disposed ) |
126 | { |
127 | return; |
128 | } |
129 | |
130 | IoSessionManager manager = managerFilterChain.getManager(); |
131 | if( manager instanceof DatagramConnector ) |
132 | { |
133 | ( ( DatagramConnector ) manager ).closeSession( this ); |
134 | if( wait ) |
135 | { |
136 | while( disposed ) |
137 | { |
138 | try |
139 | { |
140 | wait(); |
141 | } |
142 | catch( InterruptedException e ) |
143 | { |
144 | } |
145 | } |
146 | } |
147 | } |
148 | } |
149 | |
150 | Queue getWriteBufferQueue() |
151 | { |
152 | return writeBufferQueue; |
153 | } |
154 | |
155 | Queue getWriteMarkerQueue() |
156 | { |
157 | return writeMarkerQueue; |
158 | } |
159 | |
160 | public void write( ByteBuffer buf, Object marker ) |
161 | { |
162 | filterChain.filterWrite( this, buf, marker ); |
163 | } |
164 | |
165 | public int getScheduledWriteRequests() |
166 | { |
167 | synchronized( writeBufferQueue ) |
168 | { |
169 | return writeBufferQueue.size(); |
170 | } |
171 | } |
172 | |
173 | public TransportType getTransportType() |
174 | { |
175 | return TransportType.DATAGRAM; |
176 | } |
177 | |
178 | public boolean isConnected() |
179 | { |
180 | return ch.isConnected(); |
181 | } |
182 | |
183 | public SessionConfig getConfig() |
184 | { |
185 | return config; |
186 | } |
187 | |
188 | public SocketAddress getRemoteAddress() |
189 | { |
190 | return remoteAddress; |
191 | } |
192 | |
193 | void setRemoteAddress( SocketAddress remoteAddress ) |
194 | { |
195 | this.remoteAddress = remoteAddress; |
196 | } |
197 | |
198 | public SocketAddress getLocalAddress() |
199 | { |
200 | return localAddress; |
201 | } |
202 | } |