1 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 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 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, config, 92 getListeners(), new Object (), new AnonymousSocketAddress(), handler, entry); 94 95 try { 97 IoFilterChain filterChain = localSession.getFilterChain(); 98 this.getFilterChainBuilder().buildFilterChain(filterChain); 99 config.getFilterChainBuilder().buildFilterChain(filterChain); 100 config.getThreadModel().buildFilterChain(filterChain); 101 102 localSession.setAttribute(AbstractIoFilterChain.CONNECT_FUTURE, 104 future); 105 getListeners().fireSessionCreated(localSession); 106 VmPipeIdleStatusChecker.getInstance().addSession(localSession); 107 } catch (Throwable t) { 108 future.setException(t); 109 return future; 110 } 111 112 VmPipeSessionImpl remoteSession = localSession.getRemoteSession(); 114 try { 115 IoFilterChain filterChain = remoteSession.getFilterChain(); 116 entry.getAcceptor().getFilterChainBuilder().buildFilterChain( 117 filterChain); 118 entry.getConfig().getFilterChainBuilder().buildFilterChain( 119 filterChain); 120 entry.getConfig().getThreadModel().buildFilterChain(filterChain); 121 122 entry.getListeners().fireSessionCreated(remoteSession); 124 VmPipeIdleStatusChecker.getInstance().addSession(remoteSession); 125 } catch (Throwable t) { 126 ExceptionMonitor.getInstance().exceptionCaught(t); 127 remoteSession.close(); 128 } 129 130 ((VmPipeFilterChain) localSession.getFilterChain()).start(); 131 ((VmPipeFilterChain) remoteSession.getFilterChain()).start(); 132 133 return future; 134 } 135 136 public IoServiceConfig getDefaultConfig() { 137 return defaultConfig; 138 } 139 }
| Popular Tags
|