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.common.AbstractIoSession;
25  import org.apache.mina.common.DefaultIoFilterChain;
26  import org.apache.mina.common.IoFilterChain;
27  import org.apache.mina.common.IoHandler;
28  import org.apache.mina.common.IoProcessor;
29  import org.apache.mina.common.IoService;
30  import org.apache.mina.common.IoSession;
31  import org.apache.tomcat.jni.Address;
32  import org.apache.tomcat.jni.Socket;
33  
34  /**
35   * {@link IoSession} for the {@link AprSocketConnector}
36   * 
37   * @author The Apache MINA Project (dev@mina.apache.org)
38   * @version $Rev: 597276 $, $Date: 2007-11-21 17:44:09 -0700 (Wed, 21 Nov 2007) $
39   */
40  abstract class AprSession extends AbstractIoSession {
41      private long descriptor;
42  
43      private final IoService service;
44      private final IoProcessor<AprSession> processor;
45  
46      private final IoFilterChain filterChain = new DefaultIoFilterChain(this);
47      private final IoHandler handler;
48  
49      private final InetSocketAddress remoteAddress;
50      private final InetSocketAddress localAddress;
51  
52      private boolean readable = true;
53      private boolean writable = true;
54      private boolean interestedInRead;
55      private boolean interestedInWrite;
56      
57      /**
58       * Creates a new instance.
59       */
60      AprSession(
61              IoService service, IoProcessor<AprSession> processor, long descriptor) throws Exception {
62          this.service = service;
63          this.processor = processor;
64          this.handler = service.getHandler();
65          this.descriptor = descriptor;
66          
67          long ra = Address.get(Socket.APR_REMOTE, descriptor);
68          long la = Address.get(Socket.APR_LOCAL, descriptor);
69  
70          this.remoteAddress = new InetSocketAddress(Address.getip(ra), Address.getInfo(ra).port);
71          this.localAddress = new InetSocketAddress(Address.getip(la), Address.getInfo(la).port);
72      }
73  
74      AprSession(
75              IoService service, IoProcessor<AprSession> processor,
76              long descriptor, InetSocketAddress remoteAddress) throws Exception {
77          this.service = service;
78          this.processor = processor;
79          this.handler = service.getHandler();
80          this.descriptor = descriptor;
81          
82          long la = Address.get(Socket.APR_LOCAL, descriptor);
83  
84          this.remoteAddress = remoteAddress;
85          this.localAddress = new InetSocketAddress(Address.getip(la), Address.getInfo(la).port);
86      }
87  
88      long getDescriptor() {
89          return descriptor;
90      }
91  
92      @Override
93      protected IoProcessor<AprSession> getProcessor() {
94          return processor;
95      }
96  
97      public InetSocketAddress getLocalAddress() {
98          return localAddress;
99      }
100 
101     public InetSocketAddress getRemoteAddress() {
102         return remoteAddress;
103     }
104 
105     public IoFilterChain getFilterChain() {
106         return filterChain;
107     }
108 
109     public IoHandler getHandler() {
110         return handler;
111     }
112     
113     public IoService getService() {
114         return service;
115     }
116 
117     @Override
118     public InetSocketAddress getServiceAddress() {
119         return (InetSocketAddress) super.getServiceAddress();
120     }
121 
122     boolean isReadable() {
123         return readable;
124     }
125 
126     void setReadable(boolean readable) {
127         this.readable = readable;
128     }
129 
130     boolean isWritable() {
131         return writable;
132     }
133 
134     void setWritable(boolean writable) {
135         this.writable = writable;
136     }
137 
138     boolean isInterestedInRead() {
139         return interestedInRead;
140     }
141 
142     void setInterestedInRead(boolean isOpRead) {
143         this.interestedInRead = isOpRead;
144     }
145 
146     boolean isInterestedInWrite() {
147         return interestedInWrite;
148     }
149 
150     void setInterestedInWrite(boolean isOpWrite) {
151         this.interestedInWrite = isOpWrite;
152     }
153 }