1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
44
45
46
47
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
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
98 try {
99 IoFilterChain filterChain = localSession.getFilterChain();
100 this.getFilterChainBuilder().buildFilterChain(filterChain);
101 config.getFilterChainBuilder().buildFilterChain(filterChain);
102 config.getThreadModel().buildFilterChain(filterChain);
103
104
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
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
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
133
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 }