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