blob: 9a1959a05450e5f5d77e91515ccd2815a8407474 [file] [log] [blame]
Madan Jampani3289fbf2016-01-13 14:14:27 -08001/*
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 */
16package org.onosproject.store.primitives.impl;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import java.io.ByteArrayInputStream;
21import java.io.DataInputStream;
22import java.io.IOException;
23import java.net.InetAddress;
24import java.net.InetSocketAddress;
25import java.util.Map;
26import java.util.concurrent.CompletableFuture;
27import java.util.concurrent.atomic.AtomicBoolean;
28import java.util.function.Consumer;
29
30import org.apache.commons.io.IOUtils;
31import org.onlab.util.Tools;
Madan Jampanif778c962016-01-31 22:56:38 -080032import org.onosproject.cluster.PartitionId;
Madan Jampani3289fbf2016-01-13 14:14:27 -080033import org.onosproject.store.cluster.messaging.MessagingService;
34
35import com.google.common.collect.Maps;
36
37import io.atomix.catalyst.transport.Address;
38import io.atomix.catalyst.transport.Connection;
39import io.atomix.catalyst.transport.Server;
40import io.atomix.catalyst.util.concurrent.SingleThreadContext;
41import io.atomix.catalyst.util.concurrent.ThreadContext;
42
43/**
44 * {@link Server} implementation for {@link CopycatTransport}.
45 */
46public class CopycatTransportServer implements Server {
47
48 private final AtomicBoolean listening = new AtomicBoolean(false);
Madan Jampani71d13e12016-01-13 17:14:35 -080049 private CompletableFuture<Void> listenFuture = new CompletableFuture<>();
Madan Jampanif778c962016-01-31 22:56:38 -080050 private final PartitionId partitionId;
Madan Jampani3289fbf2016-01-13 14:14:27 -080051 private final MessagingService messagingService;
52 private final String messageSubject;
53 private final Map<Long, CopycatTransportConnection> connections = Maps.newConcurrentMap();
54
Madan Jampanif778c962016-01-31 22:56:38 -080055 CopycatTransportServer(PartitionId partitionId, MessagingService messagingService) {
56 this.partitionId = checkNotNull(partitionId);
Madan Jampani3289fbf2016-01-13 14:14:27 -080057 this.messagingService = checkNotNull(messagingService);
Madan Jampanif778c962016-01-31 22:56:38 -080058 this.messageSubject = String.format("onos-copycat-%s", partitionId);
Madan Jampani3289fbf2016-01-13 14:14:27 -080059 }
60
61 @Override
62 public CompletableFuture<Void> listen(Address address, Consumer<Connection> listener) {
Madan Jampani71d13e12016-01-13 17:14:35 -080063 if (listening.compareAndSet(false, true)) {
64 ThreadContext context = ThreadContext.currentContextOrThrow();
65 listen(address, listener, context);
Madan Jampani3289fbf2016-01-13 14:14:27 -080066 }
67 return listenFuture;
68 }
69
Madan Jampani71d13e12016-01-13 17:14:35 -080070 private void listen(Address address, Consumer<Connection> listener, ThreadContext context) {
Madan Jampani3289fbf2016-01-13 14:14:27 -080071 messagingService.registerHandler(messageSubject, (sender, payload) -> {
72 try (DataInputStream input = new DataInputStream(new ByteArrayInputStream(payload))) {
73 long connectionId = input.readLong();
74 InetAddress senderHost = InetAddress.getByAddress(sender.host().toOctets());
75 int senderPort = sender.port();
76 Address senderAddress = new Address(new InetSocketAddress(senderHost, senderPort));
77 AtomicBoolean newConnection = new AtomicBoolean(false);
78 CopycatTransportConnection connection = connections.computeIfAbsent(connectionId, k -> {
79 newConnection.set(true);
80 return new CopycatTransportConnection(connectionId,
81 CopycatTransport.Mode.SERVER,
Madan Jampanif778c962016-01-31 22:56:38 -080082 partitionId,
Madan Jampani3289fbf2016-01-13 14:14:27 -080083 senderAddress,
84 messagingService,
85 getOrCreateContext(context));
86 });
87 byte[] request = IOUtils.toByteArray(input);
88 return CompletableFuture.supplyAsync(
89 () -> {
90 if (newConnection.get()) {
91 listener.accept(connection);
92 }
93 return connection;
94 }, context.executor()).thenCompose(c -> c.handle(request));
95 } catch (IOException e) {
96 return Tools.exceptionalFuture(e);
97 }
98 });
Madan Jampani3289fbf2016-01-13 14:14:27 -080099 context.execute(() -> {
100 listenFuture.complete(null);
101 });
102 }
103
104 @Override
105 public CompletableFuture<Void> close() {
106 messagingService.unregisterHandler(messageSubject);
107 return CompletableFuture.completedFuture(null);
108 }
109
110 /**
111 * Returns the current execution context or creates one.
112 */
113 private ThreadContext getOrCreateContext(ThreadContext parentContext) {
114 ThreadContext context = ThreadContext.currentContext();
115 if (context != null) {
116 return context;
117 }
Madan Jampanif778c962016-01-31 22:56:38 -0800118 return new SingleThreadContext("copycat-transport-server-" + partitionId, parentContext.serializer().clone());
Madan Jampani3289fbf2016-01-13 14:14:27 -0800119 }
120}