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