blob: 7dfc59b03d516cf925992dd6cb38366c0dad4b01 [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;
32import org.onosproject.store.cluster.messaging.MessagingService;
33
34import com.google.common.collect.Maps;
35
36import io.atomix.catalyst.transport.Address;
37import io.atomix.catalyst.transport.Connection;
38import io.atomix.catalyst.transport.Server;
39import io.atomix.catalyst.util.concurrent.SingleThreadContext;
40import io.atomix.catalyst.util.concurrent.ThreadContext;
41
42/**
43 * {@link Server} implementation for {@link CopycatTransport}.
44 */
45public class CopycatTransportServer implements Server {
46
47 private final AtomicBoolean listening = new AtomicBoolean(false);
Madan Jampani71d13e12016-01-13 17:14:35 -080048 private CompletableFuture<Void> listenFuture = new CompletableFuture<>();
Madan Jampani3289fbf2016-01-13 14:14:27 -080049 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 Jampani71d13e12016-01-13 17:14:35 -080062 if (listening.compareAndSet(false, true)) {
63 ThreadContext context = ThreadContext.currentContextOrThrow();
64 listen(address, listener, context);
Madan Jampani3289fbf2016-01-13 14:14:27 -080065 }
66 return listenFuture;
67 }
68
Madan Jampani71d13e12016-01-13 17:14:35 -080069 private void listen(Address address, Consumer<Connection> listener, ThreadContext context) {
Madan Jampani3289fbf2016-01-13 14:14:27 -080070 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 Jampani3289fbf2016-01-13 14:14:27 -080098 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}