Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 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 static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
| 20 | import java.io.ByteArrayInputStream; |
| 21 | import java.io.DataInputStream; |
| 22 | import java.io.IOException; |
| 23 | import java.net.InetAddress; |
| 24 | import java.net.InetSocketAddress; |
| 25 | import java.util.Map; |
| 26 | import java.util.concurrent.CompletableFuture; |
| 27 | import java.util.concurrent.atomic.AtomicBoolean; |
| 28 | import java.util.function.Consumer; |
| 29 | |
| 30 | import org.apache.commons.io.IOUtils; |
| 31 | import org.onlab.util.Tools; |
| 32 | import org.onosproject.store.cluster.messaging.MessagingService; |
| 33 | |
| 34 | import com.google.common.collect.Maps; |
| 35 | |
| 36 | import io.atomix.catalyst.transport.Address; |
| 37 | import io.atomix.catalyst.transport.Connection; |
| 38 | import io.atomix.catalyst.transport.Server; |
| 39 | import io.atomix.catalyst.util.concurrent.SingleThreadContext; |
| 40 | import io.atomix.catalyst.util.concurrent.ThreadContext; |
| 41 | |
| 42 | /** |
| 43 | * {@link Server} implementation for {@link CopycatTransport}. |
| 44 | */ |
| 45 | public class CopycatTransportServer implements Server { |
| 46 | |
| 47 | private final AtomicBoolean listening = new AtomicBoolean(false); |
Madan Jampani | 71d13e1 | 2016-01-13 17:14:35 -0800 | [diff] [blame^] | 48 | private CompletableFuture<Void> listenFuture = new CompletableFuture<>(); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 49 | private final String clusterName; |
| 50 | private final MessagingService messagingService; |
| 51 | private final String messageSubject; |
| 52 | private final Map<Long, CopycatTransportConnection> connections = Maps.newConcurrentMap(); |
| 53 | |
| 54 | CopycatTransportServer(String clusterName, MessagingService messagingService) { |
| 55 | this.clusterName = checkNotNull(clusterName); |
| 56 | this.messagingService = checkNotNull(messagingService); |
| 57 | this.messageSubject = String.format("onos-copycat-%s", clusterName); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public CompletableFuture<Void> listen(Address address, Consumer<Connection> listener) { |
Madan Jampani | 71d13e1 | 2016-01-13 17:14:35 -0800 | [diff] [blame^] | 62 | if (listening.compareAndSet(false, true)) { |
| 63 | ThreadContext context = ThreadContext.currentContextOrThrow(); |
| 64 | listen(address, listener, context); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 65 | } |
| 66 | return listenFuture; |
| 67 | } |
| 68 | |
Madan Jampani | 71d13e1 | 2016-01-13 17:14:35 -0800 | [diff] [blame^] | 69 | private void listen(Address address, Consumer<Connection> listener, ThreadContext context) { |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 70 | messagingService.registerHandler(messageSubject, (sender, payload) -> { |
| 71 | try (DataInputStream input = new DataInputStream(new ByteArrayInputStream(payload))) { |
| 72 | long connectionId = input.readLong(); |
| 73 | InetAddress senderHost = InetAddress.getByAddress(sender.host().toOctets()); |
| 74 | int senderPort = sender.port(); |
| 75 | Address senderAddress = new Address(new InetSocketAddress(senderHost, senderPort)); |
| 76 | AtomicBoolean newConnection = new AtomicBoolean(false); |
| 77 | CopycatTransportConnection connection = connections.computeIfAbsent(connectionId, k -> { |
| 78 | newConnection.set(true); |
| 79 | return new CopycatTransportConnection(connectionId, |
| 80 | CopycatTransport.Mode.SERVER, |
| 81 | clusterName, |
| 82 | senderAddress, |
| 83 | messagingService, |
| 84 | getOrCreateContext(context)); |
| 85 | }); |
| 86 | byte[] request = IOUtils.toByteArray(input); |
| 87 | return CompletableFuture.supplyAsync( |
| 88 | () -> { |
| 89 | if (newConnection.get()) { |
| 90 | listener.accept(connection); |
| 91 | } |
| 92 | return connection; |
| 93 | }, context.executor()).thenCompose(c -> c.handle(request)); |
| 94 | } catch (IOException e) { |
| 95 | return Tools.exceptionalFuture(e); |
| 96 | } |
| 97 | }); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 98 | context.execute(() -> { |
| 99 | listenFuture.complete(null); |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public CompletableFuture<Void> close() { |
| 105 | messagingService.unregisterHandler(messageSubject); |
| 106 | return CompletableFuture.completedFuture(null); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns the current execution context or creates one. |
| 111 | */ |
| 112 | private ThreadContext getOrCreateContext(ThreadContext parentContext) { |
| 113 | ThreadContext context = ThreadContext.currentContext(); |
| 114 | if (context != null) { |
| 115 | return context; |
| 116 | } |
| 117 | return new SingleThreadContext("copycat-transport-server-" + clusterName, parentContext.serializer().clone()); |
| 118 | } |
| 119 | } |