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 | b6ee9e9 | 2017-06-21 15:26:28 -0700 | [diff] [blame] | 59 | private static final int MAX_MESSAGE_SIZE = 1024 * 1024; |
| 60 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 61 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 62 | private final long connectionId; |
| 63 | private final String localSubject; |
| 64 | private final String remoteSubject; |
| 65 | private final PartitionId partitionId; |
| 66 | private final Endpoint endpoint; |
| 67 | private final MessagingService messagingService; |
| 68 | private final ThreadContext context; |
| 69 | private final Map<Class, InternalHandler> handlers = new ConcurrentHashMap<>(); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 70 | private final Listeners<Throwable> exceptionListeners = new Listeners<>(); |
| 71 | private final Listeners<Connection> closeListeners = new Listeners<>(); |
| 72 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 73 | CopycatTransportConnection( |
| 74 | long connectionId, |
| 75 | Mode mode, |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 76 | PartitionId partitionId, |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 77 | Endpoint endpoint, |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 78 | MessagingService messagingService, |
| 79 | ThreadContext context) { |
| 80 | this.connectionId = connectionId; |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 81 | this.partitionId = checkNotNull(partitionId, "partitionId cannot be null"); |
| 82 | this.localSubject = mode.getLocalSubject(partitionId, connectionId); |
| 83 | this.remoteSubject = mode.getRemoteSubject(partitionId, connectionId); |
| 84 | this.endpoint = checkNotNull(endpoint, "endpoint cannot be null"); |
| 85 | this.messagingService = checkNotNull(messagingService, "messagingService cannot be null"); |
| 86 | this.context = checkNotNull(context, "context cannot be null"); |
| 87 | messagingService.registerHandler(localSubject, this::handle); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
Jordan Halterman | fda46f9 | 2017-04-14 10:49:44 -0700 | [diff] [blame] | 91 | public CompletableFuture<Void> send(Object message) { |
| 92 | ThreadContext context = ThreadContext.currentContextOrThrow(); |
| 93 | CompletableFuture<Void> future = new CompletableFuture<>(); |
| 94 | try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
| 95 | DataOutputStream dos = new DataOutputStream(baos); |
| 96 | dos.writeByte(MESSAGE); |
| 97 | context.serializer().writeObject(message, baos); |
| 98 | if (message instanceof ReferenceCounted) { |
| 99 | ((ReferenceCounted<?>) message).release(); |
| 100 | } |
| 101 | |
Jordan Halterman | b6ee9e9 | 2017-06-21 15:26:28 -0700 | [diff] [blame] | 102 | byte[] bytes = baos.toByteArray(); |
| 103 | if (bytes.length > MAX_MESSAGE_SIZE) { |
| 104 | throw new IllegalArgumentException(message + " exceeds maximum message size " + MAX_MESSAGE_SIZE); |
| 105 | } |
| 106 | messagingService.sendAsync(endpoint, remoteSubject, bytes) |
Jordan Halterman | fda46f9 | 2017-04-14 10:49:44 -0700 | [diff] [blame] | 107 | .whenComplete((r, e) -> { |
| 108 | if (e != null) { |
| 109 | context.executor().execute(() -> future.completeExceptionally(e)); |
| 110 | } else { |
| 111 | context.executor().execute(() -> future.complete(null)); |
| 112 | } |
| 113 | }); |
| 114 | } catch (SerializationException | IOException e) { |
| 115 | future.completeExceptionally(e); |
| 116 | } |
| 117 | return future; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public <T, U> CompletableFuture<U> sendAndReceive(T message) { |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 122 | ThreadContext context = ThreadContext.currentContextOrThrow(); |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 123 | CompletableFuture<U> future = new CompletableFuture<>(); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 124 | try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 125 | DataOutputStream dos = new DataOutputStream(baos); |
| 126 | dos.writeByte(MESSAGE); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 127 | context.serializer().writeObject(message, baos); |
| 128 | if (message instanceof ReferenceCounted) { |
| 129 | ((ReferenceCounted<?>) message).release(); |
| 130 | } |
Jordan Halterman | b6ee9e9 | 2017-06-21 15:26:28 -0700 | [diff] [blame] | 131 | |
| 132 | byte[] bytes = baos.toByteArray(); |
| 133 | if (bytes.length > MAX_MESSAGE_SIZE) { |
| 134 | throw new IllegalArgumentException(message + " exceeds maximum message size " + MAX_MESSAGE_SIZE); |
| 135 | } |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 136 | messagingService.sendAndReceive(endpoint, |
| 137 | remoteSubject, |
Jordan Halterman | b6ee9e9 | 2017-06-21 15:26:28 -0700 | [diff] [blame] | 138 | bytes, |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 139 | context.executor()) |
Jordan Halterman | 2f7a5d0 | 2017-05-03 16:56:53 -0700 | [diff] [blame] | 140 | .whenComplete((response, error) -> handleResponse(response, error, future)); |
Madan Jampani | 0da01a4 | 2016-03-18 14:33:18 -0700 | [diff] [blame] | 141 | } catch (SerializationException | IOException e) { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 142 | future.completeExceptionally(e); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 143 | } |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 144 | return future; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 147 | /** |
| 148 | * Handles a response received from the other side of the connection. |
| 149 | */ |
| 150 | private <T> void handleResponse( |
| 151 | byte[] response, |
| 152 | Throwable error, |
| 153 | CompletableFuture<T> future) { |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 154 | if (error != null) { |
Jordan Halterman | 2f7a5d0 | 2017-05-03 16:56:53 -0700 | [diff] [blame] | 155 | Throwable rootCause = Throwables.getRootCause(error); |
Jordan Halterman | b6ee9e9 | 2017-06-21 15:26:28 -0700 | [diff] [blame] | 156 | if (rootCause instanceof MessagingException.NoRemoteHandler) { |
Jordan Halterman | 2f7a5d0 | 2017-05-03 16:56:53 -0700 | [diff] [blame] | 157 | future.completeExceptionally(new TransportException(error)); |
Jordan Halterman | b6ee9e9 | 2017-06-21 15:26:28 -0700 | [diff] [blame] | 158 | close(rootCause); |
| 159 | } else if (rootCause instanceof SocketException) { |
| 160 | future.completeExceptionally(new TransportException(error)); |
Jordan Halterman | 2f7a5d0 | 2017-05-03 16:56:53 -0700 | [diff] [blame] | 161 | } else { |
| 162 | future.completeExceptionally(error); |
| 163 | } |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 164 | return; |
| 165 | } |
Jordan Halterman | 2f7a5d0 | 2017-05-03 16:56:53 -0700 | [diff] [blame] | 166 | |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 167 | checkNotNull(response); |
| 168 | InputStream input = new ByteArrayInputStream(response); |
| 169 | try { |
| 170 | byte status = (byte) input.read(); |
| 171 | if (status == FAILURE) { |
| 172 | Throwable t = context.serializer().readObject(input); |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 173 | future.completeExceptionally(t); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 174 | } else { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 175 | try { |
| 176 | future.complete(context.serializer().readObject(input)); |
| 177 | } catch (SerializationException e) { |
| 178 | future.completeExceptionally(e); |
| 179 | } |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 180 | } |
| 181 | } catch (IOException e) { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 182 | future.completeExceptionally(e); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 186 | /** |
| 187 | * Handles a message sent to the connection. |
| 188 | */ |
| 189 | private CompletableFuture<byte[]> handle(Endpoint sender, byte[] payload) { |
| 190 | try (DataInputStream input = new DataInputStream(new ByteArrayInputStream(payload))) { |
| 191 | byte type = input.readByte(); |
| 192 | switch (type) { |
| 193 | case MESSAGE: |
| 194 | return handleMessage(IOUtils.toByteArray(input)); |
| 195 | case CLOSE: |
| 196 | return handleClose(); |
| 197 | default: |
| 198 | throw new IllegalStateException("Invalid message type"); |
| 199 | } |
| 200 | } catch (IOException e) { |
| 201 | Throwables.propagate(e); |
| 202 | return null; |
| 203 | } |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 206 | /** |
| 207 | * Handles a message from the other side of the connection. |
| 208 | */ |
| 209 | @SuppressWarnings("unchecked") |
| 210 | private CompletableFuture<byte[]> handleMessage(byte[] message) { |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 211 | try { |
| 212 | Object request = context.serializer().readObject(new ByteArrayInputStream(message)); |
| 213 | InternalHandler handler = handlers.get(request.getClass()); |
| 214 | if (handler == null) { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 215 | log.warn("No handler registered on connection {}-{} for type {}", |
| 216 | partitionId, connectionId, request.getClass()); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 217 | return Tools.exceptionalFuture(new IllegalStateException( |
| 218 | "No handler registered for " + request.getClass())); |
| 219 | } |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 220 | |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 221 | return handler.handle(request).handle((result, error) -> { |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 222 | try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
| 223 | baos.write(error != null ? FAILURE : SUCCESS); |
| 224 | context.serializer().writeObject(error != null ? error : result, baos); |
Jordan Halterman | b6ee9e9 | 2017-06-21 15:26:28 -0700 | [diff] [blame] | 225 | byte[] bytes = baos.toByteArray(); |
| 226 | if (bytes.length > MAX_MESSAGE_SIZE) { |
| 227 | throw new IllegalArgumentException("response exceeds maximum message size " + MAX_MESSAGE_SIZE); |
| 228 | } |
| 229 | return bytes; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 230 | } catch (IOException e) { |
| 231 | Throwables.propagate(e); |
| 232 | return null; |
| 233 | } |
| 234 | }); |
| 235 | } catch (Exception e) { |
| 236 | return Tools.exceptionalFuture(e); |
| 237 | } |
| 238 | } |
| 239 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 240 | /** |
| 241 | * Handles a close request from the other side of the connection. |
| 242 | */ |
| 243 | private CompletableFuture<byte[]> handleClose() { |
| 244 | CompletableFuture<byte[]> future = new CompletableFuture<>(); |
| 245 | context.executor().execute(() -> { |
Jordan Halterman | 2f7a5d0 | 2017-05-03 16:56:53 -0700 | [diff] [blame] | 246 | close(null); |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 247 | ByteBuffer responseBuffer = ByteBuffer.allocate(1); |
| 248 | responseBuffer.put(SUCCESS); |
| 249 | future.complete(responseBuffer.array()); |
| 250 | }); |
| 251 | return future; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | @Override |
Jordan Halterman | fda46f9 | 2017-04-14 10:49:44 -0700 | [diff] [blame] | 255 | public <T, U> Connection handler(Class<T> type, Consumer<T> handler) { |
| 256 | return handler(type, r -> { |
| 257 | handler.accept(r); |
| 258 | return null; |
| 259 | }); |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | 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] | 264 | if (log.isTraceEnabled()) { |
| 265 | log.trace("Registered handler on connection {}-{}: {}", partitionId, connectionId, type); |
| 266 | } |
| 267 | handlers.put(type, new InternalHandler(handler, ThreadContext.currentContextOrThrow())); |
| 268 | return this; |
| 269 | } |
| 270 | |
| 271 | @Override |
Jordan Halterman | fda46f9 | 2017-04-14 10:49:44 -0700 | [diff] [blame] | 272 | public Listener<Throwable> onException(Consumer<Throwable> consumer) { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 273 | return exceptionListeners.add(consumer); |
| 274 | } |
| 275 | |
| 276 | @Override |
Jordan Halterman | fda46f9 | 2017-04-14 10:49:44 -0700 | [diff] [blame] | 277 | public Listener<Connection> onClose(Consumer<Connection> consumer) { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 278 | return closeListeners.add(consumer); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | @Override |
| 282 | public CompletableFuture<Void> close() { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 283 | log.debug("Closing connection {}-{}", partitionId, connectionId); |
| 284 | |
| 285 | ByteBuffer requestBuffer = ByteBuffer.allocate(1); |
| 286 | requestBuffer.put(CLOSE); |
| 287 | |
| 288 | ThreadContext context = ThreadContext.currentContextOrThrow(); |
| 289 | CompletableFuture<Void> future = new CompletableFuture<>(); |
| 290 | messagingService.sendAndReceive(endpoint, remoteSubject, requestBuffer.array(), context.executor()) |
| 291 | .whenComplete((payload, error) -> { |
Jordan Halterman | 2f7a5d0 | 2017-05-03 16:56:53 -0700 | [diff] [blame] | 292 | close(error); |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 293 | Throwable wrappedError = error; |
| 294 | if (error != null) { |
| 295 | Throwable rootCause = Throwables.getRootCause(error); |
Jordan Halterman | b6ee9e9 | 2017-06-21 15:26:28 -0700 | [diff] [blame] | 296 | if (rootCause instanceof MessagingException.NoRemoteHandler) { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 297 | wrappedError = new TransportException(error); |
| 298 | } |
| 299 | future.completeExceptionally(wrappedError); |
| 300 | } else { |
| 301 | ByteBuffer responseBuffer = ByteBuffer.wrap(payload); |
| 302 | if (responseBuffer.get() == SUCCESS) { |
| 303 | future.complete(null); |
| 304 | } else { |
| 305 | future.completeExceptionally(new TransportException("Failed to close connection")); |
| 306 | } |
| 307 | } |
| 308 | }); |
| 309 | return future; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 312 | /** |
| 313 | * Cleans up the connection, unregistering handlers registered on the MessagingService. |
| 314 | */ |
Jordan Halterman | 2f7a5d0 | 2017-05-03 16:56:53 -0700 | [diff] [blame] | 315 | private void close(Throwable error) { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 316 | log.debug("Connection {}-{} closed", partitionId, connectionId); |
| 317 | messagingService.unregisterHandler(localSubject); |
Jordan Halterman | 2f7a5d0 | 2017-05-03 16:56:53 -0700 | [diff] [blame] | 318 | if (error != null) { |
| 319 | exceptionListeners.accept(error); |
| 320 | } |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 321 | closeListeners.accept(this); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 324 | /** |
| 325 | * Connection mode used to indicate whether this side of the connection is |
| 326 | * a client or server. |
| 327 | */ |
| 328 | enum Mode { |
| 329 | |
| 330 | /** |
| 331 | * Represents the client side of a bi-directional connection. |
| 332 | */ |
| 333 | CLIENT { |
| 334 | @Override |
| 335 | String getLocalSubject(PartitionId partitionId, long connectionId) { |
| 336 | return String.format("onos-copycat-%s-%d-client", partitionId, connectionId); |
| 337 | } |
| 338 | |
| 339 | @Override |
| 340 | String getRemoteSubject(PartitionId partitionId, long connectionId) { |
| 341 | return String.format("onos-copycat-%s-%d-server", partitionId, connectionId); |
| 342 | } |
| 343 | }, |
| 344 | |
| 345 | /** |
| 346 | * Represents the server side of a bi-directional connection. |
| 347 | */ |
| 348 | SERVER { |
| 349 | @Override |
| 350 | String getLocalSubject(PartitionId partitionId, long connectionId) { |
| 351 | return String.format("onos-copycat-%s-%d-server", partitionId, connectionId); |
| 352 | } |
| 353 | |
| 354 | @Override |
| 355 | String getRemoteSubject(PartitionId partitionId, long connectionId) { |
| 356 | return String.format("onos-copycat-%s-%d-client", partitionId, connectionId); |
| 357 | } |
| 358 | }; |
| 359 | |
| 360 | /** |
| 361 | * Returns the local messaging service subject for the connection in this mode. |
| 362 | * Subjects generated by the connection mode are guaranteed to be globally unique. |
| 363 | * |
| 364 | * @param partitionId the partition ID to which the connection belongs. |
| 365 | * @param connectionId the connection ID. |
| 366 | * @return the globally unique local subject for the connection. |
| 367 | */ |
| 368 | abstract String getLocalSubject(PartitionId partitionId, long connectionId); |
| 369 | |
| 370 | /** |
| 371 | * Returns the remote messaging service subject for the connection in this mode. |
| 372 | * Subjects generated by the connection mode are guaranteed to be globally unique. |
| 373 | * |
| 374 | * @param partitionId the partition ID to which the connection belongs. |
| 375 | * @param connectionId the connection ID. |
| 376 | * @return the globally unique remote subject for the connection. |
| 377 | */ |
| 378 | abstract String getRemoteSubject(PartitionId partitionId, long connectionId); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 381 | /** |
| 382 | * Internal container for a handler/context pair. |
| 383 | */ |
| 384 | private static class InternalHandler { |
Jordan Halterman | fda46f9 | 2017-04-14 10:49:44 -0700 | [diff] [blame] | 385 | private final Function handler; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 386 | private final ThreadContext context; |
| 387 | |
Jordan Halterman | fda46f9 | 2017-04-14 10:49:44 -0700 | [diff] [blame] | 388 | InternalHandler(Function handler, ThreadContext context) { |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 389 | this.handler = handler; |
| 390 | this.context = context; |
| 391 | } |
| 392 | |
| 393 | @SuppressWarnings("unchecked") |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 394 | CompletableFuture<Object> handle(Object message) { |
| 395 | CompletableFuture<Object> future = new CompletableFuture<>(); |
Jordan Halterman | fda46f9 | 2017-04-14 10:49:44 -0700 | [diff] [blame] | 396 | context.executor().execute(() -> { |
| 397 | CompletableFuture<Object> responseFuture = (CompletableFuture<Object>) handler.apply(message); |
| 398 | if (responseFuture != null) { |
| 399 | responseFuture.whenComplete((r, e) -> { |
| 400 | if (e != null) { |
| 401 | future.completeExceptionally((Throwable) e); |
| 402 | } else { |
| 403 | future.complete(r); |
| 404 | } |
| 405 | }); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 406 | } |
Jordan Halterman | fda46f9 | 2017-04-14 10:49:44 -0700 | [diff] [blame] | 407 | }); |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 408 | return future; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | } |