Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import java.io.ByteArrayInputStream; |
| 19 | import java.io.ByteArrayOutputStream; |
| 20 | import java.io.DataInputStream; |
| 21 | import java.io.DataOutputStream; |
| 22 | import java.io.IOException; |
| 23 | import java.io.InputStream; |
Jordan Halterman | 2f7a5d0 | 2017-05-03 16:56:53 -0700 | [diff] [blame^] | 24 | import java.net.SocketException; |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 25 | import java.nio.ByteBuffer; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 26 | import java.util.Map; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 27 | import java.util.concurrent.CompletableFuture; |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 28 | import java.util.concurrent.ConcurrentHashMap; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 29 | import java.util.function.Consumer; |
Jordan Halterman | fda46f9 | 2017-04-14 10:49:44 -0700 | [diff] [blame] | 30 | import java.util.function.Function; |
| 31 | |
| 32 | import com.google.common.base.Throwables; |
| 33 | import io.atomix.catalyst.concurrent.Listener; |
| 34 | import io.atomix.catalyst.concurrent.Listeners; |
| 35 | import io.atomix.catalyst.concurrent.ThreadContext; |
| 36 | import io.atomix.catalyst.serializer.SerializationException; |
| 37 | import io.atomix.catalyst.transport.Connection; |
| 38 | import io.atomix.catalyst.transport.TransportException; |
| 39 | import io.atomix.catalyst.util.reference.ReferenceCounted; |
| 40 | import org.apache.commons.io.IOUtils; |
| 41 | import org.onlab.util.Tools; |
| 42 | import org.onosproject.cluster.PartitionId; |
| 43 | import org.onosproject.store.cluster.messaging.Endpoint; |
| 44 | import org.onosproject.store.cluster.messaging.MessagingException; |
| 45 | import org.onosproject.store.cluster.messaging.MessagingService; |
| 46 | import org.slf4j.Logger; |
| 47 | import org.slf4j.LoggerFactory; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 48 | |
Jonathan Hart | ad0c302 | 2017-02-22 14:06:01 -0800 | [diff] [blame] | 49 | import static com.google.common.base.Preconditions.checkNotNull; |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 50 | import static org.onosproject.store.primitives.impl.CopycatTransport.CLOSE; |
| 51 | import static org.onosproject.store.primitives.impl.CopycatTransport.FAILURE; |
| 52 | import static org.onosproject.store.primitives.impl.CopycatTransport.MESSAGE; |
| 53 | import static org.onosproject.store.primitives.impl.CopycatTransport.SUCCESS; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 54 | |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 55 | /** |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 56 | * Base Copycat Transport connection. |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 57 | */ |
| 58 | public class CopycatTransportConnection implements Connection { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 59 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 60 | private final long connectionId; |
| 61 | private final String localSubject; |
| 62 | private final String remoteSubject; |
| 63 | private final PartitionId partitionId; |
| 64 | private final Endpoint endpoint; |
| 65 | private final MessagingService messagingService; |
| 66 | private final ThreadContext context; |
| 67 | private final Map<Class, InternalHandler> handlers = new ConcurrentHashMap<>(); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 68 | private final Listeners<Throwable> exceptionListeners = new Listeners<>(); |
| 69 | private final Listeners<Connection> closeListeners = new Listeners<>(); |
| 70 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 71 | CopycatTransportConnection( |
| 72 | long connectionId, |
| 73 | Mode mode, |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 74 | PartitionId partitionId, |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 75 | Endpoint endpoint, |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 76 | MessagingService messagingService, |
| 77 | ThreadContext context) { |
| 78 | this.connectionId = connectionId; |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 79 | this.partitionId = checkNotNull(partitionId, "partitionId cannot be null"); |
| 80 | this.localSubject = mode.getLocalSubject(partitionId, connectionId); |
| 81 | this.remoteSubject = mode.getRemoteSubject(partitionId, connectionId); |
| 82 | this.endpoint = checkNotNull(endpoint, "endpoint cannot be null"); |
| 83 | this.messagingService = checkNotNull(messagingService, "messagingService cannot be null"); |
| 84 | this.context = checkNotNull(context, "context cannot be null"); |
| 85 | messagingService.registerHandler(localSubject, this::handle); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
Jordan Halterman | fda46f9 | 2017-04-14 10:49:44 -0700 | [diff] [blame] | 89 | public CompletableFuture<Void> send(Object message) { |
| 90 | ThreadContext context = ThreadContext.currentContextOrThrow(); |
| 91 | CompletableFuture<Void> future = new CompletableFuture<>(); |
| 92 | try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
| 93 | DataOutputStream dos = new DataOutputStream(baos); |
| 94 | dos.writeByte(MESSAGE); |
| 95 | context.serializer().writeObject(message, baos); |
| 96 | if (message instanceof ReferenceCounted) { |
| 97 | ((ReferenceCounted<?>) message).release(); |
| 98 | } |
| 99 | |
| 100 | messagingService.sendAsync(endpoint, remoteSubject, baos.toByteArray()) |
| 101 | .whenComplete((r, e) -> { |
| 102 | if (e != null) { |
| 103 | context.executor().execute(() -> future.completeExceptionally(e)); |
| 104 | } else { |
| 105 | context.executor().execute(() -> future.complete(null)); |
| 106 | } |
| 107 | }); |
| 108 | } catch (SerializationException | IOException e) { |
| 109 | future.completeExceptionally(e); |
| 110 | } |
| 111 | return future; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public <T, U> CompletableFuture<U> sendAndReceive(T message) { |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 116 | ThreadContext context = ThreadContext.currentContextOrThrow(); |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 117 | CompletableFuture<U> future = new CompletableFuture<>(); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 118 | try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 119 | DataOutputStream dos = new DataOutputStream(baos); |
| 120 | dos.writeByte(MESSAGE); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 121 | context.serializer().writeObject(message, baos); |
| 122 | if (message instanceof ReferenceCounted) { |
| 123 | ((ReferenceCounted<?>) message).release(); |
| 124 | } |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 125 | messagingService.sendAndReceive(endpoint, |
| 126 | remoteSubject, |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 127 | baos.toByteArray(), |
| 128 | context.executor()) |
Jordan Halterman | 2f7a5d0 | 2017-05-03 16:56:53 -0700 | [diff] [blame^] | 129 | .whenComplete((response, error) -> handleResponse(response, error, future)); |
Madan Jampani | 0da01a4 | 2016-03-18 14:33:18 -0700 | [diff] [blame] | 130 | } catch (SerializationException | IOException e) { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 131 | future.completeExceptionally(e); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 132 | } |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 133 | return future; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 136 | /** |
| 137 | * Handles a response received from the other side of the connection. |
| 138 | */ |
| 139 | private <T> void handleResponse( |
| 140 | byte[] response, |
| 141 | Throwable error, |
| 142 | CompletableFuture<T> future) { |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 143 | if (error != null) { |
Jordan Halterman | 2f7a5d0 | 2017-05-03 16:56:53 -0700 | [diff] [blame^] | 144 | Throwable rootCause = Throwables.getRootCause(error); |
| 145 | if (rootCause instanceof MessagingException || rootCause instanceof SocketException) { |
| 146 | future.completeExceptionally(new TransportException(error)); |
| 147 | if (rootCause instanceof MessagingException.NoRemoteHandler) { |
| 148 | close(rootCause); |
| 149 | } |
| 150 | } else { |
| 151 | future.completeExceptionally(error); |
| 152 | } |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 153 | return; |
| 154 | } |
Jordan Halterman | 2f7a5d0 | 2017-05-03 16:56:53 -0700 | [diff] [blame^] | 155 | |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 156 | checkNotNull(response); |
| 157 | InputStream input = new ByteArrayInputStream(response); |
| 158 | try { |
| 159 | byte status = (byte) input.read(); |
| 160 | if (status == FAILURE) { |
| 161 | Throwable t = context.serializer().readObject(input); |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 162 | future.completeExceptionally(t); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 163 | } else { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 164 | try { |
| 165 | future.complete(context.serializer().readObject(input)); |
| 166 | } catch (SerializationException e) { |
| 167 | future.completeExceptionally(e); |
| 168 | } |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 169 | } |
| 170 | } catch (IOException e) { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 171 | future.completeExceptionally(e); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 175 | /** |
| 176 | * Handles a message sent to the connection. |
| 177 | */ |
| 178 | private CompletableFuture<byte[]> handle(Endpoint sender, byte[] payload) { |
| 179 | try (DataInputStream input = new DataInputStream(new ByteArrayInputStream(payload))) { |
| 180 | byte type = input.readByte(); |
| 181 | switch (type) { |
| 182 | case MESSAGE: |
| 183 | return handleMessage(IOUtils.toByteArray(input)); |
| 184 | case CLOSE: |
| 185 | return handleClose(); |
| 186 | default: |
| 187 | throw new IllegalStateException("Invalid message type"); |
| 188 | } |
| 189 | } catch (IOException e) { |
| 190 | Throwables.propagate(e); |
| 191 | return null; |
| 192 | } |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 195 | /** |
| 196 | * Handles a message from the other side of the connection. |
| 197 | */ |
| 198 | @SuppressWarnings("unchecked") |
| 199 | private CompletableFuture<byte[]> handleMessage(byte[] message) { |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 200 | try { |
| 201 | Object request = context.serializer().readObject(new ByteArrayInputStream(message)); |
| 202 | InternalHandler handler = handlers.get(request.getClass()); |
| 203 | if (handler == null) { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 204 | log.warn("No handler registered on connection {}-{} for type {}", |
| 205 | partitionId, connectionId, request.getClass()); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 206 | return Tools.exceptionalFuture(new IllegalStateException( |
| 207 | "No handler registered for " + request.getClass())); |
| 208 | } |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 209 | |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 210 | return handler.handle(request).handle((result, error) -> { |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 211 | try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
| 212 | baos.write(error != null ? FAILURE : SUCCESS); |
| 213 | context.serializer().writeObject(error != null ? error : result, baos); |
| 214 | return baos.toByteArray(); |
| 215 | } catch (IOException e) { |
| 216 | Throwables.propagate(e); |
| 217 | return null; |
| 218 | } |
| 219 | }); |
| 220 | } catch (Exception e) { |
| 221 | return Tools.exceptionalFuture(e); |
| 222 | } |
| 223 | } |
| 224 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 225 | /** |
| 226 | * Handles a close request from the other side of the connection. |
| 227 | */ |
| 228 | private CompletableFuture<byte[]> handleClose() { |
| 229 | CompletableFuture<byte[]> future = new CompletableFuture<>(); |
| 230 | context.executor().execute(() -> { |
Jordan Halterman | 2f7a5d0 | 2017-05-03 16:56:53 -0700 | [diff] [blame^] | 231 | close(null); |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 232 | ByteBuffer responseBuffer = ByteBuffer.allocate(1); |
| 233 | responseBuffer.put(SUCCESS); |
| 234 | future.complete(responseBuffer.array()); |
| 235 | }); |
| 236 | return future; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | @Override |
Jordan Halterman | fda46f9 | 2017-04-14 10:49:44 -0700 | [diff] [blame] | 240 | public <T, U> Connection handler(Class<T> type, Consumer<T> handler) { |
| 241 | return handler(type, r -> { |
| 242 | handler.accept(r); |
| 243 | return null; |
| 244 | }); |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public <T, U> Connection handler(Class<T> type, Function<T, CompletableFuture<U>> handler) { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 249 | if (log.isTraceEnabled()) { |
| 250 | log.trace("Registered handler on connection {}-{}: {}", partitionId, connectionId, type); |
| 251 | } |
| 252 | handlers.put(type, new InternalHandler(handler, ThreadContext.currentContextOrThrow())); |
| 253 | return this; |
| 254 | } |
| 255 | |
| 256 | @Override |
Jordan Halterman | fda46f9 | 2017-04-14 10:49:44 -0700 | [diff] [blame] | 257 | public Listener<Throwable> onException(Consumer<Throwable> consumer) { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 258 | return exceptionListeners.add(consumer); |
| 259 | } |
| 260 | |
| 261 | @Override |
Jordan Halterman | fda46f9 | 2017-04-14 10:49:44 -0700 | [diff] [blame] | 262 | public Listener<Connection> onClose(Consumer<Connection> consumer) { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 263 | return closeListeners.add(consumer); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public CompletableFuture<Void> close() { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 268 | log.debug("Closing connection {}-{}", partitionId, connectionId); |
| 269 | |
| 270 | ByteBuffer requestBuffer = ByteBuffer.allocate(1); |
| 271 | requestBuffer.put(CLOSE); |
| 272 | |
| 273 | ThreadContext context = ThreadContext.currentContextOrThrow(); |
| 274 | CompletableFuture<Void> future = new CompletableFuture<>(); |
| 275 | messagingService.sendAndReceive(endpoint, remoteSubject, requestBuffer.array(), context.executor()) |
| 276 | .whenComplete((payload, error) -> { |
Jordan Halterman | 2f7a5d0 | 2017-05-03 16:56:53 -0700 | [diff] [blame^] | 277 | close(error); |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 278 | Throwable wrappedError = error; |
| 279 | if (error != null) { |
| 280 | Throwable rootCause = Throwables.getRootCause(error); |
| 281 | if (MessagingException.class.isAssignableFrom(rootCause.getClass())) { |
| 282 | wrappedError = new TransportException(error); |
| 283 | } |
| 284 | future.completeExceptionally(wrappedError); |
| 285 | } else { |
| 286 | ByteBuffer responseBuffer = ByteBuffer.wrap(payload); |
| 287 | if (responseBuffer.get() == SUCCESS) { |
| 288 | future.complete(null); |
| 289 | } else { |
| 290 | future.completeExceptionally(new TransportException("Failed to close connection")); |
| 291 | } |
| 292 | } |
| 293 | }); |
| 294 | return future; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 295 | } |
| 296 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 297 | /** |
| 298 | * Cleans up the connection, unregistering handlers registered on the MessagingService. |
| 299 | */ |
Jordan Halterman | 2f7a5d0 | 2017-05-03 16:56:53 -0700 | [diff] [blame^] | 300 | private void close(Throwable error) { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 301 | log.debug("Connection {}-{} closed", partitionId, connectionId); |
| 302 | messagingService.unregisterHandler(localSubject); |
Jordan Halterman | 2f7a5d0 | 2017-05-03 16:56:53 -0700 | [diff] [blame^] | 303 | if (error != null) { |
| 304 | exceptionListeners.accept(error); |
| 305 | } |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 306 | closeListeners.accept(this); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 309 | /** |
| 310 | * Connection mode used to indicate whether this side of the connection is |
| 311 | * a client or server. |
| 312 | */ |
| 313 | enum Mode { |
| 314 | |
| 315 | /** |
| 316 | * Represents the client side of a bi-directional connection. |
| 317 | */ |
| 318 | CLIENT { |
| 319 | @Override |
| 320 | String getLocalSubject(PartitionId partitionId, long connectionId) { |
| 321 | return String.format("onos-copycat-%s-%d-client", partitionId, connectionId); |
| 322 | } |
| 323 | |
| 324 | @Override |
| 325 | String getRemoteSubject(PartitionId partitionId, long connectionId) { |
| 326 | return String.format("onos-copycat-%s-%d-server", partitionId, connectionId); |
| 327 | } |
| 328 | }, |
| 329 | |
| 330 | /** |
| 331 | * Represents the server side of a bi-directional connection. |
| 332 | */ |
| 333 | SERVER { |
| 334 | @Override |
| 335 | String getLocalSubject(PartitionId partitionId, long connectionId) { |
| 336 | return String.format("onos-copycat-%s-%d-server", partitionId, connectionId); |
| 337 | } |
| 338 | |
| 339 | @Override |
| 340 | String getRemoteSubject(PartitionId partitionId, long connectionId) { |
| 341 | return String.format("onos-copycat-%s-%d-client", partitionId, connectionId); |
| 342 | } |
| 343 | }; |
| 344 | |
| 345 | /** |
| 346 | * Returns the local messaging service subject for the connection in this mode. |
| 347 | * Subjects generated by the connection mode are guaranteed to be globally unique. |
| 348 | * |
| 349 | * @param partitionId the partition ID to which the connection belongs. |
| 350 | * @param connectionId the connection ID. |
| 351 | * @return the globally unique local subject for the connection. |
| 352 | */ |
| 353 | abstract String getLocalSubject(PartitionId partitionId, long connectionId); |
| 354 | |
| 355 | /** |
| 356 | * Returns the remote messaging service subject for the connection in this mode. |
| 357 | * Subjects generated by the connection mode are guaranteed to be globally unique. |
| 358 | * |
| 359 | * @param partitionId the partition ID to which the connection belongs. |
| 360 | * @param connectionId the connection ID. |
| 361 | * @return the globally unique remote subject for the connection. |
| 362 | */ |
| 363 | abstract String getRemoteSubject(PartitionId partitionId, long connectionId); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 366 | /** |
| 367 | * Internal container for a handler/context pair. |
| 368 | */ |
| 369 | private static class InternalHandler { |
Jordan Halterman | fda46f9 | 2017-04-14 10:49:44 -0700 | [diff] [blame] | 370 | private final Function handler; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 371 | private final ThreadContext context; |
| 372 | |
Jordan Halterman | fda46f9 | 2017-04-14 10:49:44 -0700 | [diff] [blame] | 373 | InternalHandler(Function handler, ThreadContext context) { |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 374 | this.handler = handler; |
| 375 | this.context = context; |
| 376 | } |
| 377 | |
| 378 | @SuppressWarnings("unchecked") |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 379 | CompletableFuture<Object> handle(Object message) { |
| 380 | CompletableFuture<Object> future = new CompletableFuture<>(); |
Jordan Halterman | fda46f9 | 2017-04-14 10:49:44 -0700 | [diff] [blame] | 381 | context.executor().execute(() -> { |
| 382 | CompletableFuture<Object> responseFuture = (CompletableFuture<Object>) handler.apply(message); |
| 383 | if (responseFuture != null) { |
| 384 | responseFuture.whenComplete((r, e) -> { |
| 385 | if (e != null) { |
| 386 | future.completeExceptionally((Throwable) e); |
| 387 | } else { |
| 388 | future.complete(r); |
| 389 | } |
| 390 | }); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 391 | } |
Jordan Halterman | fda46f9 | 2017-04-14 10:49:44 -0700 | [diff] [blame] | 392 | }); |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 393 | return future; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | } |