View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  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,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.transport.socket.apr;
21  
22  import java.net.InetSocketAddress;
23  
24  import org.apache.mina.core.filterchain.DefaultIoFilterChain;
25  import org.apache.mina.core.filterchain.IoFilterChain;
26  import org.apache.mina.core.service.IoProcessor;
27  import org.apache.mina.core.service.IoService;
28  import org.apache.mina.core.session.AbstractIoSession;
29  import org.apache.mina.core.session.IoSession;
30  import org.apache.tomcat.jni.Address;
31  import org.apache.tomcat.jni.Socket;
32  
33  /**
34   * An abstract {@link IoSession} serving of base for APR based sessions.
35   * 
36   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
37   */
38  public abstract class AprSession extends AbstractIoSession {
39  
40      // good old socket descriptor
41      private long descriptor;
42  
43      // the processor processing this session
44      private final IoProcessor<AprSession> processor;
45  
46      // the mandatory filter chain of this session
47      private final IoFilterChain filterChain = new DefaultIoFilterChain(this);
48  
49      // the two endpoint addresses
50      private final InetSocketAddress remoteAddress;
51  
52      private final InetSocketAddress localAddress;
53  
54      // current polling results
55      private boolean readable = true;
56  
57      private boolean writable = true;
58  
59      private boolean interestedInRead;
60  
61      private boolean interestedInWrite;
62  
63      /**
64       * Creates a new instance of {@link AprSession}. Need to be called by extending types
65       * @param service the {@link IoService} creating this session. Can be {@link AprSocketAcceptor} or 
66       *         {@link AprSocketConnector}
67       * @param processor the {@link AprIoProcessor} managing this session.
68       * @param descriptor the low level APR socket descriptor for this socket. {@see Socket#create(int, int, int, long)}
69       * @throws Exception exception produced during the setting of all the socket parameters. 
70       */
71      AprSession(IoService service, IoProcessor<AprSession> processor, long descriptor) throws Exception {
72          super(service);
73          this.processor = processor;
74          this.descriptor = descriptor;
75  
76          long ra = Address.get(Socket.APR_REMOTE, descriptor);
77          long la = Address.get(Socket.APR_LOCAL, descriptor);
78  
79          this.remoteAddress = new InetSocketAddress(Address.getip(ra), Address.getInfo(ra).port);
80          this.localAddress = new InetSocketAddress(Address.getip(la), Address.getInfo(la).port);
81      }
82  
83      /**
84       * Creates a new instance of {@link AprSession}. Need to be called by extending types. 
85       * The constructor add remote address for UDP based sessions. 
86       * @param service the {@link IoService} creating this session. Can be {@link AprSocketAcceptor} or 
87       *         {@link AprSocketConnector}
88       * @param processor the {@link AprIoProcessor} managing this session.
89       * @param descriptor the low level APR socket descriptor for this socket. {@see Socket#create(int, int, int, long)}
90       * @param remoteAddress the remote end-point
91       * @throws Exception exception produced during the setting of all the socket parameters. 
92       */
93      AprSession(IoService service, IoProcessor<AprSession> processor, long descriptor, InetSocketAddress remoteAddress)
94              throws Exception {
95          super(service);
96          this.processor = processor;
97          this.descriptor = descriptor;
98  
99          long la = Address.get(Socket.APR_LOCAL, descriptor);
100 
101         this.remoteAddress = remoteAddress;
102         this.localAddress = new InetSocketAddress(Address.getip(la), Address.getInfo(la).port);
103     }
104 
105     /**
106      * Get the socket descriptor {@see Socket#create(int, int, int, long)}.
107      * @return the low level APR socket descriptor
108      */
109     long getDescriptor() {
110         return descriptor;
111     }
112 
113     /**
114      * Set the socket descriptor.
115      * @param desc the low level APR socket descriptor created by {@see Socket#create(int, int, int, long)}
116      */
117     void setDescriptor(long desc) {
118         this.descriptor = desc;
119     }
120 
121     /**
122      * {@inheritDoc}
123      */
124     @Override
125     public IoProcessor<AprSession> getProcessor() {
126         return processor;
127     }
128 
129     /**
130      * {@inheritDoc}
131      */
132     public InetSocketAddress getLocalAddress() {
133         return localAddress;
134     }
135 
136     /**
137      * {@inheritDoc}
138      */
139     public InetSocketAddress getRemoteAddress() {
140         return remoteAddress;
141     }
142 
143     /**
144      * {@inheritDoc}
145      */
146     public IoFilterChain getFilterChain() {
147         return filterChain;
148     }
149 
150     /**
151      * {@inheritDoc}
152      */
153     @Override
154     public InetSocketAddress getServiceAddress() {
155         return (InetSocketAddress) super.getServiceAddress();
156     }
157 
158     /**
159      * Is this session was tagged are readable after a call to {@link Socket#pool(long)}.
160      * @return true if this session is ready for read operations
161      */
162     boolean isReadable() {
163         return readable;
164     }
165 
166     /**
167      * Set if this session is readable after a call to {@link Socket#pool(long)}.
168      * @param readable  true for set this session ready for read operations
169      */
170     void setReadable(boolean readable) {
171         this.readable = readable;
172     }
173 
174     /**
175      * Is this session is tagged writable after a call to {@link Socket#pool(long)}.
176      * @return true if this session is ready for write operations
177      */
178     boolean isWritable() {
179         return writable;
180     }
181 
182     /**
183      * Set if this session is writable after a call to {@link Socket#pool(long)}.
184      * @param writable true for set this session ready for write operations
185      */
186     void setWritable(boolean writable) {
187         this.writable = writable;
188     }
189 
190     /**
191      * Does this session needs to be registered for read events.
192      * Used for building poll set {@see Poll}. 
193      * @return true if registered
194      */
195     boolean isInterestedInRead() {
196         return interestedInRead;
197     }
198 
199     /**
200      * Set if this session needs to be registered for read events. 
201      * Used for building poll set {@see Poll}.
202      * @param isOpRead true if need to be registered
203      */
204     void setInterestedInRead(boolean isOpRead) {
205         this.interestedInRead = isOpRead;
206     }
207 
208     /**
209      * Does this session needs to be registered for write events.
210      * Used for building poll set {@see Poll}. 
211      * @return true if registered
212      */
213     boolean isInterestedInWrite() {
214         return interestedInWrite;
215     }
216 
217     /**
218      * Set if this session needs to be registered for write events.
219      * Used for building poll set {@see Poll}.
220      * @param isOpWrite true if need to be registered
221      */
222     void setInterestedInWrite(boolean isOpWrite) {
223         this.interestedInWrite = isOpWrite;
224     }
225 }