blob: 9a1dce939bd4032ab34b7d865a590fb2bf9e7842 [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 Jampani2f9cc712016-02-15 19:36:21 -080053 return messagingService.sendAndReceive(CopycatTransport.toEndpoint(remoteAddress),
54 PartitionManager.HELLO_MESSAGE_SUBJECT,
55 "hello".getBytes())
56 .thenApplyAsync(r -> {
57 CopycatTransportConnection connection = new CopycatTransportConnection(
58 nextConnectionId(),
59 CopycatTransport.Mode.CLIENT,
60 partitionId,
61 remoteAddress,
62 messagingService,
63 context);
64 if (mode == CopycatTransport.Mode.CLIENT) {
65 connection.setBidirectional();
66 }
67 connections.add(connection);
68 return connection;
69 }, context.executor());
Madan Jampani3289fbf2016-01-13 14:14:27 -080070 }
71
72 @Override
73 public CompletableFuture<Void> close() {
74 return CompletableFuture.allOf(connections.stream().map(Connection::close).toArray(CompletableFuture[]::new));
75 }
76
77 private long nextConnectionId() {
78 return RandomUtils.nextLong();
79 }
80 }