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.vmpipe.support;
21  
22  import java.net.SocketAddress;
23  
24  import org.apache.mina.common.IoFilterChain;
25  import org.apache.mina.common.IoHandler;
26  import org.apache.mina.common.IoService;
27  import org.apache.mina.common.IoServiceConfig;
28  import org.apache.mina.common.IoSession;
29  import org.apache.mina.common.IoSessionConfig;
30  import org.apache.mina.common.TransportType;
31  import org.apache.mina.common.IoFilter.WriteRequest;
32  import org.apache.mina.common.support.BaseIoSession;
33  import org.apache.mina.common.support.BaseIoSessionConfig;
34  import org.apache.mina.common.support.IoServiceListenerSupport;
35  import org.apache.mina.util.Queue;
36  
37  import edu.emory.mathcs.backport.java.util.concurrent.locks.Lock;
38  import edu.emory.mathcs.backport.java.util.concurrent.locks.ReentrantLock;
39  
40  /**
41   * A {@link IoSession} for in-VM transport (VM_PIPE).
42   * 
43   * @author The Apache Directory Project (mina-dev@directory.apache.org)
44   * @version $Rev: 575603 $, $Date: 2007-09-14 19:04:45 +0900 (금, 14  9월 2007) $
45   */
46  public class VmPipeSessionImpl extends BaseIoSession {
47      private static final IoSessionConfig CONFIG = new BaseIoSessionConfig() {
48      };
49  
50      private final IoService service;
51  
52      private final IoServiceConfig serviceConfig;
53  
54      private final IoServiceListenerSupport serviceListeners;
55  
56      private final SocketAddress localAddress;
57  
58      private final SocketAddress remoteAddress;
59  
60      private final SocketAddress serviceAddress;
61  
62      private final IoHandler handler;
63  
64      private final VmPipeFilterChain filterChain;
65  
66      private final VmPipeSessionImpl remoteSession;
67  
68      private final Lock lock;
69  
70      final Queue pendingDataQueue;
71  
72      /**
73       * Constructor for client-side session.
74       */
75      public VmPipeSessionImpl(IoService service, IoServiceConfig serviceConfig,
76              IoServiceListenerSupport serviceListeners,
77              SocketAddress localAddress, IoHandler handler, VmPipe remoteEntry) {
78          this.service = service;
79          this.serviceConfig = serviceConfig;
80          this.serviceListeners = serviceListeners;
81          this.lock = new ReentrantLock();
82          this.localAddress = localAddress;
83          this.remoteAddress = this.serviceAddress = remoteEntry.getAddress();
84          this.handler = handler;
85          this.filterChain = new VmPipeFilterChain(this);
86          this.pendingDataQueue = new Queue();
87  
88          remoteSession = new VmPipeSessionImpl(this, remoteEntry);
89      }
90  
91      /**
92       * Constructor for server-side session.
93       */
94      private VmPipeSessionImpl(VmPipeSessionImpl remoteSession, VmPipe entry) {
95          this.service = entry.getAcceptor();
96          this.serviceConfig = entry.getConfig();
97          this.serviceListeners = entry.getListeners();
98          this.lock = remoteSession.lock;
99          this.localAddress = this.serviceAddress = remoteSession.remoteAddress;
100         this.remoteAddress = remoteSession.localAddress;
101         this.handler = entry.getHandler();
102         this.filterChain = new VmPipeFilterChain(this);
103         this.remoteSession = remoteSession;
104         this.pendingDataQueue = new Queue();
105     }
106 
107     public IoService getService() {
108         return service;
109     }
110 
111     IoServiceListenerSupport getServiceListeners() {
112         return serviceListeners;
113     }
114 
115     public IoServiceConfig getServiceConfig() {
116         return serviceConfig;
117     }
118 
119     public IoSessionConfig getConfig() {
120         return CONFIG;
121     }
122 
123     public IoFilterChain getFilterChain() {
124         return filterChain;
125     }
126 
127     public VmPipeSessionImpl getRemoteSession() {
128         return remoteSession;
129     }
130 
131     public IoHandler getHandler() {
132         return handler;
133     }
134 
135     protected void close0() {
136         filterChain.fireFilterClose(this);
137     }
138 
139     protected void write0(WriteRequest writeRequest) {
140         this.filterChain.fireFilterWrite(this, writeRequest);
141     }
142 
143     public TransportType getTransportType() {
144         return TransportType.VM_PIPE;
145     }
146 
147     public SocketAddress getRemoteAddress() {
148         return remoteAddress;
149     }
150 
151     public SocketAddress getLocalAddress() {
152         return localAddress;
153     }
154 
155     public SocketAddress getServiceAddress() {
156         return serviceAddress;
157     }
158 
159     protected void updateTrafficMask() {
160         if (getTrafficMask().isReadable() || getTrafficMask().isWritable()) {
161             Object[] data;
162             synchronized (pendingDataQueue) {
163                 data = pendingDataQueue.toArray();
164                 pendingDataQueue.clear();
165             }
166 
167             for (int i = 0; i < data.length; i++) {
168                 if (data[i] instanceof WriteRequest) {
169                     // TODO Optimize unefficient data transfer.
170                     // Data will be returned to pendingDataQueue
171                     // if getTraffic().isWritable() is false.
172                     WriteRequest wr = (WriteRequest) data[i];
173                     filterChain.doWrite(this, wr);
174                 } else {
175                     // TODO Optimize unefficient data transfer.
176                     // Data will be returned to pendingDataQueue
177                     // if getTraffic().isReadable() is false.
178                     filterChain.fireMessageReceived(this, data[i]);
179                 }
180             }
181         }
182     }
183     
184     Lock getLock() {
185         return lock;
186     }
187 }