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.io.IOException;
23  import java.net.SocketAddress;
24  
25  import org.apache.mina.common.ConnectFuture;
26  import org.apache.mina.common.ExceptionMonitor;
27  import org.apache.mina.common.IoFilterChain;
28  import org.apache.mina.common.IoHandler;
29  import org.apache.mina.common.IoServiceConfig;
30  import org.apache.mina.common.IoSessionConfig;
31  import org.apache.mina.common.support.AbstractIoFilterChain;
32  import org.apache.mina.common.support.BaseIoConnector;
33  import org.apache.mina.common.support.BaseIoConnectorConfig;
34  import org.apache.mina.common.support.BaseIoSessionConfig;
35  import org.apache.mina.common.support.DefaultConnectFuture;
36  import org.apache.mina.transport.vmpipe.support.VmPipe;
37  import org.apache.mina.transport.vmpipe.support.VmPipeFilterChain;
38  import org.apache.mina.transport.vmpipe.support.VmPipeIdleStatusChecker;
39  import org.apache.mina.transport.vmpipe.support.VmPipeSessionImpl;
40  import org.apache.mina.util.AnonymousSocketAddress;
41  
42  /**
43   * Connects to {@link IoHandler}s which is bound on the specified
44   * {@link VmPipeAddress}.
45   *
46   * @author The Apache Directory Project (mina-dev@directory.apache.org)
47   * @version $Rev: 587373 $, $Date: 2007-10-23 04:54:05 +0200 (Tue, 23 Oct 2007) $
48   */
49  public class VmPipeConnector extends BaseIoConnector {
50      private static final IoSessionConfig CONFIG = new BaseIoSessionConfig() {
51      };
52  
53      private final IoServiceConfig defaultConfig = new BaseIoConnectorConfig() {
54          public IoSessionConfig getSessionConfig() {
55              return CONFIG;
56          }
57      };
58  
59      /**
60       * Creates a new instance.
61       */
62      public VmPipeConnector() {
63      }
64  
65      public ConnectFuture connect(SocketAddress address, IoHandler handler,
66              IoServiceConfig config) {
67          return connect(address, null, handler, config);
68      }
69  
70      public ConnectFuture connect(SocketAddress address,
71              SocketAddress localAddress, IoHandler handler,
72              IoServiceConfig config) {
73          if (address == null)
74              throw new NullPointerException("address");
75          if (handler == null)
76              throw new NullPointerException("handler");
77          if (!(address instanceof VmPipeAddress))
78              throw new IllegalArgumentException("address must be VmPipeAddress.");
79  
80          if (config == null) {
81              config = getDefaultConfig();
82          }
83  
84          VmPipe entry = VmPipeAcceptor.boundHandlers.get(address);
85          if (entry == null) {
86              return DefaultConnectFuture.newFailedFuture(new IOException(
87                      "Endpoint unavailable: " + address));
88          }
89  
90          DefaultConnectFuture future = new DefaultConnectFuture();
91          VmPipeSessionImpl localSession = new VmPipeSessionImpl(this,
92                                                                 config,getListeners(),
93                                                                 new AnonymousSocketAddress(),
94                                                                 handler,
95                                                                 entry);
96  
97          // initialize connector session
98          try {
99              IoFilterChain filterChain = localSession.getFilterChain();
100             this.getFilterChainBuilder().buildFilterChain(filterChain);
101             config.getFilterChainBuilder().buildFilterChain(filterChain);
102             config.getThreadModel().buildFilterChain(filterChain);
103 
104             // The following sentences don't throw any exceptions.
105             localSession.setAttribute(AbstractIoFilterChain.CONNECT_FUTURE, future);
106             getListeners().fireSessionCreated(localSession);
107             VmPipeIdleStatusChecker.getInstance().addSession(localSession);
108         } catch (Throwable t) {
109             future.setException(t);
110             return future;
111         }
112 
113         // initialize acceptor session
114         VmPipeSessionImpl remoteSession = localSession.getRemoteSession();
115         try {
116             IoFilterChain filterChain = remoteSession.getFilterChain();
117             entry.getAcceptor().getFilterChainBuilder().buildFilterChain(
118                     filterChain);
119             entry.getConfig().getFilterChainBuilder().buildFilterChain(
120                     filterChain);
121             entry.getConfig().getThreadModel().buildFilterChain(filterChain);
122 
123             // The following sentences don't throw any exceptions.
124             entry.getListeners().fireSessionCreated(remoteSession);
125             VmPipeIdleStatusChecker.getInstance().addSession(remoteSession);
126         } catch (Throwable t) {
127             ExceptionMonitor.getInstance().exceptionCaught(t);
128             remoteSession.close();
129         }
130 
131 
132         // Start chains, and then allow and messages read/written to be processed. This is to ensure that
133         // sessionOpened gets received before a messageReceived
134         ((VmPipeFilterChain) localSession.getFilterChain()).start();
135         ((VmPipeFilterChain) remoteSession.getFilterChain()).start();
136 
137         return future;
138     }
139 
140     public IoServiceConfig getDefaultConfig() {
141         return defaultConfig;
142     }
143 }