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.IoHandler; 27 import org.apache.mina.core.service.IoProcessor; 28 import org.apache.mina.core.service.IoService; 29 import org.apache.mina.core.session.AbstractIoSession; 30 import org.apache.mina.core.session.IoSession; 31 import org.apache.tomcat.jni.Address; 32 import org.apache.tomcat.jni.Socket; 33 34 /** 35 * An abstract {@link IoSession} serving of base for APR based sessions. 36 * 37 * @author The Apache MINA Project (dev@mina.apache.org) 38 * @version $Rev: 671930 $, $Date: 2008-06-26 17:58:30 +0200 (jeu, 26 jun 2008) $ 39 */ 40 public abstract class AprSession extends AbstractIoSession { 41 42 // good old socket descriptor 43 private long descriptor; 44 45 // the service handling this session 46 private final IoService service; 47 48 // the processor processing this session 49 private final IoProcessor<AprSession> processor; 50 51 // the mandatory filter chain of this session 52 private final IoFilterChain filterChain = new DefaultIoFilterChain(this); 53 54 // handler listeneing this session event 55 private final IoHandler handler; 56 57 // the two endpoint addresses 58 private final InetSocketAddress remoteAddress; 59 private final InetSocketAddress localAddress; 60 61 // current polling results 62 private boolean readable = true; 63 private boolean writable = true; 64 private boolean interestedInRead; 65 private boolean interestedInWrite; 66 67 /** 68 * Creates a new instance of {@link AprSession}. Need to be called by extending types 69 * @param service the {@link IoService} creating this session. Can be {@link AprSocketAcceptor} or 70 * {@link AprSocketConnector} 71 * @param processor the {@link AprIoProcessor} managing this session. 72 * @param descriptor the low level APR socket descriptor for this socket. {@see Socket#create(int, int, int, long)} 73 * @throws Exception exception produced during the setting of all the socket parameters. 74 */ 75 AprSession( 76 IoService service, IoProcessor<AprSession> processor, long descriptor) throws Exception { 77 this.service = service; 78 this.processor = processor; 79 this.handler = service.getHandler(); 80 this.descriptor = descriptor; 81 82 long ra = Address.get(Socket.APR_REMOTE, descriptor); 83 long la = Address.get(Socket.APR_LOCAL, descriptor); 84 85 this.remoteAddress = new InetSocketAddress(Address.getip(ra), Address.getInfo(ra).port); 86 this.localAddress = new InetSocketAddress(Address.getip(la), Address.getInfo(la).port); 87 } 88 89 /** 90 * Creates a new instance of {@link AprSession}. Need to be called by extending types. 91 * The constructor add remote address for UDP based sessions. 92 * @param service the {@link IoService} creating this session. Can be {@link AprSocketAcceptor} or 93 * {@link AprSocketConnector} 94 * @param processor the {@link AprIoProcessor} managing this session. 95 * @param descriptor the low level APR socket descriptor for this socket. {@see Socket#create(int, int, int, long)} 96 * @param remoteAddress the remote end-point 97 * @throws Exception exception produced during the setting of all the socket parameters. 98 */ 99 AprSession( 100 IoService service, IoProcessor<AprSession> processor, 101 long descriptor, InetSocketAddress remoteAddress) throws Exception { 102 this.service = service; 103 this.processor = processor; 104 this.handler = service.getHandler(); 105 this.descriptor = descriptor; 106 107 long la = Address.get(Socket.APR_LOCAL, descriptor); 108 109 this.remoteAddress = remoteAddress; 110 this.localAddress = new InetSocketAddress(Address.getip(la), Address.getInfo(la).port); 111 } 112 113 /** 114 * Get the socket descriptor {@see Socket#create(int, int, int, long)}. 115 * @return the low level APR socket descriptor 116 */ 117 long getDescriptor() { 118 return descriptor; 119 } 120 121 /** 122 * Set the socket descriptor. 123 * @param desc the low level APR socket descriptor created by {@see Socket#create(int, int, int, long)} 124 */ 125 void setDescriptor(long desc) { 126 this.descriptor = desc; 127 } 128 129 /** 130 * {@inheritDoc} 131 */ 132 @Override 133 public IoProcessor<AprSession> getProcessor() { 134 return processor; 135 } 136 137 /** 138 * {@inheritDoc} 139 */ 140 public InetSocketAddress getLocalAddress() { 141 return localAddress; 142 } 143 144 /** 145 * {@inheritDoc} 146 */ 147 public InetSocketAddress getRemoteAddress() { 148 return remoteAddress; 149 } 150 151 /** 152 * {@inheritDoc} 153 */ 154 public IoFilterChain getFilterChain() { 155 return filterChain; 156 } 157 158 /** 159 * {@inheritDoc} 160 */ 161 public IoHandler getHandler() { 162 return handler; 163 } 164 165 /** 166 * {@inheritDoc} 167 */ 168 public IoService getService() { 169 return service; 170 } 171 172 /** 173 * {@inheritDoc} 174 */ 175 @Override 176 public InetSocketAddress getServiceAddress() { 177 return (InetSocketAddress) super.getServiceAddress(); 178 } 179 180 /** 181 * Is this session was tagged are readable after a call to {@link Socket#pool(long)}. 182 * @return true if this session is ready for read operations 183 */ 184 boolean isReadable() { 185 return readable; 186 } 187 188 /** 189 * Set if this session is readable after a call to {@link Socket#pool(long)}. 190 * @param readable true for set this session ready for read operations 191 */ 192 void setReadable(boolean readable) { 193 this.readable = readable; 194 } 195 196 /** 197 * Is this session is tagged writable after a call to {@link Socket#pool(long)}. 198 * @return true if this session is ready for write operations 199 */ 200 boolean isWritable() { 201 return writable; 202 } 203 204 /** 205 * Set if this session is writable after a call to {@link Socket#pool(long)}. 206 * @param writable true for set this session ready for write operations 207 */ 208 void setWritable(boolean writable) { 209 this.writable = writable; 210 } 211 212 /** 213 * Does this session needs to be registered for read events. 214 * Used for building poll set {@see Poll}. 215 * @return true if registered 216 */ 217 boolean isInterestedInRead() { 218 return interestedInRead; 219 } 220 221 /** 222 * Set if this session needs to be registered for read events. 223 * Used for building poll set {@see Poll}. 224 * @param isOpRead true if need to be registered 225 */ 226 void setInterestedInRead(boolean isOpRead) { 227 this.interestedInRead = isOpRead; 228 } 229 230 /** 231 * Does this session needs to be registered for write events. 232 * Used for building poll set {@see Poll}. 233 * @return true if registered 234 */ 235 boolean isInterestedInWrite() { 236 return interestedInWrite; 237 } 238 239 /** 240 * Set if this session needs to be registered for write events. 241 * Used for building poll set {@see Poll}. 242 * @param isOpWrite true if need to be registered 243 */ 244 void setInterestedInWrite(boolean isOpWrite) { 245 this.interestedInWrite = isOpWrite; 246 } 247 }