blob: 96729c722e81f5b43c2b7610f877095e9ab91028 [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.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;
32import io.atomix.catalyst.util.concurrent.ThreadContext;
33
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 }
63 connections.add(connection);
64 return CompletableFuture.supplyAsync(() -> connection, context.executor());
Madan Jampani3289fbf2016-01-13 14:14:27 -080065 }
66
67 @Override
68 public CompletableFuture<Void> close() {
69 return CompletableFuture.allOf(connections.stream().map(Connection::close).toArray(CompletableFuture[]::new));
70 }
71
72 private long nextConnectionId() {
73 return RandomUtils.nextLong();
74 }
75 }