blob: 913dc6ce7a5718a651d8d16c111a96d894a8afb2 [file] [log] [blame]
Madan Jampani3289fbf2016-01-13 14:14:27 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Madan Jampani3289fbf2016-01-13 14:14:27 -08003 *
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.util.Set;
21import java.util.concurrent.CompletableFuture;
Madan Jampanif778c962016-01-31 22:56:38 -080022
Madan Jampani3289fbf2016-01-13 14:14:27 -080023import org.apache.commons.lang.math.RandomUtils;
Madan Jampanif778c962016-01-31 22:56:38 -080024import org.onosproject.cluster.PartitionId;
Madan Jampani3289fbf2016-01-13 14:14:27 -080025import org.onosproject.store.cluster.messaging.MessagingService;
26
27import com.google.common.collect.Sets;
28
29import io.atomix.catalyst.transport.Address;
30import io.atomix.catalyst.transport.Client;
31import io.atomix.catalyst.transport.Connection;
Madan Jampani630e7ac2016-05-31 11:34:05 -070032import io.atomix.catalyst.concurrent.ThreadContext;
Madan Jampani3289fbf2016-01-13 14:14:27 -080033
34/**
35 * {@link Client} implementation for {@link CopycatTransport}.
36 */
37public class CopycatTransportClient implements Client {
38
Madan Jampanif778c962016-01-31 22:56:38 -080039 private final PartitionId partitionId;
Madan Jampani3289fbf2016-01-13 14:14:27 -080040 private final MessagingService messagingService;
41 private final CopycatTransport.Mode mode;
Madan Jampani3289fbf2016-01-13 14:14:27 -080042 private final Set<CopycatTransportConnection> connections = Sets.newConcurrentHashSet();
43
Madan Jampanif778c962016-01-31 22:56:38 -080044 CopycatTransportClient(PartitionId partitionId, MessagingService messagingService, CopycatTransport.Mode mode) {
45 this.partitionId = checkNotNull(partitionId);
Madan Jampani3289fbf2016-01-13 14:14:27 -080046 this.messagingService = checkNotNull(messagingService);
47 this.mode = checkNotNull(mode);
Madan Jampani3289fbf2016-01-13 14:14:27 -080048 }
49
50 @Override
51 public CompletableFuture<Connection> connect(Address remoteAddress) {
Madan Jampanib06ccef2016-01-25 10:51:16 -080052 ThreadContext context = ThreadContext.currentContextOrThrow();
Madan Jampani3a9911c2016-02-21 11:25:45 -080053 CopycatTransportConnection connection = new CopycatTransportConnection(
54 nextConnectionId(),
55 CopycatTransport.Mode.CLIENT,
56 partitionId,
57 remoteAddress,
58 messagingService,
59 context);
60 if (mode == CopycatTransport.Mode.CLIENT) {
61 connection.setBidirectional();
62 }
Jon Hall9a44d6a2017-03-02 18:14:37 -080063 connection.closeListener(c -> connections.remove(c));
Madan Jampani3a9911c2016-02-21 11:25:45 -080064 connections.add(connection);
65 return CompletableFuture.supplyAsync(() -> connection, context.executor());
Madan Jampani3289fbf2016-01-13 14:14:27 -080066 }
67
68 @Override
69 public CompletableFuture<Void> close() {
70 return CompletableFuture.allOf(connections.stream().map(Connection::close).toArray(CompletableFuture[]::new));
71 }
72
73 private long nextConnectionId() {
74 return RandomUtils.nextLong();
75 }
76 }