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 import java.util.HashSet;
25 import java.util.Set;
26 import java.util.concurrent.Executor;
27
28 import org.apache.mina.core.filterchain.IoFilterChain;
29 import org.apache.mina.core.future.ConnectFuture;
30 import org.apache.mina.core.future.DefaultConnectFuture;
31 import org.apache.mina.core.future.IoFuture;
32 import org.apache.mina.core.future.IoFutureListener;
33 import org.apache.mina.core.service.AbstractIoConnector;
34 import org.apache.mina.core.service.IoHandler;
35 import org.apache.mina.core.service.TransportMetadata;
36 import org.apache.mina.core.session.IdleStatusChecker;
37 import org.apache.mina.core.session.IoSessionInitializer;
38 import org.apache.mina.util.ExceptionMonitor;
39
40
41
42
43
44
45
46 public final class VmPipeConnector extends AbstractIoConnector {
47
48
49 private IdleStatusChecker idleChecker;
50
51
52
53
54 public VmPipeConnector() {
55 this(null);
56 }
57
58
59
60
61 public VmPipeConnector(Executor executor) {
62 super(new DefaultVmPipeSessionConfig(), executor);
63 idleChecker = new IdleStatusChecker();
64
65
66 executeWorker(idleChecker.getNotifyingTask(), "idleStatusChecker");
67 }
68
69 public TransportMetadata getTransportMetadata() {
70 return VmPipeSession.METADATA;
71 }
72
73 @Override
74 public VmPipeSessionConfig getSessionConfig() {
75 return (VmPipeSessionConfig) super.getSessionConfig();
76 }
77
78 @Override
79 protected ConnectFuture connect0(SocketAddress remoteAddress, SocketAddress localAddress,
80 IoSessionInitializer<? extends ConnectFuture> sessionInitializer) {
81 VmPipe entry = VmPipeAcceptor.boundHandlers.get(remoteAddress);
82 if (entry == null) {
83 return DefaultConnectFuture.newFailedFuture(new IOException("Endpoint unavailable: " + remoteAddress));
84 }
85
86 DefaultConnectFuture future = new DefaultConnectFuture();
87
88
89 VmPipeAddress actualLocalAddress;
90 try {
91 actualLocalAddress = nextLocalAddress();
92 } catch (IOException e) {
93 return DefaultConnectFuture.newFailedFuture(e);
94 }
95
96 VmPipeSession localSession = new VmPipeSession(this, getListeners(), actualLocalAddress, getHandler(), entry);
97
98 initSession(localSession, future, sessionInitializer);
99
100
101 localSession.getCloseFuture().addListener(LOCAL_ADDRESS_RECLAIMER);
102
103
104 try {
105 IoFilterChain filterChain = localSession.getFilterChain();
106 this.getFilterChainBuilder().buildFilterChain(filterChain);
107
108
109 getListeners().fireSessionCreated(localSession);
110 idleChecker.addSession(localSession);
111 } catch (Throwable t) {
112 future.setException(t);
113 return future;
114 }
115
116
117 VmPipeSession remoteSession = localSession.getRemoteSession();
118 ((VmPipeAcceptor) remoteSession.getService()).doFinishSessionInitialization(remoteSession, null);
119 try {
120 IoFilterChain filterChain = remoteSession.getFilterChain();
121 entry.getAcceptor().getFilterChainBuilder().buildFilterChain(filterChain);
122
123
124 entry.getListeners().fireSessionCreated(remoteSession);
125 idleChecker.addSession(remoteSession);
126 } catch (Throwable t) {
127 ExceptionMonitor.getInstance().exceptionCaught(t);
128 remoteSession.close(true);
129 }
130
131
132
133 ((VmPipeFilterChain) localSession.getFilterChain()).start();
134 ((VmPipeFilterChain) remoteSession.getFilterChain()).start();
135
136 return future;
137 }
138
139 @Override
140 protected void dispose0() throws Exception {
141
142 idleChecker.getNotifyingTask().cancel();
143 }
144
145 private static final Set<VmPipeAddress> TAKEN_LOCAL_ADDRESSES = new HashSet<VmPipeAddress>();
146
147 private static int nextLocalPort = -1;
148
149 private static final IoFutureListener<IoFuture> LOCAL_ADDRESS_RECLAIMER = new LocalAddressReclaimer();
150
151 private static VmPipeAddress nextLocalAddress() throws IOException {
152 synchronized (TAKEN_LOCAL_ADDRESSES) {
153 if (nextLocalPort >= 0) {
154 nextLocalPort = -1;
155 }
156 for (int i = 0; i < Integer.MAX_VALUE; i++) {
157 VmPipeAddress answer = new VmPipeAddress(nextLocalPort--);
158 if (!TAKEN_LOCAL_ADDRESSES.contains(answer)) {
159 TAKEN_LOCAL_ADDRESSES.add(answer);
160 return answer;
161 }
162 }
163 }
164
165 throw new IOException("Can't assign a local VM pipe port.");
166 }
167
168 private static class LocalAddressReclaimer implements IoFutureListener<IoFuture> {
169 public void operationComplete(IoFuture future) {
170 synchronized (TAKEN_LOCAL_ADDRESSES) {
171 TAKEN_LOCAL_ADDRESSES.remove(future.getSession().getLocalAddress());
172 }
173 }
174 }
175 }