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;
21  
22  import java.util.concurrent.BlockingQueue;
23  import java.util.concurrent.LinkedBlockingQueue;
24  import java.util.concurrent.locks.Lock;
25  import java.util.concurrent.locks.ReentrantLock;
26  
27  import org.apache.mina.core.filterchain.IoFilterChain;
28  import org.apache.mina.core.service.DefaultTransportMetadata;
29  import org.apache.mina.core.service.IoHandler;
30  import org.apache.mina.core.service.IoProcessor;
31  import org.apache.mina.core.service.IoService;
32  import org.apache.mina.core.service.IoServiceListenerSupport;
33  import org.apache.mina.core.service.TransportMetadata;
34  import org.apache.mina.core.session.AbstractIoSession;
35  import org.apache.mina.core.session.IoSession;
36  import org.apache.mina.core.write.WriteRequestQueue;
37  
38  /**
39   * A {@link IoSession} for in-VM transport (VM_PIPE).
40   *
41   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
42   */
43  class VmPipeSession extends AbstractIoSession {
44  
45      static final TransportMetadata METADATA =
46              new DefaultTransportMetadata(
47                      "mina", "vmpipe", false, false,
48                      VmPipeAddress.class,
49                      VmPipeSessionConfig.class,
50                      Object.class);
51  
52      private static final VmPipeSessionConfig CONFIG = new DefaultVmPipeSessionConfig();
53  
54      private final IoService service;
55  
56      private final IoServiceListenerSupport serviceListeners;
57  
58      private final VmPipeAddress localAddress;
59  
60      private final VmPipeAddress remoteAddress;
61  
62      private final VmPipeAddress serviceAddress;
63  
64      private final IoHandler handler;
65  
66      private final VmPipeFilterChain filterChain;
67  
68      private final VmPipeSession remoteSession;
69  
70      private final Lock lock;
71  
72      final BlockingQueue<Object> receivedMessageQueue;
73  
74      /*
75       * Constructor for client-side session.
76       */
77      VmPipeSession(IoService service,
78                        IoServiceListenerSupport serviceListeners,
79                        VmPipeAddress localAddress, IoHandler handler, VmPipe remoteEntry) {
80          this.service = service;
81          this.serviceListeners = serviceListeners;
82          lock = new ReentrantLock();
83          this.localAddress = localAddress;
84          remoteAddress = serviceAddress = remoteEntry.getAddress();
85          this.handler = handler;
86          filterChain = new VmPipeFilterChain(this);
87          receivedMessageQueue = new LinkedBlockingQueue<Object>();
88  
89          remoteSession = new VmPipeSession(this, remoteEntry);
90      }
91  
92      /*
93       * Constructor for server-side session.
94       */
95      private VmPipeSession(VmPipeSession remoteSession, VmPipe entry) {
96          service = entry.getAcceptor();
97          serviceListeners = entry.getListeners();
98          lock = remoteSession.lock;
99          localAddress = serviceAddress = remoteSession.remoteAddress;
100         remoteAddress = remoteSession.localAddress;
101         handler = entry.getHandler();
102         filterChain = new VmPipeFilterChain(this);
103         this.remoteSession = remoteSession;
104         receivedMessageQueue = new LinkedBlockingQueue<Object>();
105     }
106 
107     public IoService getService() {
108         return service;
109     }
110 
111     @Override
112     public IoProcessor<VmPipeSession> getProcessor() {
113         return filterChain.getProcessor();
114     }
115 
116     IoServiceListenerSupport getServiceListeners() {
117         return serviceListeners;
118     }
119 
120     public VmPipeSessionConfig getConfig() {
121         return CONFIG;
122     }
123 
124     public IoFilterChain getFilterChain() {
125         return filterChain;
126     }
127 
128     public VmPipeSession getRemoteSession() {
129         return remoteSession;
130     }
131 
132     public IoHandler getHandler() {
133         return handler;
134     }
135 
136     public TransportMetadata getTransportMetadata() {
137         return METADATA;
138     }
139 
140     public VmPipeAddress getRemoteAddress() {
141         return remoteAddress;
142     }
143 
144     public VmPipeAddress getLocalAddress() {
145         return localAddress;
146     }
147 
148     @Override
149     public VmPipeAddress getServiceAddress() {
150         return serviceAddress;
151     }
152 
153     void increaseWrittenBytes0(int increment, long currentTime) {
154         super.increaseWrittenBytes(increment, currentTime);
155     }
156 
157     WriteRequestQueue getWriteRequestQueue0() {
158         return super.getWriteRequestQueue();
159     }
160 
161     Lock getLock() {
162         return lock;
163     }
164 }